forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
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_GetConstant() function
Add Py_GetConstant() and Py_GetConstantBorrowed() 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_GetConstant() and Py_GetConstantBorrowed() functions.
- Loading branch information
Showing
22 changed files
with
295 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
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_IDX = 0 | ||
Py_FALSE_IDX = 1 | ||
Py_TRUE_IDX = 2 | ||
Py_ELLIPSIS_IDX = 3 | ||
Py_NOT_IMPLEMENTED_IDX = 4 | ||
Py_ZERO_IDX = 5 | ||
Py_ONE_IDX = 6 | ||
Py_EMPTY_STR_IDX = 7 | ||
Py_EMPTY_BYTES_IDX = 8 | ||
Py_EMPTY_TUPLE_IDX = 9 | ||
|
||
INVALID_IDX = Py_EMPTY_TUPLE_IDX + 1 | ||
|
||
|
||
class CAPITest(unittest.TestCase): | ||
def check_get_constant(self, get_constant): | ||
self.assertIs(get_constant(Constant.Py_NONE_IDX), None) | ||
self.assertIs(get_constant(Constant.Py_FALSE_IDX), False) | ||
self.assertIs(get_constant(Constant.Py_TRUE_IDX), True) | ||
self.assertIs(get_constant(Constant.Py_ELLIPSIS_IDX), Ellipsis) | ||
self.assertIs(get_constant(Constant.Py_NOT_IMPLEMENTED_IDX), NotImplemented) | ||
|
||
for constant_id, constant_type, value in ( | ||
(Constant.Py_ZERO_IDX, int, 0), | ||
(Constant.Py_ONE_IDX, int, 1), | ||
(Constant.Py_EMPTY_STR_IDX, str, ""), | ||
(Constant.Py_EMPTY_BYTES_IDX, bytes, b""), | ||
(Constant.Py_EMPTY_TUPLE_IDX, 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_IDX) | ||
|
||
def test_get_constant(self): | ||
self.check_get_constant(_testlimitedcapi.get_constant) | ||
|
||
def test_get_constant_borrowed(self): | ||
self.check_get_constant(_testlimitedcapi.get_constant_borrowed) | ||
|
||
|
||
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_GetConstant` and :c:func:`Py_GetConstantBorrowed` functions to | ||
get constants. For example, ``Py_GetConstant(Py_ZERO_IDX)`` 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,80 @@ | ||
// 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 obj; | ||
} | ||
|
||
|
||
/* Test Py_GetConstantBorrowed() */ | ||
static PyObject * | ||
get_constant_borrowed(PyObject *Py_UNUSED(module), PyObject *args) | ||
{ | ||
int constant_id; | ||
if (!PyArg_ParseTuple(args, "i", &constant_id)) { | ||
return NULL; | ||
} | ||
|
||
PyObject *obj = Py_GetConstantBorrowed(constant_id); | ||
if (obj == NULL) { | ||
PyErr_SetString(PyExc_ValueError, "invalid constant identifier"); | ||
return NULL; | ||
} | ||
return Py_NewRef(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_IDX)); | ||
assert(Py_False == Py_GetConstant(Py_FALSE_IDX)); | ||
assert(Py_True == Py_GetConstant(Py_TRUE_IDX)); | ||
assert(Py_Ellipsis == Py_GetConstant(Py_ELLIPSIS_IDX)); | ||
assert(Py_NotImplemented == Py_GetConstant(Py_NOT_IMPLEMENTED_IDX)); | ||
// Other constants are tested in test_capi.test_object | ||
Py_RETURN_NONE; | ||
} | ||
|
||
static PyMethodDef test_methods[] = { | ||
{"get_constant", get_constant, METH_VARARGS}, | ||
{"get_constant_borrowed", get_constant_borrowed, 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
Oops, something went wrong.