forked from python/cpython
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-115754: Add Py_GetConstantRef() function
Add Py_GetConstantRef() and Py_GetConstant() functions. In the limited C API version 3.13, getting Py_None, Py_False, Py_True, Py_Ellipsis and Py_NotImplemented singletons is now implemented as function calls at the stable ABI level to hide implementation details. Getting these constants still return borrowed references. Add _testlimitedcapi/object.c and test_capi/test_object.py to test Py_GetConstantRef() and Py_GetConstant() functions.
- Loading branch information
Showing
19 changed files
with
286 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import enum | ||
import unittest | ||
from test.support import import_helper | ||
|
||
_testlimitedcapi = import_helper.import_module('_testlimitedcapi') | ||
|
||
|
||
class Constant(enum.IntEnum): | ||
Py_NONE = 0 | ||
Py_FALSE = 1 | ||
Py_TRUE = 2 | ||
Py_ELLIPSIS = 3 | ||
Py_NOT_IMPLEMENTED = 4 | ||
Py_ZERO = 5 | ||
Py_ONE = 6 | ||
Py_EMPTY_STR = 7 | ||
Py_EMPTY_BYTES = 8 | ||
Py_EMPTY_TUPLE = 9 | ||
|
||
INVALID = Py_EMPTY_TUPLE + 1 | ||
|
||
|
||
class CAPITest(unittest.TestCase): | ||
def check_get_constant(self, get_constant): | ||
self.assertIs(get_constant(Constant.Py_NONE), None) | ||
self.assertIs(get_constant(Constant.Py_FALSE), False) | ||
self.assertIs(get_constant(Constant.Py_TRUE), True) | ||
self.assertIs(get_constant(Constant.Py_ELLIPSIS), Ellipsis) | ||
self.assertIs(get_constant(Constant.Py_NOT_IMPLEMENTED), NotImplemented) | ||
|
||
for constant_id, constant_type, value in ( | ||
(Constant.Py_ZERO, int, 0), | ||
(Constant.Py_ONE, int, 1), | ||
(Constant.Py_EMPTY_STR, str, ""), | ||
(Constant.Py_EMPTY_BYTES, bytes, b""), | ||
(Constant.Py_EMPTY_TUPLE, tuple, ()), | ||
): | ||
with self.subTest(constant_id=constant_id): | ||
obj = get_constant(constant_id) | ||
self.assertEqual(type(obj), constant_type, obj) | ||
self.assertEqual(obj, value) | ||
|
||
with self.assertRaises(ValueError): | ||
get_constant(Constant.INVALID) | ||
|
||
def test_get_constant_ref(self): | ||
self.check_get_constant(_testlimitedcapi.get_constant_ref) | ||
|
||
def test_get_constant(self): | ||
self.check_get_constant(_testlimitedcapi.get_constant) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/C API/2024-03-15-23-55-24.gh-issue-115754.xnzc__.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Add :c:func:`Py_GetConstantRef` and :c:func:`Py_GetConstant` functions to | ||
get constants. For example, ``Py_GetConstantRef(Py_ZERO)`` returns a | ||
:term:`strong reference` to the constant zero. Patch by Victor Stinner. |
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/C API/2024-03-15-23-57-33.gh-issue-115754.zLdv82.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
In the limited C API version 3.13, getting ``Py_None``, ``Py_False``, | ||
``Py_True``, ``Py_Ellipsis`` and ``Py_NotImplemented`` singletons is now | ||
implemented as function calls at the stable ABI level to hide implementation | ||
details. Getting these constants still return borrowed references. Patch by | ||
Victor Stinner. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Need limited C API version 3.13 for Py_GetConstant() | ||
#include "pyconfig.h" // Py_GIL_DISABLED | ||
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API ) | ||
# define Py_LIMITED_API 0x030d0000 | ||
#endif | ||
|
||
#include "parts.h" | ||
#include "util.h" | ||
|
||
|
||
/* Test Py_GetConstant() */ | ||
static PyObject * | ||
get_constant(PyObject *Py_UNUSED(module), PyObject *args) | ||
{ | ||
int constant_id; | ||
if (!PyArg_ParseTuple(args, "i", &constant_id)) { | ||
return NULL; | ||
} | ||
|
||
PyObject *obj = Py_GetConstant(constant_id); | ||
if (obj == NULL) { | ||
PyErr_SetString(PyExc_ValueError, "invalid constant identifier"); | ||
return NULL; | ||
} | ||
return Py_NewRef(obj); | ||
} | ||
|
||
|
||
/* Test Py_GetConstantRef() */ | ||
static PyObject * | ||
get_constant_ref(PyObject *Py_UNUSED(module), PyObject *args) | ||
{ | ||
int constant_id; | ||
if (!PyArg_ParseTuple(args, "i", &constant_id)) { | ||
return NULL; | ||
} | ||
|
||
PyObject *obj = Py_GetConstantRef(constant_id); | ||
if (obj == NULL) { | ||
PyErr_SetString(PyExc_ValueError, "invalid constant identifier"); | ||
return NULL; | ||
} | ||
return obj; | ||
} | ||
|
||
|
||
/* Test constants */ | ||
static PyObject * | ||
test_constants(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||
{ | ||
// Test that implementation of constants in the limited C API: | ||
// check that the C code compiles. | ||
// | ||
// Test also that constants and Py_GetConstant() return the same | ||
// objects. | ||
assert(Py_None == Py_GetConstant(Py_NONE)); | ||
assert(Py_False == Py_GetConstant(Py_FALSE)); | ||
assert(Py_True == Py_GetConstant(Py_TRUE)); | ||
assert(Py_Ellipsis == Py_GetConstant(Py_ELLIPSIS)); | ||
assert(Py_NotImplemented == Py_GetConstant(Py_NOT_IMPLEMENTED)); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
static PyMethodDef test_methods[] = { | ||
{"get_constant", get_constant, METH_VARARGS}, | ||
{"get_constant_ref", get_constant_ref, METH_VARARGS}, | ||
{"test_constants", test_constants, METH_NOARGS}, | ||
{NULL}, | ||
}; | ||
|
||
int | ||
_PyTestCapi_Init_Object(PyObject *m) | ||
{ | ||
if (PyModule_AddFunctions(m, test_methods) < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.