Skip to content

Commit 1eed75d

Browse files
Preget tp_dict
1 parent 56d13fc commit 1eed75d

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

Objects/typeobject.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4144,9 +4144,8 @@ type_new_set_name(const type_new_ctx *ctx, PyTypeObject *type)
41444144

41454145
/* Set __module__ in the dict */
41464146
static int
4147-
type_new_set_module(PyTypeObject *type)
4147+
type_new_set_module(PyObject *dict)
41484148
{
4149-
PyObject *dict = lookup_tp_dict(type);
41504149
int r = PyDict_Contains(dict, &_Py_ID(__module__));
41514150
if (r < 0) {
41524151
return -1;
@@ -4173,10 +4172,9 @@ type_new_set_module(PyTypeObject *type)
41734172
/* Set ht_qualname to dict['__qualname__'] if available, else to
41744173
__name__. The __qualname__ accessor will look for ht_qualname. */
41754174
static int
4176-
type_new_set_ht_name(PyTypeObject *type)
4175+
type_new_set_ht_name(PyTypeObject *type, PyObject *dict)
41774176
{
41784177
PyHeapTypeObject *et = (PyHeapTypeObject *)type;
4179-
PyObject *dict = lookup_tp_dict(type);
41804178
PyObject *qualname;
41814179
if (PyDict_GetItemRef(dict, &_Py_ID(__qualname__), &qualname) < 0) {
41824180
return -1;
@@ -4205,9 +4203,8 @@ type_new_set_ht_name(PyTypeObject *type)
42054203
and is a string. The __doc__ accessor will first look for tp_doc;
42064204
if that fails, it will still look into __dict__. */
42074205
static int
4208-
type_new_set_doc(PyTypeObject *type)
4206+
type_new_set_doc(PyTypeObject *type, PyObject* dict)
42094207
{
4210-
PyObject *dict = lookup_tp_dict(type);
42114208
PyObject *doc = PyDict_GetItemWithError(dict, &_Py_ID(__doc__));
42124209
if (doc == NULL) {
42134210
if (PyErr_Occurred()) {
@@ -4241,9 +4238,8 @@ type_new_set_doc(PyTypeObject *type)
42414238

42424239

42434240
static int
4244-
type_new_staticmethod(PyTypeObject *type, PyObject *attr)
4241+
type_new_staticmethod(PyObject *dict, PyObject *attr)
42454242
{
4246-
PyObject *dict = lookup_tp_dict(type);
42474243
PyObject *func = PyDict_GetItemWithError(dict, attr);
42484244
if (func == NULL) {
42494245
if (PyErr_Occurred()) {
@@ -4269,9 +4265,8 @@ type_new_staticmethod(PyTypeObject *type, PyObject *attr)
42694265

42704266

42714267
static int
4272-
type_new_classmethod(PyTypeObject *type, PyObject *attr)
4268+
type_new_classmethod(PyObject *dict, PyObject *attr)
42734269
{
4274-
PyObject *dict = lookup_tp_dict(type);
42754270
PyObject *func = PyDict_GetItemWithError(dict, attr);
42764271
if (func == NULL) {
42774272
if (PyErr_Occurred()) {
@@ -4372,9 +4367,8 @@ type_new_set_slots(const type_new_ctx *ctx, PyTypeObject *type)
43724367

43734368
/* store type in class' cell if one is supplied */
43744369
static int
4375-
type_new_set_classcell(PyTypeObject *type)
4370+
type_new_set_classcell(PyTypeObject *type, PyObject *dict)
43764371
{
4377-
PyObject *dict = lookup_tp_dict(type);
43784372
PyObject *cell = PyDict_GetItemWithError(dict, &_Py_ID(__classcell__));
43794373
if (cell == NULL) {
43804374
if (PyErr_Occurred()) {
@@ -4399,9 +4393,8 @@ type_new_set_classcell(PyTypeObject *type)
43994393
}
44004394

44014395
static int
4402-
type_new_set_classdictcell(PyTypeObject *type)
4396+
type_new_set_classdictcell(PyObject *dict)
44034397
{
4404-
PyObject *dict = lookup_tp_dict(type);
44054398
PyObject *cell = PyDict_GetItemWithError(dict, &_Py_ID(__classdictcell__));
44064399
if (cell == NULL) {
44074400
if (PyErr_Occurred()) {
@@ -4432,30 +4425,33 @@ type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type)
44324425
return -1;
44334426
}
44344427

4435-
if (type_new_set_module(type) < 0) {
4428+
PyObject *dict = lookup_tp_dict(type);
4429+
assert(dict);
4430+
4431+
if (type_new_set_module(dict) < 0) {
44364432
return -1;
44374433
}
44384434

4439-
if (type_new_set_ht_name(type) < 0) {
4435+
if (type_new_set_ht_name(type, dict) < 0) {
44404436
return -1;
44414437
}
44424438

4443-
if (type_new_set_doc(type) < 0) {
4439+
if (type_new_set_doc(type, dict) < 0) {
44444440
return -1;
44454441
}
44464442

44474443
/* Special-case __new__: if it's a plain function,
44484444
make it a static function */
4449-
if (type_new_staticmethod(type, &_Py_ID(__new__)) < 0) {
4445+
if (type_new_staticmethod(dict, &_Py_ID(__new__)) < 0) {
44504446
return -1;
44514447
}
44524448

44534449
/* Special-case __init_subclass__ and __class_getitem__:
44544450
if they are plain functions, make them classmethods */
4455-
if (type_new_classmethod(type, &_Py_ID(__init_subclass__)) < 0) {
4451+
if (type_new_classmethod(dict, &_Py_ID(__init_subclass__)) < 0) {
44564452
return -1;
44574453
}
4458-
if (type_new_classmethod(type, &_Py_ID(__class_getitem__)) < 0) {
4454+
if (type_new_classmethod(dict, &_Py_ID(__class_getitem__)) < 0) {
44594455
return -1;
44604456
}
44614457

@@ -4465,10 +4461,10 @@ type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type)
44654461

44664462
type_new_set_slots(ctx, type);
44674463

4468-
if (type_new_set_classcell(type) < 0) {
4464+
if (type_new_set_classcell(type, dict) < 0) {
44694465
return -1;
44704466
}
4471-
if (type_new_set_classdictcell(type) < 0) {
4467+
if (type_new_set_classdictcell(dict) < 0) {
44724468
return -1;
44734469
}
44744470
return 0;

0 commit comments

Comments
 (0)