@@ -432,22 +432,12 @@ inline PyObject *read_class(ReaderT *reader, TypeTreeNodeObject *node, TypeTreeR
432432 return value;
433433}
434434
435- #if PY_VERSION_HEX >= 0x030e0000
436- PyObject *get_annotations = nullptr ;
437- #endif
435+ static PyObject *_get_annotations = nullptr ;
438436
439437inline PyObject *get_annotations (PyObject *clz)
440438{
441439#if PY_VERSION_HEX >= 0x030e0000
442- if (get_annotations == nullptr )
443- {
444- // from annotationlib import get_annotations
445- PyObject *annotationlib = PyImport_ImportModule (" annotationlib" );
446- get_annotations = PyObject_GetAttrString (annotationlib, " get_annotations" );
447- Py_INCREF (get_annotations);
448- Py_DECREF (annotationlib);
449- }
450- return PyObject_CallFunctionObjArgs (get_annotations, clz, NULL );
440+ return PyObject_CallFunctionObjArgs (_get_annotations, clz, NULL );
451441#else
452442 return PyObject_GetAttrString (clz, " __annotations__" );
453443#endif
@@ -1266,5 +1256,12 @@ int add_typetreenode_to_module(PyObject *m)
12661256 return -1 ;
12671257 Py_INCREF (&TypeTreeNodeType);
12681258 PyModule_AddObject (m, " TypeTreeNode" , (PyObject *)&TypeTreeNodeType);
1259+ #if PY_VERSION_HEX >= 0x030e0000
1260+ PyObject *annotationlib = PyImport_ImportModule (" annotationlib" );
1261+ _get_annotations = PyObject_GetAttrString (annotationlib, " get_annotations" );
1262+ PyModule_AddObject (m, " _get_annotations" , _get_annotations); // steals ref
1263+ Py_INCREF (_get_annotations);
1264+ Py_DECREF (annotationlib);
1265+ #endif
12691266 return 0 ;
12701267}
0 commit comments