@@ -63,48 +63,84 @@ static PyObject *do_mkdict(const char**, va_list *, int, int, int);
6363static PyObject * do_mkvalue (const char * * , va_list * , int );
6464
6565
66+ static void
67+ do_ignore (const char * * p_format , va_list * p_va , int endchar , int n , int flags )
68+ {
69+ PyObject * v ;
70+ int i ;
71+ assert (PyErr_Occurred ());
72+ v = PyTuple_New (n );
73+ for (i = 0 ; i < n ; i ++ ) {
74+ PyObject * exception , * value , * tb , * w ;
75+
76+ PyErr_Fetch (& exception , & value , & tb );
77+ w = do_mkvalue (p_format , p_va , flags );
78+ PyErr_Restore (exception , value , tb );
79+ if (w != NULL ) {
80+ if (v != NULL ) {
81+ PyTuple_SET_ITEM (v , i , w );
82+ }
83+ else {
84+ Py_DECREF (w );
85+ }
86+ }
87+ }
88+ Py_XDECREF (v );
89+ if (* * p_format != endchar ) {
90+ PyErr_SetString (PyExc_SystemError ,
91+ "Unmatched paren in format" );
92+ return ;
93+ }
94+ if (endchar )
95+ ++ * p_format ;
96+ }
97+
6698static PyObject *
6799do_mkdict (const char * * p_format , va_list * p_va , int endchar , int n , int flags )
68100{
69101 PyObject * d ;
70102 int i ;
71- int itemfailed = 0 ;
72103 if (n < 0 )
73104 return NULL ;
74- if ((d = PyDict_New ()) == NULL )
105+ if (n % 2 ) {
106+ PyErr_SetString (PyExc_SystemError ,
107+ "Bad dict format" );
108+ do_ignore (p_format , p_va , endchar , n , flags );
75109 return NULL ;
110+ }
76111 /* Note that we can't bail immediately on error as this will leak
77112 refcounts on any 'N' arguments. */
113+ if ((d = PyDict_New ()) == NULL ) {
114+ do_ignore (p_format , p_va , endchar , n , flags );
115+ return NULL ;
116+ }
78117 for (i = 0 ; i < n ; i += 2 ) {
79118 PyObject * k , * v ;
80- int err ;
119+
81120 k = do_mkvalue (p_format , p_va , flags );
82121 if (k == NULL ) {
83- itemfailed = 1 ;
84- Py_INCREF ( Py_None );
85- k = Py_None ;
122+ do_ignore ( p_format , p_va , endchar , n - i - 1 , flags ) ;
123+ Py_DECREF ( d );
124+ return NULL ;
86125 }
87126 v = do_mkvalue (p_format , p_va , flags );
88- if (v == NULL ) {
89- itemfailed = 1 ;
90- Py_INCREF (Py_None );
91- v = Py_None ;
92- }
93- err = PyDict_SetItem (d , k , v );
94- Py_DECREF (k );
95- Py_DECREF (v );
96- if (err < 0 || itemfailed ) {
127+ if (v == NULL || PyDict_SetItem (d , k , v ) < 0 ) {
128+ do_ignore (p_format , p_va , endchar , n - i - 2 , flags );
129+ Py_DECREF (k );
130+ Py_XDECREF (v );
97131 Py_DECREF (d );
98132 return NULL ;
99133 }
134+ Py_DECREF (k );
135+ Py_DECREF (v );
100136 }
101- if (d != NULL && * * p_format != endchar ) {
137+ if (* * p_format != endchar ) {
102138 Py_DECREF (d );
103- d = NULL ;
104139 PyErr_SetString (PyExc_SystemError ,
105140 "Unmatched paren in format" );
141+ return NULL ;
106142 }
107- else if (endchar )
143+ if (endchar )
108144 ++ * p_format ;
109145 return d ;
110146}
@@ -114,29 +150,24 @@ do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
114150{
115151 PyObject * v ;
116152 int i ;
117- int itemfailed = 0 ;
118153 if (n < 0 )
119154 return NULL ;
120- v = PyList_New (n );
121- if (v == NULL )
122- return NULL ;
123155 /* Note that we can't bail immediately on error as this will leak
124156 refcounts on any 'N' arguments. */
157+ v = PyList_New (n );
158+ if (v == NULL ) {
159+ do_ignore (p_format , p_va , endchar , n , flags );
160+ return NULL ;
161+ }
125162 for (i = 0 ; i < n ; i ++ ) {
126163 PyObject * w = do_mkvalue (p_format , p_va , flags );
127164 if (w == NULL ) {
128- itemfailed = 1 ;
129- Py_INCREF ( Py_None );
130- w = Py_None ;
165+ do_ignore ( p_format , p_va , endchar , n - i - 1 , flags ) ;
166+ Py_DECREF ( v );
167+ return NULL ;
131168 }
132169 PyList_SET_ITEM (v , i , w );
133170 }
134-
135- if (itemfailed ) {
136- /* do_mkvalue() should have already set an error */
137- Py_DECREF (v );
138- return NULL ;
139- }
140171 if (* * p_format != endchar ) {
141172 Py_DECREF (v );
142173 PyErr_SetString (PyExc_SystemError ,
@@ -153,37 +184,23 @@ do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
153184{
154185 PyObject * v ;
155186 int i ;
156- int itemfailed = 0 ;
157187 if (n < 0 )
158188 return NULL ;
159- if ((v = PyTuple_New (n )) == NULL )
160- return NULL ;
161189 /* Note that we can't bail immediately on error as this will leak
162190 refcounts on any 'N' arguments. */
191+ if ((v = PyTuple_New (n )) == NULL ) {
192+ do_ignore (p_format , p_va , endchar , n , flags );
193+ return NULL ;
194+ }
163195 for (i = 0 ; i < n ; i ++ ) {
164- PyObject * w ;
165-
166- if (itemfailed ) {
167- PyObject * exception , * value , * tb ;
168- PyErr_Fetch (& exception , & value , & tb );
169- w = do_mkvalue (p_format , p_va , flags );
170- PyErr_Restore (exception , value , tb );
171- }
172- else {
173- w = do_mkvalue (p_format , p_va , flags );
174- }
196+ PyObject * w = do_mkvalue (p_format , p_va , flags );
175197 if (w == NULL ) {
176- itemfailed = 1 ;
177- Py_INCREF ( Py_None );
178- w = Py_None ;
198+ do_ignore ( p_format , p_va , endchar , n - i - 1 , flags ) ;
199+ Py_DECREF ( v );
200+ return NULL ;
179201 }
180202 PyTuple_SET_ITEM (v , i , w );
181203 }
182- if (itemfailed ) {
183- /* do_mkvalue() should have already set an error */
184- Py_DECREF (v );
185- return NULL ;
186- }
187204 if (* * p_format != endchar ) {
188205 Py_DECREF (v );
189206 PyErr_SetString (PyExc_SystemError ,
0 commit comments