Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_source(obj):
_obj = source._wrap(obj)
assert _obj(1.57) == obj(1.57)
src = source.importable(obj, alias='_f')
# LEEK: for 3.x, locals may not be modified
# (see https://docs.python.org/3.6/library/functions.html#locals)
#
my_locals = locals()
exec(src, globals(), my_locals)
assert my_locals["_f"](1.57) == obj(1.57)
name = source.getname(obj)
assert name == obj.__name__ or src.split("=",1)[0].strip()
def run_source(obj):
_obj = source._wrap(obj)
assert _obj(1.57) == obj(1.57)
src = source.getimportable(obj, alias='_f')
exec(src, globals())
assert _f(1.57) == obj(1.57)
name = source.getname(obj)
assert name == obj.__name__ or src.split("=",1)[0].strip()
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
>>> f = lambda x:x**2
>>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
>>> _f = dill.temp.loadIO_source(pyfile)
>>> _f(4)
16
Optional kwds:
If 'alias' is specified, the object will be renamed to the given string.
"""
from .source import importable, getname
if PY3:
from io import BytesIO as StringIO
else:
from StringIO import StringIO
alias = kwds.pop('alias', '') #XXX: include an alias so a name is known
name = str(alias) or getname(object)
name = "\n#NAME: %s\n" % name
#XXX: assumes kwds['dir'] is writable and on $PYTHONPATH
file = StringIO()
file.write(b(''.join([importable(object, alias=alias),name])))
file.flush()
return file
def getmodule(object, _filename=None, force=False):
"""get the module of the object"""
from inspect import getmodule as getmod
module = getmod(object, _filename)
if module or not force: return module
if PY3: builtins = 'builtins'
else: builtins = '__builtin__'
builtins = __import__(builtins)
from .source import getname
name = getname(object, force=True)
return builtins if name in vars(builtins).keys() else None