I assume that you are using CPython and not another Python implementation. And I can reproduce the problem with CPython 3.6.1 (I don't have PyPy, Jython, IronPython, ... so I can't check them).
In this case, the offender is replacing PyObject_Call with _PyObject_CallNoArg in the C-equivalent of the callable_iterator.__next__ (your object is callable_iterator ).
PyObject_Call returns a new instance of datetime.datetime , and _PyObject_CallNoArg returns NULL (which is roughly equivalent to an exception in Python).
Dig a bit through CPython source code:
_PyObject_CallNoArg is just a macro for _PyObject_FastCall , which in turn is a macro for _PyObject_FastCallDict .
This _PyObject_FastCallDict function checks the type of the function ( C function or Python function or something else) and delegates to _PyCFunction_FastCallDict in this case, since datetime.now is a C function.
Since datetime.datetime.now has the METH_FASTCALL flag, it ends in the fourth case , but there _PyStack_UnpackDict returns NULL and the function is never called.
I will dwell on this and let the Python developers understand what is wrong there. @Martijn Pieters have already submitted a bug report and they will fix it (I just hope they fix it soon).
So, this is the error introduced in 3.6, and until it is fixed, you need to make sure that the method is not CFunction with the METH_FASTCALL flag. As a workaround, you can wrap it. In addition to the features mentioned in @Martijn Pieters, there is a simple one:
def now(): return datetime.datetime.now() j = iter(now, None) next(j)
MSeifert May 31 '17 at 12:22 2017-05-31 12:22
source share