| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from rpython.rtyper.annlowlevel import llhelper |
| 3 |
from rpython.rlib.rarithmetic import widen |
| 4 |
from pypy.module.cpyext.pyobject import (PyObject, make_ref, make_typedescr, decref) |
| 5 |
from pypy.module.cpyext.api import (cpython_api, CANNOT_FAIL, cts, parse_dir, |
| 6 |
bootstrap_function, slot_function) |
| 7 |
from pypy.module.cpyext.import_ import PyImport_Import |
| 8 |
from pypy.module.cpyext.typeobject import PyTypeObjectPtr |
| 9 |
from pypy.interpreter.error import OperationError |
| 10 |
from pypy.interpreter.argument import Arguments |
| 11 |
from pypy.module.__pypy__.interp_pypydatetime import (W_DateTime_Date, |
| 12 |
W_DateTime_Time, W_DateTime_Delta) |
| 13 |
from rpython.tool.sourcetools import func_renamer |
| 14 |
from pypy.module.cpyext.state import State |
| 15 |
|
| 16 |
cts.parse_header(parse_dir / 'cpyext_datetime.h') |
| 17 |
|
| 18 |
|
| 19 |
PyDateTime_CAPI = cts.gettype('PyDateTime_CAPI') |
| 20 |
|
| 21 |
|
| 22 |
@cpython_api([], lltype.Ptr(PyDateTime_CAPI)) |
| 23 |
def _PyDateTime_Import(space): |
| 24 |
state = space.fromcache(State) |
| 25 |
if len(state.datetimeAPI) > 0: |
| 26 |
return state.datetimeAPI[0] |
| 27 |
datetimeAPI = lltype.malloc(PyDateTime_CAPI, flavor='raw', |
| 28 |
track_allocation=False) |
| 29 |
|
| 30 |
w_datetime = PyImport_Import(space, space.newtext("datetime")) |
| 31 |
|
| 32 |
w_type = space.getattr(w_datetime, space.newtext("date")) |
| 33 |
datetimeAPI.c_DateType = rffi.cast( |
| 34 |
PyTypeObjectPtr, make_ref(space, w_type)) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
datetimeAPI.c_DateType.c_tp_basicsize = rffi.sizeof(PyObject.TO) |
| 39 |
|
| 40 |
w_type = space.getattr(w_datetime, space.newtext("datetime")) |
| 41 |
datetimeAPI.c_DateTimeType = rffi.cast( |
| 42 |
PyTypeObjectPtr, make_ref(space, w_type)) |
| 43 |
|
| 44 |
w_type = space.getattr(w_datetime, space.newtext("time")) |
| 45 |
datetimeAPI.c_TimeType = rffi.cast( |
| 46 |
PyTypeObjectPtr, make_ref(space, w_type)) |
| 47 |
|
| 48 |
w_delta = space.getattr(w_datetime, space.newtext("timedelta")) |
| 49 |
datetimeAPI.c_DeltaType = rffi.cast( |
| 50 |
PyTypeObjectPtr, make_ref(space, w_delta)) |
| 51 |
|
| 52 |
w_tzinfo = space.getattr(w_datetime, space.newtext("tzinfo")) |
| 53 |
datetimeAPI.c_TZInfoType = rffi.cast( |
| 54 |
PyTypeObjectPtr, make_ref(space, w_tzinfo)) |
| 55 |
|
| 56 |
|
| 57 |
w_timezone = space.getattr(w_datetime, space.newtext("timezone")) |
| 58 |
w_d0 = space.call_function(w_delta) |
| 59 |
w_tzobj = space.call_function(w_timezone, w_d0) |
| 60 |
datetimeAPI.c_TimeZone_UTC = make_ref(space, w_tzobj) |
| 61 |
|
| 62 |
w_type = space.getattr(w_datetime, space.newtext("timezone")) |
| 63 |
w_utc = space.getattr(w_type, space.newtext("utc")) |
| 64 |
datetimeAPI.c_TimeZone_UTC = make_ref(space, w_utc) |
| 65 |
|
| 66 |
datetimeAPI.c_Date_FromDate = llhelper( |
| 67 |
_PyDate_FromDate.api_func.functype, |
| 68 |
_PyDate_FromDate.api_func.get_wrapper(space)) |
| 69 |
datetimeAPI.c_Time_FromTime = llhelper( |
| 70 |
_PyTime_FromTime.api_func.functype, |
| 71 |
_PyTime_FromTime.api_func.get_wrapper(space)) |
| 72 |
datetimeAPI.c_Time_FromTimeAndFold = llhelper( |
| 73 |
_PyTime_FromTimeAndFold.api_func.functype, |
| 74 |
_PyTime_FromTimeAndFold.api_func.get_wrapper(space)) |
| 75 |
datetimeAPI.c_DateTime_FromDateAndTime = llhelper( |
| 76 |
_PyDateTime_FromDateAndTime.api_func.functype, |
| 77 |
_PyDateTime_FromDateAndTime.api_func.get_wrapper(space)) |
| 78 |
datetimeAPI.c_DateTime_FromDateAndTimeAndFold = llhelper( |
| 79 |
_PyDateTime_FromDateAndTimeAndFold.api_func.functype, |
| 80 |
_PyDateTime_FromDateAndTimeAndFold.api_func.get_wrapper(space)) |
| 81 |
datetimeAPI.c_Delta_FromDelta = llhelper( |
| 82 |
_PyDelta_FromDelta.api_func.functype, |
| 83 |
_PyDelta_FromDelta.api_func.get_wrapper(space)) |
| 84 |
|
| 85 |
datetimeAPI.c_DateTime_FromTimestamp = llhelper( |
| 86 |
_PyDateTime_FromTimestamp.api_func.functype, |
| 87 |
_PyDateTime_FromTimestamp.api_func.get_wrapper(space)) |
| 88 |
|
| 89 |
datetimeAPI.c_Date_FromTimestamp = llhelper( |
| 90 |
_PyDate_FromTimestamp.api_func.functype, |
| 91 |
_PyDate_FromTimestamp.api_func.get_wrapper(space)) |
| 92 |
|
| 93 |
datetimeAPI.c_TimeZone_FromTimeZone = llhelper( |
| 94 |
_PyTimeZone_FromTimeZone.api_func.functype, |
| 95 |
_PyTimeZone_FromTimeZone.api_func.get_wrapper(space)) |
| 96 |
|
| 97 |
state.datetimeAPI.append(datetimeAPI) |
| 98 |
return state.datetimeAPI[0] |
| 99 |
|
| 100 |
PyDateTime_Time = cts.gettype('PyDateTime_Time*') |
| 101 |
PyDateTime_DateTime = cts.gettype('PyDateTime_DateTime*') |
| 102 |
PyDateTime_Date = cts.gettype('PyDateTime_Date*') |
| 103 |
PyDateTime_Delta = cts.gettype('PyDateTime_Delta*') |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def make_check_function(func_name, type_name): |
| 108 |
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 109 |
@func_renamer(func_name) |
| 110 |
def check(space, w_obj): |
| 111 |
try: |
| 112 |
return space.is_true( |
| 113 |
space.appexec([w_obj], """(obj): |
| 114 |
from datetime import %s as datatype |
| 115 |
return isinstance(obj, datatype) |
| 116 |
""" % (type_name,))) |
| 117 |
except OperationError: |
| 118 |
return 0 |
| 119 |
|
| 120 |
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 121 |
@func_renamer(func_name + "Exact") |
| 122 |
def check_exact(space, w_obj): |
| 123 |
try: |
| 124 |
return space.is_true( |
| 125 |
space.appexec([w_obj], """(obj): |
| 126 |
from datetime import %s as datatype |
| 127 |
return type(obj) is datatype |
| 128 |
""" % (type_name,))) |
| 129 |
except OperationError: |
| 130 |
return 0 |
| 131 |
return check, check_exact |
| 132 |
|
| 133 |
PyDateTime_Check, PyDateTime_CheckExact = make_check_function( |
| 134 |
"PyDateTime_Check", "datetime") |
| 135 |
PyDate_Check, PyDate_CheckExact = make_check_function("PyDate_Check", "date") |
| 136 |
PyTime_Check, PyTime_CheckExact = make_check_function("PyTime_Check", "time") |
| 137 |
PyDelta_Check, PyDelta_CheckExact = make_check_function( |
| 138 |
"PyDelta_Check", "timedelta") |
| 139 |
PyTZInfo_Check, PyTZInfo_CheckExact = make_check_function( |
| 140 |
"PyTZInfo_Check", "tzinfo") |
| 141 |
|
| 142 |
@bootstrap_function |
| 143 |
def init_datetime(space): |
| 144 |
|
| 145 |
make_typedescr(W_DateTime_Time.typedef, |
| 146 |
basestruct=PyDateTime_Time.TO, |
| 147 |
attach=type_attach, |
| 148 |
dealloc=type_dealloc, |
| 149 |
) |
| 150 |
|
| 151 |
make_typedescr(W_DateTime_Date.typedef, |
| 152 |
basestruct=PyDateTime_DateTime.TO, |
| 153 |
attach=type_attach, |
| 154 |
dealloc=type_dealloc, |
| 155 |
) |
| 156 |
|
| 157 |
make_typedescr(W_DateTime_Delta.typedef, |
| 158 |
basestruct=PyDateTime_Delta.TO, |
| 159 |
attach=timedeltatype_attach, |
| 160 |
) |
| 161 |
|
| 162 |
def type_attach(space, py_obj, w_obj, w_userdata=None): |
| 163 |
'''Fills a newly allocated py_obj from the w_obj |
| 164 |
If it is a datetime.time or datetime.datetime, it may have tzinfo |
| 165 |
''' |
| 166 |
state = space.fromcache(State) |
| 167 |
if len(state.datetimeAPI) ==0: |
| 168 |
|
| 169 |
_PyDateTime_Import(space) |
| 170 |
if state.datetimeAPI[0].c_TimeType == py_obj.c_ob_type: |
| 171 |
py_datetime = rffi.cast(PyDateTime_Time, py_obj) |
| 172 |
w_tzinfo = space.getattr(w_obj, space.newtext('tzinfo')) |
| 173 |
if space.is_none(w_tzinfo): |
| 174 |
py_datetime.c_hastzinfo = cts.cast('unsigned char', 0) |
| 175 |
py_datetime.c_tzinfo = lltype.nullptr(PyObject.TO) |
| 176 |
else: |
| 177 |
py_datetime.c_hastzinfo = cts.cast('unsigned char', 1) |
| 178 |
py_datetime.c_tzinfo = make_ref(space, w_tzinfo) |
| 179 |
elif state.datetimeAPI[0].c_DateTimeType == py_obj.c_ob_type: |
| 180 |
|
| 181 |
py_datetime = rffi.cast(PyDateTime_DateTime, py_obj) |
| 182 |
w_tzinfo = space.getattr(w_obj, space.newtext('tzinfo')) |
| 183 |
if space.is_none(w_tzinfo): |
| 184 |
py_datetime.c_hastzinfo = cts.cast('unsigned char', 0) |
| 185 |
py_datetime.c_tzinfo = lltype.nullptr(PyObject.TO) |
| 186 |
else: |
| 187 |
py_datetime.c_hastzinfo = cts.cast('unsigned char', 1) |
| 188 |
py_datetime.c_tzinfo = make_ref(space, w_tzinfo) |
| 189 |
|
| 190 |
@slot_function([PyObject], lltype.Void) |
| 191 |
def type_dealloc(space, py_obj): |
| 192 |
from pypy.module.cpyext.object import _dealloc |
| 193 |
state = space.fromcache(State) |
| 194 |
|
| 195 |
assert len(state.datetimeAPI) > 0 |
| 196 |
if state.datetimeAPI[0].c_TimeType == py_obj.c_ob_type: |
| 197 |
py_datetime = rffi.cast(PyDateTime_Time, py_obj) |
| 198 |
if (widen(py_datetime.c_hastzinfo) != 0): |
| 199 |
decref(space, py_datetime.c_tzinfo) |
| 200 |
elif state.datetimeAPI[0].c_DateTimeType == py_obj.c_ob_type: |
| 201 |
py_datetime = rffi.cast(PyDateTime_DateTime, py_obj) |
| 202 |
if (widen(py_datetime.c_hastzinfo) != 0): |
| 203 |
decref(space, py_datetime.c_tzinfo) |
| 204 |
_dealloc(space, py_obj) |
| 205 |
|
| 206 |
def timedeltatype_attach(space, py_obj, w_obj, w_userdata=None): |
| 207 |
"Fills a newly allocated py_obj from the w_obj" |
| 208 |
py_delta = rffi.cast(PyDateTime_Delta, py_obj) |
| 209 |
days = space.int_w(space.getattr(w_obj, space.newtext('_days'))) |
| 210 |
py_delta.c_days = cts.cast('int', days) |
| 211 |
seconds = space.int_w(space.getattr(w_obj, space.newtext('_seconds'))) |
| 212 |
py_delta.c_seconds = cts.cast('int', seconds) |
| 213 |
microseconds = space.int_w(space.getattr(w_obj, space.newtext('_microseconds'))) |
| 214 |
py_delta.c_microseconds = cts.cast('int', microseconds) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, PyTypeObjectPtr], |
| 219 |
PyObject) |
| 220 |
def _PyDate_FromDate(space, year, month, day, w_type): |
| 221 |
"""Return a datetime.date object with the specified year, month and day. |
| 222 |
""" |
| 223 |
year = rffi.cast(lltype.Signed, year) |
| 224 |
month = rffi.cast(lltype.Signed, month) |
| 225 |
day = rffi.cast(lltype.Signed, day) |
| 226 |
return space.call_function( |
| 227 |
w_type, |
| 228 |
space.newint(year), space.newint(month), space.newint(day)) |
| 229 |
|
| 230 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 231 |
PyObject, PyTypeObjectPtr], PyObject) |
| 232 |
def _PyTime_FromTime(space, hour, minute, second, usecond, w_tzinfo, w_type): |
| 233 |
"""Return a ``datetime.time`` object with the specified hour, minute, second and |
| 234 |
microsecond.""" |
| 235 |
hour = rffi.cast(lltype.Signed, hour) |
| 236 |
minute = rffi.cast(lltype.Signed, minute) |
| 237 |
second = rffi.cast(lltype.Signed, second) |
| 238 |
usecond = rffi.cast(lltype.Signed, usecond) |
| 239 |
return space.call_function( |
| 240 |
w_type, |
| 241 |
space.newint(hour), space.newint(minute), space.newint(second), |
| 242 |
space.newint(usecond), w_tzinfo) |
| 243 |
|
| 244 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 245 |
rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 246 |
PyObject, PyTypeObjectPtr], PyObject) |
| 247 |
def _PyDateTime_FromDateAndTime(space, year, month, day, |
| 248 |
hour, minute, second, usecond, |
| 249 |
w_tzinfo, w_type): |
| 250 |
"""Return a datetime.datetime object with the specified year, month, day, hour, |
| 251 |
minute, second and microsecond. |
| 252 |
""" |
| 253 |
year = rffi.cast(lltype.Signed, year) |
| 254 |
month = rffi.cast(lltype.Signed, month) |
| 255 |
day = rffi.cast(lltype.Signed, day) |
| 256 |
hour = rffi.cast(lltype.Signed, hour) |
| 257 |
minute = rffi.cast(lltype.Signed, minute) |
| 258 |
second = rffi.cast(lltype.Signed, second) |
| 259 |
usecond = rffi.cast(lltype.Signed, usecond) |
| 260 |
return space.call_function( |
| 261 |
w_type, |
| 262 |
space.newint(year), space.newint(month), space.newint(day), |
| 263 |
space.newint(hour), space.newint(minute), space.newint(second), |
| 264 |
space.newint(usecond), w_tzinfo) |
| 265 |
|
| 266 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 267 |
rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 268 |
PyObject, rffi.INT_real, PyTypeObjectPtr], PyObject) |
| 269 |
def _PyDateTime_FromDateAndTimeAndFold(space, year, month, day, |
| 270 |
hour, minute, second, usecond, |
| 271 |
w_tzinfo, fold, w_type): |
| 272 |
"""Return a datetime.datetime object with the specified year, month, day, hour, |
| 273 |
minute, second, microsecond and fold. |
| 274 |
""" |
| 275 |
year = rffi.cast(lltype.Signed, year) |
| 276 |
month = rffi.cast(lltype.Signed, month) |
| 277 |
day = rffi.cast(lltype.Signed, day) |
| 278 |
hour = rffi.cast(lltype.Signed, hour) |
| 279 |
minute = rffi.cast(lltype.Signed, minute) |
| 280 |
second = rffi.cast(lltype.Signed, second) |
| 281 |
usecond = rffi.cast(lltype.Signed, usecond) |
| 282 |
fold = rffi.cast(lltype.Signed, fold) |
| 283 |
args = Arguments(space, |
| 284 |
[space.newint(year), space.newint(month), space.newint(day), |
| 285 |
space.newint(hour), space.newint(minute), space.newint(second), |
| 286 |
space.newint(usecond), w_tzinfo], |
| 287 |
keyword_names_w=[space.newtext('fold')], |
| 288 |
keywords_w = [space.newint(fold)], |
| 289 |
) |
| 290 |
return space.call_args(w_type, args) |
| 291 |
|
| 292 |
|
| 293 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 294 |
PyObject, rffi.INT_real, PyTypeObjectPtr], PyObject) |
| 295 |
def _PyTime_FromTimeAndFold(space, hour, minute, second, usecond, |
| 296 |
w_tzinfo, fold, w_type): |
| 297 |
"""Return a datetime.time object with the specified hour, |
| 298 |
minute, second, microsecond, and fold. |
| 299 |
""" |
| 300 |
hour = rffi.cast(lltype.Signed, hour) |
| 301 |
minute = rffi.cast(lltype.Signed, minute) |
| 302 |
second = rffi.cast(lltype.Signed, second) |
| 303 |
usecond = rffi.cast(lltype.Signed, usecond) |
| 304 |
fold = rffi.cast(lltype.Signed, fold) |
| 305 |
args = Arguments(space, |
| 306 |
[space.newint(hour), space.newint(minute), space.newint(second), |
| 307 |
space.newint(usecond), w_tzinfo], |
| 308 |
keyword_names_w=[space.newtext('fold')], |
| 309 |
keywords_w = [space.newint(fold)], |
| 310 |
) |
| 311 |
|
| 312 |
return space.call_args(w_type, args) |
| 313 |
|
| 314 |
@cpython_api([PyObject], PyObject) |
| 315 |
def PyDateTime_FromTimestamp(space, w_args): |
| 316 |
"""Create and return a new datetime.datetime object given an argument tuple |
| 317 |
suitable for passing to datetime.datetime.fromtimestamp(). |
| 318 |
""" |
| 319 |
w_datetime = PyImport_Import(space, space.newtext("datetime")) |
| 320 |
w_type = space.getattr(w_datetime, space.newtext("datetime")) |
| 321 |
return _PyDateTime_FromTimestamp(space, w_type, w_args, None) |
| 322 |
|
| 323 |
@cpython_api([PyObject, PyObject, PyObject], PyObject) |
| 324 |
def _PyDateTime_FromTimestamp(space, w_type, w_args, w_kwds): |
| 325 |
"""Implementation of datetime.fromtimestamp that matches the signature for |
| 326 |
PyDateTimeCAPI.DateTime_FromTimestamp |
| 327 |
""" |
| 328 |
w_method = space.getattr(w_type, space.newtext("fromtimestamp")) |
| 329 |
|
| 330 |
return space.call(w_method, w_args, w_kwds=w_kwds) |
| 331 |
|
| 332 |
@cpython_api([PyObject], PyObject) |
| 333 |
def PyDate_FromTimestamp(space, w_args): |
| 334 |
"""Create and return a new datetime.date object given an argument tuple |
| 335 |
suitable for passing to datetime.date.fromtimestamp(). |
| 336 |
""" |
| 337 |
w_datetime = PyImport_Import(space, space.newtext("datetime")) |
| 338 |
w_type = space.getattr(w_datetime, space.newtext("date")) |
| 339 |
return _PyDate_FromTimestamp(space, w_type, w_args) |
| 340 |
|
| 341 |
@cpython_api([PyObject, PyObject], PyObject) |
| 342 |
def _PyDate_FromTimestamp(space, w_type, w_args): |
| 343 |
"""Implementation of date.fromtimestamp that matches the signature for |
| 344 |
PyDateTimeCAPI.Date_FromTimestamp""" |
| 345 |
w_method = space.getattr(w_type, space.newtext("fromtimestamp")) |
| 346 |
return space.call(w_method, w_args) |
| 347 |
|
| 348 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 349 |
PyTypeObjectPtr], |
| 350 |
PyObject) |
| 351 |
def _PyDelta_FromDelta(space, days, seconds, useconds, normalize, w_type): |
| 352 |
"""Return a datetime.timedelta object representing the given number of days, |
| 353 |
seconds and microseconds. Normalization is performed so that the resulting |
| 354 |
number of microseconds and seconds lie in the ranges documented for |
| 355 |
datetime.timedelta objects. |
| 356 |
""" |
| 357 |
days = rffi.cast(lltype.Signed, days) |
| 358 |
seconds = rffi.cast(lltype.Signed, seconds) |
| 359 |
useconds = rffi.cast(lltype.Signed, useconds) |
| 360 |
return space.call_function( |
| 361 |
w_type, |
| 362 |
space.newint(days), space.newint(seconds), space.newint(useconds)) |
| 363 |
|
| 364 |
|
| 365 |
@cpython_api([PyObject, PyObject], PyObject) |
| 366 |
def _PyTimeZone_FromTimeZone(space, w_offset, w_name): |
| 367 |
"""Return a datetime.timezone object with an unnamed fixed offset |
| 368 |
represented by the offset argument. |
| 369 |
""" |
| 370 |
w_datetime = PyImport_Import(space, space.newtext("datetime")) |
| 371 |
w_type = space.getattr(w_datetime, space.newtext("timezone")) |
| 372 |
|
| 373 |
if w_name is not None: |
| 374 |
return space.call_function(w_type, w_offset, w_name) |
| 375 |
|
| 376 |
return space.call_function(w_type, w_offset) |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 381 |
def PyDateTime_GET_YEAR(space, w_obj): |
| 382 |
"""Return the year, as a positive int. |
| 383 |
""" |
| 384 |
return space.int_w(space.getattr(w_obj, space.newtext("year"))) |
| 385 |
|
| 386 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 387 |
def PyDateTime_GET_MONTH(space, w_obj): |
| 388 |
"""Return the month, as an int from 1 through 12. |
| 389 |
""" |
| 390 |
return space.int_w(space.getattr(w_obj, space.newtext("month"))) |
| 391 |
|
| 392 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 393 |
def PyDateTime_GET_DAY(space, w_obj): |
| 394 |
"""Return the day, as an int from 1 through 31. |
| 395 |
""" |
| 396 |
return space.int_w(space.getattr(w_obj, space.newtext("day"))) |
| 397 |
|
| 398 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 399 |
def PyDateTime_GET_FOLD(space, w_obj): |
| 400 |
"""Return the fold, as an int 0 or 1 |
| 401 |
""" |
| 402 |
return space.int_w(space.getattr(w_obj, space.newtext("fold"))) |
| 403 |
|
| 404 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 405 |
def PyDateTime_DATE_GET_HOUR(space, w_obj): |
| 406 |
"""Return the hour, as an int from 0 through 23. |
| 407 |
""" |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
try: |
| 413 |
return space.int_w(space.getattr(w_obj, space.newtext("hour"))) |
| 414 |
except OperationError: |
| 415 |
return 0 |
| 416 |
|
| 417 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 418 |
def PyDateTime_DATE_GET_MINUTE(space, w_obj): |
| 419 |
"""Return the minute, as an int from 0 through 59. |
| 420 |
""" |
| 421 |
try: |
| 422 |
return space.int_w(space.getattr(w_obj, space.newtext("minute"))) |
| 423 |
except OperationError: |
| 424 |
return 0 |
| 425 |
|
| 426 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 427 |
def PyDateTime_DATE_GET_SECOND(space, w_obj): |
| 428 |
"""Return the second, as an int from 0 through 59. |
| 429 |
""" |
| 430 |
try: |
| 431 |
return space.int_w(space.getattr(w_obj, space.newtext("second"))) |
| 432 |
except OperationError: |
| 433 |
return 0 |
| 434 |
|
| 435 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 436 |
def PyDateTime_DATE_GET_MICROSECOND(space, w_obj): |
| 437 |
"""Return the microsecond, as an int from 0 through 999999. |
| 438 |
""" |
| 439 |
try: |
| 440 |
return space.int_w(space.getattr(w_obj, space.newtext("microsecond"))) |
| 441 |
except OperationError: |
| 442 |
return 0 |
| 443 |
|
| 444 |
@cpython_api([rffi.VOIDP], PyObject, error=CANNOT_FAIL) |
| 445 |
def PyDateTime_DATE_GET_TZINFO(space, w_obj): |
| 446 |
"""Return the tzinfo, a PyObject |
| 447 |
""" |
| 448 |
try: |
| 449 |
ret = space.getattr(w_obj, space.newtext("tzinfo")) |
| 450 |
if not ret: |
| 451 |
return space.w_None |
| 452 |
return ret |
| 453 |
except OperationError: |
| 454 |
return space.w_None |
| 455 |
|
| 456 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 457 |
def PyDateTime_TIME_GET_HOUR(space, w_obj): |
| 458 |
"""Return the hour, as an int from 0 through 23. |
| 459 |
""" |
| 460 |
return space.int_w(space.getattr(w_obj, space.newtext("hour"))) |
| 461 |
|
| 462 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 463 |
def PyDateTime_TIME_GET_MINUTE(space, w_obj): |
| 464 |
"""Return the minute, as an int from 0 through 59. |
| 465 |
""" |
| 466 |
return space.int_w(space.getattr(w_obj, space.newtext("minute"))) |
| 467 |
|
| 468 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 469 |
def PyDateTime_TIME_GET_SECOND(space, w_obj): |
| 470 |
"""Return the second, as an int from 0 through 59. |
| 471 |
""" |
| 472 |
return space.int_w(space.getattr(w_obj, space.newtext("second"))) |
| 473 |
|
| 474 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 475 |
def PyDateTime_TIME_GET_MICROSECOND(space, w_obj): |
| 476 |
"""Return the microsecond, as an int from 0 through 999999. |
| 477 |
""" |
| 478 |
return space.int_w(space.getattr(w_obj, space.newtext("microsecond"))) |
| 479 |
|
| 480 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 481 |
def PyDateTime_TIME_GET_FOLD(space, w_obj): |
| 482 |
"""Return the fold, either 0 or 1 |
| 483 |
""" |
| 484 |
return space.int_w(space.getattr(w_obj, space.newtext("fold"))) |
| 485 |
|
| 486 |
@cpython_api([rffi.VOIDP], PyObject, error=CANNOT_FAIL) |
| 487 |
def PyDateTime_TIME_GET_TZINFO(space, w_obj): |
| 488 |
"""Return the tzinfo, a PyObject |
| 489 |
""" |
| 490 |
try: |
| 491 |
ret = space.getattr(w_obj, space.newtext("tzinfo")) |
| 492 |
if not ret: |
| 493 |
return space.w_None |
| 494 |
return ret |
| 495 |
except OperationError: |
| 496 |
return space.w_None |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 503 |
def PyDateTime_DELTA_GET_DAYS(space, w_obj): |
| 504 |
return space.int_w(space.getattr(w_obj, space.newtext("days"))) |
| 505 |
|
| 506 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 507 |
def PyDateTime_DELTA_GET_SECONDS(space, w_obj): |
| 508 |
return space.int_w(space.getattr(w_obj, space.newtext("seconds"))) |
| 509 |
|
| 510 |
@cpython_api([rffi.VOIDP], rffi.INT_real, error=CANNOT_FAIL) |
| 511 |
def PyDateTime_DELTA_GET_MICROSECONDS(space, w_obj): |
| 512 |
return space.int_w(space.getattr(w_obj, space.newtext("microseconds"))) |