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

Skip to content

Commit e440e47

Browse files
committed
Patch #957398: Add public API for Generator Object/Type.
1 parent 09e2cb0 commit e440e47

7 files changed

Lines changed: 207 additions & 138 deletions

File tree

Doc/api/concrete.tex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,3 +2603,34 @@ \subsection{Cell Objects \label{cell-objects}}
26032603
reference counts are adjusted, and no checks are made for safety;
26042604
\var{cell} must be non-\NULL{} and must be a cell object.
26052605
\end{cfuncdesc}
2606+
2607+
2608+
\subsection{Generator Objects \label{gen-objects}}
2609+
2610+
Generator objects are what Python uses to implement generator iterators.
2611+
They are normally created by iterating over a function that yields values,
2612+
rather than explicitly calling \cfunction{PyGen_New}.
2613+
2614+
\begin{ctypedesc}{PyGenObject}
2615+
The C structure used for generator objects.
2616+
\end{ctypedesc}
2617+
2618+
\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2619+
The type object corresponding to generator objects
2620+
\end{cvardesc}
2621+
2622+
\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2623+
Return true if \var{ob} is a generator object; \var{ob} must not be
2624+
\NULL.
2625+
\end{cfuncdesc}
2626+
2627+
\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2628+
Return true if \var{ob}'s type is \var{PyGen_Type}
2629+
is a generator object; \var{ob} must not be
2630+
\NULL.
2631+
\end{cfuncdesc}
2632+
2633+
\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2634+
Create and return a new generator object based on the \var{frame} object.
2635+
The parameter must not be \NULL.
2636+
\end{cfuncdesc}

Include/ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ PyAPI_FUNC(char *) PyEval_GetFuncName(PyObject *);
6464
PyAPI_FUNC(char *) PyEval_GetFuncDesc(PyObject *);
6565

6666
PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
67+
PyAPI_FUNC(PyObject *) PyEval_EvaluateFrame(PyObject *);
6768

6869
/* this used to be handled on a per-thread basis - now just two globals */
6970
PyAPI_DATA(volatile int) _Py_Ticker;

Include/genobject.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/* Generator object interface */
3+
4+
#ifndef Py_GENOBJECT_H
5+
#define Py_GENOBJECT_H
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
typedef struct {
11+
PyObject_HEAD
12+
/* The gi_ prefix is intended to remind of generator-iterator. */
13+
14+
PyFrameObject *gi_frame;
15+
16+
/* True if generator is being executed. */
17+
int gi_running;
18+
19+
/* List of weak reference. */
20+
PyObject *gi_weakreflist;
21+
} PyGenObject;
22+
23+
PyAPI_DATA(PyTypeObject) PyGen_Type;
24+
25+
#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
26+
#define PyGen_CheckExact(op) ((op)->ob_type == &PyGen_Type)
27+
28+
PyAPI_FUNC(PyObject *) PyGen_New(PyFrameObject *);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
#endif /* !Py_GENOBJECT_H */

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ OBJECT_OBJS= \
261261
Objects/complexobject.o \
262262
Objects/descrobject.o \
263263
Objects/enumobject.o \
264+
Objects/genobject.o \
264265
Objects/fileobject.o \
265266
Objects/floatobject.o \
266267
Objects/frameobject.o \
@@ -478,6 +479,7 @@ PYTHON_HEADERS= \
478479
Include/descrobject.h \
479480
Include/dictobject.h \
480481
Include/enumobject.h \
482+
Include/genobject.h \
481483
Include/fileobject.h \
482484
Include/floatobject.h \
483485
Include/funcobject.h \

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ Build
497497
C API
498498
-----
499499

500+
- New public functions PyEval_EvaluateFrame and PyGen_New to expose
501+
generator objects.
502+
500503
- New public functions Py_IncRef() and Py_DecRef(), exposing the
501504
functionality of the Py_XINCREF() and Py_XDECREF macros. Useful for
502505
runtime dynamic embedding of Python. See patch #938302, by Bob

Objects/genobject.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Generator object implementation */
2+
3+
#include "Python.h"
4+
#include "frameobject.h"
5+
#include "genobject.h"
6+
#include "ceval.h"
7+
#include "structmember.h"
8+
9+
static int
10+
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
11+
{
12+
return visit((PyObject *)gen->gi_frame, arg);
13+
}
14+
15+
static void
16+
gen_dealloc(PyGenObject *gen)
17+
{
18+
_PyObject_GC_UNTRACK(gen);
19+
if (gen->gi_weakreflist != NULL)
20+
PyObject_ClearWeakRefs((PyObject *) gen);
21+
Py_DECREF(gen->gi_frame);
22+
PyObject_GC_Del(gen);
23+
}
24+
25+
static PyObject *
26+
gen_iternext(PyGenObject *gen)
27+
{
28+
PyThreadState *tstate = PyThreadState_GET();
29+
PyFrameObject *f = gen->gi_frame;
30+
PyObject *result;
31+
32+
if (gen->gi_running) {
33+
PyErr_SetString(PyExc_ValueError,
34+
"generator already executing");
35+
return NULL;
36+
}
37+
if (f->f_stacktop == NULL)
38+
return NULL;
39+
40+
/* Generators always return to their most recent caller, not
41+
* necessarily their creator. */
42+
Py_XINCREF(tstate->frame);
43+
assert(f->f_back == NULL);
44+
f->f_back = tstate->frame;
45+
46+
gen->gi_running = 1;
47+
result = PyEval_EvaluateFrame((PyObject *)f);
48+
gen->gi_running = 0;
49+
50+
/* Don't keep the reference to f_back any longer than necessary. It
51+
* may keep a chain of frames alive or it could create a reference
52+
* cycle. */
53+
Py_XDECREF(f->f_back);
54+
f->f_back = NULL;
55+
56+
/* If the generator just returned (as opposed to yielding), signal
57+
* that the generator is exhausted. */
58+
if (result == Py_None && f->f_stacktop == NULL) {
59+
Py_DECREF(result);
60+
result = NULL;
61+
}
62+
63+
return result;
64+
}
65+
66+
static PyObject *
67+
gen_getiter(PyObject *gen)
68+
{
69+
Py_INCREF(gen);
70+
return gen;
71+
}
72+
73+
static PyMemberDef gen_memberlist[] = {
74+
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), RO},
75+
{"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO},
76+
{NULL} /* Sentinel */
77+
};
78+
79+
PyTypeObject PyGen_Type = {
80+
PyObject_HEAD_INIT(&PyType_Type)
81+
0, /* ob_size */
82+
"generator", /* tp_name */
83+
sizeof(PyGenObject), /* tp_basicsize */
84+
0, /* tp_itemsize */
85+
/* methods */
86+
(destructor)gen_dealloc, /* tp_dealloc */
87+
0, /* tp_print */
88+
0, /* tp_getattr */
89+
0, /* tp_setattr */
90+
0, /* tp_compare */
91+
0, /* tp_repr */
92+
0, /* tp_as_number */
93+
0, /* tp_as_sequence */
94+
0, /* tp_as_mapping */
95+
0, /* tp_hash */
96+
0, /* tp_call */
97+
0, /* tp_str */
98+
PyObject_GenericGetAttr, /* tp_getattro */
99+
0, /* tp_setattro */
100+
0, /* tp_as_buffer */
101+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
102+
0, /* tp_doc */
103+
(traverseproc)gen_traverse, /* tp_traverse */
104+
0, /* tp_clear */
105+
0, /* tp_richcompare */
106+
offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
107+
(getiterfunc)gen_getiter, /* tp_iter */
108+
(iternextfunc)gen_iternext, /* tp_iternext */
109+
0, /* tp_methods */
110+
gen_memberlist, /* tp_members */
111+
0, /* tp_getset */
112+
0, /* tp_base */
113+
0, /* tp_dict */
114+
};
115+
116+
PyObject *
117+
PyGen_New(PyFrameObject *f)
118+
{
119+
PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
120+
if (gen == NULL) {
121+
Py_DECREF(f);
122+
return NULL;
123+
}
124+
gen->gi_frame = f;
125+
gen->gi_running = 0;
126+
gen->gi_weakreflist = NULL;
127+
_PyObject_GC_TRACK(gen);
128+
return (PyObject *)gen;
129+
}

0 commit comments

Comments
 (0)