Python 2.7 API PyImport_ImportModule() returns NULL 如果脚本包含 dlopen()

Python 2.7 API PyImport_ImportModule() returns NULL if script includes dlopen()

我正在使用 Python C API 来加载一些脚本,但是,在 macOS X 上,我注意到如果脚本包含 dylib,例如 import datetime 那么它将失败:

#include <stdio.h>
#ifdef __APPLE__
#include "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Python.framework/Headers/Python.h"
#else
#include "/usr/include/python2.7/Python.h"
#endif
#include <stdio.h>
#include <string.h>

void testingLoadModuleByPath(char * path, const char * name, const char * content)
{
  char fullpath[256];
  sprintf(fullpath, "%s/%s.py", path, name);
  FILE * file = fopen(fullpath, "w");
  fwrite(content, 1, strlen(content), file);
  fclose(file);
  Py_Initialize();
  PySys_SetPath(path);
  PyObject * h = PyImport_ImportModule(name);
  if (h == NULL) {
    PyErr_Print();
  } else {
    printf("%s: ok\n", name);
  }
  Py_Finalize();
}

int main(int argc, const char * argv[]) {
  testingLoadModuleByPath("/tmp", "mysys", "import sys\nclass Person:\n\thome = ''\n\tdef __init__(self):\n\t\thome = sys.path\n");
  testingLoadModuleByPath("/tmp", "mydat", "import datetime\nclass Person:\n\tbirthday = ''\n\tdef __init__(self):\n\t\tself.birthday = datetime.datetime.now().strftime(\"%A, %d. %B %Y %I:%M%p\")\n");
  return 0;
}

如果成功,两个测试都应该显示 "OK",但这是实际结果:

$ clang -o pytst *.c -lpython2.7 && ./pytst
mysys: ok
Traceback (most recent call last):
  File "/tmp/mydat.py", line 1, in <module>
    import datetime
ImportError: No module named datetime
Program ended with exit code: 0

那么问题来了,如何在Python C API中使用PyImport_ImportModule()导入datetime等.dylib/.so库?

无论如何,代码在 Ubuntu 16.04 LTS 上运行良好:

$ clang -o pytst *.c -lpython2.7 && ./pytst
mysys: ok
mydat: ok

最后我发现只有Python 2.7才有这个问题,Python 3.5+没有这个问题