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:
Serhiy Storchaka 2022-06-14 07:15:26 +03:00 committed by GitHub
parent 7b2064b4b9
commit 6fd4c8ec77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 114 additions and 248 deletions

View file

@ -77,13 +77,7 @@ init_normalization(Parser *p)
if (p->normalize) {
return 1;
}
PyObject *m = PyImport_ImportModule("unicodedata");
if (!m)
{
return 0;
}
p->normalize = PyObject_GetAttrString(m, "normalize");
Py_DECREF(m);
p->normalize = _PyImport_GetModuleAttrString("unicodedata", "normalize");
if (!p->normalize)
{
return 0;

View file

@ -421,7 +421,7 @@ error:
static int
fp_setreadl(struct tok_state *tok, const char* enc)
{
PyObject *readline, *io, *stream;
PyObject *readline, *open, *stream;
int fd;
long pos;
@ -438,13 +438,13 @@ fp_setreadl(struct tok_state *tok, const char* enc)
return 0;
}
io = PyImport_ImportModule("io");
if (io == NULL) {
open = _PyImport_GetModuleAttrString("io", "open");
if (open == NULL) {
return 0;
}
stream = _PyObject_CallMethod(io, &_Py_ID(open), "isisOOO",
stream = PyObject_CallFunction(open, "isisOOO",
fd, "r", -1, enc, Py_None, Py_None, Py_False);
Py_DECREF(io);
Py_DECREF(open);
if (stream == NULL) {
return 0;
}