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'
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_source(obj): _obj = source._wrap(obj) assert _obj(1.57) == obj(1.57) src = source.getimportable(obj, alias='_f') exec src in globals(), locals() assert _f(1.57) == obj(1.57) name = source.getname(obj) assert name == obj.__name__ or src.split("=",1)[0].strip()
def test1(obj): _obj = source._wrap(obj) assert _obj(1.57) == obj(1.57) src = source.getsource(obj, alias="_f") exec src in globals(), locals() assert _f(1.57) == obj(1.57) name = source._get_name(obj) assert name == obj.__name__ or src.split("=", 1)[0].strip()
def test_source(obj): _obj = source._wrap(obj) assert _obj(1.57) == obj(1.57) src = source.importable(obj, alias='_f') exec src in globals(), locals() assert _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 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.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()
# yes, same as 'f', but things are tricky when it comes to pointers squared = lambda x:x**2 class Bar: pass _bar = Bar() # inspect.getsourcelines # dill.source.getblocks 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 assert getimportable(add) == 'from %s import add\n' % __name__ assert getimportable(squared) == 'from %s import squared\n' % __name__ assert getimportable(Foo) == 'from %s import Foo\n' % __name__ assert getimportable(Foo.bar) == 'from %s import bar\n' % __name__ assert getimportable(_foo.bar) == 'from %s import bar\n' % __name__ assert getimportable(None) == 'None\n'
class Bar: pass _bar = Bar() # inspect.getsourcelines # dill.source.getblocks 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 assert getimportable(add) == 'from %s import add\n' % __name__ assert getimportable(squared) == 'from %s import squared\n' % __name__ assert getimportable(Foo) == 'from %s import Foo\n' % __name__ assert getimportable(Foo.bar) == 'from %s import bar\n' % __name__ assert getimportable(_foo.bar) == 'from %s import bar\n' % __name__