File size: 7,059 Bytes
8193465 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#!/usr/bin/env python
#
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 2008-2016 California Institute of Technology.
# Copyright (c) 2016-2025 The Uncertainty Quantification Foundation.
# License: 3-clause BSD. The full license text is available at:
# - https://github.com/uqfoundation/dill/blob/master/LICENSE
from dill.source import getsource, getname, _wrap, getimport
from dill.source import importable
from dill._dill import IS_PYPY
import sys
PY310b = 0x30a00b1
f = lambda x: x**2
def g(x): return f(x) - x
def h(x):
def g(x): return x
return g(x) - x
class Foo(object):
def bar(self, x):
return x*x+x
_foo = Foo()
def add(x,y):
return x+y
# 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
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
# test itself
def test_itself():
assert getimport(getimport)=='from dill.source import getimport\n'
# builtin functions and objects
def test_builtin():
assert getimport(pow) == 'pow\n'
assert getimport(100) == '100\n'
assert getimport(True) == 'True\n'
assert getimport(pow, builtin=True) == 'from builtins import pow\n'
assert getimport(100, builtin=True) == '100\n'
assert getimport(True, builtin=True) == 'True\n'
# this is kinda BS... you can't import a None
assert getimport(None) == 'None\n'
assert getimport(None, builtin=True) == 'None\n'
# other imported functions
def test_imported():
from math import sin
assert getimport(sin) == 'from math import sin\n'
# interactively defined functions
def test_dynamic():
assert getimport(add) == 'from %s import add\n' % __name__
# interactive lambdas
assert getimport(squared) == 'from %s import squared\n' % __name__
# classes and class instances
def test_classes():
from io import BytesIO as StringIO
y = "from _io import BytesIO\n"
x = y if (IS_PYPY or sys.hexversion >= PY310b) else "from io import BytesIO\n"
s = StringIO()
assert getimport(StringIO) == x
assert getimport(s) == y
# interactively defined classes and class instances
assert getimport(Foo) == 'from %s import Foo\n' % __name__
assert getimport(_foo) == 'from %s import Foo\n' % __name__
# test importable
def test_importable():
assert importable(add, source=False) == 'from %s import add\n' % __name__
assert importable(squared, source=False) == 'from %s import squared\n' % __name__
assert importable(Foo, source=False) == 'from %s import Foo\n' % __name__
assert importable(Foo.bar, source=False) == 'from %s import bar\n' % __name__
assert importable(_foo.bar, source=False) == 'from %s import bar\n' % __name__
assert importable(None, source=False) == 'None\n'
assert importable(100, source=False) == '100\n'
assert importable(add, source=True) == 'def add(x,y):\n return x+y\n'
assert importable(squared, source=True) == 'squared = lambda x:x**2\n'
assert importable(None, source=True) == 'None\n'
assert importable(Bar, source=True) == 'class Bar:\n pass\n'
assert importable(Foo, source=True) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
assert importable(Foo.bar, source=True) == 'def bar(self, x):\n return x*x+x\n'
assert importable(Foo.bar, source=False) == 'from %s import bar\n' % __name__
assert importable(Foo.bar, alias='memo', source=False) == 'from %s import bar as memo\n' % __name__
assert importable(Foo, alias='memo', source=False) == 'from %s import Foo as memo\n' % __name__
assert importable(squared, alias='memo', source=False) == 'from %s import squared as memo\n' % __name__
assert importable(squared, alias='memo', source=True) == 'memo = squared = lambda x:x**2\n'
assert importable(add, alias='memo', source=True) == 'def add(x,y):\n return x+y\n\nmemo = add\n'
assert importable(None, alias='memo', source=True) == 'memo = None\n'
assert importable(100, alias='memo', source=True) == 'memo = 100\n'
assert importable(add, builtin=True, source=False) == 'from %s import add\n' % __name__
assert importable(squared, builtin=True, source=False) == 'from %s import squared\n' % __name__
assert importable(Foo, builtin=True, source=False) == 'from %s import Foo\n' % __name__
assert importable(Foo.bar, builtin=True, source=False) == 'from %s import bar\n' % __name__
assert importable(_foo.bar, builtin=True, source=False) == 'from %s import bar\n' % __name__
assert importable(None, builtin=True, source=False) == 'None\n'
assert importable(100, builtin=True, source=False) == '100\n'
def test_numpy():
try:
import numpy as np
y = np.array
x = y([1,2,3])
assert importable(x, source=False) == 'from numpy import array\narray([1, 2, 3])\n'
assert importable(y, source=False) == 'from %s import array\n' % y.__module__
assert importable(x, source=True) == 'from numpy import array\narray([1, 2, 3])\n'
assert importable(y, source=True) == 'from %s import array\n' % y.__module__
y = np.int64
x = y(0)
assert importable(x, source=False) == 'from numpy import int64\nint64(0)\n'
assert importable(y, source=False) == 'from %s import int64\n' % y.__module__
assert importable(x, source=True) == 'from numpy import int64\nint64(0)\n'
assert importable(y, source=True) == 'from %s import int64\n' % y.__module__
y = np.bool_
x = y(0)
import warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=FutureWarning)
warnings.filterwarnings('ignore', category=DeprecationWarning)
if hasattr(np, 'bool'): b = 'bool_' if np.bool is bool else 'bool'
else: b = 'bool_'
assert importable(x, source=False) == 'from numpy import %s\n%s(False)\n' % (b,b)
assert importable(y, source=False) == 'from %s import %s\n' % (y.__module__,b)
assert importable(x, source=True) == 'from numpy import %s\n%s(False)\n' % (b,b)
assert importable(y, source=True) == 'from %s import %s\n' % (y.__module__,b)
except ImportError: pass
#NOTE: if before getimport(pow), will cause pow to throw AssertionError
def test_foo():
assert importable(_foo, source=True).startswith("import dill\nclass Foo(object):\n def bar(self, x):\n return x*x+x\ndill.loads(")
if __name__ == '__main__':
test_getsource()
test_itself()
test_builtin()
test_imported()
test_dynamic()
test_classes()
test_importable()
test_numpy()
test_foo()
|