@@ -1935,6 +1935,34 @@ tuple_index(PyObject *self, Py_ssize_t len, PyObject *item)
1935
1935
return -1 ;
1936
1936
}
1937
1937
1938
+ // tuple(t for t in args if isinstance(t, TypeVar))
1939
+ static PyObject *
1940
+ make_parameters (PyObject * args )
1941
+ {
1942
+ Py_ssize_t len = PyTuple_GET_SIZE (args );
1943
+ PyObject * parameters = PyTuple_New (len );
1944
+ if (parameters == NULL )
1945
+ return NULL ;
1946
+ Py_ssize_t iparam = 0 ;
1947
+ for (Py_ssize_t iarg = 0 ; iarg < len ; iarg ++ ) {
1948
+ PyObject * t = PyTuple_GET_ITEM (args , iarg );
1949
+ if (is_typevar (t )) {
1950
+ if (tuple_index (parameters , iparam , t ) < 0 ) {
1951
+ Py_INCREF (t );
1952
+ PyTuple_SET_ITEM (parameters , iparam , t );
1953
+ iparam ++ ;
1954
+ }
1955
+ }
1956
+ }
1957
+ if (iparam < len ) {
1958
+ if (_PyTuple_Resize (& parameters , iparam ) < 0 ) {
1959
+ Py_XDECREF (parameters );
1960
+ return NULL ;
1961
+ }
1962
+ }
1963
+ return parameters ;
1964
+ }
1965
+
1938
1966
static PyObject *
1939
1967
ga_getitem (PyObject * self , PyObject * item )
1940
1968
{
@@ -1999,6 +2027,9 @@ static const char* const attr_exceptions[] = {
1999
2027
"__args__" ,
2000
2028
"__parameters__" ,
2001
2029
"__mro_entries__" ,
2030
+ "__reduce_ex__" , // needed so we don't look up object.__reduce_ex__
2031
+ "__reduce__" ,
2032
+ "__setstate__" ,
2002
2033
NULL ,
2003
2034
};
2004
2035
@@ -2077,10 +2108,30 @@ ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
2077
2108
self );
2078
2109
}
2079
2110
2111
+ static PyObject *
2112
+ ga_reduce (PyObject * self , PyObject * Py_UNUSED (ignored ))
2113
+ {
2114
+ gaobject * alias = (gaobject * )self ;
2115
+ return Py_BuildValue ("O(OO)" , Py_TYPE (alias ),
2116
+ alias -> origin , alias -> args );
2117
+ }
2118
+
2119
+ static PyObject *
2120
+ ga_setstate (PyObject * self , PyObject * state )
2121
+ {
2122
+ gaobject * alias = (gaobject * )self ;
2123
+ PyObject * parameters = make_parameters (alias -> args );
2124
+ Py_INCREF (parameters );
2125
+ alias -> parameters = parameters ;
2126
+ Py_RETURN_NONE ;
2127
+ }
2128
+
2080
2129
static PyMethodDef ga_methods [] = {
2081
2130
{"__mro_entries__" , ga_mro_entries , METH_O },
2082
2131
{"__instancecheck__" , ga_instancecheck , METH_O },
2083
2132
{"__subclasscheck__" , ga_subclasscheck , METH_O },
2133
+ {"__reduce__" , ga_reduce , METH_NOARGS },
2134
+ {"__setstate__" , ga_setstate , METH_O },
2084
2135
{0 }
2085
2136
};
2086
2137
@@ -2112,7 +2163,7 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2112
2163
// - __eq__
2113
2164
PyTypeObject Py_GenericAliasType = {
2114
2165
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
2115
- .tp_name = "GenericAlias" ,
2166
+ .tp_name = "types. GenericAlias" ,
2116
2167
.tp_basicsize = sizeof (gaobject ),
2117
2168
.tp_dealloc = ga_dealloc ,
2118
2169
.tp_repr = ga_repr ,
@@ -2129,34 +2180,6 @@ PyTypeObject Py_GenericAliasType = {
2129
2180
.tp_free = PyObject_GC_Del ,
2130
2181
};
2131
2182
2132
- // tuple(t for t in args if isinstance(t, TypeVar))
2133
- static PyObject *
2134
- make_parameters (PyObject * args )
2135
- {
2136
- Py_ssize_t len = PyTuple_GET_SIZE (args );
2137
- PyObject * parameters = PyTuple_New (len );
2138
- if (parameters == NULL )
2139
- return NULL ;
2140
- Py_ssize_t iparam = 0 ;
2141
- for (Py_ssize_t iarg = 0 ; iarg < len ; iarg ++ ) {
2142
- PyObject * t = PyTuple_GET_ITEM (args , iarg );
2143
- if (is_typevar (t )) {
2144
- if (tuple_index (parameters , iparam , t ) < 0 ) {
2145
- Py_INCREF (t );
2146
- PyTuple_SET_ITEM (parameters , iparam , t );
2147
- iparam ++ ;
2148
- }
2149
- }
2150
- }
2151
- if (iparam < len ) {
2152
- if (_PyTuple_Resize (& parameters , iparam ) < 0 ) {
2153
- Py_XDECREF (parameters );
2154
- return NULL ;
2155
- }
2156
- }
2157
- return parameters ;
2158
- }
2159
-
2160
2183
PyObject *
2161
2184
Py_GenericAlias (PyObject * origin , PyObject * args )
2162
2185
{
0 commit comments