@@ -454,37 +454,11 @@ inline PyObject *read_class(ReaderT *reader, TypeTreeNodeObject *node, TypeTreeR
454454 return value;
455455}
456456
457- #if PY_VERSION_HEX >= 0x030e0000
458- PyObject *get_annotations = nullptr ;
459- #endif
460-
461- inline PyObject *get_annotations (PyObject *clz)
462- {
463- #if PY_VERSION_HEX >= 0x030e0000
464- if (get_annotations == nullptr )
465- {
466- // from annotationlib import get_annotations
467- PyObject *annotationlib = PyImport_ImportModule (" annotationlib" );
468- get_annotations = PyObject_GetAttrString (annotationlib, " get_annotations" );
469- Py_INCREF (get_annotations);
470- Py_DECREF (annotationlib);
471- }
472- return PyObject_CallFunctionObjArgs (get_annotations, clz, NULL );
473- #else
474- return PyObject_GetAttrString (clz, " __annotations__" );
475- #endif
476- }
477-
478457inline PyObject *parse_class (PyObject *kwargs, TypeTreeNodeObject *node, TypeTreeReaderConfigT *config)
479458{
480459 PyObject *instance = nullptr ;
481460 PyObject *clz = nullptr ;
482461 PyObject *args = PyTuple_New (0 );
483- PyObject *annotations = nullptr ;
484- PyObject *extras = nullptr ;
485- // dict iterator values
486- PyObject *key, *value = nullptr ;
487- Py_ssize_t pos;
488462
489463 if (node->_data_type == NodeDataType::PPtr)
490464 {
@@ -518,63 +492,16 @@ inline PyObject *parse_class(PyObject *kwargs, TypeTreeNodeObject *node, TypeTre
518492 }
519493 PyErr_Clear ();
520494
521- // possibly extra fields
522- annotations = get_annotations (clz);
523- if (annotations == nullptr )
524- {
525- PyErr_SetString (PyExc_ValueError, " Failed to get annotations" );
526- goto PARSE_CLASS_CLEANUP;
527- }
528- extras = PyDict_New ();
529- for (int i = 0 ; i < PyList_GET_SIZE (node->m_Children ); i++)
530- {
531- TypeTreeNodeObject *child = (TypeTreeNodeObject *)PyList_GET_ITEM (node->m_Children , i); // - borrowed ref +/- 0
532- if (PyDict_Contains (annotations, child->_clean_name ) == 1 )
533- {
534- continue ;
535- }
536- PyObject *extra_value = PyDict_GetItem (kwargs, child->_clean_name ); // - borrowed ref +/- 0
537- PyDict_SetItem (extras, child->_clean_name , extra_value); // +1
538- PyDict_DelItem (kwargs, child->_clean_name ); // -1
539- }
540-
541- if (PyDict_Size (extras) == 0 )
542- {
543- Py_DECREF (clz); // 1->0
544- clz = PyObject_GetAttrString (config->classes , " UnknownObject" ); // 0->1
545- PyDict_SetItemString (kwargs, " __node__" , (PyObject *)node);
546- }
547-
548- instance = PyObject_Call (clz, args, kwargs);
549- if (instance != nullptr )
550- {
551- pos = 0 ;
552- while (PyDict_Next (extras, &pos, &key, &value))
553- {
554- PyObject_GenericSetAttr (instance, key, value);
555- }
556- goto PARSE_CLASS_CLEANUP;
557- }
558- PyErr_Clear ();
559-
560- // if we still failed to create an instance, fallback to UnknownObject
495+ // fallback to UnknownObject
561496 Py_DECREF (clz);
562497 clz = PyObject_GetAttrString (config->classes , " UnknownObject" );
563498 PyDict_SetItemString (kwargs, " __node__" , (PyObject *)node);
564- // merge extras back into kwargs
565- pos = 0 ;
566- while (PyDict_Next (extras, &pos, &key, &value))
567- {
568- PyDict_SetItem (kwargs, key, value);
569- }
570499 instance = PyObject_Call (clz, args, kwargs);
571500
572501PARSE_CLASS_CLEANUP:
573502 Py_DECREF (args);
574503 Py_DECREF (kwargs);
575504 Py_XDECREF (clz);
576- Py_XDECREF (annotations);
577- Py_XDECREF (extras);
578505 return instance;
579506}
580507
0 commit comments