1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
from rpython.rtyper.tool import rffi_platform as platform
from rpython.rtyper.lltypesystem import rffi
from pypy.interpreter.error import (OperationError, oefmt,
strerror as _strerror, exception_from_saved_errno)
from pypy.interpreter.gateway import unwrap_spec
from pypy.interpreter.timeutils import (
SECS_TO_NS, MS_TO_NS, US_TO_NS, monotonic as _monotonic, timestamp_w)
from pypy.interpreter.unicodehelper import decode_utf8, encode_utf8
from rpython.rtyper.lltypesystem import lltype
from rpython.rlib.rarithmetic import (
intmask, r_ulonglong, r_longfloat, widen, ovfcheck, ovfcheck_float_to_int)
from rpython.rlib.rtime import (GETTIMEOFDAY_NO_TZ, TIMEVAL,
HAVE_GETTIMEOFDAY, HAVE_FTIME)
from rpython.rlib import rposix, rtime
from rpython.translator.tool.cbuild import ExternalCompilationInfo
import math
import os
import sys
import time as pytime
if HAVE_FTIME:
from rpython.rlib.rtime import TIMEB, c_ftime
_POSIX = os.name == "posix"
_WIN = os.name == "nt"
_MACOSX = sys.platform == "darwin"
_CYGWIN = sys.platform == "cygwin"
_time_zones = []
if _CYGWIN:
_time_zones = ["GMT-12", "GMT-11", "GMT-10", "GMT-9", "GMT-8", "GMT-7",
"GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT-2", "GMT-1",
"GMT", "GMT+1", "GMT+2", "GMT+3", "GMT+4", "GMT+5",
"GMT+6", "GMT+7", "GMT+8", "GMT+9", "GMT+10", "GMT+11",
"GMT+12", "GMT+13", "GMT+14"]
if _WIN:
# Interruptible sleeps on Windows:
# We install a specific Console Ctrl Handler which sets an 'event'.
# time.sleep() will actually call WaitForSingleObject with the desired
# timeout. On Ctrl-C, the signal handler is called, the event is set,
# and the wait function exits.
from rpython.rlib import rwin32
from pypy.interpreter.error import wrap_oserror
from rpython.rlib import rthread as thread
eci = ExternalCompilationInfo(
includes = ['windows.h'],
post_include_bits = [
"RPY_EXTERN\n"
"BOOL pypy_timemodule_setCtrlHandler(HANDLE event);\n"
"RPY_EXTERN ULONGLONG pypy_GetTickCount64(FARPROC address);"
],
separate_module_sources=['''
static HANDLE interrupt_event;
static BOOL WINAPI CtrlHandlerRoutine(
DWORD dwCtrlType)
{
SetEvent(interrupt_event);
/* allow other default handlers to be called.
* Default Python handler will setup the
* KeyboardInterrupt exception.
*/
return 0;
}
BOOL pypy_timemodule_setCtrlHandler(HANDLE event)
{
interrupt_event = event;
return SetConsoleCtrlHandler(CtrlHandlerRoutine, TRUE);
}
ULONGLONG pypy_GetTickCount64(FARPROC address) {
ULONGLONG (WINAPI *func)();
*(FARPROC*)&func = address;
return func();
}
'''],
)
_setCtrlHandlerRoutine = rffi.llexternal(
'pypy_timemodule_setCtrlHandler',
[rwin32.HANDLE], rwin32.BOOL,
compilation_info=eci,
save_err=rffi.RFFI_SAVE_LASTERROR)
pypy_GetTickCount64 = rffi.llexternal(
'pypy_GetTickCount64',
[rffi.VOIDP],
rffi.ULONGLONG, compilation_info=eci)
class GlobalState:
def __init__(self):
self.init()
def init(self):
self.interrupt_event = rwin32.NULL_HANDLE
def startup(self, space):
# Initialize the event handle used to signal Ctrl-C
try:
globalState.interrupt_event = rwin32.CreateEvent(
rffi.NULL, True, False, rffi.NULL)
except WindowsError as e:
raise wrap_oserror(space, e)
if not _setCtrlHandlerRoutine(globalState.interrupt_event):
raise wrap_oserror(space,
rwin32.lastSavedWindowsError("SetConsoleCtrlHandler"))
globalState = GlobalState()
class State:
def __init__(self, space):
self.main_thread = 0
def _cleanup_(self):
self.main_thread = 0
globalState.init()
def startup(self, space):
self.main_thread = thread.get_ident()
globalState.startup(space)
def get_interrupt_event(self):
return globalState.interrupt_event
class TimeState(object):
GetTickCount64_handle = lltype.nullptr(rffi.VOIDP.TO)
def __init__(self):
self.n_overflow = 0
self.last_ticks = 0
self.divisor = 0.0
self.counter_start = 0
def check_GetTickCount64(self, *args):
if (self.GetTickCount64_handle !=
lltype.nullptr(rffi.VOIDP.TO)):
return True
from rpython.rlib.rdynload import GetModuleHandle, dlsym
hKernel32 = GetModuleHandle("KERNEL32")
try:
GetTickCount64_handle = dlsym(hKernel32, 'GetTickCount64')
except KeyError:
return False
self.GetTickCount64_handle = GetTickCount64_handle
return True
def GetTickCount64(self, *args):
assert (self.GetTickCount64_handle !=
lltype.nullptr(rffi.VOIDP.TO))
return pypy_GetTickCount64(
self.GetTickCount64_handle, *args)
time_state = TimeState()
_includes = ["time.h"]
if _POSIX:
_includes.append('sys/time.h')
if _MACOSX:
_includes.append('mach/mach_time.h')
class CConfig:
_compilation_info_ = ExternalCompilationInfo(
includes=_includes,
libraries=rtime.libraries
)
CLOCKS_PER_SEC = platform.ConstantInteger("CLOCKS_PER_SEC")
has_gettimeofday = platform.Has('gettimeofday')
HAS_TM_ZONE = False
if _POSIX:
calling_conv = 'c'
CConfig.timeval = platform.Struct("struct timeval",
[("tv_sec", rffi.INT),
("tv_usec", rffi.INT)])
if _CYGWIN:
CConfig.tm = platform.Struct("struct tm", [("tm_sec", rffi.INT),
("tm_min", rffi.INT), ("tm_hour", rffi.INT), ("tm_mday", rffi.INT),
("tm_mon", rffi.INT), ("tm_year", rffi.INT), ("tm_wday", rffi.INT),
("tm_yday", rffi.INT), ("tm_isdst", rffi.INT)])
else:
CConfig.tm = platform.Struct("struct tm", [("tm_sec", rffi.INT),
("tm_min", rffi.INT), ("tm_hour", rffi.INT), ("tm_mday", rffi.INT),
("tm_mon", rffi.INT), ("tm_year", rffi.INT), ("tm_wday", rffi.INT),
("tm_yday", rffi.INT), ("tm_isdst", rffi.INT), ("tm_gmtoff", rffi.LONG),
("tm_zone", rffi.CCHARP)])
HAS_TM_ZONE = True
elif _WIN:
calling_conv = 'win'
CConfig.tm = platform.Struct("struct tm", [("tm_sec", rffi.INT),
("tm_min", rffi.INT), ("tm_hour", rffi.INT), ("tm_mday", rffi.INT),
("tm_mon", rffi.INT), ("tm_year", rffi.INT), ("tm_wday", rffi.INT),
("tm_yday", rffi.INT), ("tm_isdst", rffi.INT)])
if _MACOSX:
CConfig.TIMEBASE_INFO = platform.Struct("struct mach_timebase_info", [
("numer", rffi.UINT),
("denom", rffi.UINT),
])
# XXX: optionally support the 2 additional tz fields
_STRUCT_TM_ITEMS = 9
if HAS_TM_ZONE:
_STRUCT_TM_ITEMS = 11
class cConfig:
pass
for k, v in platform.configure(CConfig).items():
setattr(cConfig, k, v)
cConfig.tm.__name__ = "_tm"
def external(name, args, result, eci=CConfig._compilation_info_, **kwds):
if _WIN and rffi.sizeof(rffi.TIME_T) == 8:
# Recent Microsoft compilers use 64bit time_t and
# the corresponding functions are named differently
if (rffi.TIME_T in args or rffi.TIME_TP in args
or result in (rffi.TIME_T, rffi.TIME_TP)):
name = '_' + name + '64'
_calling_conv = kwds.pop('calling_conv', calling_conv)
return rffi.llexternal(name, args, result,
compilation_info=eci,
calling_conv=_calling_conv,
releasegil=False,
**kwds)
if _POSIX:
cConfig.timeval.__name__ = "_timeval"
timeval = cConfig.timeval
CLOCKS_PER_SEC = cConfig.CLOCKS_PER_SEC
HAS_CLOCK_GETTIME = rtime.HAS_CLOCK_GETTIME
HAS_CLOCK_HIGHRES = rtime.CLOCK_HIGHRES is not None
HAS_CLOCK_MONOTONIC = rtime.CLOCK_MONOTONIC is not None
HAS_MONOTONIC = (_WIN or _MACOSX or
(HAS_CLOCK_GETTIME and (HAS_CLOCK_HIGHRES or HAS_CLOCK_MONOTONIC)))
tm = cConfig.tm
glob_buf = lltype.malloc(tm, flavor='raw', zero=True, immortal=True)
if _WIN:
_GetSystemTimeAsFileTime = rwin32.winexternal('GetSystemTimeAsFileTime',
[lltype.Ptr(rwin32.FILETIME)],
lltype.Void)
LPDWORD = rwin32.LPDWORD
_GetSystemTimeAdjustment = rwin32.winexternal(
'GetSystemTimeAdjustment',
[LPDWORD, LPDWORD, rwin32.LPBOOL],
rffi.INT)
def gettimeofday(space, w_info=None):
with lltype.scoped_alloc(rwin32.FILETIME) as system_time:
_GetSystemTimeAsFileTime(system_time)
quad_part = (system_time.c_dwLowDateTime |
(r_ulonglong(system_time.c_dwHighDateTime) << 32))
# 11,644,473,600,000,000: number of microseconds between
# the 1st january 1601 and the 1st january 1970 (369 years + 80 leap
# days).
# We can't use that big number when translating for
# 32-bit system (which windows always is currently)
# XXX: Need to come up with a better solution
offset = (r_ulonglong(16384) * r_ulonglong(27) * r_ulonglong(390625)
* r_ulonglong(79) * r_ulonglong(853))
microseconds = quad_part / 10 - offset
tv_sec = microseconds / 1000000
tv_usec = microseconds % 1000000
if w_info:
with lltype.scoped_alloc(LPDWORD.TO, 1) as time_adjustment, \
lltype.scoped_alloc(LPDWORD.TO, 1) as time_increment, \
lltype.scoped_alloc(rwin32.LPBOOL.TO, 1) as is_time_adjustment_disabled:
_GetSystemTimeAdjustment(time_adjustment, time_increment,
is_time_adjustment_disabled)
_setinfo(space, w_info, "GetSystemTimeAsFileTime()",
time_increment[0] * 1e-7, False, True)
return space.newfloat(tv_sec + tv_usec * 1e-6)
else:
if HAVE_GETTIMEOFDAY:
if GETTIMEOFDAY_NO_TZ:
c_gettimeofday = external('gettimeofday',
[lltype.Ptr(TIMEVAL)], rffi.INT)
else:
c_gettimeofday = external('gettimeofday',
[lltype.Ptr(TIMEVAL), rffi.VOIDP], rffi.INT)
def gettimeofday(space, w_info=None):
if HAVE_GETTIMEOFDAY:
with lltype.scoped_alloc(TIMEVAL) as timeval:
if GETTIMEOFDAY_NO_TZ:
errcode = c_gettimeofday(timeval)
else:
void = lltype.nullptr(rffi.VOIDP.TO)
errcode = c_gettimeofday(timeval, void)
if rffi.cast(rffi.LONG, errcode) == 0:
if w_info is not None:
_setinfo(space, w_info, "gettimeofday()", 1e-6, False, True)
return space.newfloat(
widen(timeval.c_tv_sec) +
widen(timeval.c_tv_usec) * 1e-6)
if HAVE_FTIME:
with lltype.scoped_alloc(TIMEB) as t:
c_ftime(t)
result = (widen(t.c_time) +
widen(t.c_millitm) * 0.001)
if w_info is not None:
_setinfo(space, w_info, "ftime()", 1e-3,
False, True)
return space.newfloat(result)
else:
if w_info:
_setinfo(space, w_info, "time()", 1.0, False, True)
return space.newint(c_time(lltype.nullptr(rffi.TIME_TP.TO)))
TM_P = lltype.Ptr(tm)
c_time = external('time', [rffi.TIME_TP], rffi.TIME_T)
c_gmtime = external('gmtime', [rffi.TIME_TP], TM_P,
save_err=rffi.RFFI_SAVE_ERRNO)
c_mktime = external('mktime', [TM_P], rffi.TIME_T)
c_localtime = external('localtime', [rffi.TIME_TP], TM_P,
save_err=rffi.RFFI_SAVE_ERRNO)
if HAS_CLOCK_GETTIME:
from rpython.rlib.rtime import TIMESPEC, c_clock_gettime
from rpython.rlib.rtime import c_clock_settime, c_clock_getres
if _POSIX:
c_tzset = external('tzset', [], lltype.Void)
if _WIN:
win_eci = ExternalCompilationInfo(
includes = ["time.h"],
post_include_bits = ["RPY_EXTERN "
"long pypy_get_timezone();\n"
"RPY_EXTERN "
"int pypy_get_daylight();\n"
"RPY_EXTERN "
"int pypy_get_tzname(size_t, int, char*);\n"
"RPY_EXTERN "
"void pypy__tzset();"],
separate_module_sources = ["""
long pypy_get_timezone() {
long timezone;
_get_timezone(&timezone);
return timezone;
};
int pypy_get_daylight() {
int daylight;
_get_daylight(&daylight);
return daylight;
};
int pypy_get_tzname(size_t len, int index, char * tzname) {
size_t s;
errno_t ret = _get_tzname(&s, tzname, len, index);
return (int)s;
};
void pypy__tzset() { _tzset(); }
"""])
# Ensure sure that we use _tzset() and timezone from the same C Runtime.
c_tzset = external('pypy__tzset', [], lltype.Void, win_eci)
c_get_timezone = external('pypy_get_timezone', [], rffi.LONG, win_eci)
c_get_daylight = external('pypy_get_daylight', [], rffi.INT, win_eci)
c_get_tzname = external('pypy_get_tzname',
[rffi.SIZE_T, rffi.INT, rffi.CCHARP],
rffi.INT, win_eci, calling_conv='c')
c_strftime = external('strftime', [rffi.CCHARP, rffi.SIZE_T, rffi.CCHARP, TM_P],
rffi.SIZE_T)
def _init_timezone(space):
timezone = daylight = altzone = 0
tzname = ["", ""]
if _WIN:
c_tzset()
timezone = c_get_timezone()
altzone = timezone - 3600
daylight = c_get_daylight()
with rffi.scoped_alloc_buffer(100) as buf:
s = c_get_tzname(100, 0, buf.raw)
tzname[0] = buf.str(s)
s = c_get_tzname(100, 1, buf.raw)
tzname[1] = buf.str(s)
if _POSIX:
if _CYGWIN:
YEAR = (365 * 24 + 6) * 3600
# about January 11th
t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR + 10 * 24 * 3600)
# we cannot have reference to stack variable, put it on the heap
t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
t_ref[0] = rffi.cast(rffi.TIME_T, t)
p = c_localtime(t_ref)
q = c_gmtime(t_ref)
janzone = (p.c_tm_hour + 24 * p.c_tm_mday) - (q.c_tm_hour + 24 * q.c_tm_mday)
if janzone < -12:
janname = " "
elif janzone > 14:
janname = " "
else:
janname = _time_zones[janzone - 12]
janzone = janzone * 3600
# about July 11th
tt = t + YEAR / 2
t_ref[0] = rffi.cast(rffi.TIME_T, tt)
p = c_localtime(t_ref)
q = c_gmtime(t_ref)
julyzone = (p.c_tm_hour + 24 * p.c_tm_mday) - (q.c_tm_hour + 24 * q.c_tm_mday)
if julyzone < -12:
julyname = " "
elif julyzone > 14:
julyname = " "
else:
julyname = _time_zones[julyzone - 12]
julyzone = julyzone * 3600
lltype.free(t_ref, flavor='raw')
if janzone < julyzone:
# DST is reversed in the southern hemisphere
timezone = julyzone
altzone = janzone
daylight = int(janzone != julyzone)
tzname = [julyname, janname]
else:
timezone = janzone
altzone = julyzone
daylight = int(janzone != julyzone)
tzname = [janname, julyname]
else:
YEAR = (365 * 24 + 6) * 3600
t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR)
# we cannot have reference to stack variable, put it on the heap
t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
t_ref[0] = rffi.cast(rffi.TIME_T, t)
p = c_localtime(t_ref)
janzone = -p.c_tm_gmtoff
tm_zone = rffi.charp2str(p.c_tm_zone)
janname = [" ", tm_zone][bool(tm_zone)]
tt = t + YEAR / 2
t_ref[0] = rffi.cast(rffi.TIME_T, tt)
p = c_localtime(t_ref)
lltype.free(t_ref, flavor='raw')
tm_zone = rffi.charp2str(p.c_tm_zone)
julyzone = -p.c_tm_gmtoff
julyname = [" ", tm_zone][bool(tm_zone)]
if janzone < julyzone:
# DST is reversed in the southern hemisphere
timezone = julyzone
altzone = janzone
daylight = int(janzone != julyzone)
tzname = [julyname, janname]
else:
timezone = janzone
altzone = julyzone
daylight = int(janzone != julyzone)
tzname = [janname, julyname]
_set_module_object(space, "timezone", space.newint(timezone))
_set_module_object(space, 'daylight', space.newint(daylight))
tzname_w = [space.newunicode(tzname[0].decode('latin-1')),
space.newunicode(tzname[1].decode('latin-1'))]
_set_module_object(space, 'tzname', space.newtuple(tzname_w))
_set_module_object(space, 'altzone', space.newint(altzone))
def _get_error_msg():
errno = rposix.get_saved_errno()
return _strerror(errno)
from errno import EINTR
if not _WIN:
from rpython.rlib.rtime import c_select
from rpython.rlib import rwin32
def sleep(space, w_secs):
ns = timestamp_w(space, w_secs)
if not (ns >= 0):
raise oefmt(space.w_ValueError,
"sleep length must be non-negative")
end_time = _monotonic(space) + float(ns) / SECS_TO_NS
while True:
if _WIN:
# as decreed by Guido, only the main thread can be
# interrupted.
main_thread = space.fromcache(State).main_thread
interruptible = (main_thread == thread.get_ident())
millisecs = ns // MS_TO_NS
if millisecs == 0 or not interruptible:
rtime.sleep(float(ns) / SECS_TO_NS)
break
interrupt_event = space.fromcache(State).get_interrupt_event()
rwin32.ResetEvent(interrupt_event)
rc = rwin32.WaitForSingleObject(interrupt_event, millisecs)
if rc != rwin32.WAIT_OBJECT_0:
break
else:
void = lltype.nullptr(rffi.VOIDP.TO)
with lltype.scoped_alloc(TIMEVAL) as t:
seconds = ns // SECS_TO_NS
us = (ns % SECS_TO_NS) // US_TO_NS
rffi.setintfield(t, 'c_tv_sec', int(seconds))
rffi.setintfield(t, 'c_tv_usec', int(us))
res = rffi.cast(rffi.LONG, c_select(0, void, void, void, t))
if res == 0:
break # normal path
if rposix.get_saved_errno() != EINTR:
raise exception_from_saved_errno(space, space.w_OSError)
space.getexecutioncontext().checksignals()
secs = end_time - _monotonic(space) # retry
if secs <= 0:
break
def _get_module_object(space, obj_name):
w_module = space.getbuiltinmodule('time')
w_obj = space.getattr(w_module, space.newtext(obj_name))
return w_obj
def _set_module_object(space, obj_name, w_obj_value):
w_module = space.getbuiltinmodule('time')
space.setattr(w_module, space.newtext(obj_name), w_obj_value)
def _get_inttime(space, w_seconds):
# w_seconds can be a wrapped None (it will be automatically wrapped
# in the callers, so we never get a real None here).
if space.is_none(w_seconds):
seconds = pytime.time()
else:
seconds = space.float_w(w_seconds)
#
t = rffi.cast(rffi.TIME_T, seconds)
#
# Logic from CPython: How much info did we lose? We assume that
# time_t is an integral type. If we lost a second or more, the
# input doesn't fit in a time_t; call it an error.
diff = seconds - rffi.cast(lltype.Float, t)
if diff <= -1.0 or diff >= 1.0:
raise oefmt(space.w_OverflowError,
"timestamp out of range for platform time_t")
return t
def _tm_to_tuple(space, t):
time_tuple = [
space.newint(rffi.getintfield(t, 'c_tm_year') + 1900),
space.newint(rffi.getintfield(t, 'c_tm_mon') + 1), # want january == 1
space.newint(rffi.getintfield(t, 'c_tm_mday')),
space.newint(rffi.getintfield(t, 'c_tm_hour')),
space.newint(rffi.getintfield(t, 'c_tm_min')),
space.newint(rffi.getintfield(t, 'c_tm_sec')),
space.newint((rffi.getintfield(t, 'c_tm_wday') + 6) % 7), # want monday == 0
space.newint(rffi.getintfield(t, 'c_tm_yday') + 1), # want january, 1 == 1
space.newint(rffi.getintfield(t, 'c_tm_isdst'))]
if HAS_TM_ZONE:
# CPython calls PyUnicode_DecodeLocale here should we do the same?
tm_zone = decode_utf8(space, rffi.charp2str(t.c_tm_zone),
allow_surrogates=True)
extra = [space.newunicode(tm_zone),
space.newint(rffi.getintfield(t, 'c_tm_gmtoff'))]
w_time_tuple = space.newtuple(time_tuple + extra)
else:
w_time_tuple = space.newtuple(time_tuple)
w_struct_time = _get_module_object(space, 'struct_time')
w_obj = space.call_function(w_struct_time, w_time_tuple)
return w_obj
def _gettmarg(space, w_tup, allowNone=True):
if space.is_none(w_tup):
if not allowNone:
raise oefmt(space.w_TypeError, "tuple expected")
# default to the current local time
tt = rffi.cast(rffi.TIME_T, pytime.time())
t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
t_ref[0] = tt
pbuf = c_localtime(t_ref)
rffi.setintfield(pbuf, "c_tm_year",
rffi.getintfield(pbuf, "c_tm_year") + 1900)
lltype.free(t_ref, flavor='raw')
if not pbuf:
raise OperationError(space.w_ValueError,
space.newunicode(_get_error_msg()))
return pbuf
tup_w = space.fixedview(w_tup)
if len(tup_w) < 9:
raise oefmt(space.w_TypeError,
"argument must be sequence of at least length 9, not %d",
len(tup_w))
y = space.c_int_w(tup_w[0])
tm_mon = space.c_int_w(tup_w[1])
if tm_mon == 0:
tm_mon = 1
tm_mday = space.c_int_w(tup_w[2])
if tm_mday == 0:
tm_mday = 1
tm_yday = space.c_int_w(tup_w[7])
if tm_yday == 0:
tm_yday = 1
rffi.setintfield(glob_buf, 'c_tm_mon', tm_mon)
rffi.setintfield(glob_buf, 'c_tm_mday', tm_mday)
rffi.setintfield(glob_buf, 'c_tm_hour', space.c_int_w(tup_w[3]))
rffi.setintfield(glob_buf, 'c_tm_min', space.c_int_w(tup_w[4]))
rffi.setintfield(glob_buf, 'c_tm_sec', space.c_int_w(tup_w[5]))
rffi.setintfield(glob_buf, 'c_tm_wday', space.c_int_w(tup_w[6]))
rffi.setintfield(glob_buf, 'c_tm_yday', tm_yday)
rffi.setintfield(glob_buf, 'c_tm_isdst', space.c_int_w(tup_w[8]))
#
if HAS_TM_ZONE :
old_tm_zone = glob_buf.c_tm_zone
glob_buf.c_tm_zone = lltype.nullptr(rffi.CCHARP.TO)
rffi.setintfield(glob_buf, 'c_tm_gmtoff', 0)
if len(tup_w) >= 10:
# NOTE this is not cleanly solved!
# it saves the string that is later deleted when this
# function is called again. A refactoring of this module
# could remove this
tm_zone = encode_utf8(space, space.unicode_w(tup_w[9]), allow_surrogates=True)
malloced_str = rffi.str2charp(tm_zone, track_allocation=False)
if old_tm_zone != lltype.nullptr(rffi.CCHARP.TO):
rffi.free_charp(old_tm_zone, track_allocation=False)
glob_buf.c_tm_zone = malloced_str
if len(tup_w) >= 11:
rffi.setintfield(glob_buf, 'c_tm_gmtoff', space.c_int_w(tup_w[10]))
# tm_wday does not need checking of its upper-bound since taking "%
# 7" in _gettmarg() automatically restricts the range.
if rffi.getintfield(glob_buf, 'c_tm_wday') < -1:
raise oefmt(space.w_ValueError, "day of week out of range")
rffi.setintfield(glob_buf, 'c_tm_year', y)
rffi.setintfield(glob_buf, 'c_tm_mon',
rffi.getintfield(glob_buf, 'c_tm_mon') - 1)
rffi.setintfield(glob_buf, 'c_tm_wday',
(rffi.getintfield(glob_buf, 'c_tm_wday') + 1) % 7)
rffi.setintfield(glob_buf, 'c_tm_yday',
rffi.getintfield(glob_buf, 'c_tm_yday') - 1)
return glob_buf
def _checktm(space, t_ref):
"""Checks added to make sure strftime() and asctime() do not crash
Python by indexing blindly into some array for a textual
representation by some bad index (fixes bug #897625). No check for
year or wday since handled in _gettmarg()."""
if not 0 <= rffi.getintfield(t_ref, 'c_tm_mon') <= 11:
raise oefmt(space.w_ValueError, "month out of range")
if not 1 <= rffi.getintfield(t_ref, 'c_tm_mday') <= 31:
raise oefmt(space.w_ValueError, "day of month out of range")
if not 0 <= rffi.getintfield(t_ref, 'c_tm_hour') <= 23:
raise oefmt(space.w_ValueError, "hour out of range")
if not 0 <= rffi.getintfield(t_ref, 'c_tm_min') <= 59:
raise oefmt(space.w_ValueError, "minute out of range")
if not 0 <= rffi.getintfield(t_ref, 'c_tm_sec') <= 61:
raise oefmt(space.w_ValueError, "seconds out of range")
# tm_wday does not need checking: "% 7" in _gettmarg() automatically
# restricts the range
if not 0 <= rffi.getintfield(t_ref, 'c_tm_yday') <= 365:
raise oefmt(space.w_ValueError, "day of year out of range")
def time(space, w_info=None):
"""time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them."""
if HAS_CLOCK_GETTIME:
with lltype.scoped_alloc(TIMESPEC) as timespec:
ret = c_clock_gettime(rtime.CLOCK_REALTIME, timespec)
if ret == 0:
if w_info is not None:
with lltype.scoped_alloc(TIMESPEC) as tsres:
ret = c_clock_getres(rtime.CLOCK_REALTIME, tsres)
if ret == 0:
res = _timespec_to_seconds(tsres)
else:
res = 1e-9
_setinfo(space, w_info, "clock_gettime(CLOCK_REALTIME)",
res, False, True)
return space.newfloat(_timespec_to_seconds(timespec))
else:
return gettimeofday(space, w_info)
def ctime(space, w_seconds=None):
"""ctime([seconds]) -> string
Convert a time in seconds since the Epoch to a string in local time.
This is equivalent to asctime(localtime(seconds)). When the time tuple is
not present, current time as returned by localtime() is used."""
seconds = _get_inttime(space, w_seconds)
with lltype.scoped_alloc(rffi.TIME_TP.TO, 1) as t_ref:
t_ref[0] = seconds
p = c_localtime(t_ref)
if not p:
raise oefmt(space.w_OSError, "unconvertible time")
rffi.setintfield(p, "c_tm_year", rffi.getintfield(p, "c_tm_year") + 1900)
return _asctime(space, p)
# by now w_tup is an optional argument (and not *args)
# because of the ext. compiler bugs in handling such arguments (*args, **kwds)
def asctime(space, w_tup=None):
"""asctime([tuple]) -> string
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used."""
buf_value = _gettmarg(space, w_tup)
_checktm(space, buf_value)
return _asctime(space, buf_value)
_wday_names = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
_mon_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"]
def _asctime(space, t_ref):
# Inspired by Open Group reference implementation available at
# http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html
getif = rffi.getintfield
args = [space.newtext(_wday_names[getif(t_ref, 'c_tm_wday')]),
space.newtext(_mon_names[getif(t_ref, 'c_tm_mon')]),
space.newint(getif(t_ref, 'c_tm_mday')),
space.newint(getif(t_ref, 'c_tm_hour')),
space.newint(getif(t_ref, 'c_tm_min')),
space.newint(getif(t_ref, 'c_tm_sec')),
space.newint(getif(t_ref, 'c_tm_year'))]
return space.mod(space.newtext("%.3s %.3s%3d %.2d:%.2d:%.2d %d"),
space.newtuple(args))
def gmtime(space, w_seconds=None):
"""gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,
tm_sec, tm_wday, tm_yday, tm_isdst)
Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
GMT). When 'seconds' is not passed in, convert the current time instead.
"""
# rpython does not support that a variable has two incompatible builtins
# as value so we have to duplicate the code. NOT GOOD! see localtime() too
seconds = _get_inttime(space, w_seconds)
t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
t_ref[0] = seconds
p = c_gmtime(t_ref)
lltype.free(t_ref, flavor='raw')
if not p:
raise OperationError(space.w_ValueError,
space.newunicode(_get_error_msg()))
return _tm_to_tuple(space, p)
def localtime(space, w_seconds=None):
"""localtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,
tm_sec, tm_wday, tm_yday, tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead."""
seconds = _get_inttime(space, w_seconds)
t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
t_ref[0] = seconds
p = c_localtime(t_ref)
lltype.free(t_ref, flavor='raw')
if not p:
raise OperationError(space.w_OSError,
space.newunicode(_get_error_msg()))
return _tm_to_tuple(space, p)
def mktime(space, w_tup):
"""mktime(tuple) -> floating point number
Convert a time tuple in local time to seconds since the Epoch."""
buf = _gettmarg(space, w_tup, allowNone=False)
rffi.setintfield(buf, "c_tm_wday", -1)
rffi.setintfield(buf, "c_tm_year", rffi.getintfield(buf, "c_tm_year") - 1900)
tt = c_mktime(buf)
# A return value of -1 does not necessarily mean an error, but tm_wday
# cannot remain set to -1 if mktime succeeds.
if tt == -1 and rffi.getintfield(buf, "c_tm_wday") == -1:
raise oefmt(space.w_OverflowError, "mktime argument out of range")
return space.newfloat(float(tt))
if HAS_CLOCK_GETTIME:
def _timespec_to_seconds(timespec):
return widen(timespec.c_tv_sec) + widen(timespec.c_tv_nsec) * 1e-9
@unwrap_spec(clk_id='c_int')
def clock_gettime(space, clk_id):
with lltype.scoped_alloc(TIMESPEC) as timespec:
ret = c_clock_gettime(clk_id, timespec)
if ret != 0:
raise exception_from_saved_errno(space, space.w_OSError)
secs = _timespec_to_seconds(timespec)
return space.newfloat(secs)
@unwrap_spec(clk_id='c_int', secs=float)
def clock_settime(space, clk_id, secs):
with lltype.scoped_alloc(TIMESPEC) as timespec:
integer_secs = rffi.cast(TIMESPEC.c_tv_sec, secs)
frac = secs - widen(integer_secs)
rffi.setintfield(timespec, 'c_tv_sec', integer_secs)
rffi.setintfield(timespec, 'c_tv_nsec', int(frac * 1e9))
ret = c_clock_settime(clk_id, timespec)
if ret != 0:
raise exception_from_saved_errno(space, space.w_OSError)
@unwrap_spec(clk_id='c_int')
def clock_getres(space, clk_id):
with lltype.scoped_alloc(TIMESPEC) as timespec:
ret = c_clock_getres(clk_id, timespec)
if ret != 0:
raise exception_from_saved_errno(space, space.w_OSError)
secs = _timespec_to_seconds(timespec)
return space.newfloat(secs)
if _POSIX:
def tzset(space):
"""tzset()
Initialize, or reinitialize, the local timezone to the value stored in
os.environ['TZ']. The TZ environment variable should be specified in
standard Unix timezone format as documented in the tzset man page
(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
fall back to UTC. If the TZ environment variable is not set, the local
timezone is set to the systems best guess of wallclock time.
Changing the TZ environment variable without calling tzset *may* change
the local timezone used by methods such as localtime, but this behaviour
should not be relied on"""
c_tzset()
# reset timezone, altzone, daylight and tzname
_init_timezone(space)
@unwrap_spec(format='text')
def strftime(space, format, w_tup=None):
"""strftime(format[, tuple]) -> string
Convert a time tuple to a string according to a format specification.
See the library reference manual for formatting codes. When the time tuple
is not present, current time as returned by localtime() is used."""
buf_value = _gettmarg(space, w_tup)
_checktm(space, buf_value)
# Normalize tm_isdst just in case someone foolishly implements %Z
# based on the assumption that tm_isdst falls within the range of
# [-1, 1]
if rffi.getintfield(buf_value, 'c_tm_isdst') < -1:
rffi.setintfield(buf_value, 'c_tm_isdst', -1)
elif rffi.getintfield(buf_value, 'c_tm_isdst') > 1:
rffi.setintfield(buf_value, 'c_tm_isdst', 1)
rffi.setintfield(buf_value, "c_tm_year",
rffi.getintfield(buf_value, "c_tm_year") - 1900)
if _WIN:
# check that the format string contains only valid directives
length = len(format)
i = 0
while i < length:
if format[i] == '%':
i += 1
if i < length and format[i] == '#':
# not documented by python
i += 1
if i >= length or format[i] not in "aAbBcdHIjmMpSUwWxXyYzZ%":
raise oefmt(space.w_ValueError, "invalid format string")
i += 1
i = 1024
while True:
outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw')
try:
buflen = c_strftime(outbuf, i, format, buf_value)
if buflen > 0 or i >= 256 * len(format):
# if the buffer is 256 times as long as the format,
# it's probably not failing for lack of room!
# More likely, the format yields an empty result,
# e.g. an empty format, or %Z when the timezone
# is unknown.
result = rffi.charp2strn(outbuf, intmask(buflen))
return space.newtext(result)
finally:
lltype.free(outbuf, flavor='raw')
i += i
if HAS_MONOTONIC:
if _WIN:
_GetTickCount = rwin32.winexternal('GetTickCount', [], rwin32.DWORD)
def monotonic(space, w_info=None):
result = 0
HAS_GETTICKCOUNT64 = time_state.check_GetTickCount64()
if HAS_GETTICKCOUNT64:
result = time_state.GetTickCount64() * 1e-3
else:
ticks = _GetTickCount()
if ticks < time_state.last_ticks:
time_state.n_overflow += 1
time_state.last_ticks = ticks
result = math.ldexp(time_state.n_overflow, 32)
result = result + ticks
result = result * 1e-3
if w_info is not None:
if HAS_GETTICKCOUNT64:
implementation = "GetTickCount64()"
else:
implementation = "GetTickCount()"
resolution = 1e-7
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as time_adjustment, \
lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as time_increment, \
lltype.scoped_alloc(rwin32.LPBOOL.TO, 1) as is_time_adjustment_disabled:
ok = _GetSystemTimeAdjustment(time_adjustment,
time_increment,
is_time_adjustment_disabled)
if not ok:
# Is this right? Cargo culting...
raise wrap_oserror(space,
rwin32.lastSavedWindowsError("GetSystemTimeAdjustment"))
resolution = resolution * time_increment[0]
_setinfo(space, w_info, implementation, resolution, True, False)
return space.newfloat(result)
elif _MACOSX:
c_mach_timebase_info = external('mach_timebase_info',
[lltype.Ptr(cConfig.TIMEBASE_INFO)],
lltype.Void)
c_mach_absolute_time = external('mach_absolute_time', [], rffi.ULONGLONG)
timebase_info = lltype.malloc(cConfig.TIMEBASE_INFO, flavor='raw',
zero=True, immortal=True)
def monotonic(space, w_info=None):
if rffi.getintfield(timebase_info, 'c_denom') == 0:
c_mach_timebase_info(timebase_info)
time = rffi.cast(lltype.Signed, c_mach_absolute_time())
numer = rffi.getintfield(timebase_info, 'c_numer')
denom = rffi.getintfield(timebase_info, 'c_denom')
nanosecs = time * numer / denom
if w_info is not None:
res = (numer / denom) * 1e-9
_setinfo(space, w_info, "mach_absolute_time()", res, True, False)
secs = nanosecs / 10**9
rest = nanosecs % 10**9
return space.newfloat(float(secs) + float(rest) * 1e-9)
else:
assert _POSIX
def monotonic(space, w_info=None):
if rtime.CLOCK_HIGHRES is not None:
clk_id = rtime.CLOCK_HIGHRES
implementation = "clock_gettime(CLOCK_HIGHRES)"
else:
clk_id = rtime.CLOCK_MONOTONIC
implementation = "clock_gettime(CLOCK_MONOTONIC)"
w_result = clock_gettime(space, clk_id)
if w_info is not None:
with lltype.scoped_alloc(TIMESPEC) as tsres:
ret = c_clock_getres(clk_id, tsres)
if ret == 0:
res = _timespec_to_seconds(tsres)
else:
res = 1e-9
_setinfo(space, w_info, implementation, res, True, False)
return w_result
if _WIN:
# hacking to avoid LARGE_INTEGER which is a union...
QueryPerformanceCounter = external(
'QueryPerformanceCounter', [rffi.CArrayPtr(lltype.SignedLongLong)],
lltype.Void)
QueryPerformanceCounter = rwin32.winexternal('QueryPerformanceCounter',
[rffi.CArrayPtr(lltype.SignedLongLong)],
rwin32.DWORD)
QueryPerformanceFrequency = rwin32.winexternal(
'QueryPerformanceFrequency', [rffi.CArrayPtr(lltype.SignedLongLong)],
rffi.INT)
def win_perf_counter(space, w_info=None):
with lltype.scoped_alloc(rffi.CArray(rffi.lltype.SignedLongLong), 1) as a:
succeeded = True
if time_state.divisor == 0.0:
QueryPerformanceCounter(a)
time_state.counter_start = a[0]
succeeded = QueryPerformanceFrequency(a)
time_state.divisor = float(a[0])
if succeeded and time_state.divisor != 0.0:
QueryPerformanceCounter(a)
diff = a[0] - time_state.counter_start
else:
raise ValueError("Failed to generate the result.")
resolution = 1 / time_state.divisor
if w_info is not None:
_setinfo(space, w_info, "QueryPerformanceCounter()", resolution,
True, False)
return space.newfloat(float(diff) / time_state.divisor)
def perf_counter(space, w_info=None):
try:
return win_perf_counter(space, w_info=w_info)
except ValueError:
if HAS_MONOTONIC:
try:
return monotonic(space, w_info=w_info)
except Exception:
pass
return time(space, w_info=w_info)
else:
def perf_counter(space, w_info=None):
if HAS_MONOTONIC:
try:
return monotonic(space, w_info=w_info)
except Exception:
pass
return time(space, w_info=w_info)
if _WIN:
def process_time(space, w_info=None):
from rpython.rlib.rposix import GetCurrentProcess, GetProcessTimes
current_process = GetCurrentProcess()
with lltype.scoped_alloc(rwin32.FILETIME) as creation_time, \
lltype.scoped_alloc(rwin32.FILETIME) as exit_time, \
lltype.scoped_alloc(rwin32.FILETIME) as kernel_time, \
lltype.scoped_alloc(rwin32.FILETIME) as user_time:
worked = GetProcessTimes(current_process, creation_time, exit_time,
kernel_time, user_time)
if not worked:
raise wrap_oserror(space,
rwin32.lastSavedWindowsError("GetProcessTimes"))
kernel_time2 = (kernel_time.c_dwLowDateTime |
r_ulonglong(kernel_time.c_dwHighDateTime) << 32)
user_time2 = (user_time.c_dwLowDateTime |
r_ulonglong(user_time.c_dwHighDateTime) << 32)
if w_info is not None:
_setinfo(space, w_info, "GetProcessTimes()", 1e-7, True, False)
return space.newfloat((float(kernel_time2) + float(user_time2)) * 1e-7)
else:
have_times = hasattr(rposix, 'c_times')
def process_time(space, w_info=None):
if HAS_CLOCK_GETTIME and (
rtime.CLOCK_PROF is not None or
rtime.CLOCK_PROCESS_CPUTIME_ID is not None):
if rtime.CLOCK_PROF is not None:
clk_id = rtime.CLOCK_PROF
implementation = "clock_gettime(CLOCK_PROF)"
else:
clk_id = rtime.CLOCK_PROCESS_CPUTIME_ID
implementation = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)"
with lltype.scoped_alloc(TIMESPEC) as timespec:
ret = c_clock_gettime(clk_id, timespec)
if ret == 0:
if w_info is not None:
with lltype.scoped_alloc(TIMESPEC) as tsres:
ret = c_clock_getres(clk_id, tsres)
if ret == 0:
res = _timespec_to_seconds(tsres)
else:
res = 1e-9
_setinfo(space, w_info,
implementation, res, True, False)
return space.newfloat(_timespec_to_seconds(timespec))
if True: # XXX available except if it isn't?
from rpython.rlib.rtime import (c_getrusage, RUSAGE, RUSAGE_SELF,
decode_timeval)
with lltype.scoped_alloc(RUSAGE) as rusage:
ret = c_getrusage(RUSAGE_SELF, rusage)
if ret == 0:
if w_info is not None:
_setinfo(space, w_info,
"getrusage(RUSAGE_SELF)", 1e-6, True, False)
return space.newfloat(decode_timeval(rusage.c_ru_utime) +
decode_timeval(rusage.c_ru_stime))
if have_times:
with lltype.scoped_alloc(rposix.TMS) as tms:
ret = rposix.c_times(tms)
if rffi.cast(lltype.Signed, ret) != -1:
cpu_time = float(rffi.cast(lltype.Signed,
tms.c_tms_utime) +
rffi.cast(lltype.Signed,
tms.c_tms_stime))
if w_info is not None:
_setinfo(space, w_info, "times()",
1.0 / rposix.CLOCK_TICKS_PER_SECOND,
True, False)
return space.newfloat(cpu_time / rposix.CLOCK_TICKS_PER_SECOND)
return clock(space)
_clock = external('clock', [], rposix.CLOCK_T)
def clock(space, w_info=None):
"""clock() -> floating point number
Return the CPU time or real time since the start of the process or since
the first call to clock(). This has as much precision as the system
records."""
if _WIN:
try:
return win_perf_counter(space, w_info=w_info)
except ValueError:
pass
value = widen(_clock())
if value == widen(rffi.cast(rposix.CLOCK_T, -1)):
raise oefmt(space.w_RuntimeError,
"the processor time used is not available or its value"
"cannot be represented")
if w_info is not None:
_setinfo(space, w_info,
"clock()", 1.0 / CLOCKS_PER_SEC, True, False)
return space.newfloat(float(value) / CLOCKS_PER_SEC)
def _setinfo(space, w_info, impl, res, mono, adj):
space.setattr(w_info, space.newtext('implementation'), space.newtext(impl))
space.setattr(w_info, space.newtext('resolution'), space.newfloat(res))
space.setattr(w_info, space.newtext('monotonic'), space.newbool(mono))
space.setattr(w_info, space.newtext('adjustable'), space.newbool(adj))
|