【发布时间】:2012-02-08 20:03:44
【问题描述】:
我正在尝试通过 ctypes 将文件描述符传递给在 fd 上执行写入的 C 函数。在linux上它可以工作。在 Windows 上它没有,我不明白为什么(我没有作为 Windows 开发人员的经验)
//C func signature:
void fun(struct bah *opaque, int fd)
来自python(细节省略):
mylib.fun.argtypes = [POINTER(bah), c_int]
fh = open(filename,'wb')
#doesn't work on windows, works on linux/unix
mylib.fun(some_ctypes_struct, fh.fileno())
#doesn't work on windows
mylib.fun(bah_struct, ctypes.cdll.msvcrt._open(filename,_O_FLAGS_MASK, ACCMASK)
#doesn't work
mylib.fun(bah_struct, os.open(...))
程序因断言_osfile(fh) & FOPEN 失败而在write()s 上死机
cl.exe: 16.00.40219.01 用于 x86 python 2.7.2 msc v.1500 32bit
我该怎么做?不,我不想将 open() 卸载到 lib。我想以一种与平台无关的安全方式传递已经打开的文件描述符。
附加信息,以防万一: 该库是 tinycdb,我使用简短的 cmake 规范和少量脏补丁将其快速移植到 Windows,以使 getopt 和 dll 导出工作。该库和 exe 工具按预期工作(经过测试)。 tinycdb 的 python ctypes 包装器按预期在 linux 上工作。 windows给了我眼球。他不会接受 fd 是一个有效的描述符,即使我在用它自己的 (msvcrt) _open libcall 打开它之后传递它。
当然,如果我在库中打开()/关闭()文件,但我无法更改 API,一切正常。
【问题讨论】:
-
Python 和您的 DLL 是否使用相同的 C 库 (msvcrt)? (dependencywalker.com 是一种检查方式。)
标签: python windows ctypes file-descriptor