@@ -231,15 +231,16 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
231
231
PyObject * errors )
232
232
/*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/
233
233
{
234
- self -> decoder = Py_NewRef (decoder );
235
234
236
235
if (errors == NULL ) {
237
- self -> errors = Py_NewRef (& _Py_ID (strict ));
236
+ errors = Py_NewRef (& _Py_ID (strict ));
238
237
}
239
238
else {
240
- self -> errors = Py_NewRef (errors );
239
+ errors = Py_NewRef (errors );
241
240
}
242
241
242
+ Py_XSETREF (self -> errors , errors );
243
+ Py_XSETREF (self -> decoder , Py_NewRef (decoder ));
243
244
self -> translate = translate ? 1 : 0 ;
244
245
self -> seennl = 0 ;
245
246
self -> pendingcr = 0 ;
@@ -274,6 +275,13 @@ check_decoded(PyObject *decoded)
274
275
return 0 ;
275
276
}
276
277
278
+ #define CHECK_INITIALIZED_DECODER (self ) \
279
+ if (self->errors == NULL) { \
280
+ PyErr_SetString(PyExc_ValueError, \
281
+ "IncrementalNewlineDecoder.__init__() not called"); \
282
+ return NULL; \
283
+ }
284
+
277
285
#define SEEN_CR 1
278
286
#define SEEN_LF 2
279
287
#define SEEN_CRLF 4
@@ -287,11 +295,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
287
295
Py_ssize_t output_len ;
288
296
nldecoder_object * self = (nldecoder_object * ) myself ;
289
297
290
- if (self -> decoder == NULL ) {
291
- PyErr_SetString (PyExc_ValueError ,
292
- "IncrementalNewlineDecoder.__init__ not called" );
293
- return NULL ;
294
- }
298
+ CHECK_INITIALIZED_DECODER (self );
295
299
296
300
/* decode input (with the eventual \r from a previous pass) */
297
301
if (self -> decoder != Py_None ) {
@@ -502,6 +506,8 @@ _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
502
506
PyObject * buffer ;
503
507
unsigned long long flag ;
504
508
509
+ CHECK_INITIALIZED_DECODER (self );
510
+
505
511
if (self -> decoder != Py_None ) {
506
512
PyObject * state = PyObject_CallMethodNoArgs (self -> decoder ,
507
513
& _Py_ID (getstate ));
@@ -546,6 +552,8 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
546
552
PyObject * buffer ;
547
553
unsigned long long flag ;
548
554
555
+ CHECK_INITIALIZED_DECODER (self );
556
+
549
557
if (!PyTuple_Check (state )) {
550
558
PyErr_SetString (PyExc_TypeError , "state argument must be a tuple" );
551
559
return NULL ;
@@ -576,6 +584,8 @@ static PyObject *
576
584
_io_IncrementalNewlineDecoder_reset_impl (nldecoder_object * self )
577
585
/*[clinic end generated code: output=32fa40c7462aa8ff input=728678ddaea776df]*/
578
586
{
587
+ CHECK_INITIALIZED_DECODER (self );
588
+
579
589
self -> seennl = 0 ;
580
590
self -> pendingcr = 0 ;
581
591
if (self -> decoder != Py_None )
@@ -587,6 +597,8 @@ _io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self)
587
597
static PyObject *
588
598
incrementalnewlinedecoder_newlines_get (nldecoder_object * self , void * context )
589
599
{
600
+ CHECK_INITIALIZED_DECODER (self );
601
+
590
602
switch (self -> seennl ) {
591
603
case SEEN_CR :
592
604
return PyUnicode_FromString ("\r" );
0 commit comments