Skip to content

Commit 5876736

Browse files
committed
tentative fix to py3's dependency on a filename->module->__loader__ chain
for executing inspect.findsource ... --HG-- branch : trunk
1 parent f97e082 commit 5876736

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

py/_code/source.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import inspect, tokenize
44
import py
5+
from types import ModuleType
56
cpy_compile = compile
67

78
try:
@@ -212,8 +213,15 @@ def compile(self, filename=None, mode='exec',
212213
else:
213214
if flag & _AST_FLAG:
214215
return co
215-
from types import ModuleType
216216
lines = [(x + "\n") for x in self.lines]
217+
if sys.version_info[0] >= 3:
218+
# XXX py3's inspect.getsourcefile() checks for a module
219+
# and a pep302 __loader__ ... we don't have a module
220+
# at code compile-time so we need to fake it here
221+
m = ModuleType("_pycodecompile_pseudo_module")
222+
py.std.inspect.modulesbyfile[filename] = None
223+
py.std.sys.modules[None] = m
224+
m.__loader__ = 1
217225
py.std.linecache.cache[filename] = (1, None, lines, filename)
218226
return co
219227

0 commit comments

Comments
 (0)