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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 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
Prev Previous commit
Next Next commit
make _PyMapping_GetOptionalItem2 private and comment scopes
Co-Authored-By: Ken Jin <[email protected]>
Co-Authored-By: Brandt Bucher <[email protected]>
  • Loading branch information
3 people committed Dec 22, 2025
commit fdfd8a0476b5d09ea727f898d0265619fa0d573c
1 change: 0 additions & 1 deletion Include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,6 @@ PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
*/
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
PyAPI_FUNC(int) PyMapping_GetOptionalItem(PyObject *, PyObject *, PyObject **);
PyAPI_FUNC(PyObject*) PyMapping_GetOptionalItem2(PyObject *, PyObject *, int *);
PyAPI_FUNC(int) PyMapping_GetOptionalItemString(PyObject *, const char *, PyObject **);
#endif

Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ _Py_assert_within_stack_bounds(
_PyInterpreterFrame *frame, _PyStackRef *stack_pointer,
const char *filename, int lineno);

// Like PyMapping_GetOptionalItem, but returns the PyObject* instead of taking
// it as an out parameter. This helps MSVC's escape analysis when used with
// tail calling.
PyAPI_FUNC(PyObject*) _PyMapping_GetOptionalItem2(PyObject* obj, PyObject* key, int* err);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
}

PyObject*
PyMapping_GetOptionalItem2(PyObject *obj, PyObject *key, int *err)
_PyMapping_GetOptionalItem2(PyObject *obj, PyObject *key, int *err)
{
PyObject* result;
*err = PyMapping_GetOptionalItem(obj, key, &result);
Expand Down
15 changes: 9 additions & 6 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ dummy_func(

inst(LOAD_BUILD_CLASS, ( -- bc)) {
int err;
PyObject *bc_o = PyMapping_GetOptionalItem2(BUILTINS(), &_Py_ID(__build_class__), &err);
PyObject *bc_o = _PyMapping_GetOptionalItem2(BUILTINS(), &_Py_ID(__build_class__), &err);
ERROR_IF(err < 0);
if (bc_o == NULL) {
_PyErr_SetString(tstate, PyExc_NameError,
Expand Down Expand Up @@ -1712,7 +1712,7 @@ dummy_func(
inst(LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) {
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
int err;
PyObject *v_o = PyMapping_GetOptionalItem2(PyStackRef_AsPyObjectBorrow(mod_or_class_dict), name, &err);
PyObject *v_o = _PyMapping_GetOptionalItem2(PyStackRef_AsPyObjectBorrow(mod_or_class_dict), name, &err);

PyStackRef_CLOSE(mod_or_class_dict);
ERROR_IF(err < 0);
Expand All @@ -1736,11 +1736,11 @@ dummy_func(
else {
/* Slow-path if globals or builtins is not a dict */
/* namespace 1: globals */
v_o = PyMapping_GetOptionalItem2(GLOBALS(), name, &err);
v_o = _PyMapping_GetOptionalItem2(GLOBALS(), name, &err);
ERROR_IF(err < 0);
if (v_o == NULL) {
/* namespace 2: builtins */
v_o = PyMapping_GetOptionalItem2(BUILTINS(), name, &err);
v_o = _PyMapping_GetOptionalItem2(BUILTINS(), name, &err);
ERROR_IF(err < 0);
if (v_o == NULL) {
_PyEval_FormatExcCheckArg(
Expand Down Expand Up @@ -1906,7 +1906,7 @@ dummy_func(
assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
int err;
PyObject* value_o = PyMapping_GetOptionalItem2(class_dict, name, &err);
PyObject* value_o = _PyMapping_GetOptionalItem2(class_dict, name, &err);
if (err < 0) {
ERROR_NO_POP();
}
Expand Down Expand Up @@ -2082,7 +2082,7 @@ dummy_func(
}
/* check if __annotations__ in locals()... */
int err;
PyObject* ann_dict = PyMapping_GetOptionalItem2(LOCALS(), &_Py_ID(__annotations__), &err);
PyObject* ann_dict = _PyMapping_GetOptionalItem2(LOCALS(), &_Py_ID(__annotations__), &err);
ERROR_IF(err < 0);
if (ann_dict == NULL) {
ann_dict = PyDict_New();
Expand Down Expand Up @@ -2188,6 +2188,7 @@ dummy_func(
// handle any case whose performance we care about
PyObject *super;
{
Comment thread
chris-eibl marked this conversation as resolved.
// scope to tell MSVC that stack is not escaping
PyObject *stack[] = {class, self};
super = PyObject_Vectorcall(global_super, stack, oparg & 2, NULL);
}
Expand Down Expand Up @@ -2251,6 +2252,7 @@ dummy_func(
int method_found = 0;
PyObject *attr_o;
{
// scope to tell MSVC that method_found_ptr is not escaping
int *method_found_ptr = &method_found;
attr_o = _PySuper_Lookup(cls, self, name,
Py_TYPE(self)->tp_getattro == PyObject_GenericGetAttr ? method_found_ptr : NULL);
Expand Down Expand Up @@ -3482,6 +3484,7 @@ dummy_func(
(void)lasti; // Shut up compiler warning if asserts are off
PyObject* res_o;
{
// scope to tell MSVC that stack is not escaping
PyObject *stack[5] = {NULL, PyStackRef_AsPyObjectBorrow(exit_self), exc, val_o, tb};
int has_self = !PyStackRef_IsNull(exit_self);
res_o = PyObject_Vectorcall(exit_func_o, stack + 2 - has_self,
Expand Down
6 changes: 3 additions & 3 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.