Commit 11163657 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add another way for extensions to register static constants

well, ones that aren't quite constant.  They can just register
their C static variables and then change them at will.

The _warnings module does this
parent 7d327a89
......@@ -135,6 +135,7 @@ PyAPI_FUNC(void) PyType_SetDict(PyTypeObject*, PyObject*) PYSTON_NOEXCEPT;
// get decref'd when the interpreter shuts down. This functions returns its argument.
// PyType_Ready calls this automatically.
PyAPI_FUNC(PyObject*) PyGC_RegisterStaticConstant(PyObject*) PYSTON_NOEXCEPT;
PyAPI_FUNC(void) PyGC_RegisterStaticConstantLocation(PyObject**) PYSTON_NOEXCEPT;
// Gets gc.garbage
PyAPI_FUNC(PyObject*) _PyGC_GetGarbage(void) PYSTON_NOEXCEPT;
......
......@@ -538,8 +538,8 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
goto handle_error;
}
else if (!is_true) {
Py_DECREF(*filename);
*filename = PyString_FromString("__main__");
Py_DECREF(*filename);
*filename = PyString_FromString("__main__");
if (*filename == NULL)
goto handle_error;
}
......@@ -901,7 +901,7 @@ _PyWarnings_Init(void)
_filters = init_filters();
if (_filters == NULL)
return;
PyGC_RegisterStaticConstant(_filters);
PyGC_RegisterStaticConstantLocation(&_filters);
Py_INCREF(_filters);
if (PyModule_AddObject(m, "filters", _filters) < 0)
return;
......
......@@ -4091,7 +4091,12 @@ void BoxedGetsetDescriptor::dealloc(Box* _o) noexcept {
#endif
std::vector<Box*> constants;
std::vector<Box**> constant_locations;
std::vector<Box*> late_constants;
extern "C" void PyGC_RegisterStaticConstantLocation(Box** ptr) noexcept {
constant_locations.push_back(ptr);
}
extern "C" PyObject* PyGC_RegisterStaticConstant(Box* b) noexcept {
constants.push_back(b);
return b;
......@@ -4827,6 +4832,11 @@ extern "C" void Py_Finalize() noexcept {
break;
}
for (auto p : constant_locations) {
Py_DECREF(*p);
}
constant_locations.clear();
clearAllICs();
PyType_ClearCache();
PyOS_FiniInterrupts();
......@@ -4851,6 +4861,7 @@ extern "C" void Py_Finalize() noexcept {
while (PyGC_Collect())
;
assert(!constants.size());
assert(!constant_locations.size());
_Py_ReleaseInternedStrings();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment