]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Initialize UTC in the timezones cache
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 14 Nov 2021 18:30:18 +0000 (19:30 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 14 Nov 2021 19:48:53 +0000 (20:48 +0100)
Not having it might cause spurious errors in particular cases, we see it
failing sometimes in the CI on Windows (which lacks the timezone
database) on Python 3.6 with a ridiculous:

    unknown PostgreSQL timezone: 'UTC'; will use UTC

The similar cache on the Python side is primed the same way.

psycopg_c/psycopg_c/types/datetime.pyx

index 4f7d2257646e2d428c849157e0dcaf72a2f636bc..448ddd173b73d018cf83ec8607184f3f2b72cbb5 100644 (file)
@@ -1105,13 +1105,17 @@ cdef object _timezone_from_seconds(int sec, __cache={}):
     return tz
 
 
-cdef object _timezone_from_connection(pq.PGconn pgconn, __cache={}):
+cdef _timezones = {}
+_timezones[None] = timezone_utc
+_timezones[b"UTC"] = timezone_utc
+
+cdef object _timezone_from_connection(pq.PGconn pgconn):
     """Return the Python timezone info of the connection's timezone."""
     if pgconn is None:
         return timezone_utc
 
-    tzname = libpq.PQparameterStatus(pgconn._pgconn_ptr, b"TimeZone")
-    cdef PyObject *ptr = PyDict_GetItem(__cache, tzname)
+    cdef bytes tzname = libpq.PQparameterStatus(pgconn._pgconn_ptr, b"TimeZone")
+    cdef PyObject *ptr = PyDict_GetItem(_timezones, tzname)
     if ptr != NULL:
         return <object>ptr
 
@@ -1130,7 +1134,7 @@ cdef object _timezone_from_connection(pq.PGconn pgconn, __cache={}):
         )
         zi = timezone_utc
 
-    __cache[tzname] = zi
+    _timezones[tzname] = zi
     return zi