Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d77c4fb

Browse files
committed
Merge branch 'loadclass' into tvobject
2 parents 8a0ecb7 + e0acb87 commit d77c4fb

14 files changed

Lines changed: 498 additions & 450 deletions

Include/cpython/funcobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef struct {
4242
PyObject *func_module; /* The __module__ attribute, can be anything */
4343
PyObject *func_annotations; /* Annotations, a dict or NULL */
4444
PyObject *func_typeparams; /* Tuple of active type variables or NULL */
45+
PyObject *func_class_dict; /* Class dict, a dict or NULL */
4546
vectorcallfunc vectorcall;
4647
/* Version number for use by specializer.
4748
* Can set to non-zero when we want to specialize.
@@ -68,6 +69,7 @@ PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
6869
PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
6970
PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
7071
PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
72+
PyAPI_FUNC(PyObject *) PyFunction_GetClassDict(PyObject *);
7173
PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
7274
PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
7375
PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
@@ -100,6 +102,11 @@ static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
100102
}
101103
#define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))
102104

105+
static inline PyObject* PyFunction_GET_CLASS_DICT(PyObject *func) {
106+
return _PyFunction_CAST(func)->func_class_dict;
107+
}
108+
#define PyFunction_GET_CLASS_DICT(func) PyFunction_GET_CLASS_DICT(_PyObject_CAST(func))
109+
103110
static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {
104111
return _PyFunction_CAST(func)->func_module;
105112
}

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct _Py_global_strings {
124124
STRUCT_FOR_ID(__getnewargs__)
125125
STRUCT_FOR_ID(__getnewargs_ex__)
126126
STRUCT_FOR_ID(__getstate__)
127+
STRUCT_FOR_ID(__globals__)
127128
STRUCT_FOR_ID(__gt__)
128129
STRUCT_FOR_ID(__hash__)
129130
STRUCT_FOR_ID(__iadd__)

Include/internal/pycore_intrinsics.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
/* Binary Functions: */
2020

2121
#define INTRINSIC_PREP_RERAISE_STAR 1
22-
#define INTRINSIC_TYPEVAR_WITH_BOUND 2
23-
#define INTRINSIC_TYPEVAR_WITH_CONSTRAINTS 3
24-
#define INTRINSIC_SET_FUNCTION_TYPE_PARAMS 4
22+
#define INTRINSIC_SET_CLASS_DICT 2
23+
#define INTRINSIC_TYPEVAR_WITH_BOUND 3
24+
#define INTRINSIC_TYPEVAR_WITH_CONSTRAINTS 4
25+
#define INTRINSIC_SET_FUNCTION_TYPE_PARAMS 5
2526

26-
#define MAX_INTRINSIC_2 4
27+
#define MAX_INTRINSIC_2 5
2728

2829

2930
typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def _write_atomic(path, data, mode=0o666):
442442
# Python 3.12b1 3526 (Add instrumentation support)
443443
# Python 3.12b1 3527 (Add LOAD_SUPER_ATTR)
444444
# Python 3.12b1 3528 (Add LOAD_SUPER_ATTR_METHOD specialization)
445+
# Python 3.12b1 3529 (Add PEP 695 changes)
445446

446447
# Python 3.13 will start with 3550
447448

@@ -458,7 +459,7 @@ def _write_atomic(path, data, mode=0o666):
458459
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
459460
# in PC/launcher.c must also be updated.
460461

461-
MAGIC_NUMBER = (3528).to_bytes(2, 'little') + b'\r\n'
462+
MAGIC_NUMBER = (3529).to_bytes(2, 'little') + b'\r\n'
462463

463464
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
464465

Objects/funcobject.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
119119
op->func_defaults = Py_XNewRef(constr->fc_defaults);
120120
op->func_kwdefaults = Py_XNewRef(constr->fc_kwdefaults);
121121
op->func_closure = Py_XNewRef(constr->fc_closure);
122+
op->func_class_dict = NULL;
122123
op->func_doc = Py_NewRef(Py_None);
123124
op->func_dict = NULL;
124125
op->func_weakreflist = NULL;
@@ -204,6 +205,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
204205
op->func_module = module;
205206
op->func_annotations = NULL;
206207
op->func_typeparams = NULL;
208+
op->func_class_dict = NULL;
207209
op->vectorcall = _PyFunction_Vectorcall;
208210
op->func_version = 0;
209211
_PyObject_GC_TRACK(op);
@@ -264,6 +266,16 @@ PyFunction_GetGlobals(PyObject *op)
264266
return ((PyFunctionObject *) op) -> func_globals;
265267
}
266268

269+
PyObject *
270+
PyFunction_GetClassDict(PyObject *op)
271+
{
272+
if (!PyFunction_Check(op)) {
273+
PyErr_BadInternalCall();
274+
return NULL;
275+
}
276+
return ((PyFunctionObject *) op) -> func_class_dict;
277+
}
278+
267279
PyObject *
268280
PyFunction_GetModule(PyObject *op)
269281
{
@@ -454,6 +466,7 @@ static PyMemberDef func_memberlist[] = {
454466
{"__globals__", T_OBJECT, OFF(func_globals), READONLY},
455467
{"__module__", T_OBJECT, OFF(func_module), 0},
456468
{"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
469+
{"__class_dict__",T_OBJECT, OFF(func_class_dict), READONLY},
457470
{NULL} /* Sentinel */
458471
};
459472

@@ -813,6 +826,7 @@ func_clear(PyFunctionObject *op)
813826
Py_CLEAR(op->func_closure);
814827
Py_CLEAR(op->func_annotations);
815828
Py_CLEAR(op->func_typeparams);
829+
Py_CLEAR(op->func_class_dict);
816830
// Don't Py_CLEAR(op->func_code), since code is always required
817831
// to be non-NULL. Similarly, name and qualname shouldn't be NULL.
818832
// However, name and qualname could be str subclasses, so they
@@ -868,6 +882,7 @@ func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
868882
Py_VISIT(f->func_closure);
869883
Py_VISIT(f->func_annotations);
870884
Py_VISIT(f->func_typeparams);
885+
Py_VISIT(f->func_class_dict);
871886
Py_VISIT(f->func_qualname);
872887
return 0;
873888
}

Objects/typeobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,6 +3040,9 @@ type_new_set_module(PyTypeObject *type)
30403040
if (PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), module) < 0) {
30413041
return -1;
30423042
}
3043+
if (PyDict_SetItem(type->tp_dict, &_Py_ID(__globals__), globals) < 0) {
3044+
return -1;
3045+
}
30433046
return 0;
30443047
}
30453048

Python/bltinmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
195195
Py_TYPE(ns)->tp_name);
196196
goto error;
197197
}
198+
PyObject *globals = PyEval_GetGlobals();
199+
if ((globals == NULL)
200+
|| (PyMapping_SetItemString(ns, "__globals__", globals) < 0)) {
201+
goto error;
202+
}
198203
PyThreadState *tstate = _PyThreadState_GET();
199204
EVAL_CALL_STAT_INC(EVAL_CALL_BUILD_CLASS);
200205
cell = _PyEval_Vector(tstate, (PyFunctionObject *)func, ns, NULL, 0, NULL);

0 commit comments

Comments
 (0)