summaryrefslogtreecommitdiff
path: root/Lib/test/test_cmd_line.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-11-29 17:20:38 +0100
committerGitHub <noreply@github.com>2017-11-29 17:20:38 +0100
commit5d39e0429029324cae90bba2f19fb689b007c7d6 (patch)
treed414a4bc635c750d07c93d94835d932d3524c062 /Lib/test/test_cmd_line.py
parentc15bb49d71f97d400b295d88e5b075e89cb8ba20 (diff)
downloadcpython-git-5d39e0429029324cae90bba2f19fb689b007c7d6.tar.gz
bpo-32030: Rework memory allocators (#4625)
* Fix _PyMem_SetupAllocators("debug"): always restore allocators to the defaults, rather than only caling _PyMem_SetupDebugHooks(). * Add _PyMem_SetDefaultAllocator() helper to set the "default" allocator. * Add _PyMem_GetAllocatorsName(): get the name of the allocators * main() now uses debug hooks on memory allocators if Py_DEBUG is defined, rather than calling directly malloc() * Document default memory allocators in C API documentation * _Py_InitializeCore() now fails with a fatal user error if PYTHONMALLOC value is an unknown memory allocator, instead of failing with a fatal internal error. * Add new tests on the PYTHONMALLOC environment variable * Add support.with_pymalloc() * Add the _testcapi.WITH_PYMALLOC constant and expose it as support.with_pymalloc(). * sysconfig.get_config_var('WITH_PYMALLOC') doesn't work on Windows, so replace it with support.with_pymalloc(). * pythoninfo: add _testcapi collector for pymem
Diffstat (limited to 'Lib/test/test_cmd_line.py')
-rw-r--r--Lib/test/test_cmd_line.py52
1 files changed, 50 insertions, 2 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 7f95fccf79..96405e70af 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -5,6 +5,7 @@
import os
import subprocess
import sys
+import sysconfig
import tempfile
import unittest
from test import support
@@ -559,10 +560,14 @@ class CmdLineTest(unittest.TestCase):
except ImportError:
pass
else:
- code = "import _testcapi; _testcapi.pymem_api_misuse()"
+ code = "import _testcapi; print(_testcapi.pymem_getallocatorsname())"
with support.SuppressCrashReport():
out = self.run_xdev("-c", code, check_exitcode=False)
- self.assertIn("Debug memory block at address p=", out)
+ if support.with_pymalloc():
+ alloc_name = "pymalloc_debug"
+ else:
+ alloc_name = "malloc_debug"
+ self.assertEqual(out, alloc_name)
try:
import faulthandler
@@ -573,6 +578,49 @@ class CmdLineTest(unittest.TestCase):
out = self.run_xdev("-c", code)
self.assertEqual(out, "True")
+ def check_pythonmalloc(self, env_var, name):
+ code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())'
+ env = dict(os.environ)
+ if env_var is not None:
+ env['PYTHONMALLOC'] = env_var
+ else:
+ env.pop('PYTHONMALLOC', None)
+ args = (sys.executable, '-c', code)
+ proc = subprocess.run(args,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=True,
+ env=env)
+ self.assertEqual(proc.stdout.rstrip(), name)
+ self.assertEqual(proc.returncode, 0)
+
+ def test_pythonmalloc(self):
+ # Test the PYTHONMALLOC environment variable
+ pydebug = hasattr(sys, "gettotalrefcount")
+ pymalloc = support.with_pymalloc()
+ if pymalloc:
+ default_name = 'pymalloc_debug' if pydebug else 'pymalloc'
+ default_name_debug = 'pymalloc_debug'
+ else:
+ default_name = 'malloc_debug' if pydebug else 'malloc'
+ default_name_debug = 'malloc_debug'
+
+ tests = [
+ (None, default_name),
+ ('debug', default_name_debug),
+ ('malloc', 'malloc'),
+ ('malloc_debug', 'malloc_debug'),
+ ]
+ if pymalloc:
+ tests.extend((
+ ('pymalloc', 'pymalloc'),
+ ('pymalloc_debug', 'pymalloc_debug'),
+ ))
+
+ for env_var, name in tests:
+ with self.subTest(env_var=env_var, name=name):
+ self.check_pythonmalloc(env_var, name)
+
class IgnoreEnvironmentTest(unittest.TestCase):