Skip to content

Commit 4775b6c

Browse files
committed
update tests
1 parent bd05f5f commit 4775b6c

3 files changed

Lines changed: 29 additions & 36 deletions

File tree

Lib/test/test_capi/test_function.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -325,28 +325,6 @@ def annofn(arg: int) -> str:
325325
with self.assertRaises(SystemError):
326326
_testcapi.function_get_annotations(None)
327327

328-
def test_function_old_codes(self):
329-
def f():
330-
pass
331-
332-
def g():
333-
pass
334-
335-
def h():
336-
pass
337-
338-
old_codes = _testcapi.function_get_old_codes(f)
339-
self.assertIsNone(old_codes)
340-
341-
f.__code__ = g.__code__
342-
old_codes = _testcapi.function_get_old_codes(f)
343-
self.assertIsInstance(old_codes, list)
344-
self.assertEqual(len(old_codes), 1)
345-
346-
f.__code__ = h.__code__
347-
old_codes = _testcapi.function_get_old_codes(f)
348-
self.assertEqual(len(old_codes), 2)
349-
350328
# TODO: test PyFunction_New()
351329
# TODO: test PyFunction_NewWithQualName()
352330
# TODO: test PyFunction_SetVectorcall()

Lib/test/test_ctypes/test_functions.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,35 @@ def callback(*args):
463463
callback = proto(callback)
464464
self.assertRaises(ArgumentError, lambda: callback((1, 2, 3, 4), POINT()))
465465

466+
def test_reassign_code_while_running(self):
467+
freevar1 = None
468+
freevar2 = None
469+
470+
def replacement():
471+
freevar1
472+
freevar2
473+
return "replacement"
474+
475+
def original():
476+
original.__code__ = replacement.__code__
477+
return "original"
478+
479+
self.assertEqual(original(), "original")
480+
self.assertEqual(original(), "replacement")
481+
482+
def test_function_code_object_memory_leak(self):
483+
def get_function(i):
484+
ns = {}
485+
exec(f"def f(): return {i}", ns)
486+
return ns["f"]
487+
488+
def f(): pass
489+
490+
for i in range(1000):
491+
f.__code__ = get_function(i).__code__
492+
493+
self.assertEqual(f(), 999)
494+
466495

467496
if __name__ == '__main__':
468497
unittest.main()

Modules/_testcapi/function.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,6 @@ function_get_annotations(PyObject *self, PyObject *func)
130130
}
131131

132132

133-
static PyObject *
134-
function_get_old_codes(PyObject *self, PyObject *func)
135-
{
136-
PyFunctionObject *func_o = (PyFunctionObject *) func;
137-
PyObject *old_codes = func_o->func_old_codes;
138-
if (old_codes == NULL) {
139-
Py_RETURN_NONE;
140-
}
141-
142-
return Py_NewRef(old_codes);
143-
}
144-
145-
146133
static PyMethodDef test_methods[] = {
147134
{"function_get_code", function_get_code, METH_O, NULL},
148135
{"function_get_globals", function_get_globals, METH_O, NULL},
@@ -154,7 +141,6 @@ static PyMethodDef test_methods[] = {
154141
{"function_get_closure", function_get_closure, METH_O, NULL},
155142
{"function_set_closure", function_set_closure, METH_VARARGS, NULL},
156143
{"function_get_annotations", function_get_annotations, METH_O, NULL},
157-
{"function_get_old_codes", function_get_old_codes, METH_O, NULL},
158144
{NULL},
159145
};
160146

0 commit comments

Comments
 (0)