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

Skip to content

Commit 4b7625e

Browse files
committed
_PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
1 parent 8fa4567 commit 4b7625e

1 file changed

Lines changed: 25 additions & 49 deletions

File tree

Python/bltinmodule.c

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,58 +1837,33 @@ _PyBuiltin_Init(void)
18371837
if (mod == NULL)
18381838
return NULL;
18391839
dict = PyModule_GetDict(mod);
1840-
if (PyDict_SetItemString(dict, "None", Py_None) < 0)
1841-
return NULL;
1842-
if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
1843-
return NULL;
1844-
if (PyDict_SetItemString(dict, "NotImplemented",
1845-
Py_NotImplemented) < 0)
1846-
return NULL;
1847-
if (PyDict_SetItemString(dict, "classmethod",
1848-
(PyObject *) &PyClassMethod_Type) < 0)
1849-
return NULL;
1840+
1841+
#define SETBUILTIN(NAME, OBJECT) \
1842+
if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
1843+
return NULL
1844+
1845+
SETBUILTIN("None", Py_None);
1846+
SETBUILTIN("Ellipsis", Py_Ellipsis);
1847+
SETBUILTIN("NotImplemented", Py_NotImplemented);
1848+
SETBUILTIN("classmethod", &PyClassMethod_Type);
18501849
#ifndef WITHOUT_COMPLEX
1851-
if (PyDict_SetItemString(dict, "complex",
1852-
(PyObject *) &PyComplex_Type) < 0)
1853-
return NULL;
1850+
SETBUILTIN("complex", &PyComplex_Type);
18541851
#endif
1855-
if (PyDict_SetItemString(dict, "dictionary",
1856-
(PyObject *) &PyDict_Type) < 0)
1857-
return NULL;
1858-
if (PyDict_SetItemString(dict, "float",
1859-
(PyObject *) &PyFloat_Type) < 0)
1860-
return NULL;
1861-
if (PyDict_SetItemString(dict, "property",
1862-
(PyObject *) &PyProperty_Type) < 0)
1863-
return NULL;
1864-
if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
1865-
return NULL;
1866-
if (PyDict_SetItemString(dict, "list", (PyObject *) &PyList_Type) < 0)
1867-
return NULL;
1868-
if (PyDict_SetItemString(dict, "long", (PyObject *) &PyLong_Type) < 0)
1869-
return NULL;
1870-
if (PyDict_SetItemString(dict, "object",
1871-
(PyObject *) &PyBaseObject_Type) < 0)
1872-
return NULL;
1873-
if (PyDict_SetItemString(dict, "staticmethod",
1874-
(PyObject *) &PyStaticMethod_Type) < 0)
1875-
return NULL;
1876-
if (PyDict_SetItemString(dict, "str", (PyObject *) &PyString_Type) < 0)
1877-
return NULL;
1878-
if (PyDict_SetItemString(dict, "super",
1879-
(PyObject *) &PySuper_Type) < 0)
1880-
return NULL;
1881-
if (PyDict_SetItemString(dict, "tuple",
1882-
(PyObject *) &PyTuple_Type) < 0)
1883-
return NULL;
1884-
if (PyDict_SetItemString(dict, "type", (PyObject *) &PyType_Type) < 0)
1885-
return NULL;
1886-
if (PyDict_SetItemString(dict, "file", (PyObject *) &PyFile_Type) < 0)
1887-
return NULL;
1852+
SETBUILTIN("dictionary", &PyDict_Type);
1853+
SETBUILTIN("float", &PyFloat_Type);
1854+
SETBUILTIN("property", &PyProperty_Type);
1855+
SETBUILTIN("int", &PyInt_Type);
1856+
SETBUILTIN("list", &PyList_Type);
1857+
SETBUILTIN("long", &PyLong_Type);
1858+
SETBUILTIN("object", &PyBaseObject_Type);
1859+
SETBUILTIN("staticmethod", &PyStaticMethod_Type);
1860+
SETBUILTIN("str", &PyString_Type);
1861+
SETBUILTIN("super", &PySuper_Type);
1862+
SETBUILTIN("tuple", &PyTuple_Type);
1863+
SETBUILTIN("type", &PyType_Type);
1864+
SETBUILTIN("file", &PyFile_Type);
18881865
#ifdef Py_USING_UNICODE
1889-
if (PyDict_SetItemString(dict, "unicode",
1890-
(PyObject *) &PyUnicode_Type) < 0)
1891-
return NULL;
1866+
SETBUILTIN("unicode", &PyUnicode_Type);
18921867
#endif
18931868
debug = PyInt_FromLong(Py_OptimizeFlag == 0);
18941869
if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
@@ -1898,6 +1873,7 @@ _PyBuiltin_Init(void)
18981873
Py_XDECREF(debug);
18991874

19001875
return mod;
1876+
#undef SETBUILTIN
19011877
}
19021878

19031879
/* Helper for filter(): filter a tuple through a function */

0 commit comments

Comments
 (0)