[Python-Dev] Why is PyDateTimeAPI not initialized by datetime module import?
Alexander Belopolsky
alexander.belopolsky at gmail.com
Thu Jan 26 13:55:34 EST 2017
On Thu, Jan 26, 2017 at 11:00 AM, Skip Montanaro <skip.montanaro at gmail.com>
wrote:
> I just got burned (wasted a good day or so) by the fact that PyDateTimeAPI
> wasn't initialized. The datetime.rst doc states (emphasis mine):
>
> Before using any of these functions, the header file :file:`datetime.h`
> must be included in your source (note that this is not included by
> :file:`Python.h`), and the macro :c:macro:`PyDateTime_IMPORT` must be
> invoked, *usually as part of the module initialisation function*.
>
> I thought that surely the datetime module itself would initialize that
> stuff. Why not?
>
It is fairly common for the modules that export C API in a capsule to only
initialize that capsule when explicitly asked. For example, if you want to
use numpy C API, you need to call import_array() in your module
initialization function. [1] I don't know how expensive PyDateTime_IMPORT
is, but it cannot be called in _datetimemodule.c because it is guarded by
the Py_BUILD_CORE macro and is not available when _datetimemodule.c itself
is compiled. I don't know whether this can be easily circumvented, but it
is not hard to remember that one should initialize C API before using it.
Maybe we can improve the error message when
PyDateTime_IMPORT is forgotten. Feel free to open an issue for that.
[1]:
https://docs.scipy.org/doc/numpy-1.12.0/user/c-info.how-to-extend.html#required-subroutine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170126/bcaa3744/attachment.html>
More information about the Python-Dev
mailing list