Skip to content

Commit e3b8adc

Browse files
replace NoExtraItems globals with static singletons and getters
1 parent c338766 commit e3b8adc

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Include/internal/pycore_typevarobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ extern int _Py_typing_type_repr(PyUnicodeWriter *, PyObject *);
2121
extern PyTypeObject _PyTypeAlias_Type;
2222
extern PyTypeObject _PyNoDefault_Type;
2323
extern PyObject _Py_NoDefaultStruct;
24-
extern PyTypeObject _PyNoExtraItems_Type;
25-
extern PyObject _Py_NoExtraItemsStruct;
24+
PyAPI_FUNC(PyObject *) _Py_GetNoExtraItemsSingleton(void);
25+
PyAPI_FUNC(PyTypeObject *) _Py_GetNoExtraItemsType(void);
2626

2727
#ifdef __cplusplus
2828
}

Modules/_typingmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ _typing_exec(PyObject *m)
7070
if (PyModule_AddObjectRef(m, "NoDefault", (PyObject *)&_Py_NoDefaultStruct) < 0) {
7171
return -1;
7272
}
73-
if (PyModule_AddObjectRef(m, "NoExtraItems", (PyObject *)&_Py_NoExtraItemsStruct) < 0) {
73+
if (PyModule_AddObjectRef(m, "NoExtraItems", _Py_GetNoExtraItemsSingleton()) < 0) {
7474
return -1;
7575
}
7676
return 0;

Objects/typevarobject.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ PyTypeObject _PyNoDefault_Type = {
123123

124124
PyObject _Py_NoDefaultStruct = _PyObject_HEAD_INIT(&_PyNoDefault_Type);
125125

126-
/* NoExtraItems: a marker object for TypeDict extra_items when it's unset. */
126+
/* NoExtraItems: a marker object for TypedDict extra_items when it's unset. */
127+
128+
static PyTypeObject _PyNoExtraItems_Type;
129+
static PyObject _Py_NoExtraItemsStruct;
127130

128131
static PyObject *
129132
NoExtraItems_repr(PyObject *op)
@@ -167,7 +170,7 @@ PyDoc_STRVAR(noextraitems_doc,
167170
"--\n\n"
168171
"The type of the NoExtraItems singleton.");
169172

170-
PyTypeObject _PyNoExtraItems_Type = {
173+
static PyTypeObject _PyNoExtraItems_Type = {
171174
PyVarObject_HEAD_INIT(&PyType_Type, 0)
172175
"NoExtraItemsType",
173176
.tp_dealloc = noextraitems_dealloc,
@@ -178,7 +181,16 @@ PyTypeObject _PyNoExtraItems_Type = {
178181
.tp_new = noextraitems_new,
179182
};
180183

181-
PyObject _Py_NoExtraItemsStruct = _PyObject_HEAD_INIT(&_PyNoExtraItems_Type);
184+
static PyObject _Py_NoExtraItemsStruct = _PyObject_HEAD_INIT(&_PyNoExtraItems_Type);
185+
186+
/* Accessors to avoid exporting global variables. */
187+
PyObject * _Py_GetNoExtraItemsSingleton(void) {
188+
return &_Py_NoExtraItemsStruct;
189+
}
190+
191+
PyTypeObject * _Py_GetNoExtraItemsType(void) {
192+
return &_PyNoExtraItems_Type;
193+
}
182194

183195
typedef struct {
184196
PyObject_HEAD

0 commit comments

Comments
 (0)