95
95
_PyTime_DoubleToTimet(double x)
101
/* How much info did we lose? time_t may be an integral or
102
* floating type, and we don't know which. If it's integral,
103
* we don't know whether C truncates, rounds, returns the floor,
104
* etc. If we lost a second or more, the C rounding is
105
* unreasonable, or the input just doesn't fit in a time_t;
106
* call it an error regardless. Note that the original cast to
107
* time_t can cause a C error too, but nothing we can do to
110
diff = x - (double)result;
111
if (diff <= -1.0 || diff >= 1.0) {
112
PyErr_SetString(PyExc_ValueError,
113
"timestamp out of range for platform time_t");
101
/* How much info did we lose? time_t may be an integral or
102
* floating type, and we don't know which. If it's integral,
103
* we don't know whether C truncates, rounds, returns the floor,
104
* etc. If we lost a second or more, the C rounding is
105
* unreasonable, or the input just doesn't fit in a time_t;
106
* call it an error regardless. Note that the original cast to
107
* time_t can cause a C error too, but nothing we can do to
110
diff = x - (double)result;
111
if (diff <= -1.0 || diff >= 1.0) {
112
PyErr_SetString(PyExc_ValueError,
113
"timestamp out of range for platform time_t");
119
119
static PyObject *
120
120
time_time(PyObject *self, PyObject *unused)
125
PyErr_SetFromErrno(PyExc_IOError);
128
return PyFloat_FromDouble(secs);
125
PyErr_SetFromErrno(PyExc_IOError);
128
return PyFloat_FromDouble(secs);
131
131
PyDoc_STRVAR(time_doc,
358
358
gettmarg(PyObject *args, struct tm *p)
363
memset((void *) p, '\0', sizeof(struct tm));
365
if (PyTuple_Check(args)) {
369
else if (Py_TYPE(args) == &StructTimeType) {
370
t = structtime_totuple(args);
373
PyErr_SetString(PyExc_TypeError,
374
"Tuple or struct_time argument required");
378
if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
394
PyObject *accept = PyDict_GetItemString(moddict,
396
if (accept == NULL || !PyLong_CheckExact(accept) ||
397
!PyObject_IsTrue(accept)) {
398
PyErr_SetString(PyExc_ValueError,
399
"year >= 1900 required");
402
if (69 <= y && y <= 99)
404
else if (0 <= y && y <= 68)
407
PyErr_SetString(PyExc_ValueError,
408
"year out of range");
412
p->tm_year = y - 1900;
414
p->tm_wday = (p->tm_wday + 1) % 7;
363
memset((void *) p, '\0', sizeof(struct tm));
365
if (PyTuple_Check(args)) {
369
else if (Py_TYPE(args) == &StructTimeType) {
370
t = structtime_totuple(args);
373
PyErr_SetString(PyExc_TypeError,
374
"Tuple or struct_time argument required");
378
if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
394
PyObject *accept = PyDict_GetItemString(moddict,
396
if (accept == NULL || !PyLong_CheckExact(accept) ||
397
!PyObject_IsTrue(accept)) {
398
PyErr_SetString(PyExc_ValueError,
399
"year >= 1900 required");
402
if (69 <= y && y <= 99)
404
else if (0 <= y && y <= 68)
407
PyErr_SetString(PyExc_ValueError,
408
"year out of range");
412
p->tm_year = y - 1900;
414
p->tm_wday = (p->tm_wday + 1) % 7;
419
419
#ifdef HAVE_STRFTIME
430
430
static PyObject *
431
431
time_strftime(PyObject *self, PyObject *args)
433
PyObject *tup = NULL;
435
const time_char *fmt;
436
PyObject *format, *tmpfmt;
437
size_t fmtlen, buflen;
438
time_char *outbuf = 0;
441
memset((void *) &buf, '\0', sizeof(buf));
443
/* Will always expect a unicode string to be passed as format.
444
Given that there's no str type anymore in py3k this seems safe.
446
if (!PyArg_ParseTuple(args, "U|O:strftime", &format, &tup))
450
time_t tt = time(NULL);
451
buf = *localtime(&tt);
452
} else if (!gettmarg(tup, &buf))
455
/* Checks added to make sure strftime() does not crash Python by
456
indexing blindly into some array for a textual representation
457
by some bad index (fixes bug #897625).
459
Also support values of zero from Python code for arguments in which
460
that is out of range by forcing that value to the lowest value that
461
is valid (fixed bug #1520914).
463
Valid ranges based on what is allowed in struct tm:
465
- tm_year: [0, max(int)] (1)
466
- tm_mon: [0, 11] (2)
471
- tm_wday: [0, 6] (1)
472
- tm_yday: [0, 365] (2)
473
- tm_isdst: [-max(int), max(int)]
475
(1) gettmarg() handles bounds-checking.
476
(2) Python's acceptable range is one greater than the range in C,
477
thus need to check against automatic decrement by gettmarg().
479
if (buf.tm_mon == -1)
481
else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
482
PyErr_SetString(PyExc_ValueError, "month out of range");
485
if (buf.tm_mday == 0)
487
else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
488
PyErr_SetString(PyExc_ValueError, "day of month out of range");
491
if (buf.tm_hour < 0 || buf.tm_hour > 23) {
492
PyErr_SetString(PyExc_ValueError, "hour out of range");
495
if (buf.tm_min < 0 || buf.tm_min > 59) {
496
PyErr_SetString(PyExc_ValueError, "minute out of range");
499
if (buf.tm_sec < 0 || buf.tm_sec > 61) {
500
PyErr_SetString(PyExc_ValueError, "seconds out of range");
503
/* tm_wday does not need checking of its upper-bound since taking
504
``% 7`` in gettmarg() automatically restricts the range. */
505
if (buf.tm_wday < 0) {
506
PyErr_SetString(PyExc_ValueError, "day of week out of range");
509
if (buf.tm_yday == -1)
511
else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
512
PyErr_SetString(PyExc_ValueError, "day of year out of range");
515
if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
516
PyErr_SetString(PyExc_ValueError,
517
"daylight savings flag out of range");
433
PyObject *tup = NULL;
435
const time_char *fmt;
436
PyObject *format, *tmpfmt;
437
size_t fmtlen, buflen;
438
time_char *outbuf = 0;
441
memset((void *) &buf, '\0', sizeof(buf));
443
/* Will always expect a unicode string to be passed as format.
444
Given that there's no str type anymore in py3k this seems safe.
446
if (!PyArg_ParseTuple(args, "U|O:strftime", &format, &tup))
450
time_t tt = time(NULL);
451
buf = *localtime(&tt);
452
} else if (!gettmarg(tup, &buf))
455
/* Checks added to make sure strftime() does not crash Python by
456
indexing blindly into some array for a textual representation
457
by some bad index (fixes bug #897625).
459
Also support values of zero from Python code for arguments in which
460
that is out of range by forcing that value to the lowest value that
461
is valid (fixed bug #1520914).
463
Valid ranges based on what is allowed in struct tm:
465
- tm_year: [0, max(int)] (1)
466
- tm_mon: [0, 11] (2)
471
- tm_wday: [0, 6] (1)
472
- tm_yday: [0, 365] (2)
473
- tm_isdst: [-max(int), max(int)]
475
(1) gettmarg() handles bounds-checking.
476
(2) Python's acceptable range is one greater than the range in C,
477
thus need to check against automatic decrement by gettmarg().
479
if (buf.tm_mon == -1)
481
else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
482
PyErr_SetString(PyExc_ValueError, "month out of range");
485
if (buf.tm_mday == 0)
487
else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
488
PyErr_SetString(PyExc_ValueError, "day of month out of range");
491
if (buf.tm_hour < 0 || buf.tm_hour > 23) {
492
PyErr_SetString(PyExc_ValueError, "hour out of range");
495
if (buf.tm_min < 0 || buf.tm_min > 59) {
496
PyErr_SetString(PyExc_ValueError, "minute out of range");
499
if (buf.tm_sec < 0 || buf.tm_sec > 61) {
500
PyErr_SetString(PyExc_ValueError, "seconds out of range");
503
/* tm_wday does not need checking of its upper-bound since taking
504
``% 7`` in gettmarg() automatically restricts the range. */
505
if (buf.tm_wday < 0) {
506
PyErr_SetString(PyExc_ValueError, "day of week out of range");
509
if (buf.tm_yday == -1)
511
else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
512
PyErr_SetString(PyExc_ValueError, "day of year out of range");
515
if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
516
PyErr_SetString(PyExc_ValueError,
517
"daylight savings flag out of range");
521
521
#ifdef HAVE_WCSFTIME
522
tmpfmt = PyBytes_FromStringAndSize(NULL,
523
sizeof(wchar_t) * (PyUnicode_GetSize(format)+1));
526
/* This assumes that PyUnicode_AsWideChar doesn't do any UTF-16
528
if (PyUnicode_AsWideChar((PyUnicodeObject*)format,
529
(wchar_t*)PyBytes_AS_STRING(tmpfmt),
530
PyUnicode_GetSize(format)+1) == (size_t)-1)
531
/* This shouldn't fail. */
532
Py_FatalError("PyUnicode_AsWideChar failed");
534
fmt = (wchar_t*)PyBytes_AS_STRING(format);
522
tmpfmt = PyBytes_FromStringAndSize(NULL,
523
sizeof(wchar_t) * (PyUnicode_GetSize(format)+1));
526
/* This assumes that PyUnicode_AsWideChar doesn't do any UTF-16
528
if (PyUnicode_AsWideChar((PyUnicodeObject*)format,
529
(wchar_t*)PyBytes_AS_STRING(tmpfmt),
530
PyUnicode_GetSize(format)+1) == (size_t)-1)
531
/* This shouldn't fail. */
532
Py_FatalError("PyUnicode_AsWideChar failed");
534
fmt = (wchar_t*)PyBytes_AS_STRING(format);
536
/* Convert the unicode string to an ascii one */
537
format = PyUnicode_AsEncodedString(format, TZNAME_ENCODING, NULL);
540
fmt = PyBytes_AS_STRING(format);
536
/* Convert the unicode string to an ascii one */
537
format = PyUnicode_AsEncodedString(format, TZNAME_ENCODING, NULL);
540
fmt = PyBytes_AS_STRING(format);
543
543
#if defined(MS_WINDOWS) && defined(HAVE_WCSFTIME)
544
/* check that the format string contains only valid directives */
545
for(outbuf = wcschr(fmt, L'%');
547
outbuf = wcschr(outbuf+2, L'%'))
550
++outbuf; /* not documented by python, */
551
if (outbuf[1]=='\0' ||
552
!wcschr(L"aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1]))
554
PyErr_SetString(PyExc_ValueError, "Invalid format string");
544
/* check that the format string contains only valid directives */
545
for(outbuf = wcschr(fmt, L'%');
547
outbuf = wcschr(outbuf+2, L'%'))
550
++outbuf; /* not documented by python, */
551
if (outbuf[1]=='\0' ||
552
!wcschr(L"aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1]))
554
PyErr_SetString(PyExc_ValueError, "Invalid format string");
560
fmtlen = time_strlen(fmt);
560
fmtlen = time_strlen(fmt);
562
/* I hate these functions that presume you know how big the output
563
* will be ahead of time...
565
for (i = 1024; ; i += i) {
566
outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
567
if (outbuf == NULL) {
569
return PyErr_NoMemory();
571
buflen = format_time(outbuf, i, fmt, &buf);
572
if (buflen > 0 || i >= 256 * fmtlen) {
573
/* If the buffer is 256 times as long as the format,
574
it's probably not failing for lack of room!
575
More likely, the format yields an empty result,
576
e.g. an empty format, or %Z when the timezone
562
/* I hate these functions that presume you know how big the output
563
* will be ahead of time...
565
for (i = 1024; ; i += i) {
566
outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
567
if (outbuf == NULL) {
569
return PyErr_NoMemory();
571
buflen = format_time(outbuf, i, fmt, &buf);
572
if (buflen > 0 || i >= 256 * fmtlen) {
573
/* If the buffer is 256 times as long as the format,
574
it's probably not failing for lack of room!
575
More likely, the format yields an empty result,
576
e.g. an empty format, or %Z when the timezone
579
579
#ifdef HAVE_WCSFTIME
580
ret = PyUnicode_FromWideChar(outbuf, buflen);
580
ret = PyUnicode_FromWideChar(outbuf, buflen);
582
ret = PyUnicode_Decode(outbuf, buflen,
583
TZNAME_ENCODING, NULL);
582
ret = PyUnicode_Decode(outbuf, buflen,
583
TZNAME_ENCODING, NULL);
590
590
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
591
/* VisualStudio .NET 2005 does this properly */
592
if (buflen == 0 && errno == EINVAL) {
593
PyErr_SetString(PyExc_ValueError, "Invalid format string");
591
/* VisualStudio .NET 2005 does this properly */
592
if (buflen == 0 && errno == EINVAL) {
593
PyErr_SetString(PyExc_ValueError, "Invalid format string");
755
755
PyInit_timezone(PyObject *m) {
756
756
/* This code moved from PyInit_time wholesale to allow calling it from
757
time_tzset. In the future, some parts of it can be moved back
758
(for platforms that don't HAVE_WORKING_TZSET, when we know what they
759
are), and the extraneous calls to tzset(3) should be removed.
760
I haven't done this yet, as I don't want to change this code as
761
little as possible when introducing the time.tzset and time.tzsetwall
762
methods. This should simply be a method of doing the following once,
763
at the top of this function and removing the call to tzset() from
770
And I'm lazy and hate C so nyer.
757
time_tzset. In the future, some parts of it can be moved back
758
(for platforms that don't HAVE_WORKING_TZSET, when we know what they
759
are), and the extraneous calls to tzset(3) should be removed.
760
I haven't done this yet, as I don't want to change this code as
761
little as possible when introducing the time.tzset and time.tzsetwall
762
methods. This should simply be a method of doing the following once,
763
at the top of this function and removing the call to tzset() from
770
And I'm lazy and hate C so nyer.
772
772
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
773
PyObject *otz0, *otz1;
773
PyObject *otz0, *otz1;
776
PyModule_AddIntConstant(m, "timezone", _timezone);
776
PyModule_AddIntConstant(m, "timezone", _timezone);
777
777
#else /* !PYOS_OS2 */
778
PyModule_AddIntConstant(m, "timezone", timezone);
778
PyModule_AddIntConstant(m, "timezone", timezone);
779
779
#endif /* PYOS_OS2 */
780
780
#ifdef HAVE_ALTZONE
781
PyModule_AddIntConstant(m, "altzone", altzone);
781
PyModule_AddIntConstant(m, "altzone", altzone);
784
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
784
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
785
785
#else /* !PYOS_OS2 */
786
PyModule_AddIntConstant(m, "altzone", timezone-3600);
786
PyModule_AddIntConstant(m, "altzone", timezone-3600);
787
787
#endif /* PYOS_OS2 */
789
PyModule_AddIntConstant(m, "daylight", daylight);
790
otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
791
otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
792
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
789
PyModule_AddIntConstant(m, "daylight", daylight);
790
otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
791
otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
792
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
793
793
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
794
794
#ifdef HAVE_STRUCT_TM_TM_ZONE
796
796
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
799
long janzone, julyzone;
800
char janname[10], julyname[10];
801
t = (time((time_t *)0) / YEAR) * YEAR;
803
janzone = -p->tm_gmtoff;
804
strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
808
julyzone = -p->tm_gmtoff;
809
strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
799
long janzone, julyzone;
800
char janname[10], julyname[10];
801
t = (time((time_t *)0) / YEAR) * YEAR;
803
janzone = -p->tm_gmtoff;
804
strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
808
julyzone = -p->tm_gmtoff;
809
strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
812
if( janzone < julyzone ) {
813
/* DST is reversed in the southern hemisphere */
814
PyModule_AddIntConstant(m, "timezone", julyzone);
815
PyModule_AddIntConstant(m, "altzone", janzone);
816
PyModule_AddIntConstant(m, "daylight",
817
janzone != julyzone);
818
PyModule_AddObject(m, "tzname",
819
Py_BuildValue("(zz)",
822
PyModule_AddIntConstant(m, "timezone", janzone);
823
PyModule_AddIntConstant(m, "altzone", julyzone);
824
PyModule_AddIntConstant(m, "daylight",
825
janzone != julyzone);
826
PyModule_AddObject(m, "tzname",
827
Py_BuildValue("(zz)",
812
if( janzone < julyzone ) {
813
/* DST is reversed in the southern hemisphere */
814
PyModule_AddIntConstant(m, "timezone", julyzone);
815
PyModule_AddIntConstant(m, "altzone", janzone);
816
PyModule_AddIntConstant(m, "daylight",
817
janzone != julyzone);
818
PyModule_AddObject(m, "tzname",
819
Py_BuildValue("(zz)",
822
PyModule_AddIntConstant(m, "timezone", janzone);
823
PyModule_AddIntConstant(m, "altzone", julyzone);
824
PyModule_AddIntConstant(m, "daylight",
825
janzone != julyzone);
826
PyModule_AddObject(m, "tzname",
827
Py_BuildValue("(zz)",
832
832
#endif /* HAVE_STRUCT_TM_TM_ZONE */
833
833
#ifdef __CYGWIN__
835
PyModule_AddIntConstant(m, "timezone", _timezone);
836
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
837
PyModule_AddIntConstant(m, "daylight", _daylight);
838
PyModule_AddObject(m, "tzname",
839
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
835
PyModule_AddIntConstant(m, "timezone", _timezone);
836
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
837
PyModule_AddIntConstant(m, "daylight", _daylight);
838
PyModule_AddObject(m, "tzname",
839
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
840
840
#endif /* __CYGWIN__ */
841
841
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
845
845
static PyMethodDef time_methods[] = {
846
{"time", time_time, METH_NOARGS, time_doc},
846
{"time", time_time, METH_NOARGS, time_doc},
847
847
#ifdef HAVE_CLOCK
848
{"clock", time_clock, METH_NOARGS, clock_doc},
848
{"clock", time_clock, METH_NOARGS, clock_doc},
850
{"sleep", time_sleep, METH_VARARGS, sleep_doc},
851
{"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
852
{"localtime", time_localtime, METH_VARARGS, localtime_doc},
853
{"asctime", time_asctime, METH_VARARGS, asctime_doc},
854
{"ctime", time_ctime, METH_VARARGS, ctime_doc},
850
{"sleep", time_sleep, METH_VARARGS, sleep_doc},
851
{"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
852
{"localtime", time_localtime, METH_VARARGS, localtime_doc},
853
{"asctime", time_asctime, METH_VARARGS, asctime_doc},
854
{"ctime", time_ctime, METH_VARARGS, ctime_doc},
855
855
#ifdef HAVE_MKTIME
856
{"mktime", time_mktime, METH_O, mktime_doc},
856
{"mktime", time_mktime, METH_O, mktime_doc},
858
858
#ifdef HAVE_STRFTIME
859
{"strftime", time_strftime, METH_VARARGS, strftime_doc},
859
{"strftime", time_strftime, METH_VARARGS, strftime_doc},
861
{"strptime", time_strptime, METH_VARARGS, strptime_doc},
861
{"strptime", time_strptime, METH_VARARGS, strptime_doc},
862
862
#ifdef HAVE_WORKING_TZSET
863
{"tzset", time_tzset, METH_NOARGS, tzset_doc},
863
{"tzset", time_tzset, METH_NOARGS, tzset_doc},
865
{NULL, NULL} /* sentinel */
865
{NULL, NULL} /* sentinel */
916
916
static struct PyModuleDef timemodule = {
917
PyModuleDef_HEAD_INIT,
917
PyModuleDef_HEAD_INIT,
929
929
PyInit_time(void)
933
m = PyModule_Create(&timemodule);
937
/* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
938
p = Py_GETENV("PYTHONY2K");
939
PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
940
/* Squirrel away the module's dictionary for the y2k check */
941
moddict = PyModule_GetDict(m);
944
/* Set, or reset, module variables like time.timezone */
933
m = PyModule_Create(&timemodule);
937
/* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
938
p = Py_GETENV("PYTHONY2K");
939
PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
940
/* Squirrel away the module's dictionary for the y2k check */
941
moddict = PyModule_GetDict(m);
944
/* Set, or reset, module variables like time.timezone */
947
947
#ifdef MS_WINDOWS
948
/* Helper to allow interrupts for Windows.
949
If Ctrl+C event delivered while not sleeping
952
main_thread = PyThread_get_thread_ident();
953
hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
954
SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
948
/* Helper to allow interrupts for Windows.
949
If Ctrl+C event delivered while not sleeping
952
main_thread = PyThread_get_thread_ident();
953
hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
954
SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
955
955
#endif /* MS_WINDOWS */
957
PyStructSequence_InitType(&StructTimeType,
958
&struct_time_type_desc);
960
Py_INCREF(&StructTimeType);
961
PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
957
PyStructSequence_InitType(&StructTimeType,
958
&struct_time_type_desc);
960
Py_INCREF(&StructTimeType);
961
PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
1014
1014
/* XXX Should test for MS_WINDOWS first! */
1015
1015
#if defined(HAVE_SELECT) && !defined(__EMX__)
1018
frac = fmod(secs, 1.0);
1020
t.tv_sec = (long)secs;
1021
t.tv_usec = (long)(frac*1000000.0);
1022
Py_BEGIN_ALLOW_THREADS
1023
if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
1018
frac = fmod(secs, 1.0);
1020
t.tv_sec = (long)secs;
1021
t.tv_usec = (long)(frac*1000000.0);
1022
Py_BEGIN_ALLOW_THREADS
1023
if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
1025
if (errno != EINTR) {
1025
if (errno != EINTR) {
1030
PyErr_SetFromErrno(PyExc_IOError);
1034
Py_END_ALLOW_THREADS
1030
PyErr_SetFromErrno(PyExc_IOError);
1034
Py_END_ALLOW_THREADS
1035
1035
#elif defined(__WATCOMC__) && !defined(__QNX__)
1036
/* XXX Can't interrupt this sleep */
1037
Py_BEGIN_ALLOW_THREADS
1038
delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
1039
Py_END_ALLOW_THREADS
1036
/* XXX Can't interrupt this sleep */
1037
Py_BEGIN_ALLOW_THREADS
1038
delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
1039
Py_END_ALLOW_THREADS
1040
1040
#elif defined(MS_WINDOWS)
1042
double millisecs = secs * 1000.0;
1043
unsigned long ul_millis;
1042
double millisecs = secs * 1000.0;
1043
unsigned long ul_millis;
1045
if (millisecs > (double)ULONG_MAX) {
1046
PyErr_SetString(PyExc_OverflowError,
1047
"sleep length is too large");
1050
Py_BEGIN_ALLOW_THREADS
1051
/* Allow sleep(0) to maintain win32 semantics, and as decreed
1052
* by Guido, only the main thread can be interrupted.
1054
ul_millis = (unsigned long)millisecs;
1055
if (ul_millis == 0 ||
1056
main_thread != PyThread_get_thread_ident())
1060
ResetEvent(hInterruptEvent);
1061
rc = WaitForSingleObject(hInterruptEvent, ul_millis);
1062
if (rc == WAIT_OBJECT_0) {
1063
/* Yield to make sure real Python signal
1069
PyErr_SetFromErrno(PyExc_IOError);
1073
Py_END_ALLOW_THREADS
1045
if (millisecs > (double)ULONG_MAX) {
1046
PyErr_SetString(PyExc_OverflowError,
1047
"sleep length is too large");
1050
Py_BEGIN_ALLOW_THREADS
1051
/* Allow sleep(0) to maintain win32 semantics, and as decreed
1052
* by Guido, only the main thread can be interrupted.
1054
ul_millis = (unsigned long)millisecs;
1055
if (ul_millis == 0 ||
1056
main_thread != PyThread_get_thread_ident())
1060
ResetEvent(hInterruptEvent);
1061
rc = WaitForSingleObject(hInterruptEvent, ul_millis);
1062
if (rc == WAIT_OBJECT_0) {
1063
/* Yield to make sure real Python signal
1069
PyErr_SetFromErrno(PyExc_IOError);
1073
Py_END_ALLOW_THREADS
1075
1075
#elif defined(PYOS_OS2)
1076
/* This Sleep *IS* Interruptable by Exceptions */
1077
Py_BEGIN_ALLOW_THREADS
1078
if (DosSleep(secs * 1000) != NO_ERROR) {
1080
PyErr_SetFromErrno(PyExc_IOError);
1083
Py_END_ALLOW_THREADS
1076
/* This Sleep *IS* Interruptable by Exceptions */
1077
Py_BEGIN_ALLOW_THREADS
1078
if (DosSleep(secs * 1000) != NO_ERROR) {
1080
PyErr_SetFromErrno(PyExc_IOError);
1083
Py_END_ALLOW_THREADS
1084
1084
#elif defined(PLAN9)
1086
double millisecs = secs * 1000.0;
1087
if (millisecs > (double)LONG_MAX) {
1088
PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
1091
/* This sleep *CAN BE* interrupted. */
1092
Py_BEGIN_ALLOW_THREADS
1093
if(sleep((long)millisecs) < 0){
1095
PyErr_SetFromErrno(PyExc_IOError);
1098
Py_END_ALLOW_THREADS
1086
double millisecs = secs * 1000.0;
1087
if (millisecs > (double)LONG_MAX) {
1088
PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
1091
/* This sleep *CAN BE* interrupted. */
1092
Py_BEGIN_ALLOW_THREADS
1093
if(sleep((long)millisecs) < 0){
1095
PyErr_SetFromErrno(PyExc_IOError);
1098
Py_END_ALLOW_THREADS
1101
/* XXX Can't interrupt this sleep */
1102
Py_BEGIN_ALLOW_THREADS
1104
Py_END_ALLOW_THREADS
1101
/* XXX Can't interrupt this sleep */
1102
Py_BEGIN_ALLOW_THREADS
1104
Py_END_ALLOW_THREADS