Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13130bc

Browse files
committed
Use the abstract object interfaces when digging around in module objects
instead of directly manipulating the underlying dictionary.
1 parent 94a7eba commit 13130bc

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

Modules/parsermodule.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,7 +2816,7 @@ static PyMethodDef parser_functions[] = {
28162816
"Creates an ST object from a tree representation."},
28172817

28182818
/* private stuff: support pickle module */
2819-
{"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
2819+
{"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
28202820
"Returns the pickle magic to allow ST objects to be pickled."},
28212821

28222822
{NULL, NULL, 0, NULL}
@@ -2828,44 +2828,42 @@ DL_EXPORT(void) initparser(void); /* supply a prototype */
28282828
DL_EXPORT(void)
28292829
initparser(void)
28302830
{
2831-
PyObject* module;
2832-
PyObject* dict;
2831+
PyObject *module, *copyreg;
28332832

28342833
PyST_Type.ob_type = &PyType_Type;
28352834
module = Py_InitModule("parser", parser_functions);
2836-
dict = PyModule_GetDict(module);
28372835

28382836
if (parser_error == 0)
28392837
parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
28402838

28412839
if ((parser_error == 0)
2842-
|| (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
2840+
|| (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
28432841
/* caller will check PyErr_Occurred() */
28442842
return;
28452843
}
28462844
Py_INCREF(&PyST_Type);
2847-
PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyST_Type);
2845+
PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
28482846
Py_INCREF(&PyST_Type);
2849-
PyDict_SetItemString(dict, "STType", (PyObject*)&PyST_Type);
2847+
PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
28502848

2851-
PyDict_SetItemString(dict, "__copyright__",
2852-
PyString_FromString(parser_copyright_string));
2853-
PyDict_SetItemString(dict, "__doc__",
2854-
PyString_FromString(parser_doc_string));
2855-
PyDict_SetItemString(dict, "__version__",
2856-
PyString_FromString(parser_version_string));
2849+
PyModule_AddStringConstant(module, "__copyright__",
2850+
parser_copyright_string);
2851+
PyModule_AddStringConstant(module, "__doc__",
2852+
parser_doc_string);
2853+
PyModule_AddStringConstant(module, "__version__",
2854+
parser_version_string);
28572855

28582856
/* Register to support pickling.
28592857
* If this fails, the import of this module will fail because an
28602858
* exception will be raised here; should we clear the exception?
28612859
*/
2862-
module = PyImport_ImportModule("copy_reg");
2863-
if (module != NULL) {
2860+
copyreg = PyImport_ImportModule("copy_reg");
2861+
if (copyreg != NULL) {
28642862
PyObject *func, *pickler;
28652863

2866-
func = PyObject_GetAttrString(module, "pickle");
2867-
pickle_constructor = PyDict_GetItemString(dict, "sequence2st");
2868-
pickler = PyDict_GetItemString(dict, "_pickler");
2864+
func = PyObject_GetAttrString(copyreg, "pickle");
2865+
pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2866+
pickler = PyObject_GetAttrString(module, "_pickler");
28692867
Py_XINCREF(pickle_constructor);
28702868
if ((func != NULL) && (pickle_constructor != NULL)
28712869
&& (pickler != NULL)) {
@@ -2876,6 +2874,8 @@ initparser(void)
28762874
Py_XDECREF(res);
28772875
}
28782876
Py_XDECREF(func);
2879-
Py_DECREF(module);
2877+
Py_XDECREF(pickle_constructor);
2878+
Py_XDECREF(pickler);
2879+
Py_DECREF(copyreg);
28802880
}
28812881
}

0 commit comments

Comments
 (0)