Skip to content

Commit 57b51d8

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Move CiExc_StaticTypeError into ModuleState
Summary: Move the `CiExc_StaticTypeError` exception type from a file-scope global in `errors.c` into the `ModuleState` class as a `Ref<>` member. This ensures proper GC traversal and cleanup. The `errors.h` header now provides a compatibility macro `#define CiExc_StaticTypeError Ci_GetStaticTypeError()` so that all existing usage sites (15+ C/C++ files) continue to work without changes. A C accessor function `Ci_GetStaticTypeError()` is added to `module_c_state.h/cpp` following the existing pattern for C-accessible ModuleState members. Reviewed By: yoney Differential Revision: D96614265 fbshipit-source-id: 69adb19b59f6044bd233269e1b53b5569e7d7c1d
1 parent c97ca9f commit 57b51d8

8 files changed

Lines changed: 25 additions & 20 deletions

File tree

cinderx/StaticPython/classloader.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
extern "C" {
2626
#endif
2727

28-
extern PyObject* CiExc_StaticTypeError;
29-
3028
#ifndef Py_LIMITED_API
3129

3230
#endif

cinderx/StaticPython/errors.c

Lines changed: 0 additions & 5 deletions
This file was deleted.

cinderx/StaticPython/errors.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
#pragma once
44

5-
#include "cinderx/python.h"
5+
#include "cinderx/module_c_state.h"
66

7-
#ifdef __cplusplus
8-
extern "C" {
9-
#endif
10-
11-
extern PyObject* CiExc_StaticTypeError;
12-
13-
#ifdef __cplusplus
14-
}
15-
#endif
7+
// CiExc_StaticTypeError is stored in ModuleState and accessed via a C
8+
// accessor function. The macro preserves source compatibility with all
9+
// existing usage sites.
10+
#define CiExc_StaticTypeError Ci_GetStaticTypeError()

cinderx/_cinderx-lib.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,11 +1377,15 @@ int _cinderx_exec_impl(PyObject* m) {
13771377
watcher_state.setFuncWatcher(cinderx_func_watcher);
13781378
watcher_state.setTypeWatcher(cinderx_type_watcher);
13791379

1380-
CiExc_StaticTypeError =
1381-
PyErr_NewException("cinderx.StaticTypeError", PyExc_TypeError, nullptr);
1382-
if (CiExc_StaticTypeError == nullptr) {
1380+
PyObject* static_type_error = PyErr_NewException(
1381+
"cinderx.StaticTypeError", PyExc_TypeError, nullptr /* dict */);
1382+
if (static_type_error == nullptr) {
13831383
return -1;
13841384
}
1385+
JIT_CHECK(
1386+
PyType_Check(static_type_error),
1387+
"Created StaticTypeError but it isn't a type object");
1388+
state->static_type_error = Ref<PyTypeObject>::steal(static_type_error);
13851389

13861390
if (PyType_Ready(&PyCachedProperty_Type) < 0) {
13871391
return -1;

cinderx/module_c_state.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ extern "C" {
1111
vectorcallfunc Ci_PyFunction_Vectorcall;
1212
#endif
1313

14+
PyObject* Ci_GetStaticTypeError(void) {
15+
auto state = cinderx::getModuleState();
16+
return state != nullptr ? state->static_type_error.getObj() : nullptr;
17+
}
18+
1419
int Ci_Watchers_WatchDict(PyObject* dict) {
1520
return cinderx::getModuleState()->watcher_state.watchDict(dict);
1621
}

cinderx/module_c_state.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ extern "C" {
2020
extern vectorcallfunc Ci_PyFunction_Vectorcall;
2121
#endif
2222

23+
// Get the StaticTypeError exception type.
24+
PyObject* Ci_GetStaticTypeError(void);
25+
2326
// WatcherState.
2427

2528
int Ci_Watchers_WatchDict(PyObject* dict);

cinderx/module_state.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ ModuleState* s_cinderx_state;
1313
} // namespace
1414

1515
int ModuleState::traverse(visitproc visit, void* arg) {
16+
Py_VISIT(static_type_error);
1617
Py_VISIT(builtin_next);
1718
return 0;
1819
}
1920

2021
int ModuleState::clear() {
22+
static_type_error.reset();
2123
sys_clear_caches.reset();
2224
builtin_next.reset();
2325
return 0;

cinderx/module_state.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ struct ModuleState {
5555
// Type for the custom awaitable returned by CinderX's anext() replacement.
5656
Ref<PyTypeObject> anext_awaitable_type;
5757

58+
// The cinderx.StaticTypeError exception type.
59+
Ref<PyTypeObject> static_type_error;
60+
5861
// Snapshotted member dicts for standard builtin types (int, str, list, etc.)
5962
// so the JIT optimizer can look up methods during multithreaded compilation
6063
// without calling PyType_Lookup (which isn't safe off the main thread).

0 commit comments

Comments
 (0)