diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-06-14 04:15:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-14 04:15:26 (GMT) |
commit | 6fd4c8ec7740523bb81191c013118d9d6959bc9d (patch) | |
tree | df4df3a66a89cb1acff0d7721adb62d5553512e3 /Modules/_datetimemodule.c | |
parent | 7b2064b4b942e1d3c7fd74b5c463250319bc20fb (diff) | |
download | cpython-6fd4c8ec7740523bb81191c013118d9d6959bc9d.zip cpython-6fd4c8ec7740523bb81191c013118d9d6959bc9d.tar.gz cpython-6fd4c8ec7740523bb81191c013118d9d6959bc9d.tar.bz2 |
gh-93741: Add private C API _PyImport_GetModuleAttrString() (GH-93742)
It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.
Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
Diffstat (limited to 'Modules/_datetimemodule.c')
-rw-r--r-- | Modules/_datetimemodule.c | 34 |
1 files changed, 11 insertions, 23 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 06dff8f..ba24e3c 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -1718,17 +1718,17 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, goto Done; { PyObject *format; - PyObject *time = PyImport_ImportModule("time"); + PyObject *strftime = _PyImport_GetModuleAttrString("time", "strftime"); - if (time == NULL) + if (strftime == NULL) goto Done; format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); if (format != NULL) { - result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, + result = PyObject_CallFunctionObjArgs(strftime, format, timetuple, NULL); Py_DECREF(format); } - Py_DECREF(time); + Py_DECREF(strftime); } Done: Py_XDECREF(freplacement); @@ -1748,12 +1748,10 @@ static PyObject * time_time(void) { PyObject *result = NULL; - PyObject *time = PyImport_ImportModule("time"); + PyObject *time = _PyImport_GetModuleAttrString("time", "time"); if (time != NULL) { - _Py_IDENTIFIER(time); - - result = _PyObject_CallMethodIdNoArgs(time, &PyId_time); + result = PyObject_CallNoArgs(time); Py_DECREF(time); } return result; @@ -1765,31 +1763,21 @@ time_time(void) static PyObject * build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) { - PyObject *time; + PyObject *struct_time; PyObject *result; - _Py_IDENTIFIER(struct_time); - PyObject *args; - - time = PyImport_ImportModule("time"); - if (time == NULL) { + struct_time = _PyImport_GetModuleAttrString("time", "struct_time"); + if (struct_time == NULL) { return NULL; } - args = Py_BuildValue("iiiiiiiii", + result = PyObject_CallFunction(struct_time, "((iiiiiiiii))", y, m, d, hh, mm, ss, weekday(y, m, d), days_before_month(y, m) + d, dstflag); - if (args == NULL) { - Py_DECREF(time); - return NULL; - } - - result = _PyObject_CallMethodIdOneArg(time, &PyId_struct_time, args); - Py_DECREF(time); - Py_DECREF(args); + Py_DECREF(struct_time); return result; } |