mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
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.
This commit is contained in:
parent
7b2064b4b9
commit
6fd4c8ec77
24 changed files with 114 additions and 248 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue