Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-93741: Add private C API _PyImport_GetModuleAttrString()#93742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
gh-93741: Add private C API _PyImport_GetModuleAttrString()
It combines PyImport_ImportModule() and PyObject_GetAttrString()and saves 4-6 lines of code on every use.
  • Loading branch information
@serhiy-storchaka
serhiy-storchaka committedJun 12, 2022
commit619cba1c8f3b21198fdd772e8d8d5cb0640b3da3
3 changes: 3 additions & 0 deletionsInclude/cpython/import.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,6 @@ struct _frozen {
collection of frozen modules: */

PyAPI_DATA(const struct _frozen *) PyImport_FrozenModules;

PyAPI_DATA(PyObject *) _PyImport_GetModuleAttr(PyObject *, PyObject *);
PyAPI_DATA(PyObject *) _PyImport_GetModuleAttrString(const char *, const char *);
13 changes: 3 additions & 10 deletionsModules/_ctypes/callbacks.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -472,24 +472,17 @@ static void LoadPython(void)

long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
PyObject *mod, *func, *result;
PyObject *func, *result;
long retval;
static PyObject *context;

if (context == NULL)
context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");

mod = PyImport_ImportModule("ctypes");
if (!mod) {
PyErr_WriteUnraisable(context ? context : Py_None);
/* There has been a warning before about this already */
return E_FAIL;
}

func = PyObject_GetAttrString(mod, "DllGetClassObject");
Py_DECREF(mod);
func = _PyImport_GetModuleAttrString("ctypes", "DllGetClassObject");
if (!func) {
PyErr_WriteUnraisable(context ? context : Py_None);
/* There has been a warning before about this already */
return E_FAIL;
}

Expand Down
34 changes: 11 additions & 23 deletionsModules/_datetimemodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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);
Expand All@@ -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;
Expand All@@ -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;
}

Expand Down
8 changes: 2 additions & 6 deletionsModules/_elementtree.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4370,7 +4370,7 @@ static struct PyModuleDef elementtreemodule = {
PyMODINIT_FUNC
PyInit__elementtree(void)
{
PyObject *m, *temp;
PyObject *m;
elementtreestate *st;

m = PyState_FindModule(&elementtreemodule);
Expand All@@ -4394,11 +4394,7 @@ PyInit__elementtree(void)
return NULL;
st = get_elementtree_state(m);

if (!(temp = PyImport_ImportModule("copy")))
return NULL;
st->deepcopy_obj = PyObject_GetAttrString(temp, "deepcopy");
Py_XDECREF(temp);

st->deepcopy_obj = _PyImport_GetModuleAttrString("copy", "deepcopy");
if (st->deepcopy_obj == NULL) {
return NULL;
}
Expand Down
7 changes: 1 addition & 6 deletionsModules/_operator.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1752,16 +1752,11 @@ methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored))
return Py_BuildValue("ON", Py_TYPE(mc), newargs);
}
else {
PyObject *functools;
PyObject *partial;
PyObject *constructor;
PyObject *newargs[2];

functools = PyImport_ImportModule("functools");
if (!functools)
return NULL;
partial = PyObject_GetAttr(functools, &_Py_ID(partial));
Py_DECREF(functools);
partial = _PyImport_GetModuleAttrString("functools", "partial");
if (!partial)
return NULL;

Expand Down
16 changes: 2 additions & 14 deletionsModules/_pickle.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -232,8 +232,6 @@ _Pickle_InitState(PickleState *st)
{
PyObject *copyreg = NULL;
PyObject *compat_pickle = NULL;
PyObject *codecs = NULL;
PyObject *functools = NULL;

st->getattr = _PyEval_GetBuiltin(&_Py_ID(getattr));
if (st->getattr == NULL)
Expand DownExpand Up@@ -329,10 +327,7 @@ _Pickle_InitState(PickleState *st)
}
Py_CLEAR(compat_pickle);

codecs = PyImport_ImportModule("codecs");
if (codecs == NULL)
goto error;
st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
st->codecs_encode = _PyImport_GetModuleAttrString("codecs", "encode");
if (st->codecs_encode == NULL) {
goto error;
}
Expand All@@ -342,23 +337,16 @@ _Pickle_InitState(PickleState *st)
Py_TYPE(st->codecs_encode)->tp_name);
goto error;
}
Py_CLEAR(codecs);

functools = PyImport_ImportModule("functools");
if (!functools)
goto error;
st->partial = PyObject_GetAttrString(functools, "partial");
st->partial = _PyImport_GetModuleAttrString("functools", "partial");
if (!st->partial)
goto error;
Py_CLEAR(functools);

return 0;

error:
Py_CLEAR(copyreg);
Py_CLEAR(compat_pickle);
Py_CLEAR(codecs);
Py_CLEAR(functools);
_Pickle_ClearState(st);
return -1;
}
Expand Down
34 changes: 6 additions & 28 deletionsModules/_sqlite/connection.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1841,43 +1841,21 @@ static PyObject *
pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=586997aaf9808768 input=1911ca756066da89]*/
{
PyObject* retval = NULL;
PyObject* module = NULL;
PyObject* module_dict;
PyObject* pyfn_iterdump;

if (!pysqlite_check_connection(self)) {
goto finally;
}

module = PyImport_ImportModule(MODULE_NAME ".dump");
if (!module) {
goto finally;
}

module_dict = PyModule_GetDict(module);
if (!module_dict) {
goto finally;
return NULL;
}

PyObject *meth = PyUnicode_InternFromString("_iterdump");
if (meth == NULL) {
goto finally;
}
pyfn_iterdump = PyDict_GetItemWithError(module_dict, meth);
Py_DECREF(meth);
if (!pyfn_iterdump) {
PyObject *iterdump = _PyImport_GetModuleAttrString(MODULE_NAME ".dump", "_iterdump");
if (!iterdump) {
if (!PyErr_Occurred()) {
PyErr_SetString(self->OperationalError,
"Failed to obtain _iterdump() reference");
}
goto finally;
return NULL;
}

retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);

finally:
Py_XDECREF(module);
PyObject *retval = PyObject_CallOneArg(iterdump, (PyObject *)self);
Py_DECREF(iterdump);
return retval;
}

Expand Down
8 changes: 1 addition & 7 deletionsModules/_sqlite/module.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -227,14 +227,8 @@ static int converters_init(PyObject* module)
static int
load_functools_lru_cache(PyObject *module)
{
PyObject *functools = PyImport_ImportModule("functools");
if (functools == NULL) {
return -1;
}

pysqlite_state *state = pysqlite_get_state(module);
state->lru_cache = PyObject_GetAttrString(functools, "lru_cache");
Py_DECREF(functools);
state->lru_cache = _PyImport_GetModuleAttrString("functools", "lru_cache");
if (state->lru_cache == NULL) {
return -1;
}
Expand Down
12 changes: 1 addition & 11 deletionsModules/_sre/sre.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -771,22 +771,12 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyTypeObject *cls,
static PyObject*
call(const char* module, const char* function, PyObject* args)
{
PyObject* name;
PyObject* mod;
PyObject* func;
PyObject* result;

if (!args)
return NULL;
name = PyUnicode_FromString(module);
if (!name)
return NULL;
mod = PyImport_Import(name);
Py_DECREF(name);
if (!mod)
return NULL;
func = PyObject_GetAttrString(mod, function);
Py_DECREF(mod);
func = _PyImport_GetModuleAttrString(module, function);
if (!func)
return NULL;
result = PyObject_CallObject(func, args);
Expand Down
35 changes: 8 additions & 27 deletionsModules/_zoneinfo.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -659,14 +659,8 @@ zoneinfo_reduce(PyObject *obj_self, PyObject *unused)
PyZoneInfo_ZoneInfo *self = (PyZoneInfo_ZoneInfo *)obj_self;
if (self->source == SOURCE_FILE) {
// Objects constructed from files cannot be pickled.
PyObject *pickle = PyImport_ImportModule("pickle");
if (pickle == NULL) {
return NULL;
}

PyObject *pickle_error =
PyObject_GetAttrString(pickle, "PicklingError");
Py_DECREF(pickle);
_PyImport_GetModuleAttrString("pickle", "PicklingError");
if (pickle_error == NULL) {
return NULL;
}
Expand DownExpand Up@@ -2492,14 +2486,13 @@ clear_strong_cache(const PyTypeObject *const type)
static PyObject *
new_weak_cache(void)
{
PyObject *weakref_module = PyImport_ImportModule("weakref");
if (weakref_module == NULL) {
PyObject *WeakValueDictionary =
_PyImport_GetModuleAttrString("weakref", "WeakValueDictionary");
if (WeakValueDictionary == NULL) {
return NULL;
}

PyObject *weak_cache =
PyObject_CallMethod(weakref_module, "WeakValueDictionary", "");
Py_DECREF(weakref_module);
PyObject *weak_cache = PyObject_CallNoArgs(WeakValueDictionary);
Py_DECREF(WeakValueDictionary);
return weak_cache;
}

Expand DownExpand Up@@ -2656,25 +2649,13 @@ zoneinfomodule_exec(PyObject *m)
PyModule_AddObject(m, "ZoneInfo", (PyObject *)&PyZoneInfo_ZoneInfoType);

/* Populate imports */
PyObject *_tzpath_module = PyImport_ImportModule("zoneinfo._tzpath");
if (_tzpath_module == NULL) {
goto error;
}

_tzpath_find_tzfile =
PyObject_GetAttrString(_tzpath_module, "find_tzfile");
Py_DECREF(_tzpath_module);
_PyImport_GetModuleAttrString("zoneinfo._tzpath", "find_tzfile");
if (_tzpath_find_tzfile == NULL) {
goto error;
}

PyObject *io_module = PyImport_ImportModule("io");
if (io_module == NULL) {
goto error;
}

io_open = PyObject_GetAttrString(io_module, "open");
Py_DECREF(io_module);
io_open = _PyImport_GetModuleAttrString("io", "open");
if (io_open == NULL) {
goto error;
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp