Created
May 11, 2013 03:47
-
-
Save pjz/5558820 to your computer and use it in GitHub Desktop.
make something show up as a module to some supplied code you're going to exec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src = """ | |
from fakemodule import fakefunc | |
fakefunc() | |
print dir() | |
""" | |
def call_with_fakemodule(usersrc): | |
class fmod(object): | |
@staticmethod | |
def ffunc(): | |
print "called ffunc!" | |
fakefunc = ffunc | |
prefix = """ | |
import sys | |
sys.modules['fakemodule'] = __tempcontext__ | |
del __tempcontext__ | |
del sys | |
""" | |
exec (prefix + usersrc) in { '__tempcontext__' : fmod } | |
call_with_fakemodule(src) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment