static PyObject * encode(PyObject *b) { return PyUnicode_EncodeLocale(b, "surrogateescape"); }
static PyObject * time_strftime(PyObject *self, PyObject *args) { PyObject *tup = NULL; struct tm buf; const time_char *fmt; #ifdef HAVE_WCSFTIME wchar_t *format; #else PyObject *format; #endif PyObject *format_arg; size_t fmtlen, buflen; time_char *outbuf = NULL; size_t i; PyObject *ret = NULL; memset((void *) &buf, '\0', sizeof(buf)); /* Will always expect a unicode string to be passed as format. Given that there's no str type anymore in py3k this seems safe. */ if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup)) return NULL; if (tup == NULL) { time_t tt = time(NULL); if (pylocaltime(&tt, &buf) == -1) return NULL; } else if (!gettmarg(tup, &buf) || !checktm(&buf)) return NULL; #if defined(_MSC_VER) || defined(sun) || defined(_AIX) if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) { PyErr_SetString(PyExc_ValueError, "strftime() requires year in [1; 9999]"); return NULL; } #endif /* 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 (buf.tm_isdst < -1) buf.tm_isdst = -1; else if (buf.tm_isdst > 1) buf.tm_isdst = 1; #ifdef HAVE_WCSFTIME format = PyUnicode_AsWideCharString(format_arg, NULL); if (format == NULL) return NULL; fmt = format; #else /* Convert the unicode string to an ascii one */ format = PyUnicode_EncodeLocale(format_arg, "surrogateescape"); if (format == NULL) return NULL; fmt = PyBytes_AS_STRING(format); #endif #if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME) /* check that the format string contains only valid directives */ for(outbuf = strchr(fmt, '%'); outbuf != NULL; outbuf = strchr(outbuf+2, '%')) { if (outbuf[1]=='#') ++outbuf; /* not documented by python, */ if (outbuf[1]=='\0' || !strchr("aAbBcdHIjmMpSUwWxXyYzZ%", outbuf[1])) { PyErr_SetString(PyExc_ValueError, "Invalid format string"); Py_DECREF(format); return NULL; } if ((outbuf[1] == 'y') && buf.tm_year < 0) { PyErr_SetString(PyExc_ValueError, "format %y requires year >= 1900 on Windows"); Py_DECREF(format); return NULL; } } #elif (defined(_AIX) || defined(sun)) && defined(HAVE_WCSFTIME) for(outbuf = wcschr(fmt, '%'); outbuf != NULL; outbuf = wcschr(outbuf+2, '%')) { if (outbuf[1] == L'\0') break; /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0)) returns "0/" instead of "99" */ if (outbuf[1] == L'y' && buf.tm_year < 0) { PyErr_SetString(PyExc_ValueError, "format %y requires year >= 1900 on AIX"); return NULL; } } #endif fmtlen = time_strlen(fmt); /* I hate these functions that presume you know how big the output * will be ahead of time... */ for (i = 1024; ; i += i) { #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) int err; #endif outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char)); if (outbuf == NULL) { PyErr_NoMemory(); break; } buflen = format_time(outbuf, i, fmt, &buf); #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) err = errno; #endif if (buflen > 0 || i >= 256 * fmtlen) { /* 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. */ #ifdef HAVE_WCSFTIME ret = PyUnicode_FromWideChar(outbuf, buflen); #else ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, "surrogateescape"); #endif PyMem_Free(outbuf); break; } PyMem_Free(outbuf); #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) /* VisualStudio .NET 2005 does this properly */ if (buflen == 0 && err == EINVAL) { PyErr_SetString(PyExc_ValueError, "Invalid format string"); break; } #endif } #ifdef HAVE_WCSFTIME PyMem_Free(format); #else Py_DECREF(format); #endif return ret; }
PyObject * PyUnicode_ENCODE(PyObject *text) { return PyUnicode_EncodeLocale(text, _ERRORS); }