77
88static char visible_length_key [] = "n_sequence_fields" ;
99static char real_length_key [] = "n_fields" ;
10+ static char unnamed_fields_key [] = "n_unnamed_fields" ;
1011
1112/* Fields with this name have only a field index, not a field name.
1213 They are only allowed for indices < n_visible_fields. */
@@ -20,6 +21,10 @@ char *PyStructSequence_UnnamedField = "unnamed field";
2021 PyDict_GetItemString((tp)->tp_dict, real_length_key))
2122#define REAL_SIZE (op ) REAL_SIZE_TP((op)->ob_type)
2223
24+ #define UNNAMED_FIELDS_TP (tp ) PyInt_AsLong( \
25+ PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key))
26+ #define UNNAMED_FIELDS (op ) UNNAMED_FIELDS_TP((op)->ob_type)
27+
2328
2429PyObject *
2530PyStructSequence_New (PyTypeObject * type )
@@ -91,7 +96,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
9196 PyObject * dict = NULL ;
9297 PyObject * ob ;
9398 PyStructSequence * res = NULL ;
94- int len , min_len , max_len , i ;
99+ int len , min_len , max_len , i , n_unnamed_fields ;
95100 static char * kwlist [] = {"sequence" , "dict" , 0 };
96101
97102 if (!PyArg_ParseTupleAndKeywords (args , kwds , "O|O:structseq" ,
@@ -115,6 +120,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
115120 len = PySequence_Fast_GET_SIZE (arg );
116121 min_len = VISIBLE_SIZE_TP (type );
117122 max_len = REAL_SIZE_TP (type );
123+ n_unnamed_fields = UNNAMED_FIELDS_TP (type );
118124
119125 if (min_len != max_len ) {
120126 if (len < min_len ) {
@@ -151,7 +157,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
151157 }
152158 for (; i < max_len ; ++ i ) {
153159 if (dict && (ob = PyDict_GetItemString (
154- dict , type -> tp_members [i ].name ))) {
160+ dict , type -> tp_members [i - n_unnamed_fields ].name ))) {
155161 }
156162 else {
157163 ob = Py_None ;
@@ -238,11 +244,12 @@ structseq_reduce(PyStructSequence* self)
238244 PyObject * tup ;
239245 PyObject * dict ;
240246 PyObject * result ;
241- long n_fields , n_visible_fields ;
247+ long n_fields , n_visible_fields , n_unnamed_fields ;
242248 int i ;
243249
244250 n_fields = REAL_SIZE (self );
245251 n_visible_fields = VISIBLE_SIZE (self );
252+ n_unnamed_fields = UNNAMED_FIELDS (self );
246253 tup = PyTuple_New (n_visible_fields );
247254 if (!tup ) {
248255 return NULL ;
@@ -260,7 +267,8 @@ structseq_reduce(PyStructSequence* self)
260267 }
261268
262269 for (; i < n_fields ; i ++ ) {
263- PyDict_SetItemString (dict , self -> ob_type -> tp_members [i ].name ,
270+ char * n = self -> ob_type -> tp_members [i - n_unnamed_fields ].name ;
271+ PyDict_SetItemString (dict , n ,
264272 self -> ob_item [i ]);
265273 }
266274
@@ -340,7 +348,7 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
340348
341349 n_unnamed_members = 0 ;
342350 for (i = 0 ; desc -> fields [i ].name != NULL ; ++ i )
343- if (desc -> fields [0 ].name == PyStructSequence_UnnamedField )
351+ if (desc -> fields [i ].name == PyStructSequence_UnnamedField )
344352 n_unnamed_members ++ ;
345353 n_members = i ;
346354
@@ -377,6 +385,8 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
377385 PyInt_FromLong ((long ) desc -> n_in_sequence ));
378386 PyDict_SetItemString (dict , real_length_key ,
379387 PyInt_FromLong ((long ) n_members ));
388+ PyDict_SetItemString (dict , unnamed_fields_key ,
389+ PyInt_FromLong ((long ) n_unnamed_members ));
380390 PyDict_SetItemString (dict , "__safe_for_unpickling__" ,
381391 PyInt_FromLong (1 ));
382392}
0 commit comments