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

bpo-40137: Convert _functools module to use PyType_FromModuleAndSpec.#23405

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
PrevPrevious commit
NextNext commit
apply Christian's comment
  • Loading branch information
@shihai1991
shihai1991 committedNov 24, 2020
commite9a51a5994fc081daed50d7b99d04f76282800dd
3 changes: 1 addition & 2 deletionsLib/test/test_functools.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,8 +27,7 @@

py_functools=import_helper.import_fresh_module('functools',
blocked=['_functools'])
c_functools=import_helper.import_fresh_module('functools',
fresh=['_functools'])
c_functools=import_helper.import_fresh_module('functools')

decimal=import_helper.import_fresh_module('decimal',fresh=['_decimal'])

Expand Down
52 changes: 31 additions & 21 deletionsModules/_functoolsmodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,17 @@ get_functools_state(PyObject *module)
static void partial_setvectorcall(partialobject *pto);
static struct PyModuleDef _functools_module;

static inline _functools_state *
get_functools_state_by_type(PyTypeObject *type)
{
PyObject *module = _PyType_GetModuleByDef(type, &_functools_module);
if (module == NULL) {
return NULL;
}
_functools_state *state = get_functools_state(module);
return state;
}

static PyObject *
partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
Expand All@@ -55,11 +66,10 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
return NULL;
}

PyObject *module =_PyType_GetModuleByDef(type, &_functools_module);
if (module == NULL) {
_functools_state *state =get_functools_state_by_type(type);
if (state == NULL) {
return NULL;
}
_functools_state *state = get_functools_state(module);

pargs = pkw = NULL;
func = PyTuple_GET_ITEM(args, 0);
Expand DownExpand Up@@ -542,19 +552,18 @@ static PyType_Spec keyobject_type_spec = {
static PyObject *
keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
{
PyObject *object, *module;
PyObject *object;
keyobject *result;
_functools_state *state;
static char *kwargs[] = {"obj", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwargs, &object))
return NULL;

module =_PyType_GetModuleByDef(Py_TYPE(ko), &_functools_module);
if (module == NULL) {
state =get_functools_state_by_type(Py_TYPE(ko));
if (state == NULL) {
return NULL;
}
state = get_functools_state(module);
result = PyObject_New(keyobject, state->keyobject_type);
if (!result)
return NULL;
Expand All@@ -573,15 +582,13 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
PyObject *y;
PyObject *compare;
PyObject *answer;
PyObject *module;
PyObject* stack[2];
_functools_state *state;

module =_PyType_GetModuleByDef(Py_TYPE(ko), &_functools_module);
if (module == NULL) {
state =get_functools_state_by_type(Py_TYPE(ko));
if (state == NULL) {
return NULL;
}
state = get_functools_state(module);
if (!Py_IS_TYPE(other, state->keyobject_type)) {
PyErr_Format(PyExc_TypeError, "other argument must be K instance");
return NULL;
Expand DownExpand Up@@ -784,7 +791,8 @@ typedef struct lru_cache_object {
} lru_cache_object;

static PyObject *
lru_cache_make_key(PyObject *module, PyObject *args, PyObject *kwds, int typed)
lru_cache_make_key(_functools_state *state, PyObject *args,
PyObject *kwds, int typed)
{
PyObject *key, *keyword, *value;
Py_ssize_t key_size, pos, key_pos, kwds_size;
Expand DownExpand Up@@ -822,7 +830,6 @@ lru_cache_make_key(PyObject *module, PyObject *args, PyObject *kwds, int typed)
Py_INCREF(item);
PyTuple_SET_ITEM(key, key_pos++, item);
}
_functools_state *state = get_functools_state(module);
if (kwds_size) {
Py_INCREF(state->kwd_mark);
PyTuple_SET_ITEM(key, key_pos++, state->kwd_mark);
Expand DownExpand Up@@ -867,10 +874,14 @@ uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
static PyObject *
infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{
PyObject *result, *module;
PyObject *result;
Py_hash_t hash;
module = _PyType_GetModuleByDef(Py_TYPE(self), &_functools_module);
PyObject *key = lru_cache_make_key(module, args, kwds, self->typed);
_functools_state *state;
state = get_functools_state_by_type(Py_TYPE(self));
if (state == NULL) {
return NULL;
}
PyObject *key = lru_cache_make_key(state, args, kwds, self->typed);
if (!key)
return NULL;
hash = PyObject_Hash(key);
Expand DownExpand Up@@ -968,15 +979,15 @@ static PyObject *
bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{
lru_list_elem *link;
PyObject *key, *result, *testresult, *module;
PyObject *key, *result, *testresult;
Py_hash_t hash;
_functools_state *state;

module =_PyType_GetModuleByDef(Py_TYPE(self), &_functools_module);
if (module == NULL) {
state =get_functools_state_by_type(Py_TYPE(self));
if (state == NULL) {
return NULL;
}
key = lru_cache_make_key(module, args, kwds, self->typed);
key = lru_cache_make_key(state, args, kwds, self->typed);
if (!key)
return NULL;
hash = PyObject_Hash(key);
Expand DownExpand Up@@ -1030,7 +1041,6 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
self->root.next == &self->root)
{
/* Cache is not full, so put the result in a new link */
state = get_functools_state(module);
link = (lru_list_elem *)PyObject_New(lru_list_elem,
state->lru_list_elem_type);
if (link == NULL) {
Expand Down
18 changes: 5 additions & 13 deletionsModules/_pickle.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3637,19 +3637,11 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
goto error;
}
if (cls != obj) {
/* bpo-40137: Compare types' name in case some type objects are
created in heap. */
if (PyType_Check(cls) && PyType_Check(obj)) {
const char *cls_name = _PyType_Name((PyTypeObject *)cls);
const char *obj_name = _PyType_Name((PyTypeObject *)obj);
if (strcmp(cls_name, obj_name)) {
Py_DECREF(cls);
PyErr_Format(st->PicklingError,
"Can't pickle %R: it's not the same object as %S.%S",
obj, module_name, global_name);
goto error;
}
}
Py_DECREF(cls);
PyErr_Format(st->PicklingError,
"Can't pickle %R: it's not the same object as %S.%S",
obj, module_name, global_name);
goto error;
}
Py_DECREF(cls);

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp