Unverified Kaydet (Commit) c510c6b8 authored tarafından Benjamin Peterson's avatar Benjamin Peterson Kaydeden (comit) GitHub

Simplify PyInit_timezone. (GH-9467)

Reduce the knotty preprocessor conditional logic, dedent unnecessarily nested
code, and handle errors properly.

The first edition of this change (afde1c1a)
failed (bpo-34715) because FreeBSD doesn't define the timezone globals. That's
why we're now checking for HAVE_DECL_TZNAME.
üst a4ae828e
...@@ -1522,7 +1522,7 @@ PyDoc_STRVAR(get_clock_info_doc, ...@@ -1522,7 +1522,7 @@ PyDoc_STRVAR(get_clock_info_doc,
\n\ \n\
Get information of the specified clock."); Get information of the specified clock.");
#if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) #ifndef HAVE_DECL_TZNAME
static void static void
get_zone(char *zone, int n, struct tm *p) get_zone(char *zone, int n, struct tm *p)
{ {
...@@ -1543,7 +1543,7 @@ get_gmtoff(time_t t, struct tm *p) ...@@ -1543,7 +1543,7 @@ get_gmtoff(time_t t, struct tm *p)
return timegm(p) - t; return timegm(p) - t;
#endif #endif
} }
#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */ #endif // !HAVE_DECL_TZNAME
static void static void
PyInit_timezone(PyObject *m) { PyInit_timezone(PyObject *m) {
...@@ -1563,7 +1563,7 @@ PyInit_timezone(PyObject *m) { ...@@ -1563,7 +1563,7 @@ PyInit_timezone(PyObject *m) {
And I'm lazy and hate C so nyer. And I'm lazy and hate C so nyer.
*/ */
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__) #ifdef HAVE_DECL_TZNAME
PyObject *otz0, *otz1; PyObject *otz0, *otz1;
tzset(); tzset();
PyModule_AddIntConstant(m, "timezone", timezone); PyModule_AddIntConstant(m, "timezone", timezone);
...@@ -1574,11 +1574,20 @@ PyInit_timezone(PyObject *m) { ...@@ -1574,11 +1574,20 @@ PyInit_timezone(PyObject *m) {
#endif #endif
PyModule_AddIntConstant(m, "daylight", daylight); PyModule_AddIntConstant(m, "daylight", daylight);
otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape"); otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
if (otz0 == NULL) {
return;
}
otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape"); otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)); if (otz1 == NULL) {
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ Py_DECREF(otz0);
{ return;
#define YEAR ((time_t)((365 * 24 + 6) * 3600)) }
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
if (tzname_obj == NULL)
return;
PyModule_AddObject(m, "tzname", tzname_obj);
#else // !HAVE_DECL_TZNAME
static const time_t YEAR = (365 * 24 + 6) * 3600;
time_t t; time_t t;
struct tm p; struct tm p;
long janzone, julyzone; long janzone, julyzone;
...@@ -1594,34 +1603,23 @@ PyInit_timezone(PyObject *m) { ...@@ -1594,34 +1603,23 @@ PyInit_timezone(PyObject *m) {
julyzone = -get_gmtoff(t, &p); julyzone = -get_gmtoff(t, &p);
julyname[9] = '\0'; julyname[9] = '\0';
if( janzone < julyzone ) { PyObject *tzname_obj;
if (janzone < julyzone) {
/* DST is reversed in the southern hemisphere */ /* DST is reversed in the southern hemisphere */
PyModule_AddIntConstant(m, "timezone", julyzone); PyModule_AddIntConstant(m, "timezone", julyzone);
PyModule_AddIntConstant(m, "altzone", janzone); PyModule_AddIntConstant(m, "altzone", janzone);
PyModule_AddIntConstant(m, "daylight", PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
janzone != julyzone); tzname_obj = Py_BuildValue("(zz)", julyname, janname);
PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)",
julyname, janname));
} else { } else {
PyModule_AddIntConstant(m, "timezone", janzone); PyModule_AddIntConstant(m, "timezone", janzone);
PyModule_AddIntConstant(m, "altzone", julyzone); PyModule_AddIntConstant(m, "altzone", julyzone);
PyModule_AddIntConstant(m, "daylight", PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
janzone != julyzone); tzname_obj = Py_BuildValue("(zz)", janname, julyname);
PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)",
janname, julyname));
} }
} if (tzname_obj == NULL)
#ifdef __CYGWIN__ return;
tzset(); PyModule_AddObject(m, "tzname", tzname_obj);
PyModule_AddIntConstant(m, "timezone", _timezone); #endif // !HAVE_DECL_TZNAME
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
PyModule_AddIntConstant(m, "daylight", _daylight);
PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#endif /* __CYGWIN__ */
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
} }
......
...@@ -396,6 +396,10 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ ...@@ -396,6 +396,10 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
/* Define to 1 if you have the <direct.h> header file. */ /* Define to 1 if you have the <direct.h> header file. */
#define HAVE_DIRECT_H 1 #define HAVE_DIRECT_H 1
/* Define to 1 if you have the declaration of `tzname', and to 0 if you don't.
*/
#define HAVE_DECL_TZNAME 1
/* Define if you have dirent.h. */ /* Define if you have dirent.h. */
/* #define DIRENT 1 */ /* #define DIRENT 1 */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment