blob: b9c4f8bdcb382e7daba7bc279b44b9ef1b749c5b [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001"""This module includes tests of the code object representation.
2
3>>> def f(x):
4... def g(y):
5... return x + y
6... return g
7...
8
Neal Norwitz221085d2007-02-25 20:55:47 +00009>>> dump(f.__code__)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010name: f
11argcount: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010012posonlyargcount: 0
Guido van Rossum4f72a782006-10-27 23:31:49 +000013kwonlyargcount: 0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014names: ()
15varnames: ('x', 'g')
16cellvars: ('x',)
17freevars: ()
18nlocals: 2
19flags: 3
Antoine Pitrou86a36b52011-11-25 18:56:07 +010020consts: ('None', '<code object g>', "'f.<locals>.g'")
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021
Neal Norwitz221085d2007-02-25 20:55:47 +000022>>> dump(f(4).__code__)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023name: g
24argcount: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010025posonlyargcount: 0
Guido van Rossum4f72a782006-10-27 23:31:49 +000026kwonlyargcount: 0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027names: ()
28varnames: ('y',)
29cellvars: ()
30freevars: ('x',)
31nlocals: 1
32flags: 19
33consts: ('None',)
34
35>>> def h(x, y):
36... a = x + y
37... b = x - y
38... c = a * b
39... return c
Tim Peters536cf992005-12-25 23:18:31 +000040...
Guido van Rossum4f72a782006-10-27 23:31:49 +000041
Neal Norwitz221085d2007-02-25 20:55:47 +000042>>> dump(h.__code__)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043name: h
44argcount: 2
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010045posonlyargcount: 0
Guido van Rossum4f72a782006-10-27 23:31:49 +000046kwonlyargcount: 0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047names: ()
48varnames: ('x', 'y', 'a', 'b', 'c')
49cellvars: ()
50freevars: ()
51nlocals: 5
52flags: 67
53consts: ('None',)
54
55>>> def attrs(obj):
Guido van Rossum7131f842007-02-09 20:13:25 +000056... print(obj.attr1)
57... print(obj.attr2)
58... print(obj.attr3)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059
Neal Norwitz221085d2007-02-25 20:55:47 +000060>>> dump(attrs.__code__)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061name: attrs
62argcount: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010063posonlyargcount: 0
Guido van Rossum4f72a782006-10-27 23:31:49 +000064kwonlyargcount: 0
Georg Brandl88fc6642007-02-09 21:28:07 +000065names: ('print', 'attr1', 'attr2', 'attr3')
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000066varnames: ('obj',)
67cellvars: ()
68freevars: ()
69nlocals: 1
70flags: 67
71consts: ('None',)
72
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073>>> def optimize_away():
74... 'doc string'
75... 'not a docstring'
76... 53
Guido van Rossume2a383d2007-01-15 16:59:06 +000077... 0x53
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078
Neal Norwitz221085d2007-02-25 20:55:47 +000079>>> dump(optimize_away.__code__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080name: optimize_away
81argcount: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010082posonlyargcount: 0
Guido van Rossum4f72a782006-10-27 23:31:49 +000083kwonlyargcount: 0
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084names: ()
85varnames: ()
86cellvars: ()
87freevars: ()
88nlocals: 0
89flags: 67
90consts: ("'doc string'", 'None')
91
Guido van Rossum4f72a782006-10-27 23:31:49 +000092>>> def keywordonly_args(a,b,*,k1):
93... return a,b,k1
94...
95
Neal Norwitz221085d2007-02-25 20:55:47 +000096>>> dump(keywordonly_args.__code__)
Guido van Rossum4f72a782006-10-27 23:31:49 +000097name: keywordonly_args
98argcount: 2
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010099posonlyargcount: 0
Guido van Rossum4f72a782006-10-27 23:31:49 +0000100kwonlyargcount: 1
101names: ()
102varnames: ('a', 'b', 'k1')
103cellvars: ()
104freevars: ()
105nlocals: 3
106flags: 67
107consts: ('None',)
108
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100109>>> def posonly_args(a,b,/,c):
110... return a,b,c
111...
112
113>>> dump(posonly_args.__code__)
114name: posonly_args
Pablo Galindocd74e662019-06-01 18:08:04 +0100115argcount: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100116posonlyargcount: 2
117kwonlyargcount: 0
118names: ()
119varnames: ('a', 'b', 'c')
120cellvars: ()
121freevars: ()
122nlocals: 3
123flags: 67
124consts: ('None',)
125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126"""
127
Nick Coghlan078f1812017-12-03 11:12:20 +1000128import inspect
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300129import sys
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700130import threading
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000131import unittest
Collin Winter4222e9c2010-03-18 22:46:40 +0000132import weakref
Victor Stinnera4b091e2017-06-23 15:08:55 +0200133try:
134 import ctypes
135except ImportError:
136 ctypes = None
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700137from test.support import (run_doctest, run_unittest, cpython_only,
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300138 check_impl_detail, gc_collect)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000139
Collin Winter4222e9c2010-03-18 22:46:40 +0000140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141def consts(t):
142 """Yield a doctest-safe sequence of object reprs."""
143 for elt in t:
144 r = repr(elt)
145 if r.startswith("<code object"):
146 yield "<code object %s>" % elt.co_name
147 else:
148 yield r
149
150def dump(co):
151 """Print out a text representation of a code object."""
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100152 for attr in ["name", "argcount", "posonlyargcount",
153 "kwonlyargcount", "names", "varnames",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000154 "cellvars", "freevars", "nlocals", "flags"]:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000155 print("%s: %s" % (attr, getattr(co, "co_" + attr)))
156 print("consts:", tuple(consts(co.co_consts)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157
Nick Coghlan078f1812017-12-03 11:12:20 +1000158# Needed for test_closure_injection below
159# Defined at global scope to avoid implicitly closing over __class__
160def external_getitem(self, i):
161 return f"Foreign getitem: {super().__getitem__(i)}"
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000162
163class CodeTest(unittest.TestCase):
164
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200165 @cpython_only
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000166 def test_newempty(self):
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200167 import _testcapi
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000168 co = _testcapi.code_newempty("filename", "funcname", 15)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000169 self.assertEqual(co.co_filename, "filename")
170 self.assertEqual(co.co_name, "funcname")
171 self.assertEqual(co.co_firstlineno, 15)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000172
Nick Coghlan078f1812017-12-03 11:12:20 +1000173 @cpython_only
174 def test_closure_injection(self):
175 # From https://bugs.python.org/issue32176
Victor Stinnera9f05d62019-05-24 23:57:23 +0200176 from types import FunctionType
Nick Coghlan078f1812017-12-03 11:12:20 +1000177
178 def create_closure(__class__):
179 return (lambda: __class__).__closure__
180
181 def new_code(c):
182 '''A new code object with a __class__ cell added to freevars'''
Victor Stinnera9f05d62019-05-24 23:57:23 +0200183 return c.replace(co_freevars=c.co_freevars + ('__class__',))
Nick Coghlan078f1812017-12-03 11:12:20 +1000184
185 def add_foreign_method(cls, name, f):
186 code = new_code(f.__code__)
187 assert not f.__closure__
188 closure = create_closure(cls)
189 defaults = f.__defaults__
190 setattr(cls, name, FunctionType(code, globals(), name, defaults, closure))
191
192 class List(list):
193 pass
194
195 add_foreign_method(List, "__getitem__", external_getitem)
196
197 # Ensure the closure injection actually worked
198 function = List.__getitem__
199 class_ref = function.__closure__[0].cell_contents
200 self.assertIs(class_ref, List)
201
202 # Ensure the code correctly indicates it accesses a free variable
203 self.assertFalse(function.__code__.co_flags & inspect.CO_NOFREE,
204 hex(function.__code__.co_flags))
205
206 # Ensure the zero-arg super() call in the injected method works
207 obj = List([1, 2, 3])
208 self.assertEqual(obj[0], "Foreign getitem: 1")
Serhiy Storchaka09f3d082016-10-04 18:17:22 +0300209
Victor Stinnera9f05d62019-05-24 23:57:23 +0200210 def test_constructor(self):
211 def func(): pass
212 co = func.__code__
213 CodeType = type(co)
214
215 # test code constructor
Miss Islington (bot)10955352021-08-23 12:21:06 -0700216 CodeType(co.co_argcount,
Victor Stinnera9f05d62019-05-24 23:57:23 +0200217 co.co_posonlyargcount,
218 co.co_kwonlyargcount,
219 co.co_nlocals,
220 co.co_stacksize,
221 co.co_flags,
222 co.co_code,
223 co.co_consts,
224 co.co_names,
225 co.co_varnames,
226 co.co_filename,
227 co.co_name,
228 co.co_firstlineno,
229 co.co_lnotab,
230 co.co_freevars,
231 co.co_cellvars)
232
233 def test_replace(self):
234 def func():
235 x = 1
236 return x
237 code = func.__code__
238
239 # different co_name, co_varnames, co_consts
240 def func2():
241 y = 2
242 return y
Adam Johnson892221b2019-11-19 19:45:20 +0000243 code2 = func2.__code__
Victor Stinnera9f05d62019-05-24 23:57:23 +0200244
245 for attr, value in (
246 ("co_argcount", 0),
247 ("co_posonlyargcount", 0),
248 ("co_kwonlyargcount", 0),
249 ("co_nlocals", 0),
250 ("co_stacksize", 0),
251 ("co_flags", code.co_flags | inspect.CO_COROUTINE),
252 ("co_firstlineno", 100),
253 ("co_code", code2.co_code),
254 ("co_consts", code2.co_consts),
255 ("co_names", ("myname",)),
256 ("co_varnames", code2.co_varnames),
257 ("co_freevars", ("freevar",)),
258 ("co_cellvars", ("cellvar",)),
259 ("co_filename", "newfilename"),
260 ("co_name", "newname"),
Mark Shannon877df852020-11-12 09:43:29 +0000261 ("co_linetable", code2.co_linetable),
Victor Stinnera9f05d62019-05-24 23:57:23 +0200262 ):
263 with self.subTest(attr=attr, value=value):
264 new_code = code.replace(**{attr: value})
265 self.assertEqual(getattr(new_code, attr), value)
266
Mark Shannonc76da792021-04-29 13:12:51 +0100267 def test_empty_linetable(self):
268 def func():
269 pass
270 new_code = code = func.__code__.replace(co_linetable=b'')
271 self.assertEqual(list(new_code.co_lines()), [])
272
Victor Stinnera9f05d62019-05-24 23:57:23 +0200273
Serhiy Storchaka09f3d082016-10-04 18:17:22 +0300274def isinterned(s):
275 return s is sys.intern(('_' + s + '_')[1:-1])
276
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300277class CodeConstsTest(unittest.TestCase):
278
279 def find_const(self, consts, value):
280 for v in consts:
281 if v == value:
282 return v
Serhiy Storchaka09f3d082016-10-04 18:17:22 +0300283 self.assertIn(value, consts) # raises an exception
284 self.fail('Should never be reached')
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300285
286 def assertIsInterned(self, s):
Serhiy Storchaka09f3d082016-10-04 18:17:22 +0300287 if not isinterned(s):
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300288 self.fail('String %r is not interned' % (s,))
289
Serhiy Storchaka09f3d082016-10-04 18:17:22 +0300290 def assertIsNotInterned(self, s):
291 if isinterned(s):
292 self.fail('String %r is interned' % (s,))
293
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300294 @cpython_only
295 def test_interned_string(self):
296 co = compile('res = "str_value"', '?', 'exec')
297 v = self.find_const(co.co_consts, 'str_value')
298 self.assertIsInterned(v)
299
300 @cpython_only
301 def test_interned_string_in_tuple(self):
302 co = compile('res = ("str_value",)', '?', 'exec')
303 v = self.find_const(co.co_consts, ('str_value',))
304 self.assertIsInterned(v[0])
305
306 @cpython_only
307 def test_interned_string_in_frozenset(self):
308 co = compile('res = a in {"str_value"}', '?', 'exec')
309 v = self.find_const(co.co_consts, frozenset(('str_value',)))
310 self.assertIsInterned(tuple(v)[0])
311
312 @cpython_only
313 def test_interned_string_default(self):
314 def f(a='str_value'):
315 return a
316 self.assertIsInterned(f())
317
Serhiy Storchaka09f3d082016-10-04 18:17:22 +0300318 @cpython_only
319 def test_interned_string_with_null(self):
320 co = compile(r'res = "str\0value!"', '?', 'exec')
321 v = self.find_const(co.co_consts, 'str\0value!')
322 self.assertIsNotInterned(v)
323
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000324
Collin Winter4222e9c2010-03-18 22:46:40 +0000325class CodeWeakRefTest(unittest.TestCase):
326
327 def test_basic(self):
328 # Create a code object in a clean environment so that we know we have
329 # the only reference to it left.
330 namespace = {}
331 exec("def f(): pass", globals(), namespace)
332 f = namespace["f"]
333 del namespace
334
335 self.called = False
336 def callback(code):
337 self.called = True
338
339 # f is now the last reference to the function, and through it, the code
340 # object. While we hold it, check that we can create a weakref and
341 # deref it. Then delete it, and check that the callback gets called and
342 # the reference dies.
343 coderef = weakref.ref(f.__code__, callback)
344 self.assertTrue(bool(coderef()))
345 del f
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300346 gc_collect() # For PyPy or other GCs.
Collin Winter4222e9c2010-03-18 22:46:40 +0000347 self.assertFalse(bool(coderef()))
348 self.assertTrue(self.called)
349
350
Victor Stinnera4b091e2017-06-23 15:08:55 +0200351if check_impl_detail(cpython=True) and ctypes is not None:
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700352 py = ctypes.pythonapi
353 freefunc = ctypes.CFUNCTYPE(None,ctypes.c_voidp)
354
355 RequestCodeExtraIndex = py._PyEval_RequestCodeExtraIndex
356 RequestCodeExtraIndex.argtypes = (freefunc,)
357 RequestCodeExtraIndex.restype = ctypes.c_ssize_t
358
359 SetExtra = py._PyCode_SetExtra
360 SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
361 SetExtra.restype = ctypes.c_int
362
363 GetExtra = py._PyCode_GetExtra
364 GetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t,
365 ctypes.POINTER(ctypes.c_voidp))
366 GetExtra.restype = ctypes.c_int
367
368 LAST_FREED = None
369 def myfree(ptr):
370 global LAST_FREED
371 LAST_FREED = ptr
372
373 FREE_FUNC = freefunc(myfree)
374 FREE_INDEX = RequestCodeExtraIndex(FREE_FUNC)
375
376 class CoExtra(unittest.TestCase):
377 def get_func(self):
378 # Defining a function causes the containing function to have a
379 # reference to the code object. We need the code objects to go
380 # away, so we eval a lambda.
381 return eval('lambda:42')
382
383 def test_get_non_code(self):
384 f = self.get_func()
385
386 self.assertRaises(SystemError, SetExtra, 42, FREE_INDEX,
387 ctypes.c_voidp(100))
388 self.assertRaises(SystemError, GetExtra, 42, FREE_INDEX,
389 ctypes.c_voidp(100))
390
391 def test_bad_index(self):
392 f = self.get_func()
393 self.assertRaises(SystemError, SetExtra, f.__code__,
394 FREE_INDEX+100, ctypes.c_voidp(100))
395 self.assertEqual(GetExtra(f.__code__, FREE_INDEX+100,
396 ctypes.c_voidp(100)), 0)
397
398 def test_free_called(self):
399 # Verify that the provided free function gets invoked
400 # when the code object is cleaned up.
401 f = self.get_func()
402
403 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(100))
404 del f
405 self.assertEqual(LAST_FREED, 100)
406
407 def test_get_set(self):
408 # Test basic get/set round tripping.
409 f = self.get_func()
410
411 extra = ctypes.c_voidp()
412
413 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(200))
414 # reset should free...
415 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(300))
416 self.assertEqual(LAST_FREED, 200)
417
418 extra = ctypes.c_voidp()
419 GetExtra(f.__code__, FREE_INDEX, extra)
420 self.assertEqual(extra.value, 300)
421 del f
422
423 def test_free_different_thread(self):
424 # Freeing a code object on a different thread then
425 # where the co_extra was set should be safe.
426 f = self.get_func()
427 class ThreadTest(threading.Thread):
428 def __init__(self, f, test):
429 super().__init__()
430 self.f = f
431 self.test = test
432 def run(self):
433 del self.f
434 self.test.assertEqual(LAST_FREED, 500)
435
436 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(500))
437 tt = ThreadTest(f, self)
438 del f
439 tt.start()
440 tt.join()
441 self.assertEqual(LAST_FREED, 500)
442
Pablo Galindof00828a2019-05-09 16:52:02 +0100443
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444def test_main(verbose=None):
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 from test import test_code
446 run_doctest(test_code, verbose)
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700447 tests = [CodeTest, CodeConstsTest, CodeWeakRefTest]
Victor Stinnera4b091e2017-06-23 15:08:55 +0200448 if check_impl_detail(cpython=True) and ctypes is not None:
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700449 tests.append(CoExtra)
450 run_unittest(*tests)
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000451
Collin Winter4222e9c2010-03-18 22:46:40 +0000452if __name__ == "__main__":
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000453 test_main()