summaryrefslogtreecommitdiffstats
path: root/Parser
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 /Parser
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 'Parser')
-rw-r--r--Parser/pegen.c8
-rw-r--r--Parser/tokenizer.c10
2 files changed, 6 insertions, 12 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index ec4fee3..dbf105a 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -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;
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index d5b549c..952265e 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -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;
}