-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
bpo-29881: Add new C API for static variables #770
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,7 +213,7 @@ static int compiler_async_comprehension_generator( | |
| expr_ty elt, expr_ty val, int type); | ||
|
|
||
| static PyCodeObject *assemble(struct compiler *, int addNone); | ||
| static PyObject *__doc__; | ||
| _Py_STATICVAR(__doc__); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I know, it was more to show how the API can be used. |
||
|
|
||
| #define CAPSULE_NAME "compile.c compiler unit" | ||
|
|
||
|
|
@@ -306,10 +306,8 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, | |
| PyCompilerFlags local_flags; | ||
| int merged; | ||
|
|
||
| if (!__doc__) { | ||
| __doc__ = PyUnicode_InternFromString("__doc__"); | ||
| if (!__doc__) | ||
| return NULL; | ||
| if (_PY_STATICVAR_INIT(&__doc__, PyUnicode_InternFromString("__doc__"))) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (!compiler_init(&c)) | ||
|
|
@@ -1441,7 +1439,7 @@ compiler_body(struct compiler *c, asdl_seq *stmts, string docstring) | |
| /* if not -OO mode, set docstring */ | ||
| if (c->c_optimize < 2 && docstring) { | ||
| ADDOP_O(c, LOAD_CONST, docstring, consts); | ||
| ADDOP_NAME(c, STORE_NAME, __doc__, names); | ||
| ADDOP_NAME(c, STORE_NAME, __doc__.obj, names); | ||
| } | ||
| VISIT_SEQ(c, stmt, stmts); | ||
| return 1; | ||
|
|
@@ -1452,14 +1450,14 @@ compiler_mod(struct compiler *c, mod_ty mod) | |
| { | ||
| PyCodeObject *co; | ||
| int addNone = 1; | ||
| static PyObject *module; | ||
| if (!module) { | ||
| module = PyUnicode_InternFromString("<module>"); | ||
| if (!module) | ||
| return NULL; | ||
| _Py_STATICVAR(module); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
|
||
| if (_PY_STATICVAR_INIT(&module, PyUnicode_InternFromString("<module>"))) { | ||
| return 0; | ||
| } | ||
|
|
||
| /* Use 0 for firstlineno initially, will fixup in assemble(). */ | ||
| if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) | ||
| if (!compiler_enter_scope(c, module.obj, COMPILER_SCOPE_MODULE, mod, 0)) | ||
| return NULL; | ||
| switch (mod->kind) { | ||
| case Module_kind: | ||
|
|
@@ -2639,12 +2637,10 @@ compiler_from_import(struct compiler *c, stmt_ty s) | |
|
|
||
| PyObject *names = PyTuple_New(n); | ||
| PyObject *level; | ||
| static PyObject *empty_string; | ||
| _Py_STATICVAR(empty_string); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if (!empty_string) { | ||
| empty_string = PyUnicode_FromString(""); | ||
| if (!empty_string) | ||
| return 0; | ||
| if (_PY_STATICVAR_INIT(&empty_string, PyUnicode_FromString(""))) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (!names) | ||
|
|
@@ -2679,7 +2675,7 @@ compiler_from_import(struct compiler *c, stmt_ty s) | |
| ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); | ||
| } | ||
| else { | ||
| ADDOP_NAME(c, IMPORT_NAME, empty_string, names); | ||
| ADDOP_NAME(c, IMPORT_NAME, empty_string.obj, names); | ||
| } | ||
| for (i = 0; i < n; i++) { | ||
| alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); | ||
|
|
@@ -2709,17 +2705,18 @@ compiler_from_import(struct compiler *c, stmt_ty s) | |
| static int | ||
| compiler_assert(struct compiler *c, stmt_ty s) | ||
| { | ||
| static PyObject *assertion_error = NULL; | ||
| _Py_STATICVAR(assertion_error); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| basicblock *end; | ||
| PyObject* msg; | ||
|
|
||
| if (c->c_optimize) | ||
| return 1; | ||
| if (assertion_error == NULL) { | ||
| assertion_error = PyUnicode_InternFromString("AssertionError"); | ||
| if (assertion_error == NULL) | ||
| return 0; | ||
|
|
||
| if (_PY_STATICVAR_INIT(&assertion_error, | ||
| PyUnicode_InternFromString("AssertionError"))) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (s->v.Assert.test->kind == Tuple_kind && | ||
| asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { | ||
| msg = PyUnicode_FromString("assertion is always true, " | ||
|
|
@@ -2739,7 +2736,7 @@ compiler_assert(struct compiler *c, stmt_ty s) | |
| if (end == NULL) | ||
| return 0; | ||
| ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); | ||
| ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); | ||
| ADDOP_O(c, LOAD_GLOBAL, assertion_error.obj, names); | ||
| if (s->v.Assert.msg) { | ||
| VISIT(c, expr, s->v.Assert.msg); | ||
| ADDOP_I(c, CALL_FUNCTION, 1); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1721,33 +1721,33 @@ PyImport_ReloadModule(PyObject *m) | |
| PyObject * | ||
| PyImport_Import(PyObject *module_name) | ||
| { | ||
| static PyObject *silly_list = NULL; | ||
| static PyObject *builtins_str = NULL; | ||
| static PyObject *import_str = NULL; | ||
| _Py_STATICVAR(empty_list); | ||
| _Py_STATICVAR(builtins_str); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| _Py_STATICVAR(import_str); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| PyObject *globals = NULL; | ||
| PyObject *import = NULL; | ||
| PyObject *builtins = NULL; | ||
| PyObject *modules = NULL; | ||
| PyObject *r = NULL; | ||
|
|
||
| /* Initialize constant string objects */ | ||
| if (silly_list == NULL) { | ||
| import_str = PyUnicode_InternFromString("__import__"); | ||
| if (import_str == NULL) | ||
| return NULL; | ||
| builtins_str = PyUnicode_InternFromString("__builtins__"); | ||
| if (builtins_str == NULL) | ||
| return NULL; | ||
| silly_list = PyList_New(0); | ||
| if (silly_list == NULL) | ||
| return NULL; | ||
| if (_PY_STATICVAR_INIT(&empty_list, PyList_New(0))) { | ||
| return NULL; | ||
| } | ||
| if (_PY_STATICVAR_INIT(&builtins_str, | ||
| PyUnicode_InternFromString("__builtins__"))) { | ||
| return NULL; | ||
| } | ||
| if (_PY_STATICVAR_INIT(&import_str, | ||
| PyUnicode_InternFromString("__import__"))) { | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Get the builtins from current globals */ | ||
| globals = PyEval_GetGlobals(); | ||
| if (globals != NULL) { | ||
| Py_INCREF(globals); | ||
| builtins = PyObject_GetItem(globals, builtins_str); | ||
| builtins = PyObject_GetItem(globals, builtins_str.obj); | ||
| if (builtins == NULL) | ||
| goto err; | ||
| } | ||
|
|
@@ -1757,27 +1757,27 @@ PyImport_Import(PyObject *module_name) | |
| NULL, NULL, NULL, 0); | ||
| if (builtins == NULL) | ||
| return NULL; | ||
| globals = Py_BuildValue("{OO}", builtins_str, builtins); | ||
| globals = Py_BuildValue("{OO}", builtins_str.obj, builtins); | ||
| if (globals == NULL) | ||
| goto err; | ||
| } | ||
|
|
||
| /* Get the __import__ function from the builtins */ | ||
| if (PyDict_Check(builtins)) { | ||
| import = PyObject_GetItem(builtins, import_str); | ||
| import = PyObject_GetItem(builtins, import_str.obj); | ||
| if (import == NULL) | ||
| PyErr_SetObject(PyExc_KeyError, import_str); | ||
| PyErr_SetObject(PyExc_KeyError, import_str.obj); | ||
| } | ||
| else | ||
| import = PyObject_GetAttr(builtins, import_str); | ||
| import = PyObject_GetAttr(builtins, import_str.obj); | ||
| if (import == NULL) | ||
| goto err; | ||
|
|
||
| /* Call the __import__ function with the proper argument list | ||
| Always use absolute import here. | ||
| Calling for side-effect of import. */ | ||
| r = PyObject_CallFunction(import, "OOOOi", module_name, globals, | ||
| globals, silly_list, 0, NULL); | ||
| globals, empty_list.obj, 0, NULL); | ||
| if (r == NULL) | ||
| goto err; | ||
| Py_DECREF(r); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,7 +162,7 @@ sys_displayhook(PyObject *self, PyObject *o) | |
| PyInterpreterState *interp = PyThreadState_GET()->interp; | ||
| PyObject *modules = interp->modules; | ||
| PyObject *builtins; | ||
| static PyObject *newline = NULL; | ||
| _Py_STATICVAR(newline); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| int err; | ||
|
|
||
| builtins = _PyDict_GetItemId(modules, &PyId_builtins); | ||
|
|
@@ -197,12 +197,10 @@ sys_displayhook(PyObject *self, PyObject *o) | |
| return NULL; | ||
| } | ||
| } | ||
| if (newline == NULL) { | ||
| newline = PyUnicode_FromString("\n"); | ||
| if (newline == NULL) | ||
| return NULL; | ||
| if (_PY_STATICVAR_INIT(&newline, PyUnicode_FromString("\n"))) { | ||
| return NULL; | ||
| } | ||
| if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0) | ||
| if (PyFile_WriteObject(newline.obj, outf, Py_PRINT_RAW) != 0) | ||
| return NULL; | ||
| if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) | ||
| return NULL; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_PY_STATICVAR_INIT()can be used if extract the code for gettingarray._array_reconstructorinto a separate function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in PR #780.