Skip to content

Commit 3071867

Browse files
authored
gh-106320: Remove private _PyMem API (#107187)
Move private _PyMem functions to the internal C API (pycore_pymem.h): * _PyMem_GetCurrentAllocatorName() * _PyMem_RawStrdup() * _PyMem_RawWcsdup() * _PyMem_Strdup() No longer export these functions. Move pymem_getallocatorsname() function from _testcapi to _testinternalcapi, since the API moved to the internal C API.
1 parent d27eb1e commit 3071867

File tree

8 files changed

+38
-35
lines changed

8 files changed

+38
-35
lines changed

Include/cpython/pymem.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
77
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
88
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
99

10-
/* Try to get the allocators name set by _PyMem_SetupAllocators(). */
11-
PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);
12-
13-
/* strdup() using PyMem_RawMalloc() */
14-
PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
15-
16-
/* strdup() using PyMem_Malloc() */
17-
PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
18-
19-
/* wcsdup() using PyMem_RawMalloc() */
20-
PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
21-
2210

2311
typedef enum {
2412
/* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */

Include/internal/pycore_pymem.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pymem.h" // PyMemAllocatorName
11+
// Try to get the allocators name set by _PyMem_SetupAllocators().
12+
// Return NULL if unknown.
13+
// Export for shared _testinternalcapi extension.
14+
PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);
1215

16+
// strdup() using PyMem_RawMalloc()
17+
extern char* _PyMem_RawStrdup(const char *str);
18+
19+
// strdup() using PyMem_Malloc().
20+
// Export for shared _pickle extension.
21+
PyAPI_FUNC(char*) _PyMem_Strdup(const char *str);
22+
23+
// wcsdup() using PyMem_RawMalloc()
24+
extern wchar_t* _PyMem_RawWcsdup(const wchar_t *str);
1325

1426
typedef struct {
1527
/* We tag each block with an API ID in order to tag API violations */

Lib/test/pythoninfo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,11 @@ def collect_decimal(info_add):
637637

638638
def collect_testcapi(info_add):
639639
try:
640-
import _testcapi
640+
import _testinternalcapi
641641
except ImportError:
642642
return
643643

644-
call_func(info_add, 'pymem.allocator', _testcapi, 'pymem_getallocatorsname')
644+
call_func(info_add, 'pymem.allocator', _testinternalcapi, 'pymem_getallocatorsname')
645645

646646

647647
def collect_resource(info_add):

Lib/test/test_cmd_line.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -717,11 +717,11 @@ def test_xdev(self):
717717

718718
# Memory allocator debug hooks
719719
try:
720-
import _testcapi
720+
import _testinternalcapi
721721
except ImportError:
722722
pass
723723
else:
724-
code = "import _testcapi; print(_testcapi.pymem_getallocatorsname())"
724+
code = "import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())"
725725
with support.SuppressCrashReport():
726726
out = self.run_xdev("-c", code, check_exitcode=False)
727727
if support.with_pymalloc():
@@ -783,7 +783,7 @@ def test_warnings_filter_precedence(self):
783783
self.assertEqual(out, expected_filters)
784784

785785
def check_pythonmalloc(self, env_var, name):
786-
code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())'
786+
code = 'import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())'
787787
env = dict(os.environ)
788788
env.pop('PYTHONDEVMODE', None)
789789
if env_var is not None:

Lib/test/test_sys.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,12 +960,12 @@ def test_debugmallocstats(self):
960960
"sys.getallocatedblocks unavailable on this build")
961961
def test_getallocatedblocks(self):
962962
try:
963-
import _testcapi
963+
import _testinternalcapi
964964
except ImportError:
965965
with_pymalloc = support.with_pymalloc()
966966
else:
967967
try:
968-
alloc_name = _testcapi.pymem_getallocatorsname()
968+
alloc_name = _testinternalcapi.pymem_getallocatorsname()
969969
except RuntimeError as exc:
970970
# "cannot get allocators name" (ex: tracemalloc is used)
971971
with_pymalloc = True

Modules/_testcapi/mem.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,6 @@ test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
440440
Py_RETURN_NONE;
441441
}
442442

443-
static PyObject *
444-
test_pymem_getallocatorsname(PyObject *self, PyObject *args)
445-
{
446-
const char *name = _PyMem_GetCurrentAllocatorName();
447-
if (name == NULL) {
448-
PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
449-
return NULL;
450-
}
451-
return PyUnicode_FromString(name);
452-
}
453-
454443
static PyObject *
455444
test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
456445
{
@@ -589,7 +578,6 @@ tracemalloc_untrack(PyObject *self, PyObject *args)
589578
static PyMethodDef test_methods[] = {
590579
{"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
591580
{"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
592-
{"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
593581
{"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
594582
{"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
595583
{"remove_mem_hooks", remove_mem_hooks, METH_NOARGS,

Modules/_testinternalcapi.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,18 @@ check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
15191519
}
15201520

15211521

1522+
static PyObject *
1523+
test_pymem_getallocatorsname(PyObject *self, PyObject *args)
1524+
{
1525+
const char *name = _PyMem_GetCurrentAllocatorName();
1526+
if (name == NULL) {
1527+
PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
1528+
return NULL;
1529+
}
1530+
return PyUnicode_FromString(name);
1531+
}
1532+
1533+
15221534
static PyMethodDef module_functions[] = {
15231535
{"get_configs", get_configs, METH_NOARGS},
15241536
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -1581,6 +1593,7 @@ static PyMethodDef module_functions[] = {
15811593
{"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS},
15821594
{"check_pyobject_uninitialized_is_freed",
15831595
check_pyobject_uninitialized_is_freed, METH_NOARGS},
1596+
{"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
15841597
{NULL, NULL} /* sentinel */
15851598
};
15861599

Modules/getpath.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* Return the initial module search path. */
22

33
#include "Python.h"
4+
#include "pycore_fileutils.h" // _Py_abspath()
5+
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
6+
#include "pycore_pathconfig.h" // _PyPathConfig_ReadGlobal()
7+
#include "pycore_pymem.h" // _PyMem_RawWcsdup()
8+
49
#include "marshal.h" // PyMarshal_ReadObjectFromString
510
#include "osdefs.h" // DELIM
6-
#include "pycore_initconfig.h"
7-
#include "pycore_fileutils.h"
8-
#include "pycore_pathconfig.h"
911
#include <wchar.h>
1012

1113
#ifdef MS_WINDOWS

0 commit comments

Comments
 (0)