[pypy-dev] PyPy's PyDateTime_Delta definition

Amaury Forgeot d'Arc amauryfa at gmail.com
Fri Sep 6 23:34:47 CEST 2013


2013/9/6 Skip Montanaro <skip at pobox.com>

> Python 2.7's PyDateTime_Delta structure is defined like this:
>
> typedef struct
> {
>     PyObject_HEAD
>     long hashcode;              /* -1 when unknown */
>     int days;                   /* -MAX_DELTA_DAYS <= days <=
> MAX_DELTA_DAYS */
>     int seconds;                /* 0 <= seconds < 24*3600 is invariant */
>     int microseconds;           /* 0 <= microseconds < 1000000 is
> invariant */
> } PyDateTime_Delta;
>
> In contrast, PyPy's version leaves out everything except
> PyObject_HEAD:
>
> typedef struct {
>     PyObject_HEAD
> } PyDateTime_Delta;
>
> That doesn't seem to be compatible with C extension modules that want
> to manipulate such objects.  This is my first foray into trying to
> compile some Boost.Python wrappers for libraries at work which
> manipulate Python datetime objects. Why are the other four fields not
> defined? Is there a way to get around this?
>

Sure. Use macros like PyDateTime_DELTA_GET_DAYS(). The structure is not
part of the public API anyway.
With CPython the macros don't make any difference, but PyPy implements them
as actual function calls.

Remember that with PyPy, the C API needs to create a copy of each Python
object (at a fixed address). So to implement the additional fields, it
would be necessary to allocate more and copy more, even when the attributes
are not needed.
Oh, by the way, PyPy uses a pure Python implementation of datetime.py. So
there is little chance we can implement some shortcut in C to directly
reference some internal storage of the datetime types.

-- 
Amaury Forgeot d'Arc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20130906/61f802d8/attachment.html>


More information about the pypy-dev mailing list