Skip to content

Instantly share code, notes, and snippets.

@pjz
Created May 11, 2013 03:47
Show Gist options
  • Save pjz/5558820 to your computer and use it in GitHub Desktop.
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
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