classification
Title: The 4th parameter of method always None or 0 on x64 Windows.
Type: behavior Stage:
Components: Windows Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: cgohlke, ghazel, ocean-city, owenl, stan.mihai, theller
Priority: normal Keywords: patch

Created on 2010-09-17 09:11 by owenl, last changed 2011-02-01 02:02 by owenl. This issue is now closed.

Files
File name Uploaded Description Edit
issues.zip owenl, 2010-09-17 09:11 some simple codes for reproduce this issue
testPy2.dll owenl, 2010-09-19 08:27 use this dll to reproduce the issue
ctypes_win64.diff stan.mihai, 2011-01-26 09:16 one-liner fix
Messages (12)
msg116649 - (view) Author: Owen (owenl) Date: 2010-09-17 09:11
OS: Windows 2003STD x64 en

I have try to call python method from c++ dll by "WINFUNCTYPE".
But the 4th parameter is always None or 0 if the type is int or void* (float is works fine).

Following is the part of source code:
==code==========================================
import sys
from ctypes import *

windll.LoadLibrary("testPy2.dll")
#########################################

def test3(param1,param2,param3,param4,param5,param6,param7,param8,param9,param10):
  print("================")
  print(param1)
  print(param2)
  print(param3)
  # the 4th param4 is always 0.
  print(param4)
  print(param5)
  print(param6)
  print(param7)
  print(param8)
  print(param9)
  print(param10)
  print("================")
  return 20
  
C_METHOD_TYPE4 = WINFUNCTYPE(c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32)

windll.testPy2.fntestPy7(9,C_METHOD_TYPE4(test3))
==code==========================================

To my knowledge, both visual c++ and gcc use registers for the first few parameters, and then after that use the stack.  Maybe this is the reason.

I have attached some simple codes for reproduce this issue.
issues
  - testPy2      <- source code of the dll
  - test.py      <- python file to reproduce the issue
  - testPy2.dll  <- the dll to reproduce the issue
msg116847 - (view) Author: Owen (owenl) Date: 2010-09-19 01:35
Please reproduce this issue by 64bit Python.
msg116851 - (view) Author: Owen (owenl) Date: 2010-09-19 08:27
Note:
This issue also occurs on other 64 bit windows OS(i.e. windows xp 64bit)

Load "testPy2.dll" needs vc++ runtime library (http://download.microsoft.com/download/2/d/6/2d61c766-107b-409d-8fba-c39e61ca08e8/vcredist_x64.exe)

Update "testPy2.dll", use this to reproduce the issue.
python code:
import sys
from ctypes import *

windll.LoadLibrary("testPy2.dll")
#########################################

def test3(param1,param2,param3,param4,param5,param6,param7,param8,param9,param10):
  print("================")
  print(param1)
  print(param2)
  print(param3)
  print(param4)
  print(param5)
  print(param6)
  print(param7)
  print(param8)
  print(param9)
  print(param10)
  print("================")
  return 20
  
C_METHOD_TYPE4 = WINFUNCTYPE(c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32)

windll.testPy2.fntestPy7(9,C_METHOD_TYPE4(test3))

#########################################

def test4(param1,param2,param3,param4,param5,param6,param7,param8,param9,param10):
  print("================")
  print(param1)
  print(param2)
  print(param3)
  print(param4)
  print(param5)
  print(param6)
  print(param7)
  print(param8)
  print(param9)
  print(param10)
  print("================")
  return 20
  
C_METHOD_TYPE5 = WINFUNCTYPE(c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32, c_int32)

windll.testPy2.fntestPy8(10,C_METHOD_TYPE5(test4))

#########################################

def test5(param1,param2,param3,param4,param5,param6,param7,param8,param9,param10):
  print("================")
  print(param1)
  print(param2)
  print(param3)
  print(param4)
  print(param5)
  print(param6)
  print(param7)
  print(param8)
  print(param9)
  print(param10)
  print("================")
  return 20
  
C_METHOD_TYPE6 = WINFUNCTYPE(c_int32, c_float, c_float, c_float, c_float, c_float, c_float, c_float, c_float, c_float, c_float)

windll.testPy2.fntestPy9(11,C_METHOD_TYPE6(test5))
msg117927 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-03 19:34
Probably this issue is duplicate of #9266.
msg117940 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-04 07:17
I don't have x64 machine, so I cannot test this.
So this is just an idea.

It seems
Modules/_ctypes/libffi_msvc is a bit old.
Modules/_ctypes/libffi/src/x86 is newer.
Maybe this issue can be fixed by using newer one?

Thank you.
msg126698 - (view) Author: Owen (owenl) Date: 2011-01-21 06:46
I tested this issue in Python2.7.1, Python3.1.3 and Python 3.2rc1. It's still can reproduce. Would you please check this "Callback functions" issue?
msg126710 - (view) Author: Christoph Gohlke (cgohlke) Date: 2011-01-21 09:34
The provided example has two problems: The DLL should be loaded as cdll, not windll. C_METHOD_TYPE4 uses c_int32 as parameter type while pyFunc4Type in testPy2.cpp uses LPVOID (64 bit on win-amd64). Even with those corrections the issue remains.
msg126711 - (view) Author: Owen (owenl) Date: 2011-01-21 09:57
yes, I tried lots of types. The issue still happens. The same case in Ubuntu and Mac were works well.
msg126712 - (view) Author: Christoph Gohlke (cgohlke) Date: 2011-01-21 10:01
The patch attached to #8275 fixes this issue and possibly also #9266.

Tested with Python 2.7.1 64 bit on Windows 7.
msg126714 - (view) Author: Owen (owenl) Date: 2011-01-21 10:37
wow~~~ It works on my PC too (Windows 2003 STD x64). Thanks.
msg127093 - (view) Author: stan mihai (stan.mihai) Date: 2011-01-26 09:16
Disabling optimizations doesn't really fix the issue, just hides it, for now.

The problem was an uninitialized variable. Attached is the patch that fixes it.
msg127676 - (view) Author: Owen (owenl) Date: 2011-02-01 02:02
Thanks, this issue has been fixed. :-)
fixing revisions:
r88284 (3.2), r88285 (3.1) and r88286 (2.7)
History
Date User Action Args
2011-02-01 02:02:21owenlsetstatus: open -> closed

messages: + msg127676
resolution: fixed
nosy: theller, ghazel, ocean-city, cgohlke, owenl, stan.mihai
2011-01-26 09:16:03stan.mihaisetfiles: + ctypes_win64.diff

nosy: + stan.mihai
messages: + msg127093

keywords: + patch
2011-01-21 10:37:35owenlsetnosy: theller, ghazel, ocean-city, cgohlke, owenl
messages: + msg126714
2011-01-21 10:01:36cgohlkesetnosy: theller, ghazel, ocean-city, cgohlke, owenl
messages: + msg126712
2011-01-21 09:57:59owenlsetnosy: theller, ghazel, ocean-city, cgohlke, owenl
messages: + msg126711
2011-01-21 09:34:48cgohlkesetnosy: + cgohlke
messages: + msg126710
2011-01-21 06:46:30owenlsetnosy: theller, ghazel, ocean-city, owenl
messages: + msg126698
2010-10-04 07:17:08ocean-citysetmessages: + msg117940
2010-10-03 19:36:41ghazelsetnosy: + ghazel
2010-10-03 19:34:03ocean-citysetnosy: + ocean-city
messages: + msg117927
2010-09-19 08:27:14owenlsetfiles: + testPy2.dll

messages: + msg116851
2010-09-19 01:35:32owenlsetmessages: + msg116847
2010-09-18 03:43:46terry.reedysetnosy: + theller
2010-09-17 09:13:24owenlsettype: behavior
2010-09-17 09:11:56owenlcreate