2222#include <stdio.h>
2323#include <string.h>
2424
25+ /**
26+ * TODO(colinbarry) Python 3.9 doesn't support the PyDateTime_DATE_GET_TZINFO
27+ * macro. Until we require Python 3.10, this shim function does exactly the
28+ * same thing whilst keeping 3.9 compatibility.
29+ */
30+ PyObject * INTERNAL_PyDateTime_DATE_GET_TZINFO (PyObject * obj )
31+ {
32+ PyObject * tzinfo = NULL ;
33+
34+ if (PyDateTime_Check (obj )) {
35+ tzinfo = ((PyDateTime_DateTime * )obj )-> tzinfo ;
36+ } else if (PyTime_Check (obj )) {
37+ tzinfo = ((PyDateTime_Time * )obj )-> tzinfo ;
38+ }
39+
40+ return tzinfo ? tzinfo : Py_None ;
41+ }
42+
2543void py_datetime_import_init () { PyDateTime_IMPORT ; }
2644
2745PyObject * mg_list_to_py_tuple (const mg_list * list ) {
@@ -703,7 +721,7 @@ mg_date_time *py_date_time_to_mg_date_time(PyObject *obj) {
703721 }
704722 int64_t subseconds = subseconds_as_nanoseconds (obj );
705723
706- PyObject * tzinfo = PyDateTime_DATE_GET_TZINFO (obj );
724+ PyObject * tzinfo = INTERNAL_PyDateTime_DATE_GET_TZINFO (obj );
707725 if (tzinfo == Py_None ) {
708726 return NULL ;
709727 }
@@ -727,7 +745,7 @@ mg_date_time_zone_id *py_date_time_to_mg_date_time_zone_id(PyObject *obj) {
727745 }
728746 int64_t subseconds = subseconds_as_nanoseconds (obj );
729747
730- PyObject * tzinfo = PyDateTime_DATE_GET_TZINFO (obj );
748+ PyObject * tzinfo = INTERNAL_PyDateTime_DATE_GET_TZINFO (obj );
731749 if (tzinfo == Py_None ) {
732750 return NULL ;
733751 }
@@ -818,7 +836,7 @@ mg_value *py_object_to_mg_value(PyObject *object) {
818836 }
819837 ret = mg_value_make_local_time (lt );
820838 } else if (PyDateTime_CheckExact (object )) {
821- PyObject * tzinfo = PyDateTime_DATE_GET_TZINFO (object );
839+ PyObject * tzinfo = INTERNAL_PyDateTime_DATE_GET_TZINFO (object );
822840 if (tzinfo != Py_None ) {
823841 // The `timezone` may either be an offset based `datetime.timezone`, or
824842 // some kind of instance of `tzinfo`. In the case of the former, we
0 commit comments