- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,作为一名 Linux 用户,我在 Windows 上遇到了一些我无法解释的非常令人费解的事情。
我有一个类似于此示例的项目结构:
D:\PROJECT
|
| tolkien.py
| __init__.py
|
\---MiddleEarth
| gondor.py
| isengrad.c
| __init__.py
|
\---lib
isengrad.so
问题: 我将 isengrad.c
编译到共享库 isengrad.so
中,然后将其加载到 gondor.py
。我的目标是将 gondor.py
导入 tolkien.py
。
虽然 gondor.py
直接运行时运行完美,但当我导入它时,代码在我通过 ctypes.CDLL
加载共享库时退出,没有任何错误信息。
复制:文件的内容(添加了一些“状态消息”以跟踪问题发生的位置):
isengrad.c:
int isengrad(int hobbit){
return hobbit/2;
}
然后用
编译成 isengrad.soD:\project>chdir MiddleEarth
D:\project\MiddleEarth>gcc -fPIC -shared -o lib/isengrad.so isengrad.c
然后在 gondor.py 中访问共享库:
print("started gondor")
import os, ctypes
path_to_isengrad = "D:/project/MiddleEarth/lib/isengrad.so"
print("gondor loads isengrad")
gondor = ctypes.CDLL(path_to_isengrad) # <--- crashes here when imported, not when ran directly
print("gondor loaded isengrad")
gondor.isengrad.argtypes = (ctypes.c_int,)
def faramir(hobbit):
catched_hobbits = gondor.isengrad(hobbit)
return catched_hobbits
if __name__ == '__main__':
print(faramir(5))
print("gondor ran")
print("gondor finished")
然后导入 tolkien.py:
print("started tolkien")
from MiddleEarth import gondor
print("tolkien imported gondor")
got = gondor.faramir(4)
print(got)
print("tolkien worked")
现在检查当我直接使用 gondor.py
VS 当我将它导入 tolkien.py
时会发生什么:
D:\project>python MiddleEarth/gondor.py
started gondor
gondor loads isengrad
gondor loaded isengrad
2
gondor ran
gondor finished
D:\project>python tolkien.py
started tolkien
started gondor
gondor loads isengrad
D:\project>
直接运行完全没有问题。但是导入它会导致整个事情在加载共享库时崩溃而没有任何文字和回溯。这怎么会发生?我什至对共享库的路径进行了硬编码,所以不同的工作目录应该不是问题......我在 Kubuntu 上的同一个项目没有任何问题,所以这可能是一些与 Windows 相关的东西。
环境:
Python 3.7.3(默认,2019 年 3 月 27 日,17:13:21)[MSC v.1915 64 位 (AMD64)]::Anaconda, Inc. on win32
Windows 10 10.0.17134 Build 17134
(安装在 C:)最佳答案
从我看到这个问题的那一刻起,我就想说这是未定义的行为 (UB)。 Python 带有它的 C 运行时(UCRTLib),而 Cygwin .dll自带。在进程中混合使用编译器和 C 运行时,通常会导致灾难。
找到官方说法[Cygwin]: 6.15. Can I link with both MSVCRT*.DLL and cygwin1.dll? (强调是我的):
No, you must use one or the other, they are mutually exclusive.
检查 [SO]: How to circumvent Windows Universal CRT headers dependency on vcruntime.h (@CristiFati's answer)有关 MSVCRT*.DLL
的更多详细信息现在,UB 的美妙之处在于它描述了一种看似随机的行为。
我准备了一个综合示例(稍微修改了您的代码)。
isengrad.c:
#if defined(_WIN32)
# define ISENGRAD_EXPORT_API __declspec(dllexport)
#else
# define ISENGRAD_EXPORT_API
#endif
ISENGRAD_EXPORT_API int isengrad(int hobbit) {
return hobbit / 2;
}
script0.py:
#!/usr/bin/env python3
import sys
import ctypes
dll_name = "./lib/isengrad_{0:s}_{1:03d}.dll".format(sys.argv[1][:3] if sys.argv else sys.platform[:3].lower(), ctypes.sizeof(ctypes.c_void_p) * 8)
print("Attempting to load: {0:s}".format(dll_name))
isengrad_dll = ctypes.CDLL(dll_name)
print("DLL Loaded")
def main():
isengrad_func = isengrad_dll.isengrad
isengrad_func.argtypes = [ctypes.c_int]
isengrad_func.restype = ctypes.c_int
res = isengrad_func(46)
print("{0:s} returned {1:}".format(isengrad_func.__name__, res))
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")
script1.py:
#!/usr/bin/env python3
import sys
import script0
def main():
pass
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")
输出:
Cygwin 32 位:
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q056855348]> ~/sopr.sh
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[032bit prompt]> gcc -shared -fPIC -o lib/isengrad_cyg_032.dll isengrad.c
[032bit prompt]> ls lib/*.dll
lib/isengrad_cyg_032.dll lib/isengrad_cyg_064.dll lib/isengrad_win_032.dll lib/isengrad_win_064.dll
[032bit prompt]>
[032bit prompt]> python3 script0.py cyg
Attempting to load: ./lib/isengrad_cyg_032.dll
DLL Loaded
Python 3.6.4 (default, Jan 7 2018, 17:45:56) [GCC 6.4.0] 32bit on cygwin
isengrad returned 23
Done.
[032bit prompt]>
[032bit prompt]> python3 script1.py cyg
Attempting to load: ./lib/isengrad_cyg_032.dll
DLL Loaded
Python 3.6.4 (default, Jan 7 2018, 17:45:56) [GCC 6.4.0] 32bit on cygwin
Done.
[032bit prompt]>
[032bit prompt]> python3 script0.py win
Attempting to load: ./lib/isengrad_win_032.dll
DLL Loaded
Python 3.6.4 (default, Jan 7 2018, 17:45:56) [GCC 6.4.0] 32bit on cygwin
isengrad returned 23
Done.
[032bit prompt]>
[032bit prompt]> python3 script1.py win
Attempting to load: ./lib/isengrad_win_032.dll
DLL Loaded
Python 3.6.4 (default, Jan 7 2018, 17:45:56) [GCC 6.4.0] 32bit on cygwin
Done.
Cygwin 64 位:
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q056855348]> ~/sopr.sh
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[064bit prompt]> gcc -shared -fPIC -o lib/isengrad_cyg_064.dll isengrad.c
[064bit prompt]> ls lib/*.dll
lib/isengrad_cyg_032.dll lib/isengrad_cyg_064.dll lib/isengrad_win_032.dll lib/isengrad_win_064.dll
[064bit prompt]>
[064bit prompt]> python3 script0.py cyg
Attempting to load: ./lib/isengrad_cyg_064.dll
DLL Loaded
Python 3.6.8 (default, Feb 14 2019, 22:09:48) [GCC 7.4.0] 64bit on cygwin
isengrad returned 23
Done.
[064bit prompt]>
[064bit prompt]> python3 script1.py cyg
Attempting to load: ./lib/isengrad_cyg_064.dll
DLL Loaded
Python 3.6.8 (default, Feb 14 2019, 22:09:48) [GCC 7.4.0] 64bit on cygwin
Done.
[064bit prompt]>
[064bit prompt]> python3 script0.py win
Attempting to load: ./lib/isengrad_win_064.dll
DLL Loaded
Python 3.6.8 (default, Feb 14 2019, 22:09:48) [GCC 7.4.0] 64bit on cygwin
isengrad returned 23
Done.
[064bit prompt]>
[064bit prompt]> python3 script1.py win
Attempting to load: ./lib/isengrad_win_064.dll
DLL Loaded
Python 3.6.8 (default, Feb 14 2019, 22:09:48) [GCC 7.4.0] 64bit on cygwin
Done.
命令:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056855348]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[prompt]> dir /b lib
[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.14
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
[prompt]> cl /nologo /DDLL isengrad.c /link /NOLOGO /DLL /OUT:lib\isengrad_win_064.dll
isengrad.c
Creating library lib\isengrad_win_064.lib and object lib\isengrad_win_064.exp
[prompt]>
[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x86
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.14
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
[prompt]> cl /nologo /DDLL isengrad.c /link /NOLOGO /DLL /OUT:lib\isengrad_win_032.dll
isengrad.c
Creating library lib\isengrad_win_032.lib and object lib\isengrad_win_032.exp
[prompt]> dir /b lib\*.dll
isengrad_cyg_032.dll
isengrad_cyg_064.dll
isengrad_win_032.dll
isengrad_win_064.dll
[prompt]> set _PATH=%PATH%
[prompt]> :: Python 32bit
[prompt]> set PATH=%_PATH%;e:\Install\x86\Cygwin\Cygwin\Version\bin
[prompt]> "e:\Work\Dev\VEnvs\py_032_03.07.03_test0\Scripts\python.exe" script0.py win
Attempting to load: ./lib/isengrad_win_032.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] 32bit on win32
isengrad returned 23
Done.
[prompt]> "e:\Work\Dev\VEnvs\py_032_03.07.03_test0\Scripts\python.exe" script1.py win
Attempting to load: ./lib/isengrad_win_032.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] 32bit on win32
Done.
[prompt]> "e:\Work\Dev\VEnvs\py_032_03.07.03_test0\Scripts\python.exe" script0.py cyg
Attempting to load: ./lib/isengrad_cyg_032.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] 32bit on win32
isengrad returned 23
Done.
[prompt]> "e:\Work\Dev\VEnvs\py_032_03.07.03_test0\Scripts\python.exe" script1.py cyg
Attempting to load: ./lib/isengrad_cyg_032.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] 32bit on win32
Done.
[prompt]> :: Python 64bit
[prompt]> set PATH=%_PATH%;c:\Install\x64\Cygwin\Cygwin\AllVers\bin
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" script0.py win
Attempting to load: ./lib/isengrad_win_064.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32
isengrad returned 23
Done.
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" script1.py win
Attempting to load: ./lib/isengrad_win_064.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32
Done.
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" script0.py cyg
Attempting to load: ./lib/isengrad_cyg_064.dll
DLL Loaded
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32
isengrad returned 23
Done.
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" script1.py cyg
Attempting to load: ./lib/isengrad_cyg_064.dll
[prompt]>
[prompt]> echo %errorlevel%
-1073741819
正如所见,交叉编译器 .exe - .dll 在 7 种(共 8 种)情况下工作(在 64 位 上崩溃 Win Python with script1.py),而同一个编译器在所有 8 个中都能正常工作。
因此,我建议在使用此类环境时,尽量使用于构建各个部分的编译器保持一致(或至少兼容)。
我只是想到了在 64bit 上可能出错的一个原因:sizeof(long)
通常不同(以下大小以字节为单位):
sizeof(long double)
也是一样(即 2 * sizeof(long)
)。
因此,如果 Cygwin .dll 公开了一些大于 2 ** 64 的 long 值> (1 << 64),在Win 进程中会被截断,此时可能会发生崩溃.从理论上讲,这种情况也应该影响相反的情况,但事实并非如此。
还有其他因素可能会导致此行为,例如默认内存对齐等。
关于python - Paradoxon : silent crash on Python's ctypes. CDLL 在导入时出现,但在直接运行时却没有 - 这怎么可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855348/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!