@@ -257,7 +257,7 @@ static PyArray_Descr *
257257_convert_from_tuple (PyObject * obj , int align )
258258{
259259 if (PyTuple_GET_SIZE (obj ) != 2 ) {
260- PyErr_Format (PyExc_TypeError ,
260+ PyErr_Format (PyExc_TypeError ,
261261 "Tuple must have size 2, but has size %zd" ,
262262 PyTuple_GET_SIZE (obj ));
263263 return NULL ;
@@ -449,8 +449,8 @@ _convert_from_array_descr(PyObject *obj, int align)
449449 for (int i = 0 ; i < n ; i ++ ) {
450450 PyObject * item = PyList_GET_ITEM (obj , i );
451451 if (!PyTuple_Check (item ) || (PyTuple_GET_SIZE (item ) < 2 )) {
452- PyErr_Format (PyExc_TypeError ,
453- "Field elements must be 2- or 3-tuples, got '%R'" ,
452+ PyErr_Format (PyExc_TypeError ,
453+ "Field elements must be 2- or 3-tuples, got '%R'" ,
454454 item );
455455 goto fail ;
456456 }
@@ -461,7 +461,7 @@ _convert_from_array_descr(PyObject *obj, int align)
461461 }
462462 else if (PyTuple_Check (name )) {
463463 if (PyTuple_GET_SIZE (name ) != 2 ) {
464- PyErr_Format (PyExc_TypeError ,
464+ PyErr_Format (PyExc_TypeError ,
465465 "If a tuple, the first element of a field tuple must have "
466466 "two elements, not %zd" ,
467467 PyTuple_GET_SIZE (name ));
@@ -475,7 +475,7 @@ _convert_from_array_descr(PyObject *obj, int align)
475475 }
476476 }
477477 else {
478- PyErr_SetString (PyExc_TypeError ,
478+ PyErr_SetString (PyExc_TypeError ,
479479 "First element of field tuple is "
480480 "neither a tuple nor str" );
481481 goto fail ;
@@ -3101,6 +3101,30 @@ arraydescr_newbyteorder(PyArray_Descr *self, PyObject *args)
31013101 return (PyObject * )PyArray_DescrNewByteorder (self , endian );
31023102}
31033103
3104+ static PyObject *
3105+ arraydescr_class_getitem (PyObject * cls , PyObject * args )
3106+ {
3107+ PyObject * generic_alias ;
3108+
3109+ #ifdef Py_GENERICALIASOBJECT_H
3110+ Py_ssize_t args_len ;
3111+
3112+ args_len = PyTuple_Check (args ) ? PyTuple_Size (args ) : 1 ;
3113+ if (args_len != 1 ) {
3114+ return PyErr_Format (PyExc_TypeError ,
3115+ "Too %s arguments for %s" ,
3116+ args_len > 1 ? "many" : "few" ,
3117+ ((PyTypeObject * )cls )-> tp_name );
3118+ }
3119+ generic_alias = Py_GenericAlias (cls , args );
3120+ #else
3121+ PyErr_SetString (PyExc_TypeError ,
3122+ "Type subscription requires python >= 3.9" );
3123+ generic_alias = NULL ;
3124+ #endif
3125+ return generic_alias ;
3126+ }
3127+
31043128static PyMethodDef arraydescr_methods [] = {
31053129 /* for pickling */
31063130 {"__reduce__" ,
@@ -3112,6 +3136,10 @@ static PyMethodDef arraydescr_methods[] = {
31123136 {"newbyteorder" ,
31133137 (PyCFunction )arraydescr_newbyteorder ,
31143138 METH_VARARGS , NULL },
3139+ /* for typing; requires python >= 3.9 */
3140+ {"__class_getitem__" ,
3141+ (PyCFunction )arraydescr_class_getitem ,
3142+ METH_CLASS | METH_O , NULL },
31153143 {NULL , NULL , 0 , NULL } /* sentinel */
31163144};
31173145
0 commit comments