提交 dc00b2b6 authored 作者: James Bergstra's avatar James Bergstra

tricky merge of cc and cmodule

......@@ -830,9 +830,7 @@ class CLinker(link.Linker):
This method is a callback for `ModuleCache.module_from_key`
"""
if location is None:
location = get_compiledir()
#backport
#location = get_compiledir() if location is None else location
location = cmodule.dlimport_workdir(get_compiledir())
mod = self.build_dynamic_module()
get_lock()
try:
......
......@@ -139,6 +139,8 @@ def dlimport(fullpath, suffix=None):
:returns: the dynamically loaded module (from __import__)
"""
if not os.path.isabs(fullpath):
raise ValueError('`fullpath` must be an absolute path', fullpath)
if suffix is None:
if fullpath.endswith('.so'):
suffix = '.so'
......@@ -156,21 +158,26 @@ def dlimport(fullpath, suffix=None):
else:
raise ValueError('path has wrong suffix', (fullpath, suffix))
workdir = fullpath[:-len(module_name)- 1 - len(suffix)]
#debug("WORKDIR", workdir)
#debug("module_name", module_name)
pathcopy = list(sys.path)
sys.path = [workdir]
debug("WORKDIR", workdir)
debug("module_name", module_name)
sys.path[0:0] = [workdir] #insert workdir at beginning (temporarily)
try:
rval = __import__(module_name, {}, {}, [module_name])
if not rval:
error('__import__ failed', fullpath)
raise Exception('__import__ failed', fullpath)
finally:
sys.path = pathcopy
del sys.path[0]
assert fullpath.startswith(rval.__file__)
return rval
def dlimport_workdir(basedir):
"""Return a directory where you should put your .so file for dlimport to be able to load
it, given a basedir which should normally be the result of get_compiledir()"""
return tempfile.mkdtemp(dir=basedir)
def last_access_time(path):
"""Return the number of seconds since the epoch of the last access of a given file"""
return os.stat(path)[stat.ST_ATIME]
......@@ -347,6 +354,11 @@ class ModuleCache(object):
compilelock.release_lock()
def module_from_key(self, key, fn=None):
"""
:param fn: a callable object that will return a module for the key (it is called only if the key isn't in
the cache). This function will be called with a single keyword argument "location"
that is a path on the filesystem wherein the function should write the module.
"""
rval = None
try:
_version, _rest = key
......@@ -366,7 +378,7 @@ class ModuleCache(object):
rval = self.module_from_name[name]
else:
# we have never seen this key before
location = tempfile.mkdtemp(dir=self.dirname)
location = dlimport_workdir(self.dirname)
#debug("LOCATION*", location)
try:
module = fn(location=location) # WILL FAIL FOR BAD C CODE
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论