Skip to content

Commit fdfd8a0

Browse files
chris-eiblFidget-Spinnerbrandtbucher
committed
make _PyMapping_GetOptionalItem2 private and comment scopes
Co-Authored-By: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Co-Authored-By: Brandt Bucher <brandt@python.org>
1 parent a75042a commit fdfd8a0

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

Include/abstract.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,6 @@ PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
887887
*/
888888
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
889889
PyAPI_FUNC(int) PyMapping_GetOptionalItem(PyObject *, PyObject *, PyObject **);
890-
PyAPI_FUNC(PyObject*) PyMapping_GetOptionalItem2(PyObject *, PyObject *, int *);
891890
PyAPI_FUNC(int) PyMapping_GetOptionalItemString(PyObject *, const char *, PyObject **);
892891
#endif
893892

Include/internal/pycore_ceval.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,11 @@ _Py_assert_within_stack_bounds(
475475
_PyInterpreterFrame *frame, _PyStackRef *stack_pointer,
476476
const char *filename, int lineno);
477477

478+
// Like PyMapping_GetOptionalItem, but returns the PyObject* instead of taking
479+
// it as an out parameter. This helps MSVC's escape analysis when used with
480+
// tail calling.
481+
PyAPI_FUNC(PyObject*) _PyMapping_GetOptionalItem2(PyObject* obj, PyObject* key, int* err);
482+
478483
#ifdef __cplusplus
479484
}
480485
#endif

Objects/abstract.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
225225
}
226226

227227
PyObject*
228-
PyMapping_GetOptionalItem2(PyObject *obj, PyObject *key, int *err)
228+
_PyMapping_GetOptionalItem2(PyObject *obj, PyObject *key, int *err)
229229
{
230230
PyObject* result;
231231
*err = PyMapping_GetOptionalItem(obj, key, &result);

Python/bytecodes.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ dummy_func(
15081508

15091509
inst(LOAD_BUILD_CLASS, ( -- bc)) {
15101510
int err;
1511-
PyObject *bc_o = PyMapping_GetOptionalItem2(BUILTINS(), &_Py_ID(__build_class__), &err);
1511+
PyObject *bc_o = _PyMapping_GetOptionalItem2(BUILTINS(), &_Py_ID(__build_class__), &err);
15121512
ERROR_IF(err < 0);
15131513
if (bc_o == NULL) {
15141514
_PyErr_SetString(tstate, PyExc_NameError,
@@ -1712,7 +1712,7 @@ dummy_func(
17121712
inst(LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) {
17131713
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
17141714
int err;
1715-
PyObject *v_o = PyMapping_GetOptionalItem2(PyStackRef_AsPyObjectBorrow(mod_or_class_dict), name, &err);
1715+
PyObject *v_o = _PyMapping_GetOptionalItem2(PyStackRef_AsPyObjectBorrow(mod_or_class_dict), name, &err);
17161716

17171717
PyStackRef_CLOSE(mod_or_class_dict);
17181718
ERROR_IF(err < 0);
@@ -1736,11 +1736,11 @@ dummy_func(
17361736
else {
17371737
/* Slow-path if globals or builtins is not a dict */
17381738
/* namespace 1: globals */
1739-
v_o = PyMapping_GetOptionalItem2(GLOBALS(), name, &err);
1739+
v_o = _PyMapping_GetOptionalItem2(GLOBALS(), name, &err);
17401740
ERROR_IF(err < 0);
17411741
if (v_o == NULL) {
17421742
/* namespace 2: builtins */
1743-
v_o = PyMapping_GetOptionalItem2(BUILTINS(), name, &err);
1743+
v_o = _PyMapping_GetOptionalItem2(BUILTINS(), name, &err);
17441744
ERROR_IF(err < 0);
17451745
if (v_o == NULL) {
17461746
_PyEval_FormatExcCheckArg(
@@ -1906,7 +1906,7 @@ dummy_func(
19061906
assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
19071907
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
19081908
int err;
1909-
PyObject* value_o = PyMapping_GetOptionalItem2(class_dict, name, &err);
1909+
PyObject* value_o = _PyMapping_GetOptionalItem2(class_dict, name, &err);
19101910
if (err < 0) {
19111911
ERROR_NO_POP();
19121912
}
@@ -2082,7 +2082,7 @@ dummy_func(
20822082
}
20832083
/* check if __annotations__ in locals()... */
20842084
int err;
2085-
PyObject* ann_dict = PyMapping_GetOptionalItem2(LOCALS(), &_Py_ID(__annotations__), &err);
2085+
PyObject* ann_dict = _PyMapping_GetOptionalItem2(LOCALS(), &_Py_ID(__annotations__), &err);
20862086
ERROR_IF(err < 0);
20872087
if (ann_dict == NULL) {
20882088
ann_dict = PyDict_New();
@@ -2188,6 +2188,7 @@ dummy_func(
21882188
// handle any case whose performance we care about
21892189
PyObject *super;
21902190
{
2191+
// scope to tell MSVC that stack is not escaping
21912192
PyObject *stack[] = {class, self};
21922193
super = PyObject_Vectorcall(global_super, stack, oparg & 2, NULL);
21932194
}
@@ -2251,6 +2252,7 @@ dummy_func(
22512252
int method_found = 0;
22522253
PyObject *attr_o;
22532254
{
2255+
// scope to tell MSVC that method_found_ptr is not escaping
22542256
int *method_found_ptr = &method_found;
22552257
attr_o = _PySuper_Lookup(cls, self, name,
22562258
Py_TYPE(self)->tp_getattro == PyObject_GenericGetAttr ? method_found_ptr : NULL);
@@ -3482,6 +3484,7 @@ dummy_func(
34823484
(void)lasti; // Shut up compiler warning if asserts are off
34833485
PyObject* res_o;
34843486
{
3487+
// scope to tell MSVC that stack is not escaping
34853488
PyObject *stack[5] = {NULL, PyStackRef_AsPyObjectBorrow(exit_self), exc, val_o, tb};
34863489
int has_self = !PyStackRef_IsNull(exit_self);
34873490
res_o = PyObject_Vectorcall(exit_func_o, stack + 2 - has_self,

Python/executor_cases.c.h

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

Python/generated_cases.c.h

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

0 commit comments

Comments
 (0)