|
19 | 19 | #include "datetime.h"
|
20 | 20 |
|
21 | 21 |
|
22 |
| -#include <stdlib.h> |
23 | 22 | #include <time.h>
|
24 | 23 |
|
25 | 24 | #ifdef MS_WINDOWS
|
@@ -1833,14 +1832,16 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
|
1833 | 1832 | PyObject *tzinfoarg)
|
1834 | 1833 | {
|
1835 | 1834 | PyObject *result = NULL; /* guilty until proved innocent */
|
| 1835 | + PyObject *strftime = NULL; /* time.strftime */ |
1836 | 1836 |
|
1837 | 1837 | PyObject *zreplacement = NULL; /* py string, replacement for %z */
|
1838 | 1838 | PyObject *colonzreplacement = NULL; /* py string, replacement for %:z */
|
1839 | 1839 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */
|
1840 | 1840 | PyObject *freplacement = NULL; /* py string, replacement for %f */
|
1841 | 1841 | #ifdef NORMALIZE_CENTURY
|
1842 |
| - long year; /* year of timetuple as long */ |
1843 |
| - char year_formatted[12]; /* formatted year with century for %Y */ |
| 1842 | + PyObject *year; /* year of timetuple */ |
| 1843 | + long year_long; /* year of timetuple as long int */ |
| 1844 | + char year_formatted[12]; /* formatted year with century for %Y/G */ |
1844 | 1845 | #endif
|
1845 | 1846 |
|
1846 | 1847 | const char *pin; /* pointer to next char in input format */
|
@@ -1878,10 +1879,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
|
1878 | 1879 | pnew = PyBytes_AsString(newfmt);
|
1879 | 1880 | usednew = 0;
|
1880 | 1881 |
|
1881 |
| - PyObject *strftime = _PyImport_GetModuleAttrString("time", "strftime"); |
1882 |
| - |
1883 |
| - if (strftime == NULL) |
| 1882 | + strftime = _PyImport_GetModuleAttrString("time", "strftime"); |
| 1883 | + if (strftime == NULL) { |
1884 | 1884 | goto Done;
|
| 1885 | + } |
1885 | 1886 |
|
1886 | 1887 | while ((ch = *pin++) != '\0') {
|
1887 | 1888 | if (ch != '%') {
|
@@ -1953,15 +1954,23 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
|
1953 | 1954 | else if (ch == 'Y' || ch == 'G') {
|
1954 | 1955 | /* 0-pad year with century as necessary */
|
1955 | 1956 | if (ch == 'G') {
|
1956 |
| - format = PyUnicode_FromString("%G"); |
1957 |
| - result = PyObject_CallFunctionObjArgs(strftime, format, |
1958 |
| - timetuple, NULL); |
1959 |
| - year = atoi(PyUnicode_AsUTF8(result)); |
1960 |
| - Py_DECREF(format); |
| 1957 | + result = PyObject_CallFunction(strftime, "sO", "%G", timetuple); |
| 1958 | + if (result == NULL) { |
| 1959 | + goto Done; |
| 1960 | + } |
| 1961 | + year = PyNumber_Long(result); |
| 1962 | + if (year == NULL) { |
| 1963 | + goto Done; |
| 1964 | + } |
1961 | 1965 | Py_DECREF(result);
|
1962 |
| - } else |
1963 |
| - year = PyLong_AsLong(PyTuple_GET_ITEM(timetuple, 0)); |
1964 |
| - ntoappend = sprintf(year_formatted, "%04ld", year); |
| 1966 | + } else { |
| 1967 | + year = PyTuple_GET_ITEM(timetuple, 0); |
| 1968 | + } |
| 1969 | + year_long = PyLong_AsLong(year); |
| 1970 | + if (year_long == -1 && PyErr_Occurred() != NULL) { |
| 1971 | + goto Done; |
| 1972 | + } |
| 1973 | + ntoappend = sprintf(year_formatted, "%04ld", year_long); |
1965 | 1974 | ptoappend = year_formatted;
|
1966 | 1975 | }
|
1967 | 1976 | #endif
|
@@ -2005,14 +2014,14 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
|
2005 | 2014 | format, timetuple, NULL);
|
2006 | 2015 | Py_DECREF(format);
|
2007 | 2016 | }
|
2008 |
| - Py_DECREF(strftime); |
2009 | 2017 | }
|
2010 | 2018 | Done:
|
2011 | 2019 | Py_XDECREF(freplacement);
|
2012 | 2020 | Py_XDECREF(zreplacement);
|
2013 | 2021 | Py_XDECREF(colonzreplacement);
|
2014 | 2022 | Py_XDECREF(Zreplacement);
|
2015 | 2023 | Py_XDECREF(newfmt);
|
| 2024 | + Py_XDECREF(strftime); |
2016 | 2025 | return result;
|
2017 | 2026 | }
|
2018 | 2027 |
|
|
0 commit comments