Commit 3962e903 authored by Stefan Behnel's avatar Stefan Behnel

Correct the return type of the ..._GET_TZINFO() macros (which return a...

Correct the return type of the ..._GET_TZINFO() macros (which return a borrowed reference) and add corresponding properties so people don't have to use them.
parent 14559510
...@@ -53,6 +53,18 @@ cdef extern from "datetime.h": ...@@ -53,6 +53,18 @@ cdef extern from "datetime.h":
#define __Pyx_TimeZone_UTC PyDateTime_TimeZone_UTC #define __Pyx_TimeZone_UTC PyDateTime_TimeZone_UTC
#define __Pyx_TimeZone_FromOffsetAndName(offset, name) PyTimeZone_FromOffsetAndName(offset, name) #define __Pyx_TimeZone_FromOffsetAndName(offset, name) PyTimeZone_FromOffsetAndName(offset, name)
#endif #endif
/* Backport for Python < 3.10 */
#if PY_VERSION_HEX < 0x030a00a1
#ifndef PyDateTime_TIME_GET_TZINFO
#define PyDateTime_TIME_GET_TZINFO(o) \
((((PyDateTime_Time*)o)->hastzinfo) ? ((PyDateTime_Time*)o)->tzinfo : Py_None)
#endif
#ifndef PyDateTime_DATE_GET_TZINFO
#define PyDateTime_DATE_GET_TZINFO(o) \
((((PyDateTime_DateTime*)o)->hastzinfo) ? ((PyDateTime_DateTime*)o)->tzinfo : Py_None)
#endif
#endif
""" """
ctypedef extern class datetime.date[object PyDateTime_Date]: ctypedef extern class datetime.date[object PyDateTime_Date]:
...@@ -85,6 +97,10 @@ cdef extern from "datetime.h": ...@@ -85,6 +97,10 @@ cdef extern from "datetime.h":
cdef inline int microsecond(self): cdef inline int microsecond(self):
return PyDateTime_TIME_GET_MICROSECOND(self) return PyDateTime_TIME_GET_MICROSECOND(self)
@property
cdef inline object tzinfo(self):
return <object>PyDateTime_TIME_GET_TZINFO(self)
@property @property
cdef inline int fold(self): cdef inline int fold(self):
# For Python < 3.6 this returns 0 no matter what # For Python < 3.6 this returns 0 no matter what
...@@ -119,6 +135,10 @@ cdef extern from "datetime.h": ...@@ -119,6 +135,10 @@ cdef extern from "datetime.h":
cdef inline int microsecond(self): cdef inline int microsecond(self):
return PyDateTime_DATE_GET_MICROSECOND(self) return PyDateTime_DATE_GET_MICROSECOND(self)
@property
cdef inline object tzinfo(self):
return <object>PyDateTime_DATE_GET_TZINFO(self)
@property @property
cdef inline int fold(self): cdef inline int fold(self):
# For Python < 3.6 this returns 0 no matter what # For Python < 3.6 this returns 0 no matter what
...@@ -217,7 +237,7 @@ cdef extern from "datetime.h": ...@@ -217,7 +237,7 @@ cdef extern from "datetime.h":
int PyDateTime_DATE_GET_SECOND(object o) int PyDateTime_DATE_GET_SECOND(object o)
int PyDateTime_DATE_GET_MICROSECOND(object o) int PyDateTime_DATE_GET_MICROSECOND(object o)
int PyDateTime_DATE_GET_FOLD(object o) int PyDateTime_DATE_GET_FOLD(object o)
object PyDateTime_DATE_GET_TZINFO(object o) PyObject* PyDateTime_DATE_GET_TZINFO(object o) # returns a borrowed reference
# Getters for time (C macros). # Getters for time (C macros).
int PyDateTime_TIME_GET_HOUR(object o) int PyDateTime_TIME_GET_HOUR(object o)
...@@ -225,7 +245,7 @@ cdef extern from "datetime.h": ...@@ -225,7 +245,7 @@ cdef extern from "datetime.h":
int PyDateTime_TIME_GET_SECOND(object o) int PyDateTime_TIME_GET_SECOND(object o)
int PyDateTime_TIME_GET_MICROSECOND(object o) int PyDateTime_TIME_GET_MICROSECOND(object o)
int PyDateTime_TIME_GET_FOLD(object o) int PyDateTime_TIME_GET_FOLD(object o)
object PyDateTime_TIME_GET_TZINFO(object o) PyObject* PyDateTime_TIME_GET_TZINFO(object o) # returns a borrowed reference
# Getters for timedelta (C macros). # Getters for timedelta (C macros).
int PyDateTime_DELTA_GET_DAYS(object o) int PyDateTime_DELTA_GET_DAYS(object o)
...@@ -310,17 +330,11 @@ cdef inline object get_utc(): ...@@ -310,17 +330,11 @@ cdef inline object get_utc():
# Get tzinfo of time # Get tzinfo of time
cdef inline object time_tzinfo(object o): cdef inline object time_tzinfo(object o):
if (<PyDateTime_Time*>o).hastzinfo: return <object>PyDateTime_TIME_GET_TZINFO(o)
return <object>(<PyDateTime_Time*>o).tzinfo
else:
return None
# Get tzinfo of datetime # Get tzinfo of datetime
cdef inline object datetime_tzinfo(object o): cdef inline object datetime_tzinfo(object o):
if (<PyDateTime_DateTime*>o).hastzinfo: return <object>PyDateTime_DATE_GET_TZINFO(o)
return <object>(<PyDateTime_DateTime*>o).tzinfo
else:
return None
# Get year of date # Get year of date
cdef inline int date_year(object o): cdef inline int date_year(object o):
......
...@@ -7,11 +7,11 @@ from cpython.datetime cimport import_datetime ...@@ -7,11 +7,11 @@ from cpython.datetime cimport import_datetime
from cpython.datetime cimport time_new, date_new, datetime_new, timedelta_new from cpython.datetime cimport time_new, date_new, datetime_new, timedelta_new
from cpython.datetime cimport datetime, time from cpython.datetime cimport datetime, time
from cpython.datetime cimport time_tzinfo, datetime_tzinfo from cpython.datetime cimport time_tzinfo, datetime_tzinfo
from cpython.datetime cimport time_hour, time_minute, time_second, time_microsecond, time_fold from cpython.datetime cimport time_hour, time_minute, time_second, time_microsecond, time_tzinfo, time_fold
from cpython.datetime cimport date_day, date_month, date_year from cpython.datetime cimport date_day, date_month, date_year
from cpython.datetime cimport datetime_day, datetime_month, datetime_year from cpython.datetime cimport datetime_day, datetime_month, datetime_year
from cpython.datetime cimport datetime_hour, datetime_minute, datetime_second, \ from cpython.datetime cimport datetime_hour, datetime_minute, datetime_second, \
datetime_microsecond, datetime_fold datetime_microsecond, datetime_tzinfo, datetime_fold
from cpython.datetime cimport timedelta_days, timedelta_seconds, timedelta_microseconds from cpython.datetime cimport timedelta_days, timedelta_seconds, timedelta_microseconds
import_datetime() import_datetime()
...@@ -30,9 +30,9 @@ def test_datetime(int year, int month, int day, int hour, ...@@ -30,9 +30,9 @@ def test_datetime(int year, int month, int day, int hour,
int minute, int second, int microsecond, int fold): int minute, int second, int microsecond, int fold):
''' '''
>>> test_datetime(2012, 12, 31, 12, 30, 59, 12345, 0) >>> test_datetime(2012, 12, 31, 12, 30, 59, 12345, 0)
(True, True, True, True, True, True, True, True) (True, True, True, True, True, True, True, True, True)
>>> test_datetime(2012, 12, 11, 12, 30, 59, 3322, 1 if sys.version_info >= (3, 7) else 0) >>> test_datetime(2012, 12, 11, 12, 30, 59, 3322, 1 if sys.version_info >= (3, 7) else 0)
(True, True, True, True, True, True, True, True) (True, True, True, True, True, True, True, True, True)
''' '''
o = datetime_new( o = datetime_new(
year, month, day, hour, minute, second, microsecond, None, fold year, month, day, hour, minute, second, microsecond, None, fold
...@@ -44,20 +44,22 @@ def test_datetime(int year, int month, int day, int hour, ...@@ -44,20 +44,22 @@ def test_datetime(int year, int month, int day, int hour,
o.minute == datetime_minute(o), \ o.minute == datetime_minute(o), \
o.second == datetime_second(o), \ o.second == datetime_second(o), \
o.microsecond == datetime_microsecond(o), \ o.microsecond == datetime_microsecond(o), \
o.tzinfo == datetime_tzinfo(o), \
o.fold == datetime_fold(o) o.fold == datetime_fold(o)
def test_time(int hour, int minute, int second, int microsecond, int fold): def test_time(int hour, int minute, int second, int microsecond, int fold):
''' '''
>>> test_time(12, 30, 59, 12345, 0) >>> test_time(12, 30, 59, 12345, 0)
(True, True, True, True, True) (True, True, True, True, True, True)
>>> test_time(12, 30, 43, 5432, 1 if sys.version_info >= (3, 7) else 0) >>> test_time(12, 30, 43, 5432, 1 if sys.version_info >= (3, 7) else 0)
(True, True, True, True, True) (True, True, True, True, True, True)
''' '''
o = time_new(hour, minute, second, microsecond, None, fold) o = time_new(hour, minute, second, microsecond, None, fold)
return o.hour == time_hour(o), \ return o.hour == time_hour(o), \
o.minute == time_minute(o), \ o.minute == time_minute(o), \
o.second == time_second(o), \ o.second == time_second(o), \
o.microsecond == time_microsecond(o), \ o.microsecond == time_microsecond(o), \
o.tzinfo == time_tzinfo(o), \
o.fold == time_fold(o) o.fold == time_fold(o)
def test_timedelta(int days, int seconds, int microseconds): def test_timedelta(int days, int seconds, int microseconds):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment