@@ -22,7 +22,6 @@ static PyMemberDef frame_memberlist[] = {
22
22
{NULL } /* Sentinel */
23
23
};
24
24
25
-
26
25
static struct _Py_frame_state *
27
26
get_frame_state (void )
28
27
{
@@ -816,73 +815,27 @@ frame_alloc(PyCodeObject *code)
816
815
}
817
816
818
817
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
-
861
818
PyFrameObject * _Py_HOT_FUNCTION
862
819
_PyFrame_New_NoTrack (PyThreadState * tstate , PyCodeObject * code ,
863
- PyObject * globals , PyObject * locals )
820
+ PyObject * globals , PyObject * builtins , PyObject * locals )
864
821
{
865
822
#ifdef Py_DEBUG
866
- if (code == NULL || globals == NULL || ! PyDict_Check ( globals ) ||
823
+ if (code == NULL || globals == NULL || builtins == NULL ||
867
824
(locals != NULL && !PyMapping_Check (locals ))) {
868
825
PyErr_BadInternalCall ();
869
826
return NULL ;
870
827
}
871
828
#endif
872
829
873
830
PyFrameObject * back = tstate -> frame ;
874
- PyObject * builtins = frame_get_builtins (back , globals );
875
- if (builtins == NULL ) {
876
- return NULL ;
877
- }
878
831
879
832
PyFrameObject * f = frame_alloc (code );
880
833
if (f == NULL ) {
881
- Py_DECREF (builtins );
882
834
return NULL ;
883
835
}
884
836
885
837
f -> f_stackdepth = 0 ;
838
+ Py_INCREF (builtins );
886
839
f -> f_builtins = builtins ;
887
840
Py_XINCREF (back );
888
841
f -> f_back = back ;
@@ -902,8 +855,9 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
902
855
f -> f_locals = locals ;
903
856
}
904
857
else {
905
- if (locals == NULL )
858
+ if (locals == NULL ) {
906
859
locals = globals ;
860
+ }
907
861
Py_INCREF (locals );
908
862
f -> f_locals = locals ;
909
863
}
@@ -925,7 +879,9 @@ PyFrameObject*
925
879
PyFrame_New (PyThreadState * tstate , PyCodeObject * code ,
926
880
PyObject * globals , PyObject * locals )
927
881
{
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 );
929
885
if (f )
930
886
_PyObject_GC_TRACK (f );
931
887
return f ;
@@ -1223,3 +1179,28 @@ PyFrame_GetBack(PyFrameObject *frame)
1223
1179
Py_XINCREF (back );
1224
1180
return back ;
1225
1181
}
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