-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-115754: Add Py_GetConstantRef() function #116883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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_CONSTANT_NONE = 0 | ||
Py_CONSTANT_FALSE = 1 | ||
Py_CONSTANT_TRUE = 2 | ||
Py_CONSTANT_ELLIPSIS = 3 | ||
Py_CONSTANT_NOT_IMPLEMENTED = 4 | ||
Py_CONSTANT_ZERO = 5 | ||
Py_CONSTANT_ONE = 6 | ||
Py_CONSTANT_EMPTY_STR = 7 | ||
Py_CONSTANT_EMPTY_BYTES = 8 | ||
Py_CONSTANT_EMPTY_TUPLE = 9 | ||
|
||
INVALID_CONSTANT = Py_CONSTANT_EMPTY_TUPLE + 1 | ||
|
||
|
||
class CAPITest(unittest.TestCase): | ||
def check_get_constant(self, get_constant): | ||
self.assertIs(get_constant(Constant.Py_CONSTANT_NONE), None) | ||
self.assertIs(get_constant(Constant.Py_CONSTANT_FALSE), False) | ||
self.assertIs(get_constant(Constant.Py_CONSTANT_TRUE), True) | ||
self.assertIs(get_constant(Constant.Py_CONSTANT_ELLIPSIS), Ellipsis) | ||
self.assertIs(get_constant(Constant.Py_CONSTANT_NOT_IMPLEMENTED), NotImplemented) | ||
|
||
for constant_id, constant_type, value in ( | ||
(Constant.Py_CONSTANT_ZERO, int, 0), | ||
(Constant.Py_CONSTANT_ONE, int, 1), | ||
(Constant.Py_CONSTANT_EMPTY_STR, str, ""), | ||
(Constant.Py_CONSTANT_EMPTY_BYTES, bytes, b""), | ||
(Constant.Py_CONSTANT_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(SystemError): | ||
get_constant(Constant.INVALID_CONSTANT) | ||
|
||
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 hidden or 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_CONSTANT_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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) { | ||
assert(PyErr_Occurred()); | ||
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) { | ||
assert(PyErr_Occurred()); | ||
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_CONSTANT_NONE)); | ||
assert(Py_False == Py_GetConstant(Py_CONSTANT_FALSE)); | ||
assert(Py_True == Py_GetConstant(Py_CONSTANT_TRUE)); | ||
assert(Py_Ellipsis == Py_GetConstant(Py_CONSTANT_ELLIPSIS)); | ||
assert(Py_NotImplemented == Py_GetConstant(Py_CONSTANT_NOT_IMPLEMENTED)); | ||
// 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 | ||
_PyTestLimitedCAPI_Init_Object(PyObject *m) | ||
{ | ||
if (PyModule_AddFunctions(m, test_methods) < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.