@@ -22,7 +22,6 @@ static PyMemberDef frame_memberlist[] = {
2222 {NULL } /* Sentinel */
2323};
2424
25-
2625static struct _Py_frame_state *
2726get_frame_state (void )
2827{
@@ -816,73 +815,27 @@ frame_alloc(PyCodeObject *code)
816815}
817816
818817
819- static inline PyObject *
820- frame_get_builtins (PyFrameObject * back , PyObject * globals )
821- {
822- PyObject * builtins ;
823-
824- if (back != NULL && back -> f_globals == globals ) {
825- /* If we share the globals, we share the builtins.
826- Save a lookup and a call. */
827- builtins = back -> f_builtins ;
828- assert (builtins != NULL );
829- Py_INCREF (builtins );
830- return builtins ;
831- }
832-
833- builtins = _PyDict_GetItemIdWithError (globals , & PyId___builtins__ );
834- if (builtins != NULL && PyModule_Check (builtins )) {
835- builtins = PyModule_GetDict (builtins );
836- assert (builtins != NULL );
837- }
838- if (builtins != NULL ) {
839- Py_INCREF (builtins );
840- return builtins ;
841- }
842-
843- if (PyErr_Occurred ()) {
844- return NULL ;
845- }
846-
847- /* No builtins! Make up a minimal one.
848- Give them 'None', at least. */
849- builtins = PyDict_New ();
850- if (builtins == NULL ) {
851- return NULL ;
852- }
853- if (PyDict_SetItemString (builtins , "None" , Py_None ) < 0 ) {
854- Py_DECREF (builtins );
855- return NULL ;
856- }
857- return builtins ;
858- }
859-
860-
861818PyFrameObject * _Py_HOT_FUNCTION
862819_PyFrame_New_NoTrack (PyThreadState * tstate , PyCodeObject * code ,
863- PyObject * globals , PyObject * locals )
820+ PyObject * globals , PyObject * builtins , PyObject * locals )
864821{
865822#ifdef Py_DEBUG
866- if (code == NULL || globals == NULL || ! PyDict_Check ( globals ) ||
823+ if (code == NULL || globals == NULL || builtins == NULL ||
867824 (locals != NULL && !PyMapping_Check (locals ))) {
868825 PyErr_BadInternalCall ();
869826 return NULL ;
870827 }
871828#endif
872829
873830 PyFrameObject * back = tstate -> frame ;
874- PyObject * builtins = frame_get_builtins (back , globals );
875- if (builtins == NULL ) {
876- return NULL ;
877- }
878831
879832 PyFrameObject * f = frame_alloc (code );
880833 if (f == NULL ) {
881- Py_DECREF (builtins );
882834 return NULL ;
883835 }
884836
885837 f -> f_stackdepth = 0 ;
838+ Py_INCREF (builtins );
886839 f -> f_builtins = builtins ;
887840 Py_XINCREF (back );
888841 f -> f_back = back ;
@@ -902,8 +855,9 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
902855 f -> f_locals = locals ;
903856 }
904857 else {
905- if (locals == NULL )
858+ if (locals == NULL ) {
906859 locals = globals ;
860+ }
907861 Py_INCREF (locals );
908862 f -> f_locals = locals ;
909863 }
@@ -925,7 +879,9 @@ PyFrameObject*
925879PyFrame_New (PyThreadState * tstate , PyCodeObject * code ,
926880 PyObject * globals , PyObject * locals )
927881{
928- PyFrameObject * f = _PyFrame_New_NoTrack (tstate , code , globals , locals );
882+ PyObject * builtins = _PyEval_BuiltinsFromGlobals (globals );
883+ PyFrameObject * f = _PyFrame_New_NoTrack (tstate , code , globals , builtins , locals );
884+ Py_DECREF (builtins );
929885 if (f )
930886 _PyObject_GC_TRACK (f );
931887 return f ;
@@ -1223,3 +1179,28 @@ PyFrame_GetBack(PyFrameObject *frame)
12231179 Py_XINCREF (back );
12241180 return back ;
12251181}
1182+
1183+ PyObject * _PyEval_BuiltinsFromGlobals (PyObject * globals ) {
1184+ PyObject * builtins = _PyDict_GetItemIdWithError (globals , & PyId___builtins__ );
1185+ if (builtins ) {
1186+ if (PyModule_Check (builtins )) {
1187+ builtins = PyModule_GetDict (builtins );
1188+ assert (builtins != NULL );
1189+ }
1190+ }
1191+ if (builtins == NULL ) {
1192+ if (PyErr_Occurred ()) {
1193+ return NULL ;
1194+ }
1195+ /* No builtins! Make up a minimal one
1196+ Give them 'None', at least. */
1197+ builtins = PyDict_New ();
1198+ if (builtins == NULL ||
1199+ PyDict_SetItemString (
1200+ builtins , "None" , Py_None ) < 0 )
1201+ return NULL ;
1202+ }
1203+ else
1204+ Py_INCREF (builtins );
1205+ return builtins ;
1206+ }
0 commit comments