summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-06-07 14:22:21 (GMT)
committerGitHub <noreply@github.com>2019-06-07 14:22:21 (GMT)
commit3bf0f3ad2046ac674d8e8a2c074a5a8b3327797d (patch)
tree36188df3d87d41c2075183f46678b013cf59fdf2 /Lib
parent3f345c39255dc3823dd989d4e3c93b12d18c44e0 (diff)
downloadcpython-3bf0f3ad2046ac674d8e8a2c074a5a8b3327797d.zip
cpython-3bf0f3ad2046ac674d8e8a2c074a5a8b3327797d.tar.gz
cpython-3bf0f3ad2046ac674d8e8a2c074a5a8b3327797d.tar.bz2
bpo-37169: Rewrite _PyObject_IsFreed() unit tests (GH-13888)
Replace two Python function calls with a single one to ensure that no memory allocation is done between the invalid object is created and when _PyObject_IsFreed() is called.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_capi.py29
1 files changed, 15 insertions, 14 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 43d7a08..6a20f47 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -705,28 +705,29 @@ class PyMemDebugTests(unittest.TestCase):
code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()'
self.check_malloc_without_gil(code)
- def check_pyobject_is_freed(self, func):
- code = textwrap.dedent('''
+ def check_pyobject_is_freed(self, func_name):
+ code = textwrap.dedent(f'''
import gc, os, sys, _testcapi
# Disable the GC to avoid crash on GC collection
gc.disable()
- obj = _testcapi.{func}()
- error = (_testcapi.pyobject_is_freed(obj) == False)
- # Exit immediately to avoid a crash while deallocating
- # the invalid object
- os._exit(int(error))
+ try:
+ _testcapi.{func_name}()
+ # Exit immediately to avoid a crash while deallocating
+ # the invalid object
+ os._exit(0)
+ except _testcapi.error:
+ os._exit(1)
''')
- code = code.format(func=func)
assert_python_ok('-c', code, PYTHONMALLOC=self.PYTHONMALLOC)
- def test_pyobject_is_freed_uninitialized(self):
- self.check_pyobject_is_freed('pyobject_uninitialized')
+ def test_pyobject_uninitialized_is_freed(self):
+ self.check_pyobject_is_freed('check_pyobject_uninitialized_is_freed')
- def test_pyobject_is_freed_forbidden_bytes(self):
- self.check_pyobject_is_freed('pyobject_forbidden_bytes')
+ def test_pyobject_forbidden_bytes_is_freed(self):
+ self.check_pyobject_is_freed('check_pyobject_forbidden_bytes_is_freed')
- def test_pyobject_is_freed_free(self):
- self.check_pyobject_is_freed('pyobject_freed')
+ def test_pyobject_freed_is_freed(self):
+ self.check_pyobject_is_freed('check_pyobject_freed_is_freed')
class PyMemMallocDebugTests(PyMemDebugTests):