Skip to content

Commit

Permalink
tentative fix to py3's dependency on a filename->module->__loader__ c…
Browse files Browse the repository at this point in the history
…hain

for executing inspect.findsource ...

--HG--
branch : trunk
  • Loading branch information
hpk42 committed May 14, 2010
1 parent f97e082 commit 5876736
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion py/_code/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import inspect, tokenize
import py
from types import ModuleType
cpy_compile = compile

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

Expand Down

0 comments on commit 5876736

Please sign in to comment.