提交 674485e1 authored 作者: Garming Sam's avatar Garming Sam

Allow dynamic module loading using special .so prefix for PyPy

CPython extensions built for PyPy should be named used a special suffix which includes the variant of Python e.g. .pypy-41.so. These can be loaded at runtime (import module) without doing anything special and will not be confused with regular Python CPython modules.
上级 096b9d3a
...@@ -269,16 +269,23 @@ def dlimport(fullpath, suffix=None): ...@@ -269,16 +269,23 @@ def dlimport(fullpath, suffix=None):
if not os.path.isabs(fullpath): if not os.path.isabs(fullpath):
raise ValueError('`fullpath` must be an absolute path', fullpath) raise ValueError('`fullpath` must be an absolute path', fullpath)
if suffix is None: if suffix is None:
if fullpath.endswith('.so'): suffix = ''
suffix = '.so'
elif fullpath.endswith('.pyd'): dist_suffix = distutils.sysconfig.get_config_var("SO")
suffix = '.pyd' if dist_suffix is not None and dist_suffix != '':
elif fullpath.endswith('.dll'): if fullpath.endswith(dist_suffix):
suffix = '.dll' suffix = dist_suffix
elif fullpath.endswith('.py'):
suffix = '.py' if suffix == '':
else: if fullpath.endswith('.so'):
suffix = '' suffix = '.so'
elif fullpath.endswith('.pyd'):
suffix = '.pyd'
elif fullpath.endswith('.dll'):
suffix = '.dll'
elif fullpath.endswith('.py'):
suffix = '.py'
rval = None rval = None
if fullpath.endswith(suffix): if fullpath.endswith(suffix):
module_name = '.'.join(fullpath.split(os.path.sep)[-2:])[:-len(suffix)] module_name = '.'.join(fullpath.split(os.path.sep)[-2:])[:-len(suffix)]
...@@ -1675,22 +1682,30 @@ def std_lib_dirs_and_libs(): ...@@ -1675,22 +1682,30 @@ def std_lib_dirs_and_libs():
elif sys.platform == 'darwin': elif sys.platform == 'darwin':
std_lib_dirs_and_libs.data = [], [] std_lib_dirs_and_libs.data = [], []
else: else:
# assume Linux if platform.python_implementation() == 'PyPy':
# Typical include directory: /usr/include/python2.6 # Assume Linux (note: Ubuntu doesn't ship this .so)
libname = "pypy-c"
# Unfortunately the only convention of this .so is that it appears
# next to the location of the interpreter binary.
libdir = os.path.dirname(os.path.realpath(sys.executable))
else:
# Assume Linux
# Typical include directory: /usr/include/python2.6
# get the name of the python library (shared object)
# get the name of the python library (shared object) libname = distutils.sysconfig.get_config_var("LDLIBRARY")
libname = distutils.sysconfig.get_config_var("LDLIBRARY")
if libname.startswith("lib"): if libname.startswith("lib"):
libname = libname[3:] libname = libname[3:]
# remove extension if present # remove extension if present
if libname.endswith(".so"): if libname.endswith(".so"):
libname = libname[:-3] libname = libname[:-3]
elif libname.endswith(".a"): elif libname.endswith(".a"):
libname = libname[:-2] libname = libname[:-2]
libdir = distutils.sysconfig.get_config_var("LIBDIR") libdir = distutils.sysconfig.get_config_var("LIBDIR")
std_lib_dirs_and_libs.data = [libname], [libdir] std_lib_dirs_and_libs.data = [libname], [libdir]
...@@ -2277,9 +2292,18 @@ class GCC_compiler(Compiler): ...@@ -2277,9 +2292,18 @@ class GCC_compiler(Compiler):
if not src_code.endswith('\n'): if not src_code.endswith('\n'):
cppfile.write('\n') cppfile.write('\n')
lib_filename = os.path.join( if platform.python_implementation() == 'PyPy':
location, suffix = '.' + get_lib_extension()
'%s.%s' % (module_name, get_lib_extension()))
dist_suffix = distutils.sysconfig.get_config_var("SO")
if dist_suffix is not None and dist_suffix != '':
suffix = dist_suffix
filepath = '%s%s' % (module_name, suffix)
else:
filepath = '%s.%s' % (module_name, get_lib_extension())
lib_filename = os.path.join(location, filepath)
_logger.debug('Generating shared lib %s', lib_filename) _logger.debug('Generating shared lib %s', lib_filename)
cmd = [theano.config.cxx, get_gcc_shared_library_arg(), '-g'] cmd = [theano.config.cxx, get_gcc_shared_library_arg(), '-g']
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论