@@ -137,7 +137,8 @@ PyCode_NewWithAnnotations(int argcount, int posonlyargcount, int kwonlyargcount,
137137 cellvars == NULL || !PyTuple_Check (cellvars ) ||
138138 name == NULL || !PyUnicode_Check (name ) ||
139139 filename == NULL || !PyUnicode_Check (filename ) ||
140- linetable == NULL || !PyBytes_Check (linetable )) {
140+ linetable == NULL || !PyBytes_Check (linetable ) ||
141+ annotations == NULL || !(annotations == Py_None || PyTuple_Check (annotations ))) {
141142 PyErr_BadInternalCall ();
142143 return NULL ;
143144 }
@@ -165,7 +166,7 @@ PyCode_NewWithAnnotations(int argcount, int posonlyargcount, int kwonlyargcount,
165166 if (intern_string_constants (consts , NULL ) < 0 ) {
166167 return NULL ;
167168 }
168- if (annotations != Py_None && intern_string_constants (annotations , NULL ) < 0 ) {
169+ if (annotations != Py_None && intern_strings (annotations ) < 0 ) {
169170 return NULL ;
170171 }
171172
@@ -231,6 +232,13 @@ PyCode_NewWithAnnotations(int argcount, int posonlyargcount, int kwonlyargcount,
231232 cell2arg = NULL ;
232233 }
233234 }
235+
236+ /* Check if annotation len is even */
237+ if (annotations != Py_None && PyTuple_GET_SIZE (annotations ) % 2 ){
238+ PyErr_SetString (PyExc_ValueError , "code: annotations len is odd" );
239+ return NULL ;
240+ }
241+
234242 co = PyObject_New (PyCodeObject , & PyCode_Type );
235243 if (co == NULL ) {
236244 if (cell2arg )
@@ -388,7 +396,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
388396 funcname_ob , /* name */
389397 firstlineno , /* firstlineno */
390398 emptystring , /* linetable */
391- nulltuple /* annotations */
399+ Py_None /* annotations */
392400 );
393401
394402failed :
@@ -580,6 +588,7 @@ code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
580588 PyObject * ourvarnames = NULL ;
581589 PyObject * ourfreevars = NULL ;
582590 PyObject * ourcellvars = NULL ;
591+ PyObject * ourannotations = NULL ;
583592
584593 if (PySys_Audit ("code.__new__" , "OOOiiiiii" ,
585594 code , filename , name , argcount , posonlyargcount ,
@@ -632,23 +641,27 @@ code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
632641 ourcellvars = PyTuple_New (0 );
633642 if (ourcellvars == NULL )
634643 goto cleanup ;
635- if (annotations == NULL )
636- goto cleanup ;
637644 if (annotations && annotations != Py_None && !PyTuple_Check (annotations )) {
638645 PyErr_SetString (
639646 PyExc_ValueError ,
640647 "code: annotations must be tuple or None" );
641648 goto cleanup ;
642649 }
643-
650+ if (annotations && annotations != Py_None )
651+ ourannotations = validate_and_copy_tuple (annotations );
652+ else
653+ ourannotations = Py_None ;
654+ if (ourannotations == NULL )
655+ goto cleanup ;
644656
645657 co = (PyObject * )PyCode_NewWithAnnotations (argcount , posonlyargcount ,
646658 kwonlyargcount ,
647659 nlocals , stacksize , flags ,
648660 code , consts , ournames ,
649661 ourvarnames , ourfreevars ,
650662 ourcellvars , filename ,
651- name , firstlineno , linetable , annotations );
663+ name , firstlineno , linetable ,
664+ ourannotations );
652665 cleanup :
653666 Py_XDECREF (ournames );
654667 Py_XDECREF (ourvarnames );
0 commit comments