diff -r fa097a336079 Lib/test/test_types.py
--- a/Lib/test/test_types.py Wed Jun 24 12:51:55 2015 -0400
+++ b/Lib/test/test_types.py Wed Jun 24 12:58:53 2015 -0400
@@ -1263,6 +1263,16 @@
self.assertIs(foo(), coro)
self.assertIs(foo().__await__(), coro)
+ def test_wrapper_wrong_arg(self):
+ with self.assertRaisesRegex(TypeError, 'int is not an iterator'):
+ types._GeneratorWrapper(1)
+
+ class NonIter:
+ def __iter__(self):
+ return self
+ with self.assertRaisesRegex(TypeError, 'NonIter is not an iterator'):
+ types._GeneratorWrapper(NonIter())
+
def test_duck_gen(self):
class GenLike:
def send(self): pass
diff -r fa097a336079 Lib/types.py
--- a/Lib/types.py Wed Jun 24 12:51:55 2015 -0400
+++ b/Lib/types.py Wed Jun 24 12:58:53 2015 -0400
@@ -165,39 +165,9 @@
import functools as _functools
import collections.abc as _collections_abc
+from _types import GeneratorWrapper as _GeneratorWrapper, \
+ coroutine as _coroutine
-class _GeneratorWrapper:
- # TODO: Implement this in C.
- def __init__(self, gen):
- self.__wrapped = gen
- self.__isgen = gen.__class__ is GeneratorType
- self.__name__ = getattr(gen, '__name__', None)
- self.__qualname__ = getattr(gen, '__qualname__', None)
- def send(self, val):
- return self.__wrapped.send(val)
- def throw(self, tp, *rest):
- return self.__wrapped.throw(tp, *rest)
- def close(self):
- return self.__wrapped.close()
- @property
- def gi_code(self):
- return self.__wrapped.gi_code
- @property
- def gi_frame(self):
- return self.__wrapped.gi_frame
- @property
- def gi_running(self):
- return self.__wrapped.gi_running
- cr_code = gi_code
- cr_frame = gi_frame
- cr_running = gi_running
- def __next__(self):
- return next(self.__wrapped)
- def __iter__(self):
- if self.__isgen:
- return self.__wrapped
- return self
- __await__ = __iter__
def coroutine(func):
"""Convert regular generator function to a coroutine."""
@@ -205,30 +175,10 @@
if not callable(func):
raise TypeError('types.coroutine() expects a callable')
- if (func.__class__ is FunctionType and
- getattr(func, '__code__', None).__class__ is CodeType):
-
- co_flags = func.__code__.co_flags
-
- # Check if 'func' is a coroutine function.
- # (0x180 == CO_COROUTINE | CO_ITERABLE_COROUTINE)
- if co_flags & 0x180:
- return func
-
- # Check if 'func' is a generator function.
- # (0x20 == CO_GENERATOR)
- if co_flags & 0x20:
- # TODO: Implement this in C.
- co = func.__code__
- func.__code__ = CodeType(
- co.co_argcount, co.co_kwonlyargcount, co.co_nlocals,
- co.co_stacksize,
- co.co_flags | 0x100, # 0x100 == CO_ITERABLE_COROUTINE
- co.co_code,
- co.co_consts, co.co_names, co.co_varnames, co.co_filename,
- co.co_name, co.co_firstlineno, co.co_lnotab, co.co_freevars,
- co.co_cellvars)
- return func
+ try:
+ return _coroutine(func)
+ except TypeError:
+ pass
# The following code is primarily to support functions that
# return generator-like objects (for instance generators
diff -r fa097a336079 Modules/Setup.dist
--- a/Modules/Setup.dist Wed Jun 24 12:51:55 2015 -0400
+++ b/Modules/Setup.dist Wed Jun 24 12:58:53 2015 -0400
@@ -119,6 +119,7 @@
atexit atexitmodule.c # Register functions to be run at interpreter-shutdown
_stat _stat.c # stat.h interface
time timemodule.c # -lm # time operations and variables
+_types _typesmodule.c # Types module speedups
# access to ISO C locale support
_locale _localemodule.c # -lintl
diff -r fa097a336079 Modules/_typesmodule.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Modules/_typesmodule.c Wed Jun 24 12:58:53 2015 -0400
@@ -0,0 +1,302 @@
+/* Helpers for types module. */
+
+#include "Python.h"
+#include "structmember.h"
+
+static PyObject *
+types_coroutine(PyObject *self, PyObject *func)
+{
+ PyCodeObject *func_code;
+
+ if (!PyFunction_Check(func)) {
+ PyErr_Format(PyExc_TypeError,
+ "callable expected, got %.50s",
+ Py_TYPE(func)->tp_name);
+ return NULL;
+ }
+
+ func_code = (PyCodeObject *)PyFunction_GET_CODE(func);
+ assert(func_code);
+
+ if (func_code->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE)) {
+ Py_INCREF(func);
+ return func;
+ }
+
+ if (!(func_code->co_flags & CO_GENERATOR)) {
+ PyErr_SetString(PyExc_TypeError,
+ "generator function expected");
+ return NULL;
+ }
+
+ func_code->co_flags |= CO_ITERABLE_COROUTINE;
+
+ Py_INCREF(func);
+ return func;
+}
+
+PyDoc_STRVAR(types_coroutine_doc,
+"coroutine(func) -> None\n\
+\n\
+Applies CO_ITERABLE_COROUTINE to generator function's code object.\n\
+This is an internal helper for types.coroutine(), do not use this \n\
+function directly.\n");
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *gw_wrapped;
+ PyObject *gw_weakreflist;
+} PyGenWrapper;
+
+static PyObject *
+PyGenWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ PyObject *gen;
+ PyGenWrapper *gw;
+
+ if (!PyArg_UnpackTuple(args, "GeneratorWrapper", 1, 1, &gen))
+ return NULL;
+
+ if (!PyGen_CheckExact(gen) && !PyIter_Check(gen)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.50s is not an iterator",
+ Py_TYPE(gen)->tp_name);
+ return NULL;
+ }
+
+ gw = PyObject_GC_New(PyGenWrapper, type);
+ if (gw == NULL)
+ return NULL;
+
+ gw->gw_weakreflist = NULL;
+
+ Py_INCREF(gen);
+ gw->gw_wrapped = gen;
+ _PyObject_GC_TRACK(gw);
+ return (PyObject *)gw;
+}
+
+static void
+PyGenWrapper_dealloc(PyGenWrapper *gw)
+{
+ _PyObject_GC_UNTRACK(gw);
+
+ Py_CLEAR(((PyGenWrapper*)gw)->gw_wrapped);
+ if (gw->gw_weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *)gw);
+
+ PyObject_GC_Del(gw);
+}
+
+static int
+PyGenWrapper_traverse(PyGenWrapper *gw, visitproc visit, void *arg)
+{
+ Py_VISIT((PyObject *)gw->gw_wrapped);
+ return 0;
+}
+
+static PyObject *
+PyGenWrapper_await(PyGenWrapper *gw)
+{
+ PyObject *wrapped = gw->gw_wrapped;
+ if (PyGen_CheckExact(wrapped)) {
+ /* Speed things up a little bit for pure generators. */
+ Py_INCREF(wrapped);
+ return wrapped;
+ }
+
+ Py_INCREF(gw);
+ return (PyObject *)gw;
+}
+
+static PyObject *
+PyGenWrapper_iternext(PyGenWrapper *gw)
+{
+ iternextfunc next = NULL;
+ PyObject *wrapped = gw->gw_wrapped;
+ PyTypeObject *type = Py_TYPE(wrapped);
+
+ if (PyGen_CheckExact(wrapped)) {
+ return _PyGen_Send((PyGenObject *)wrapped, NULL);
+ }
+
+ next = type->tp_iternext;
+ if (next == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%.50s is not an iterator",
+ type->tp_name);
+ return NULL;
+ }
+
+ return (*next)(wrapped);
+}
+
+static PyObject *
+PyGenWrapper_send(PyGenWrapper *gw, PyObject *arg)
+{
+ _Py_IDENTIFIER(send);
+ return _PyObject_CallMethodIdObjArgs(gw->gw_wrapped, &PyId_send,
+ arg, NULL);
+}
+
+static PyObject *
+PyGenWrapper_throw(PyGenWrapper *gw, PyObject *args)
+{
+ _Py_IDENTIFIER(throw);
+ PyObject *typ;
+ PyObject *tb = NULL;
+ PyObject *val = NULL;
+
+ if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
+ return NULL;
+
+ return _PyObject_CallMethodIdObjArgs(gw->gw_wrapped, &PyId_throw,
+ typ, val, tb, NULL);
+}
+
+static PyObject *
+PyGenWrapper_close(PyGenWrapper *gw, PyObject *arg)
+{
+ _Py_IDENTIFIER(close);
+ return _PyObject_CallMethodId(gw->gw_wrapped, &PyId_close, NULL);
+}
+
+static PyObject *
+PyGenWrapper_get_name(PyGenWrapper *gw)
+{
+ _Py_IDENTIFIER(__name__);
+ return _PyObject_GetAttrId(gw->gw_wrapped, &PyId___name__);
+}
+
+static PyObject *
+PyGenWrapper_get_qualname(PyGenWrapper *gw)
+{
+ _Py_IDENTIFIER(__qualname__);
+ return _PyObject_GetAttrId(gw->gw_wrapped, &PyId___qualname__);
+}
+
+static PyObject *
+PyGenWrapper_get_gi_running(PyGenWrapper *gw)
+{
+ _Py_IDENTIFIER(gi_running);
+ return _PyObject_GetAttrId(gw->gw_wrapped, &PyId_gi_running);
+}
+
+static PyObject *
+PyGenWrapper_get_gi_frame(PyGenWrapper *gw)
+{
+ _Py_IDENTIFIER(gi_frame);
+ return _PyObject_GetAttrId(gw->gw_wrapped, &PyId_gi_frame);
+}
+
+static PyObject *
+PyGenWrapper_get_gi_code(PyGenWrapper *gw)
+{
+ _Py_IDENTIFIER(gi_code);
+ return _PyObject_GetAttrId(gw->gw_wrapped, &PyId_gi_code);
+}
+
+static PyAsyncMethods GeneratorWrapperType_as_async = {
+ (unaryfunc)PyGenWrapper_await, /* am_await */
+ 0, /* am_aiter */
+ 0 /* am_anext */
+};
+
+static PyMethodDef GeneratorWrapperType_methods[] = {
+ {"send", (PyCFunction)PyGenWrapper_send, METH_O, NULL},
+ {"throw", (PyCFunction)PyGenWrapper_throw, METH_VARARGS, NULL},
+ {"close", (PyCFunction)PyGenWrapper_close, METH_NOARGS, NULL},
+ {NULL, NULL} /* Sentinel */
+};
+
+static PyGetSetDef GeneratorWrapperType_getsetlist[] = {
+ {"__name__", (getter)PyGenWrapper_get_name, NULL, NULL},
+ {"__qualname__", (getter)PyGenWrapper_get_qualname, NULL, NULL},
+ {"gi_running", (getter)PyGenWrapper_get_gi_running, NULL, NULL},
+ {"gi_code", (getter)PyGenWrapper_get_gi_code, NULL, NULL},
+ {"gi_frame", (getter)PyGenWrapper_get_gi_frame, NULL, NULL},
+ {"cr_running", (getter)PyGenWrapper_get_gi_running, NULL, NULL},
+ {"cr_code", (getter)PyGenWrapper_get_gi_code, NULL, NULL},
+ {"cr_frame", (getter)PyGenWrapper_get_gi_frame, NULL, NULL},
+ {NULL} /* Sentinel */
+};
+
+static PyTypeObject GeneratorWrapperType = {
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "GeneratorWrapper",
+ sizeof(PyGenWrapper), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)PyGenWrapper_dealloc, /* destructor tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ &GeneratorWrapperType_as_async, /* tp_as_async */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "Wrapper around generator objects with __await__ method",
+ (traverseproc)PyGenWrapper_traverse, /* traverseproc tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(PyGenWrapper, gw_weakreflist), /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)PyGenWrapper_iternext, /* tp_iternext */
+ GeneratorWrapperType_methods, /* tp_methods */
+ 0, /* tp_members */
+ GeneratorWrapperType_getsetlist, /* tp_getset */
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ PyGenWrapper_new, /* tp_new */
+ PyObject_Del, /* tp_free */
+};
+
+
+static PyMethodDef types_methods[] = {
+ {"coroutine", types_coroutine, METH_O, types_coroutine_doc},
+ {NULL, NULL} /* sentinel */
+};
+
+PyDoc_STRVAR(module_doc, "Helpers for types module.\n");
+
+static struct PyModuleDef _typesmodule = {
+ PyModuleDef_HEAD_INIT,
+ "_types",
+ module_doc,
+ -1,
+ types_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+PyMODINIT_FUNC
+PyInit__types(void)
+{
+ PyObject *m;
+
+ m = PyModule_Create(&_typesmodule);
+ if (m == NULL)
+ return NULL;
+
+ if (PyType_Ready(&GeneratorWrapperType) < 0)
+ return NULL;
+ Py_INCREF(&GeneratorWrapperType);
+ PyModule_AddObject(m, "GeneratorWrapper",
+ (PyObject *)&GeneratorWrapperType);
+
+ return m;
+}
diff -r fa097a336079 PC/config.c
--- a/PC/config.c Wed Jun 24 12:51:55 2015 -0400
+++ b/PC/config.c Wed Jun 24 12:58:53 2015 -0400
@@ -38,6 +38,7 @@
extern PyObject* PyInit__collections(void);
extern PyObject* PyInit__heapq(void);
extern PyObject* PyInit__bisect(void);
+extern PyObject* PyInit__types(void);
extern PyObject* PyInit__symtable(void);
extern PyObject* PyInit_mmap(void);
extern PyObject* PyInit__csv(void);
@@ -117,6 +118,7 @@
{"itertools", PyInit_itertools},
{"_collections", PyInit__collections},
{"_symtable", PyInit__symtable},
+ {"_types", PyInit__types},
{"mmap", PyInit_mmap},
{"_csv", PyInit__csv},
{"_sre", PyInit__sre},
diff -r fa097a336079 PCbuild/pythoncore.vcxproj
--- a/PCbuild/pythoncore.vcxproj Wed Jun 24 12:51:55 2015 -0400
+++ b/PCbuild/pythoncore.vcxproj Wed Jun 24 12:58:53 2015 -0400
@@ -228,6 +228,7 @@
+
@@ -417,4 +418,4 @@
-
\ No newline at end of file
+
diff -r fa097a336079 PCbuild/pythoncore.vcxproj.filters
--- a/PCbuild/pythoncore.vcxproj.filters Wed Jun 24 12:51:55 2015 -0400
+++ b/PCbuild/pythoncore.vcxproj.filters Wed Jun 24 12:58:53 2015 -0400
@@ -473,6 +473,9 @@
Modules
+
+ Modules
+
Modules
@@ -974,4 +977,4 @@
Resource Files
-
\ No newline at end of file
+