3
3
4
4
#include "Python.h"
5
5
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
6
+ #include "pycore_long.h" // _PyLong_GetOne()
6
7
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
7
8
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
8
9
#include "pycore_pyerrors.h" // _PyErr_Occurred()
@@ -124,6 +125,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
124
125
op -> func_weakreflist = NULL ;
125
126
op -> func_module = module ;
126
127
op -> func_annotations = NULL ;
128
+ op -> func_annotate = NULL ;
127
129
op -> func_typeparams = NULL ;
128
130
op -> vectorcall = _PyFunction_Vectorcall ;
129
131
op -> func_version = 0 ;
@@ -202,6 +204,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
202
204
op -> func_weakreflist = NULL ;
203
205
op -> func_module = module ;
204
206
op -> func_annotations = NULL ;
207
+ op -> func_annotate = NULL ;
205
208
op -> func_typeparams = NULL ;
206
209
op -> vectorcall = _PyFunction_Vectorcall ;
207
210
op -> func_version = 0 ;
@@ -512,7 +515,22 @@ static PyObject *
512
515
func_get_annotation_dict (PyFunctionObject * op )
513
516
{
514
517
if (op -> func_annotations == NULL ) {
515
- return NULL ;
518
+ if (op -> func_annotate == NULL || !PyCallable_Check (op -> func_annotate )) {
519
+ Py_RETURN_NONE ;
520
+ }
521
+ PyObject * one = _PyLong_GetOne ();
522
+ PyObject * ann_dict = _PyObject_CallOneArg (op -> func_annotate , one );
523
+ if (ann_dict == NULL ) {
524
+ return NULL ;
525
+ }
526
+ if (!PyDict_Check (ann_dict )) {
527
+ PyErr_Format (PyExc_TypeError , "__annotate__ returned non-dict of type '%.100s'" ,
528
+ Py_TYPE (ann_dict )-> tp_name );
529
+ Py_DECREF (ann_dict );
530
+ return NULL ;
531
+ }
532
+ Py_XSETREF (op -> func_annotations , ann_dict );
533
+ return ann_dict ;
516
534
}
517
535
if (PyTuple_CheckExact (op -> func_annotations )) {
518
536
PyObject * ann_tuple = op -> func_annotations ;
@@ -565,7 +583,9 @@ PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
565
583
"non-dict annotations" );
566
584
return -1 ;
567
585
}
568
- Py_XSETREF (((PyFunctionObject * )op )-> func_annotations , annotations );
586
+ PyFunctionObject * func = (PyFunctionObject * )op ;
587
+ Py_XSETREF (func -> func_annotations , annotations );
588
+ Py_CLEAR (func -> func_annotate );
569
589
return 0 ;
570
590
}
571
591
@@ -763,10 +783,44 @@ func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignor
763
783
return 0 ;
764
784
}
765
785
786
+ static PyObject *
787
+ func_get_annotate (PyFunctionObject * op , void * Py_UNUSED (ignored ))
788
+ {
789
+ if (op -> func_annotate == NULL ) {
790
+ Py_RETURN_NONE ;
791
+ }
792
+ return Py_NewRef (op -> func_annotate );
793
+ }
794
+
795
+ static int
796
+ func_set_annotate (PyFunctionObject * op , PyObject * value , void * Py_UNUSED (ignored ))
797
+ {
798
+ if (value == NULL ) {
799
+ PyErr_SetString (PyExc_TypeError ,
800
+ "__annotate__ cannot be deleted" );
801
+ return -1 ;
802
+ }
803
+ if (Py_IsNone (value )) {
804
+ Py_XSETREF (op -> func_annotate , value );
805
+ return 0 ;
806
+ }
807
+ else if (PyCallable_Check (value )) {
808
+ Py_XSETREF (op -> func_annotate , Py_XNewRef (value ));
809
+ Py_CLEAR (op -> func_annotations );
810
+ return 0 ;
811
+ }
812
+ else {
813
+ PyErr_SetString (PyExc_TypeError ,
814
+ "__annotate__ must be callable or None" );
815
+ return -1 ;
816
+ }
817
+ }
818
+
766
819
static PyObject *
767
820
func_get_annotations (PyFunctionObject * op , void * Py_UNUSED (ignored ))
768
821
{
769
- if (op -> func_annotations == NULL ) {
822
+ if (op -> func_annotations == NULL &&
823
+ (op -> func_annotate == NULL || !PyCallable_Check (op -> func_annotate ))) {
770
824
op -> func_annotations = PyDict_New ();
771
825
if (op -> func_annotations == NULL )
772
826
return NULL ;
@@ -789,6 +843,7 @@ func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(igno
789
843
return -1 ;
790
844
}
791
845
Py_XSETREF (op -> func_annotations , Py_XNewRef (value ));
846
+ Py_CLEAR (op -> func_annotate );
792
847
return 0 ;
793
848
}
794
849
@@ -836,6 +891,7 @@ static PyGetSetDef func_getsetlist[] = {
836
891
(setter )func_set_kwdefaults },
837
892
{"__annotations__" , (getter )func_get_annotations ,
838
893
(setter )func_set_annotations },
894
+ {"__annotate__" , (getter )func_get_annotate , (setter )func_set_annotate },
839
895
{"__dict__" , PyObject_GenericGetDict , PyObject_GenericSetDict },
840
896
{"__name__" , (getter )func_get_name , (setter )func_set_name },
841
897
{"__qualname__" , (getter )func_get_qualname , (setter )func_set_qualname },
@@ -972,6 +1028,7 @@ func_clear(PyFunctionObject *op)
972
1028
Py_CLEAR (op -> func_dict );
973
1029
Py_CLEAR (op -> func_closure );
974
1030
Py_CLEAR (op -> func_annotations );
1031
+ Py_CLEAR (op -> func_annotate );
975
1032
Py_CLEAR (op -> func_typeparams );
976
1033
// Don't Py_CLEAR(op->func_code), since code is always required
977
1034
// to be non-NULL. Similarly, name and qualname shouldn't be NULL.
@@ -1028,6 +1085,7 @@ func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
1028
1085
Py_VISIT (f -> func_dict );
1029
1086
Py_VISIT (f -> func_closure );
1030
1087
Py_VISIT (f -> func_annotations );
1088
+ Py_VISIT (f -> func_annotate );
1031
1089
Py_VISIT (f -> func_typeparams );
1032
1090
Py_VISIT (f -> func_qualname );
1033
1091
return 0 ;
0 commit comments