@@ -251,19 +251,16 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
251
251
PyObject * errors )
252
252
/*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/
253
253
{
254
- self -> decoder = decoder ;
255
- Py_INCREF (decoder );
256
254
257
255
if (errors == NULL ) {
258
- self -> errors = _PyUnicode_FromId (& PyId_strict );
259
- if (self -> errors == NULL )
256
+ errors = _PyUnicode_FromId (& PyId_strict );
257
+ if (errors == NULL ) {
260
258
return -1 ;
259
+ }
261
260
}
262
- else {
263
- self -> errors = errors ;
264
- }
265
- Py_INCREF (self -> errors );
266
261
262
+ Py_XSETREF (self -> errors , Py_NewRef (errors ));
263
+ Py_XSETREF (self -> decoder , Py_NewRef (decoder ));
267
264
self -> translate = translate ? 1 : 0 ;
268
265
self -> seennl = 0 ;
269
266
self -> pendingcr = 0 ;
@@ -298,6 +295,13 @@ check_decoded(PyObject *decoded)
298
295
return 0 ;
299
296
}
300
297
298
+ #define CHECK_INITIALIZED_DECODER (self ) \
299
+ if (self->errors == NULL) { \
300
+ PyErr_SetString(PyExc_ValueError, \
301
+ "IncrementalNewlineDecoder.__init__() not called"); \
302
+ return NULL; \
303
+ }
304
+
301
305
#define SEEN_CR 1
302
306
#define SEEN_LF 2
303
307
#define SEEN_CRLF 4
@@ -311,11 +315,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
311
315
Py_ssize_t output_len ;
312
316
nldecoder_object * self = (nldecoder_object * ) myself ;
313
317
314
- if (self -> decoder == NULL ) {
315
- PyErr_SetString (PyExc_ValueError ,
316
- "IncrementalNewlineDecoder.__init__ not called" );
317
- return NULL ;
318
- }
318
+ CHECK_INITIALIZED_DECODER (self );
319
319
320
320
/* decode input (with the eventual \r from a previous pass) */
321
321
if (self -> decoder != Py_None ) {
@@ -529,6 +529,8 @@ _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
529
529
PyObject * buffer ;
530
530
unsigned long long flag ;
531
531
532
+ CHECK_INITIALIZED_DECODER (self );
533
+
532
534
if (self -> decoder != Py_None ) {
533
535
PyObject * state = PyObject_CallMethodNoArgs (self -> decoder ,
534
536
_PyIO_str_getstate );
@@ -573,6 +575,8 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
573
575
PyObject * buffer ;
574
576
unsigned long long flag ;
575
577
578
+ CHECK_INITIALIZED_DECODER (self );
579
+
576
580
if (!PyTuple_Check (state )) {
577
581
PyErr_SetString (PyExc_TypeError , "state argument must be a tuple" );
578
582
return NULL ;
@@ -601,6 +605,8 @@ static PyObject *
601
605
_io_IncrementalNewlineDecoder_reset_impl (nldecoder_object * self )
602
606
/*[clinic end generated code: output=32fa40c7462aa8ff input=728678ddaea776df]*/
603
607
{
608
+ CHECK_INITIALIZED_DECODER (self );
609
+
604
610
self -> seennl = 0 ;
605
611
self -> pendingcr = 0 ;
606
612
if (self -> decoder != Py_None )
@@ -612,6 +618,8 @@ _io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self)
612
618
static PyObject *
613
619
incrementalnewlinedecoder_newlines_get (nldecoder_object * self , void * context )
614
620
{
621
+ CHECK_INITIALIZED_DECODER (self );
622
+
615
623
switch (self -> seennl ) {
616
624
case SEEN_CR :
617
625
return PyUnicode_FromString ("\r" );
0 commit comments