@@ -1219,25 +1219,27 @@ textiowrapper_closed_get(textio *self, void *context);
1219
1219
1220
1220
#define CHECK_INITIALIZED (self ) \
1221
1221
if (self->ok <= 0) { \
1222
- if (self->detached) { \
1223
- PyErr_SetString(PyExc_ValueError, \
1224
- "underlying buffer has been detached"); \
1225
- } else { \
1226
- PyErr_SetString(PyExc_ValueError, \
1227
- "I/O operation on uninitialized object"); \
1228
- } \
1222
+ PyErr_SetString(PyExc_ValueError, \
1223
+ "I/O operation on uninitialized object"); \
1224
+ return NULL; \
1225
+ }
1226
+
1227
+ #define CHECK_ATTACHED (self ) \
1228
+ CHECK_INITIALIZED(self); \
1229
+ if (self->detached) { \
1230
+ PyErr_SetString(PyExc_ValueError, \
1231
+ "underlying buffer has been detached"); \
1229
1232
return NULL; \
1230
1233
}
1231
1234
1232
- #define CHECK_INITIALIZED_INT (self ) \
1235
+ #define CHECK_ATTACHED_INT (self ) \
1233
1236
if (self->ok <= 0) { \
1234
- if (self->detached) { \
1235
- PyErr_SetString(PyExc_ValueError, \
1236
- "underlying buffer has been detached"); \
1237
- } else { \
1238
- PyErr_SetString(PyExc_ValueError, \
1239
- "I/O operation on uninitialized object"); \
1240
- } \
1237
+ PyErr_SetString(PyExc_ValueError, \
1238
+ "I/O operation on uninitialized object"); \
1239
+ return -1; \
1240
+ } else if (self->detached) { \
1241
+ PyErr_SetString(PyExc_ValueError, \
1242
+ "underlying buffer has been detached"); \
1241
1243
return -1; \
1242
1244
}
1243
1245
@@ -1246,15 +1248,14 @@ static PyObject *
1246
1248
textiowrapper_detach (textio * self )
1247
1249
{
1248
1250
PyObject * buffer , * res ;
1249
- CHECK_INITIALIZED (self );
1251
+ CHECK_ATTACHED (self );
1250
1252
res = PyObject_CallMethodObjArgs ((PyObject * )self , _PyIO_str_flush , NULL );
1251
1253
if (res == NULL )
1252
1254
return NULL ;
1253
1255
Py_DECREF (res );
1254
1256
buffer = self -> buffer ;
1255
1257
self -> buffer = NULL ;
1256
1258
self -> detached = 1 ;
1257
- self -> ok = 0 ;
1258
1259
return buffer ;
1259
1260
}
1260
1261
@@ -1299,7 +1300,7 @@ textiowrapper_write(textio *self, PyObject *args)
1299
1300
int haslf = 0 ;
1300
1301
int needflush = 0 , text_needflush = 0 ;
1301
1302
1302
- CHECK_INITIALIZED (self );
1303
+ CHECK_ATTACHED (self );
1303
1304
1304
1305
if (!PyArg_ParseTuple (args , "U:write" , & text )) {
1305
1306
return NULL ;
@@ -1556,7 +1557,7 @@ textiowrapper_read(textio *self, PyObject *args)
1556
1557
Py_ssize_t n = -1 ;
1557
1558
PyObject * result = NULL , * chunks = NULL ;
1558
1559
1559
- CHECK_INITIALIZED (self );
1560
+ CHECK_ATTACHED (self );
1560
1561
1561
1562
if (!PyArg_ParseTuple (args , "|O&:read" , & _PyIO_ConvertSsize_t , & n ))
1562
1563
return NULL ;
@@ -1931,7 +1932,7 @@ textiowrapper_readline(textio *self, PyObject *args)
1931
1932
{
1932
1933
Py_ssize_t limit = -1 ;
1933
1934
1934
- CHECK_INITIALIZED (self );
1935
+ CHECK_ATTACHED (self );
1935
1936
if (!PyArg_ParseTuple (args , "|n:readline" , & limit )) {
1936
1937
return NULL ;
1937
1938
}
@@ -2069,7 +2070,7 @@ textiowrapper_seek(textio *self, PyObject *args)
2069
2070
PyObject * res ;
2070
2071
int cmp ;
2071
2072
2072
- CHECK_INITIALIZED (self );
2073
+ CHECK_ATTACHED (self );
2073
2074
2074
2075
if (!PyArg_ParseTuple (args , "O|i:seek" , & cookieObj , & whence ))
2075
2076
return NULL ;
@@ -2249,7 +2250,7 @@ textiowrapper_tell(textio *self, PyObject *args)
2249
2250
Py_ssize_t dec_buffer_len ;
2250
2251
int dec_flags ;
2251
2252
2252
- CHECK_INITIALIZED (self );
2253
+ CHECK_ATTACHED (self );
2253
2254
CHECK_CLOSED (self );
2254
2255
2255
2256
if (!self -> seekable ) {
@@ -2452,7 +2453,7 @@ textiowrapper_truncate(textio *self, PyObject *args)
2452
2453
PyObject * pos = Py_None ;
2453
2454
PyObject * res ;
2454
2455
2455
- CHECK_INITIALIZED (self )
2456
+ CHECK_ATTACHED (self )
2456
2457
if (!PyArg_ParseTuple (args , "|O:truncate" , & pos )) {
2457
2458
return NULL ;
2458
2459
}
@@ -2475,9 +2476,10 @@ textiowrapper_repr(textio *self)
2475
2476
res = PyUnicode_FromString ("<_io.TextIOWrapper" );
2476
2477
if (res == NULL )
2477
2478
return NULL ;
2479
+
2478
2480
nameobj = _PyObject_GetAttrId ((PyObject * ) self , & PyId_name );
2479
2481
if (nameobj == NULL ) {
2480
- if (PyErr_ExceptionMatches (PyExc_AttributeError ))
2482
+ if (PyErr_ExceptionMatches (PyExc_Exception ))
2481
2483
PyErr_Clear ();
2482
2484
else
2483
2485
goto error ;
@@ -2493,7 +2495,7 @@ textiowrapper_repr(textio *self)
2493
2495
}
2494
2496
modeobj = _PyObject_GetAttrId ((PyObject * ) self , & PyId_mode );
2495
2497
if (modeobj == NULL ) {
2496
- if (PyErr_ExceptionMatches (PyExc_AttributeError ))
2498
+ if (PyErr_ExceptionMatches (PyExc_Exception ))
2497
2499
PyErr_Clear ();
2498
2500
else
2499
2501
goto error ;
@@ -2522,35 +2524,35 @@ textiowrapper_repr(textio *self)
2522
2524
static PyObject *
2523
2525
textiowrapper_fileno (textio * self , PyObject * args )
2524
2526
{
2525
- CHECK_INITIALIZED (self );
2527
+ CHECK_ATTACHED (self );
2526
2528
return _PyObject_CallMethodId (self -> buffer , & PyId_fileno , NULL );
2527
2529
}
2528
2530
2529
2531
static PyObject *
2530
2532
textiowrapper_seekable (textio * self , PyObject * args )
2531
2533
{
2532
- CHECK_INITIALIZED (self );
2534
+ CHECK_ATTACHED (self );
2533
2535
return _PyObject_CallMethodId (self -> buffer , & PyId_seekable , NULL );
2534
2536
}
2535
2537
2536
2538
static PyObject *
2537
2539
textiowrapper_readable (textio * self , PyObject * args )
2538
2540
{
2539
- CHECK_INITIALIZED (self );
2541
+ CHECK_ATTACHED (self );
2540
2542
return _PyObject_CallMethodId (self -> buffer , & PyId_readable , NULL );
2541
2543
}
2542
2544
2543
2545
static PyObject *
2544
2546
textiowrapper_writable (textio * self , PyObject * args )
2545
2547
{
2546
- CHECK_INITIALIZED (self );
2548
+ CHECK_ATTACHED (self );
2547
2549
return _PyObject_CallMethodId (self -> buffer , & PyId_writable , NULL );
2548
2550
}
2549
2551
2550
2552
static PyObject *
2551
2553
textiowrapper_isatty (textio * self , PyObject * args )
2552
2554
{
2553
- CHECK_INITIALIZED (self );
2555
+ CHECK_ATTACHED (self );
2554
2556
return _PyObject_CallMethodId (self -> buffer , & PyId_isatty , NULL );
2555
2557
}
2556
2558
@@ -2565,7 +2567,7 @@ textiowrapper_getstate(textio *self, PyObject *args)
2565
2567
static PyObject *
2566
2568
textiowrapper_flush (textio * self , PyObject * args )
2567
2569
{
2568
- CHECK_INITIALIZED (self );
2570
+ CHECK_ATTACHED (self );
2569
2571
CHECK_CLOSED (self );
2570
2572
self -> telling = self -> seekable ;
2571
2573
if (_textiowrapper_writeflush (self ) < 0 )
@@ -2578,7 +2580,7 @@ textiowrapper_close(textio *self, PyObject *args)
2578
2580
{
2579
2581
PyObject * res ;
2580
2582
int r ;
2581
- CHECK_INITIALIZED (self );
2583
+ CHECK_ATTACHED (self );
2582
2584
2583
2585
res = textiowrapper_closed_get (self , NULL );
2584
2586
if (res == NULL )
@@ -2620,7 +2622,7 @@ textiowrapper_iternext(textio *self)
2620
2622
{
2621
2623
PyObject * line ;
2622
2624
2623
- CHECK_INITIALIZED (self );
2625
+ CHECK_ATTACHED (self );
2624
2626
2625
2627
self -> telling = 0 ;
2626
2628
if (Py_TYPE (self ) == & PyTextIOWrapper_Type ) {
@@ -2656,22 +2658,22 @@ textiowrapper_iternext(textio *self)
2656
2658
static PyObject *
2657
2659
textiowrapper_name_get (textio * self , void * context )
2658
2660
{
2659
- CHECK_INITIALIZED (self );
2661
+ CHECK_ATTACHED (self );
2660
2662
return _PyObject_GetAttrId (self -> buffer , & PyId_name );
2661
2663
}
2662
2664
2663
2665
static PyObject *
2664
2666
textiowrapper_closed_get (textio * self , void * context )
2665
2667
{
2666
- CHECK_INITIALIZED (self );
2668
+ CHECK_ATTACHED (self );
2667
2669
return PyObject_GetAttr (self -> buffer , _PyIO_str_closed );
2668
2670
}
2669
2671
2670
2672
static PyObject *
2671
2673
textiowrapper_newlines_get (textio * self , void * context )
2672
2674
{
2673
2675
PyObject * res ;
2674
- CHECK_INITIALIZED (self );
2676
+ CHECK_ATTACHED (self );
2675
2677
if (self -> decoder == NULL )
2676
2678
Py_RETURN_NONE ;
2677
2679
res = PyObject_GetAttr (self -> decoder , _PyIO_str_newlines );
@@ -2697,15 +2699,15 @@ textiowrapper_errors_get(textio *self, void *context)
2697
2699
static PyObject *
2698
2700
textiowrapper_chunk_size_get (textio * self , void * context )
2699
2701
{
2700
- CHECK_INITIALIZED (self );
2702
+ CHECK_ATTACHED (self );
2701
2703
return PyLong_FromSsize_t (self -> chunk_size );
2702
2704
}
2703
2705
2704
2706
static int
2705
2707
textiowrapper_chunk_size_set (textio * self , PyObject * arg , void * context )
2706
2708
{
2707
2709
Py_ssize_t n ;
2708
- CHECK_INITIALIZED_INT (self );
2710
+ CHECK_ATTACHED_INT (self );
2709
2711
n = PyNumber_AsSsize_t (arg , PyExc_ValueError );
2710
2712
if (n == -1 && PyErr_Occurred ())
2711
2713
return -1 ;
0 commit comments