Skip to content

Commit b2f0564

Browse files
committed
Make PyDateTime macros work with managed objects
1 parent 141b778 commit b2f0564

File tree

2 files changed

+80
-29
lines changed

2 files changed

+80
-29
lines changed

graalpython/com.oracle.graal.python.cext/include/datetime.h

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,32 @@ typedef struct
124124
// o is a pointer to a time or a datetime object.
125125
#define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
126126

127-
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
128-
((PyDateTime_Date*)o)->data[1])
129-
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
130-
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
131-
132-
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
133-
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
134-
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
135-
#define PyDateTime_DATE_GET_MICROSECOND(o) \
136-
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
137-
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
138-
((PyDateTime_DateTime*)o)->data[9])
139-
#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
140-
#define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
141-
((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
127+
#define PyDateTime_GET_YEAR(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "year")))
128+
#define PyDateTime_GET_MONTH(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "month")))
129+
#define PyDateTime_GET_DAY(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "day")))
130+
131+
#define PyDateTime_DATE_GET_HOUR(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "hour")))
132+
#define PyDateTime_DATE_GET_MINUTE(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "minute")))
133+
#define PyDateTime_DATE_GET_SECOND(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "second")))
134+
#define PyDateTime_DATE_GET_MICROSECOND(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "microsecond")))
135+
#define PyDateTime_DATE_GET_FOLD(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "fold")))
136+
// TODO borrow
137+
#define PyDateTime_DATE_GET_TZINFO(o) (PyObject_GetAttrString((PyObject*)o, "tzinfo"))
142138

143139
/* Apply for time instances. */
144-
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
145-
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
146-
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
147-
#define PyDateTime_TIME_GET_MICROSECOND(o) \
148-
((((PyDateTime_Time*)o)->data[3] << 16) | \
149-
(((PyDateTime_Time*)o)->data[4] << 8) | \
150-
((PyDateTime_Time*)o)->data[5])
151-
#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
152-
#define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
153-
((PyDateTime_Time *)(o))->tzinfo : Py_None)
140+
#define PyDateTime_TIME_GET_HOUR(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "hour")))
141+
#define PyDateTime_TIME_GET_MINUTE(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "minute")))
142+
#define PyDateTime_TIME_GET_SECOND(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "second")))
143+
#define PyDateTime_TIME_GET_MICROSECOND(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "microsecond")))
144+
#define PyDateTime_TIME_GET_FOLD(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "fold")))
145+
// TODO borrow
146+
#define PyDateTime_TIME_GET_TZINFO(o) (PyObject_GetAttrString((PyObject*)o, "tzinfo"))
154147

155148
/* Apply for time delta instances */
156-
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
157-
#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
158-
#define PyDateTime_DELTA_GET_MICROSECONDS(o) \
159-
(((PyDateTime_Delta*)o)->microseconds)
149+
#define PyDateTime_DELTA_GET_DAYS(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "days")))
150+
#define PyDateTime_DELTA_GET_SECONDS(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "seconds")))
151+
#define PyDateTime_DELTA_GET_MICROSECONDS(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "microseconds")))
152+
160153

161154

162155
/* Define structure for C API. */

graalpython/com.oracle.graal.python.cext/src/datetime.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,64 @@ static int PyLong_AsInt(PyObject *arg) {
121121
return (int) ival;
122122
}
123123

124+
#undef _PyDateTime_HAS_TZINFO
125+
#undef PyDateTime_GET_YEAR
126+
#undef PyDateTime_GET_MONTH
127+
#undef PyDateTime_GET_DAY
128+
#undef PyDateTime_DATE_GET_HOUR
129+
#undef PyDateTime_DATE_GET_MINUTE
130+
#undef PyDateTime_DATE_GET_SECOND
131+
#undef PyDateTime_DATE_GET_MICROSECOND
132+
#undef PyDateTime_DATE_GET_FOLD
133+
#undef PyDateTime_DATE_GET_TZINFO
134+
#undef PyDateTime_TIME_GET_HOUR
135+
#undef PyDateTime_TIME_GET_MINUTE
136+
#undef PyDateTime_TIME_GET_SECOND
137+
#undef PyDateTime_TIME_GET_MICROSECOND
138+
#undef PyDateTime_TIME_GET_FOLD
139+
#undef PyDateTime_TIME_GET_TZINFO
140+
#undef PyDateTime_DELTA_GET_DAYS
141+
#undef PyDateTime_DELTA_GET_SECONDS
142+
#undef PyDateTime_DELTA_GET_MICROSECONDS
143+
144+
// Our normal datetime.h macros handle managed objects, redefine to handle only native
145+
146+
#define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
147+
148+
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
149+
((PyDateTime_Date*)o)->data[1])
150+
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
151+
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
152+
153+
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
154+
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
155+
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
156+
#define PyDateTime_DATE_GET_MICROSECOND(o) \
157+
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
158+
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
159+
((PyDateTime_DateTime*)o)->data[9])
160+
#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
161+
#define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
162+
((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
163+
164+
/* Apply for time instances. */
165+
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
166+
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
167+
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
168+
#define PyDateTime_TIME_GET_MICROSECOND(o) \
169+
((((PyDateTime_Time*)o)->data[3] << 16) | \
170+
(((PyDateTime_Time*)o)->data[4] << 8) | \
171+
((PyDateTime_Time*)o)->data[5])
172+
#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
173+
#define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
174+
((PyDateTime_Time *)(o))->tzinfo : Py_None)
175+
176+
/* Apply for time delta instances */
177+
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
178+
#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
179+
#define PyDateTime_DELTA_GET_MICROSECONDS(o) \
180+
(((PyDateTime_Delta*)o)->microseconds)
181+
124182

125183
/* The following code is taken from CPython '_datetimemodule.c' */
126184

0 commit comments

Comments
 (0)