Skip to content

Commit 09d15df

Browse files
committed
Rename constants: Py_CONSTANT_ZERO
1 parent 81b9685 commit 09d15df

File tree

9 files changed

+69
-69
lines changed

9 files changed

+69
-69
lines changed

Doc/c-api/object.rst

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ Object Protocol
1414
1515
Constant identifiers:
1616
17-
.. c:macro: Py_NONE_IDX
18-
.. c:macro: Py_FALSE_IDX
19-
.. c:macro: Py_TRUE_IDX
20-
.. c:macro: Py_ELLIPSIS_IDX
21-
.. c:macro: Py_NOT_IMPLEMENTED_IDX
22-
.. c:macro: Py_ZERO_IDX
23-
.. c:macro: Py_ONE_IDX
24-
.. c:macro: Py_EMPTY_STR_IDX
25-
.. c:macro: Py_EMPTY_BYTES_IDX
26-
.. c:macro: Py_EMPTY_TUPLE_IDX
17+
.. c:macro: Py_CONSTANT_NONE
18+
.. c:macro: Py_CONSTANT_FALSE
19+
.. c:macro: Py_CONSTANT_TRUE
20+
.. c:macro: Py_CONSTANT_ELLIPSIS
21+
.. c:macro: Py_CONSTANT_NOT_IMPLEMENTED
22+
.. c:macro: Py_CONSTANT_ZERO
23+
.. c:macro: Py_CONSTANT_ONE
24+
.. c:macro: Py_CONSTANT_EMPTY_STR
25+
.. c:macro: Py_CONSTANT_EMPTY_BYTES
26+
.. c:macro: Py_CONSTANT_EMPTY_TUPLE
2727
2828
.. versionadded:: 3.13
2929

Doc/whatsnew/3.13.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ New Features
16981698
(Contributed by Victor Stinner in :gh:`111696`.)
16991699

17001700
* Add :c:func:`Py_GetConstant` and :c:func:`Py_GetConstantBorrowed` functions
1701-
to get constants. For example, ``Py_GetConstant(Py_ZERO_IDX)`` returns a
1701+
to get constants. For example, ``Py_GetConstant(Py_CONSTANT_ZERO)`` returns a
17021702
:term:`strong reference` to the constant zero.
17031703
(Contributed by Victor Stinner in :gh:`115754`.)
17041704

Include/boolobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ PyAPI_DATA(PyLongObject) _Py_TrueStruct;
1919

2020
/* Use these macros */
2121
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
22-
# define Py_False Py_GetConstantBorrowed(Py_FALSE_IDX)
23-
# define Py_True Py_GetConstantBorrowed(Py_TRUE_IDX)
22+
# define Py_False Py_GetConstantBorrowed(Py_CONSTANT_FALSE)
23+
# define Py_True Py_GetConstantBorrowed(Py_CONSTANT_TRUE)
2424
#else
2525
# define Py_False _PyObject_CAST(&_Py_FalseStruct)
2626
# define Py_True _PyObject_CAST(&_Py_TrueStruct)

Include/object.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -1068,16 +1068,16 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
10681068
#endif
10691069

10701070

1071-
#define Py_NONE_IDX 0
1072-
#define Py_FALSE_IDX 1
1073-
#define Py_TRUE_IDX 2
1074-
#define Py_ELLIPSIS_IDX 3
1075-
#define Py_NOT_IMPLEMENTED_IDX 4
1076-
#define Py_ZERO_IDX 5
1077-
#define Py_ONE_IDX 6
1078-
#define Py_EMPTY_STR_IDX 7
1079-
#define Py_EMPTY_BYTES_IDX 8
1080-
#define Py_EMPTY_TUPLE_IDX 9
1071+
#define Py_CONSTANT_NONE 0
1072+
#define Py_CONSTANT_FALSE 1
1073+
#define Py_CONSTANT_TRUE 2
1074+
#define Py_CONSTANT_ELLIPSIS 3
1075+
#define Py_CONSTANT_NOT_IMPLEMENTED 4
1076+
#define Py_CONSTANT_ZERO 5
1077+
#define Py_CONSTANT_ONE 6
1078+
#define Py_CONSTANT_EMPTY_STR 7
1079+
#define Py_CONSTANT_EMPTY_BYTES 8
1080+
#define Py_CONSTANT_EMPTY_TUPLE 9
10811081

10821082
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
10831083
PyAPI_FUNC(PyObject*) Py_GetConstant(unsigned int constant_id);
@@ -1092,7 +1092,7 @@ where NULL (nil) is not suitable (since NULL often means 'error').
10921092
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
10931093

10941094
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
1095-
# define Py_None Py_GetConstantBorrowed(Py_NONE_IDX)
1095+
# define Py_None Py_GetConstantBorrowed(Py_CONSTANT_NONE)
10961096
#else
10971097
# define Py_None (&_Py_NoneStruct)
10981098
#endif
@@ -1111,7 +1111,7 @@ not implemented for a given type combination.
11111111
PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
11121112

11131113
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
1114-
# define Py_NotImplemented Py_GetConstantBorrowed(Py_NOT_IMPLEMENTED_IDX)
1114+
# define Py_NotImplemented Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED)
11151115
#else
11161116
# define Py_NotImplemented (&_Py_NotImplementedStruct)
11171117
#endif

Include/sliceobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern "C" {
99
PyAPI_DATA(PyObject) _Py_EllipsisObject; /* Don't use this directly */
1010

1111
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
12-
# define Py_Ellipsis Py_GetConstantBorrowed(Py_ELLIPSIS_IDX)
12+
# define Py_Ellipsis Py_GetConstantBorrowed(Py_CONSTANT_ELLIPSIS)
1313
#else
1414
# define Py_Ellipsis (&_Py_EllipsisObject)
1515
#endif

Lib/test/test_capi/test_object.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,42 @@
66

77

88
class Constant(enum.IntEnum):
9-
Py_NONE_IDX = 0
10-
Py_FALSE_IDX = 1
11-
Py_TRUE_IDX = 2
12-
Py_ELLIPSIS_IDX = 3
13-
Py_NOT_IMPLEMENTED_IDX = 4
14-
Py_ZERO_IDX = 5
15-
Py_ONE_IDX = 6
16-
Py_EMPTY_STR_IDX = 7
17-
Py_EMPTY_BYTES_IDX = 8
18-
Py_EMPTY_TUPLE_IDX = 9
9+
Py_CONSTANT_NONE = 0
10+
Py_CONSTANT_FALSE = 1
11+
Py_CONSTANT_TRUE = 2
12+
Py_CONSTANT_ELLIPSIS = 3
13+
Py_CONSTANT_NOT_IMPLEMENTED = 4
14+
Py_CONSTANT_ZERO = 5
15+
Py_CONSTANT_ONE = 6
16+
Py_CONSTANT_EMPTY_STR = 7
17+
Py_CONSTANT_EMPTY_BYTES = 8
18+
Py_CONSTANT_EMPTY_TUPLE = 9
1919

20-
INVALID_IDX = Py_EMPTY_TUPLE_IDX + 1
20+
INVALID_CONSTANT = Py_CONSTANT_EMPTY_TUPLE + 1
2121

2222

2323
class CAPITest(unittest.TestCase):
2424
def check_get_constant(self, get_constant):
25-
self.assertIs(get_constant(Constant.Py_NONE_IDX), None)
26-
self.assertIs(get_constant(Constant.Py_FALSE_IDX), False)
27-
self.assertIs(get_constant(Constant.Py_TRUE_IDX), True)
28-
self.assertIs(get_constant(Constant.Py_ELLIPSIS_IDX), Ellipsis)
29-
self.assertIs(get_constant(Constant.Py_NOT_IMPLEMENTED_IDX), NotImplemented)
25+
self.assertIs(get_constant(Constant.Py_CONSTANT_NONE), None)
26+
self.assertIs(get_constant(Constant.Py_CONSTANT_FALSE), False)
27+
self.assertIs(get_constant(Constant.Py_CONSTANT_TRUE), True)
28+
self.assertIs(get_constant(Constant.Py_CONSTANT_ELLIPSIS), Ellipsis)
29+
self.assertIs(get_constant(Constant.Py_CONSTANT_NOT_IMPLEMENTED), NotImplemented)
3030

3131
for constant_id, constant_type, value in (
32-
(Constant.Py_ZERO_IDX, int, 0),
33-
(Constant.Py_ONE_IDX, int, 1),
34-
(Constant.Py_EMPTY_STR_IDX, str, ""),
35-
(Constant.Py_EMPTY_BYTES_IDX, bytes, b""),
36-
(Constant.Py_EMPTY_TUPLE_IDX, tuple, ()),
32+
(Constant.Py_CONSTANT_ZERO, int, 0),
33+
(Constant.Py_CONSTANT_ONE, int, 1),
34+
(Constant.Py_CONSTANT_EMPTY_STR, str, ""),
35+
(Constant.Py_CONSTANT_EMPTY_BYTES, bytes, b""),
36+
(Constant.Py_CONSTANT_EMPTY_TUPLE, tuple, ()),
3737
):
3838
with self.subTest(constant_id=constant_id):
3939
obj = get_constant(constant_id)
4040
self.assertEqual(type(obj), constant_type, obj)
4141
self.assertEqual(obj, value)
4242

4343
with self.assertRaises(ValueError):
44-
get_constant(Constant.INVALID_IDX)
44+
get_constant(Constant.INVALID_CONSTANT)
4545

4646
def test_get_constant(self):
4747
self.check_get_constant(_testlimitedcapi.get_constant)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Add :c:func:`Py_GetConstant` and :c:func:`Py_GetConstantBorrowed` functions to
2-
get constants. For example, ``Py_GetConstant(Py_ZERO_IDX)`` returns a
2+
get constants. For example, ``Py_GetConstant(Py_CONSTANT_ZERO)`` returns a
33
:term:`strong reference` to the constant zero. Patch by Victor Stinner.

Modules/_testlimitedcapi/object.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ test_constants(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
5353
//
5454
// Test also that constants and Py_GetConstant() return the same
5555
// objects.
56-
assert(Py_None == Py_GetConstant(Py_NONE_IDX));
57-
assert(Py_False == Py_GetConstant(Py_FALSE_IDX));
58-
assert(Py_True == Py_GetConstant(Py_TRUE_IDX));
59-
assert(Py_Ellipsis == Py_GetConstant(Py_ELLIPSIS_IDX));
60-
assert(Py_NotImplemented == Py_GetConstant(Py_NOT_IMPLEMENTED_IDX));
56+
assert(Py_None == Py_GetConstant(Py_CONSTANT_NONE));
57+
assert(Py_False == Py_GetConstant(Py_CONSTANT_FALSE));
58+
assert(Py_True == Py_GetConstant(Py_CONSTANT_TRUE));
59+
assert(Py_Ellipsis == Py_GetConstant(Py_CONSTANT_ELLIPSIS));
60+
assert(Py_NotImplemented == Py_GetConstant(Py_CONSTANT_NOT_IMPLEMENTED));
6161
// Other constants are tested in test_capi.test_object
6262
Py_RETURN_NONE;
6363
}

Objects/object.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -2974,26 +2974,26 @@ _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)
29742974

29752975

29762976
static PyObject* constants[] = {
2977-
&_Py_NoneStruct, // Py_NONE_IDX
2978-
(PyObject*)(&_Py_FalseStruct), // Py_FALSE_IDX
2979-
(PyObject*)(&_Py_TrueStruct), // Py_TRUE_IDX
2980-
&_Py_EllipsisObject, // Py_ELLIPSIS_IDX
2981-
&_Py_NotImplementedStruct, // Py_NOT_IMPLEMENTED_IDX
2982-
NULL, // Py_ZERO_IDX
2983-
NULL, // Py_ONE_IDX
2984-
NULL, // Py_EMPTY_STR_IDX
2985-
NULL, // Py_EMPTY_BYTES_IDX
2986-
NULL, // Py_EMPTY_TUPLE_IDX
2977+
&_Py_NoneStruct, // Py_CONSTANT_NONE
2978+
(PyObject*)(&_Py_FalseStruct), // Py_CONSTANT_FALSE
2979+
(PyObject*)(&_Py_TrueStruct), // Py_CONSTANT_TRUE
2980+
&_Py_EllipsisObject, // Py_CONSTANT_ELLIPSIS
2981+
&_Py_NotImplementedStruct, // Py_CONSTANT_NOT_IMPLEMENTED
2982+
NULL, // Py_CONSTANT_ZERO
2983+
NULL, // Py_CONSTANT_ONE
2984+
NULL, // Py_CONSTANT_EMPTY_STR
2985+
NULL, // Py_CONSTANT_EMPTY_BYTES
2986+
NULL, // Py_CONSTANT_EMPTY_TUPLE
29872987
};
29882988

29892989
void
29902990
_Py_GetConstant_Init(void)
29912991
{
2992-
constants[Py_ZERO_IDX] = _PyLong_GetZero();
2993-
constants[Py_ONE_IDX] = _PyLong_GetOne();
2994-
constants[Py_EMPTY_STR_IDX] = PyUnicode_New(0, 0);
2995-
constants[Py_EMPTY_BYTES_IDX] = PyBytes_FromStringAndSize(NULL, 0);
2996-
constants[Py_EMPTY_TUPLE_IDX] = PyTuple_New(0);
2992+
constants[Py_CONSTANT_ZERO] = _PyLong_GetZero();
2993+
constants[Py_CONSTANT_ONE] = _PyLong_GetOne();
2994+
constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_New(0, 0);
2995+
constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize(NULL, 0);
2996+
constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0);
29972997
#ifndef NDEBUG
29982998
for (size_t i=0; i < Py_ARRAY_LENGTH(constants); i++) {
29992999
assert(constants[i] != NULL);

0 commit comments

Comments
 (0)