[Python-checkins] Simplify PyInit_timezone. (GH-9467)

Benjamin Peterson webhook-mailer at python.org
Thu Sep 20 22:52:22 EDT 2018


https://github.com/python/cpython/commit/c510c6b8b60f211793e0b84c317ea6974e8a6153
commit: c510c6b8b60f211793e0b84c317ea6974e8a6153
branch: master
author: Benjamin Peterson <benjamin at python.org>
committer: GitHub <noreply at github.com>
date: 2018年09月20日T19:52:18-07:00
summary:
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 (afde1c1a05cc8a1e8adf6403c451f6708509a605)
failed (bpo-34715) because FreeBSD doesn't define the timezone globals. That's
why we're now checking for HAVE_DECL_TZNAME.
files:
M Modules/timemodule.c
M PC/pyconfig.h
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 1a4cff23d65e..d162d93c9c63 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -1522,7 +1522,7 @@ PyDoc_STRVAR(get_clock_info_doc,
 \n\
 Get information of the specified clock.");
 
-#if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__)
+#ifndef HAVE_DECL_TZNAME
 static void
 get_zone(char *zone, int n, struct tm *p)
 {
@@ -1543,7 +1543,7 @@ get_gmtoff(time_t t, struct tm *p)
 return timegm(p) - t;
 #endif
 }
-#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */
+#endif // !HAVE_DECL_TZNAME
 
 static void
 PyInit_timezone(PyObject *m) {
@@ -1563,7 +1563,7 @@ PyInit_timezone(PyObject *m) {
 
 And I'm lazy and hate C so nyer.
 */
-#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
+#ifdef HAVE_DECL_TZNAME
 PyObject *otz0, *otz1;
 tzset();
 PyModule_AddIntConstant(m, "timezone", timezone);
@@ -1574,54 +1574,52 @@ PyInit_timezone(PyObject *m) {
 #endif
 PyModule_AddIntConstant(m, "daylight", daylight);
 otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
- otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
- PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
-#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
- {
-#define YEAR ((time_t)((365 * 24 + 6) * 3600))
- time_t t;
- struct tm p;
- long janzone, julyzone;
- char janname[10], julyname[10];
- t = (time((time_t *)0) / YEAR) * YEAR;
- _PyTime_localtime(t, &p);
- get_zone(janname, 9, &p);
- janzone = -get_gmtoff(t, &p);
- janname[9] = '0円';
- t += YEAR/2;
- _PyTime_localtime(t, &p);
- get_zone(julyname, 9, &p);
- julyzone = -get_gmtoff(t, &p);
- julyname[9] = '0円';
-
- if( janzone < julyzone ) {
- /* DST is reversed in the southern hemisphere */
- PyModule_AddIntConstant(m, "timezone", julyzone);
- PyModule_AddIntConstant(m, "altzone", janzone);
- PyModule_AddIntConstant(m, "daylight",
- janzone != julyzone);
- PyModule_AddObject(m, "tzname",
- Py_BuildValue("(zz)",
- julyname, janname));
- } else {
- PyModule_AddIntConstant(m, "timezone", janzone);
- PyModule_AddIntConstant(m, "altzone", julyzone);
- PyModule_AddIntConstant(m, "daylight",
- janzone != julyzone);
- PyModule_AddObject(m, "tzname",
- Py_BuildValue("(zz)",
- janname, julyname));
- }
+ if (otz0 == NULL) {
+ return;
 }
-#ifdef __CYGWIN__
- tzset();
- PyModule_AddIntConstant(m, "timezone", _timezone);
- 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__*/
+ otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
+ if (otz1 == NULL) {
+ Py_DECREF(otz0);
+ return;
+ }
+ 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;
+ struct tm p;
+ long janzone, julyzone;
+ char janname[10], julyname[10];
+ t = (time((time_t *)0) / YEAR) * YEAR;
+ _PyTime_localtime(t, &p);
+ get_zone(janname, 9, &p);
+ janzone = -get_gmtoff(t, &p);
+ janname[9] = '0円';
+ t += YEAR/2;
+ _PyTime_localtime(t, &p);
+ get_zone(julyname, 9, &p);
+ julyzone = -get_gmtoff(t, &p);
+ julyname[9] = '0円';
+
+ PyObject *tzname_obj;
+ if (janzone < julyzone) {
+ /* DST is reversed in the southern hemisphere */
+ PyModule_AddIntConstant(m, "timezone", julyzone);
+ PyModule_AddIntConstant(m, "altzone", janzone);
+ PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
+ tzname_obj = Py_BuildValue("(zz)", julyname, janname);
+ } else {
+ PyModule_AddIntConstant(m, "timezone", janzone);
+ PyModule_AddIntConstant(m, "altzone", julyzone);
+ PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
+ tzname_obj = Py_BuildValue("(zz)", janname, julyname);
+ }
+ if (tzname_obj == NULL)
+ return;
+ PyModule_AddObject(m, "tzname", tzname_obj);
+#endif // !HAVE_DECL_TZNAME
 }
 
 
diff --git a/PC/pyconfig.h b/PC/pyconfig.h
index 388a3c64f18c..b5e3452460c4 100644
--- a/PC/pyconfig.h
+++ b/PC/pyconfig.h
@@ -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 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 DIRENT 1 */
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /