Skip to content

Commit 68ddb59

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
[2.7] bpo-22851: Fix a segfault when accessing generator.gi_frame.f_restricted. (GH-9348)
Frame's field f_tstate is NULL when the generator is exhausted.
1 parent 6198976 commit 68ddb59

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Include/frameobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ PyAPI_DATA(PyTypeObject) PyFrame_Type;
5656

5757
#define PyFrame_Check(op) (Py_TYPE(op) == &PyFrame_Type)
5858
#define PyFrame_IsRestricted(f) \
59-
((f)->f_builtins != (f)->f_tstate->interp->builtins)
59+
((f)->f_tstate && (f)->f_builtins != (f)->f_tstate->interp->builtins)
6060

6161
PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
6262
PyObject *, PyObject *);

Lib/test/test_generators.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,16 @@ def printsolution(self, x):
18771877
18781878
"""
18791879

1880+
crash_test = """
1881+
>>> def foo(): yield
1882+
>>> gen = foo()
1883+
>>> gen.next()
1884+
>>> print gen.gi_frame.f_restricted # This would segfault.
1885+
False
1886+
1887+
"""
1888+
1889+
18801890
__test__ = {"tut": tutorial_tests,
18811891
"pep": pep_tests,
18821892
"email": email_tests,
@@ -1886,6 +1896,7 @@ def printsolution(self, x):
18861896
"weakref": weakref_tests,
18871897
"coroutine": coroutine_tests,
18881898
"refleaks": refleaks_tests,
1899+
"crash": crash_test,
18891900
}
18901901

18911902
# Magic test name that regrtest.py invokes *after* importing this module.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a segfault when accessing ``generator.gi_frame.f_restricted`` when the
2+
generator is exhausted. Patch by Zackery Spytz.

0 commit comments

Comments
 (0)