6
def inspect_isroutine():
8
>>> inspect_isroutine()
12
return inspect.isroutine(inspect_isroutine)
15
def inspect_isfunction():
17
>>> inspect_isfunction()
22
print isinstance(inspect_isfunction, types.FunctionType)
23
return inspect.isfunction(inspect_isfunction)
26
def inspect_isbuiltin():
28
>>> inspect_isbuiltin()
33
print isinstance(inspect_isfunction, types.BuiltinFunctionType)
34
return inspect.isbuiltin(inspect_isbuiltin)
37
def inspect_signature(a, b, c=123, *, d=234):
39
>>> sig = inspect_signature(1, 2)
40
>>> list(sig.parameters)
42
>>> sig.parameters['c'].default == 123
44
>>> sig.parameters['d'].default == 234
48
return inspect.signature(inspect_signature)
51
# def test___signature__(a, b, c=123, *, d=234):
53
# >>> sig = test___signature__(1, 2)
54
# >>> list(sig.parameters)
56
# >>> sig.parameters['c'].default == 123
58
# >>> sig.parameters['d'].default == 234
61
# return inspect_signature.__signature__
66
>>> test_dict.foo = 123
67
>>> test_dict.__dict__
69
>>> test_dict.__dict__ = {'bar': 321}
70
>>> test_dict.__dict__
72
>>> test_dict.func_dict
78
>>> test_name.__name__
80
>>> test_name.func_name
82
>>> test_name.__name__ = 123 #doctest:+ELLIPSIS
83
Traceback (most recent call last):
84
TypeError: __name__ must be set to a ... object
85
>>> test_name.__name__ = 'foo'
86
>>> test_name.__name__
92
>>> del test_doc.__doc__
94
>>> test_doc.__doc__ = 'docstring'
104
>>> d = {test_hash: 123}
109
>>> hash(test_hash) == hash(test_hash)
116
>>> test_closure.func_closure is None
122
>>> test_globals.func_globals is not None
124
>>> 'test_globals' in test_globals.func_globals or test_globals.func_globals
126
>>> 'test_name' in test_globals.func_globals or test_globals.func_globals
128
>>> 'not there' not in test_globals.func_globals or test_globals.func_globals
130
>>> try: test_globals.func_globals = {}
131
... except (AttributeError, TypeError): pass
132
... else: assert 0, 'FAILED'
138
>>> pickle.loads(pickle.dumps(test_reduce))()
141
return 'Hello, world!'
143
def test_method(self):
148
>>> BindingTest.test_method = test_method
149
>>> BindingTest.test_method() #doctest:+ELLIPSIS
150
Traceback (most recent call last):
152
>>> BindingTest().test_method()
153
<BindingTest instance>
156
return '<BindingTest instance>'
164
varnames = code.co_varnames
169
names = code.co_names
176
def cy_two_args(x, b):
178
def cy_default_args(x=1, b=2):
183
>>> def no_arg(): l = m = 1
184
>>> def one_arg(a): l = m = 1
185
>>> def two_args(x, b): l = m = 1
186
>>> def default_args(x=1, b=2): l = m = 1
188
>>> codeof(no_arg).co_argcount
190
>>> codeof(cy_no_arg).co_argcount
192
>>> print(codeof(no_arg).co_name)
194
>>> print(codeof(cy_no_arg).co_name)
198
>>> codeof(cy_no_arg).co_names
200
>>> varnamesof(no_arg)
202
>>> codeof(cy_no_arg).co_varnames
205
>>> codeof(one_arg).co_argcount
207
>>> codeof(cy_one_arg).co_argcount
209
>>> print(codeof(one_arg).co_name)
211
>>> print(codeof(cy_one_arg).co_name)
215
>>> codeof(cy_one_arg).co_names
217
>>> varnamesof(one_arg)
219
>>> codeof(cy_one_arg).co_varnames
222
>>> codeof(two_args).co_argcount
224
>>> codeof(cy_two_args).co_argcount
226
>>> namesof(two_args)
228
>>> codeof(cy_two_args).co_names
230
>>> varnamesof(two_args)
232
>>> codeof(cy_two_args).co_varnames
235
>>> codeof(default_args).co_argcount
237
>>> codeof(cy_default_args).co_argcount
239
>>> namesof(default_args)
241
>>> codeof(cy_default_args).co_names
243
>>> varnamesof(default_args)
245
>>> codeof(cy_default_args).co_varnames
250
def test_annotations(a: "test", b: "other" = 2, c: 123 = 4) -> "ret":
252
>>> isinstance(test_annotations.__annotations__, dict)
254
>>> sorted(test_annotations.__annotations__.items())
255
[('a', "'test'"), ('b', "'other'"), ('c', '123'), ('return', "'ret'")]
257
>>> def func_b(): return 42
258
>>> def func_c(): return 99
259
>>> inner = test_annotations(1, func_b, func_c)
260
>>> sorted(inner.__annotations__.items())
261
[('return', 'c()'), ('x', "'banana'"), ('y', 'b()')]
263
>>> inner.__annotations__ = {234: 567}
264
>>> inner.__annotations__
266
>>> inner.__annotations__ = None
267
>>> inner.__annotations__
269
>>> inner.__annotations__ = 321
270
Traceback (most recent call last):
271
TypeError: __annotations__ must be set to a dict object
272
>>> inner.__annotations__
275
>>> inner = test_annotations(1, func_b, func_c)
276
>>> sorted(inner.__annotations__.items())
277
[('return', 'c()'), ('x', "'banana'"), ('y', 'b()')]
278
>>> inner.__annotations__['abc'] = 66
279
>>> sorted(inner.__annotations__.items())
280
[('abc', 66), ('return', 'c()'), ('x', "'banana'"), ('y', 'b()')]
282
>>> inner = test_annotations(1, func_b, func_c)
283
>>> sorted(inner.__annotations__.items())
284
[('return', 'c()'), ('x', "'banana'"), ('y', 'b()')]
286
def inner(x: "banana", y: b()) -> c():
292
"Decorator to add 1 to the last argument of the function call"
294
args = args[:-1] + (args[-1] + 1,)
299
def test_decorated(x):
301
>>> test_decorated(0)
308
def test_decorated2(x):
310
>>> test_decorated2(0)
316
cdef class TestDecoratedMethods:
320
>>> TestDecoratedMethods().test(0)
329
>>> TestDecoratedMethods().test2(0)
334
def test_calls(self, x):
336
>>> TestDecoratedMethods().test_calls(2)
339
return self.test(x) + self.test2(x*10)
342
cdef class TestUnboundMethodCdef:
344
>>> C = TestUnboundMethodCdef
345
>>> C.meth is C.__dict__["meth"]
347
>>> TestUnboundMethodCdef.meth() # doctest:+ELLIPSIS
348
Traceback (most recent call last):
354
class TestUnboundMethod:
356
>>> C = TestUnboundMethod
357
>>> C.meth is C.__dict__["meth"]
359
>>> TestUnboundMethod.meth() # doctest:+ELLIPSIS
360
Traceback (most recent call last):
366
class TestStaticmethod(object):
368
>>> x = TestStaticmethod()
371
>>> x.staticmeth.__get__(42)()
375
def staticmeth(arg): return arg
378
cdef class TestOptimisedBuiltinMethod:
380
>>> obj = TestOptimisedBuiltinMethod()
388
def append(self, arg):
391
def call(self, arg, obj=None):
392
(obj or self).append(arg+1) # optimistically optimised => uses fast fallback method call
396
"""Dummy decorator for `test_firstlineno_decorated_function`"""
402
def test_firstlineno_decorated_function():
404
check that `test_firstlineno_decorated_function` starts 5 lines below `do_nothing`
406
>>> test_firstlineno_decorated_function()
409
l1 = do_nothing.__code__.co_firstlineno
410
l2 = test_firstlineno_decorated_function.__code__.co_firstlineno