~ubuntu-branches/ubuntu/precise/python3.2/precise-proposed

« back to all changes in this revision

Viewing changes to Modules/timemodule.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 18:40:39 UTC
  • mfrom: (30.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120309184039-j3yk2emxr1plyo21
Tags: 3.2.3~rc1-1
* Python 3.2.3 release candidate 1.
* Update to 20120309 from the 3.2 branch.
* Fix libpython.a symlink. Closes: #660146.
* Build-depend on xauth.
* Run the gdb tests for the debug build only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include "Python.h"
4
4
#include "_time.h"
5
5
 
6
 
#define TZNAME_ENCODING "utf-8"
7
 
 
8
6
#include <ctype.h>
9
7
 
10
8
#ifdef HAVE_SYS_TYPES_H
48
46
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
49
47
/* Win32 has better clock replacement; we have our own version below. */
50
48
#undef HAVE_CLOCK
51
 
#undef TZNAME_ENCODING
52
 
#define TZNAME_ENCODING "mbcs"
53
49
#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
54
50
 
55
51
#if defined(PYOS_OS2)
431
427
    return 1;
432
428
}
433
429
 
 
430
#ifdef MS_WINDOWS
 
431
   /* wcsftime() doesn't format correctly time zones, see issue #10653 */
 
432
#  undef HAVE_WCSFTIME
 
433
#endif
 
434
 
434
435
#ifdef HAVE_STRFTIME
435
436
#ifdef HAVE_WCSFTIME
436
437
#define time_char wchar_t
497
498
    fmt = format;
498
499
#else
499
500
    /* Convert the unicode string to an ascii one */
500
 
    format = PyUnicode_AsEncodedString(format_arg, TZNAME_ENCODING, NULL);
 
501
    format = PyUnicode_EncodeFSDefault(format_arg);
501
502
    if (format == NULL)
502
503
        return NULL;
503
504
    fmt = PyBytes_AS_STRING(format);
504
505
#endif
505
506
 
506
 
#if defined(MS_WINDOWS) && defined(HAVE_WCSFTIME)
 
507
#if defined(MS_WINDOWS)
507
508
    /* check that the format string contains only valid directives */
508
 
    for(outbuf = wcschr(fmt, L'%');
 
509
    for(outbuf = strchr(fmt, '%');
509
510
        outbuf != NULL;
510
 
        outbuf = wcschr(outbuf+2, L'%'))
 
511
        outbuf = strchr(outbuf+2, '%'))
511
512
    {
512
513
        if (outbuf[1]=='#')
513
514
            ++outbuf; /* not documented by python, */
514
515
        if (outbuf[1]=='\0' ||
515
 
            !wcschr(L"aAbBcdHIjmMpSUwWxXyYzZ%", outbuf[1]))
 
516
            !strchr("aAbBcdHIjmMpSUwWxXyYzZ%", outbuf[1]))
516
517
        {
517
518
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
518
519
            return 0;
526
527
     * will be ahead of time...
527
528
     */
528
529
    for (i = 1024; ; i += i) {
 
530
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
 
531
        int err;
 
532
#endif
529
533
        outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
530
534
        if (outbuf == NULL) {
531
535
            PyErr_NoMemory();
532
536
            break;
533
537
        }
534
538
        buflen = format_time(outbuf, i, fmt, &buf);
 
539
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
 
540
        err = errno;
 
541
#endif
535
542
        if (buflen > 0 || i >= 256 * fmtlen) {
536
543
            /* If the buffer is 256 times as long as the format,
537
544
               it's probably not failing for lack of room!
541
548
#ifdef HAVE_WCSFTIME
542
549
            ret = PyUnicode_FromWideChar(outbuf, buflen);
543
550
#else
544
 
            ret = PyUnicode_Decode(outbuf, buflen,
545
 
                                   TZNAME_ENCODING, NULL);
 
551
            ret = PyUnicode_DecodeFSDefaultAndSize(outbuf, buflen);
546
552
#endif
547
553
            PyMem_Free(outbuf);
548
554
            break;
550
556
        PyMem_Free(outbuf);
551
557
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
552
558
        /* VisualStudio .NET 2005 does this properly */
553
 
        if (buflen == 0 && errno == EINVAL) {
 
559
        if (buflen == 0 && err == EINVAL) {
554
560
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
555
561
            break;
556
562
        }
784
790
#endif /* PYOS_OS2 */
785
791
#endif
786
792
    PyModule_AddIntConstant(m, "daylight", daylight);
787
 
    otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
788
 
    otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
 
793
    otz0 = PyUnicode_DecodeFSDefaultAndSize(tzname[0], strlen(tzname[0]));
 
794
    otz1 = PyUnicode_DecodeFSDefaultAndSize(tzname[1], strlen(tzname[1]));
789
795
    PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
790
796
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
791
797
#ifdef HAVE_STRUCT_TM_TM_ZONE