summaryrefslogtreecommitdiffstats
path: root/Objects/fileobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-06-14 04:15:26 (GMT)
committerGitHub <noreply@github.com>2022-06-14 04:15:26 (GMT)
commit6fd4c8ec7740523bb81191c013118d9d6959bc9d (patch)
treedf4df3a66a89cb1acff0d7721adb62d5553512e3 /Objects/fileobject.c
parent7b2064b4b942e1d3c7fd74b5c463250319bc20fb (diff)
downloadcpython-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 'Objects/fileobject.c')
-rw-r--r--Objects/fileobject.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 8dba5b9..cbc5741 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -32,16 +32,16 @@ PyObject *
PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding,
const char *errors, const char *newline, int closefd)
{
- PyObject *io, *stream;
+ PyObject *open, *stream;
/* import _io in case we are being used to open io.py */
- io = PyImport_ImportModule("_io");
- if (io == NULL)
+ open = _PyImport_GetModuleAttrString("_io", "open");
+ if (open == NULL)
return NULL;
- stream = _PyObject_CallMethod(io, &_Py_ID(open), "isisssO", fd, mode,
+ stream = PyObject_CallFunction(open, "isisssO", fd, mode,
buffering, encoding, errors,
newline, closefd ? Py_True : Py_False);
- Py_DECREF(io);
+ Py_DECREF(open);
if (stream == NULL)
return NULL;
/* ignore name attribute because the name attribute of _BufferedIOMixin
@@ -490,7 +490,7 @@ PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction hook, void *userData) {
PyObject *
PyFile_OpenCodeObject(PyObject *path)
{
- PyObject *iomod, *f = NULL;
+ PyObject *f = NULL;
if (!PyUnicode_Check(path)) {
PyErr_Format(PyExc_TypeError, "'path' must be 'str', not '%.200s'",
@@ -502,10 +502,10 @@ PyFile_OpenCodeObject(PyObject *path)
if (hook) {
f = hook(path, _PyRuntime.open_code_userdata);
} else {
- iomod = PyImport_ImportModule("_io");
- if (iomod) {
- f = _PyObject_CallMethod(iomod, &_Py_ID(open), "Os", path, "rb");
- Py_DECREF(iomod);
+ PyObject *open = _PyImport_GetModuleAttrString("_io", "open");
+ if (open) {
+ f = PyObject_CallFunction(open, "Os", path, "rb");
+ Py_DECREF(open);
}
}