20
20
21
21
typedef struct NewNpyArrayIterObject_tag NewNpyArrayIterObject ;
22
22
23
- enum NPYITER_CONTEXT {CONTEXT_NOTENTERED , CONTEXT_INSIDE , CLOSED };
24
-
25
23
struct NewNpyArrayIterObject_tag {
26
24
PyObject_HEAD
27
25
/* The iterator */
28
26
NpyIter * iter ;
29
27
/* Flag indicating iteration started/stopped */
30
28
char started , finished ;
31
- /* iter must used as a context manager if writebackifcopy semantics used */
32
- char managed ;
29
+ /* iter operands cannot be referenced if iter is closed */
30
+ npy_bool is_closed ;
33
31
/* Child to update for nested iteration */
34
32
NewNpyArrayIterObject * nested_child ;
35
33
/* Cached values from the iterator */
@@ -89,7 +87,7 @@ npyiter_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
89
87
if (self != NULL ) {
90
88
self -> iter = NULL ;
91
89
self -> nested_child = NULL ;
92
- self -> managed = CONTEXT_NOTENTERED ;
90
+ self -> is_closed = 0 ;
93
91
}
94
92
95
93
return (PyObject * )self ;
@@ -1419,7 +1417,7 @@ static PyObject *npyiter_value_get(NewNpyArrayIterObject *self)
1419
1417
ret = npyiter_seq_item (self , 0 );
1420
1418
}
1421
1419
else {
1422
- if (self -> managed == CLOSED ) {
1420
+ if (self -> is_closed ) {
1423
1421
PyErr_SetString (PyExc_ValueError ,
1424
1422
"Iterator is closed" );
1425
1423
return NULL ;
@@ -1454,7 +1452,7 @@ static PyObject *npyiter_operands_get(NewNpyArrayIterObject *self)
1454
1452
"Iterator is invalid" );
1455
1453
return NULL ;
1456
1454
}
1457
- if (self -> managed == CLOSED ) {
1455
+ if (self -> is_closed ) {
1458
1456
PyErr_SetString (PyExc_ValueError ,
1459
1457
"Iterator is closed" );
1460
1458
return NULL ;
@@ -1489,7 +1487,7 @@ static PyObject *npyiter_itviews_get(NewNpyArrayIterObject *self)
1489
1487
return NULL ;
1490
1488
}
1491
1489
1492
- if (self -> managed == CLOSED ) {
1490
+ if (self -> is_closed ) {
1493
1491
PyErr_SetString (PyExc_ValueError ,
1494
1492
"Iterator is closed" );
1495
1493
return NULL ;
@@ -1518,7 +1516,7 @@ static PyObject *
1518
1516
npyiter_next (NewNpyArrayIterObject * self )
1519
1517
{
1520
1518
if (self -> iter == NULL || self -> iternext == NULL ||
1521
- self -> finished || ( self -> managed == CLOSED ) ) {
1519
+ self -> finished || self -> is_closed ) {
1522
1520
return NULL ;
1523
1521
}
1524
1522
@@ -1913,7 +1911,7 @@ static PyObject *npyiter_dtypes_get(NewNpyArrayIterObject *self)
1913
1911
return NULL ;
1914
1912
}
1915
1913
1916
- if (self -> managed == CLOSED ) {
1914
+ if (self -> is_closed ) {
1917
1915
PyErr_SetString (PyExc_ValueError ,
1918
1916
"Iterator is closed" );
1919
1917
return NULL ;
@@ -2015,7 +2013,7 @@ npyiter_seq_item(NewNpyArrayIterObject *self, Py_ssize_t i)
2015
2013
return NULL ;
2016
2014
}
2017
2015
2018
- if (self -> managed == CLOSED ) {
2016
+ if (self -> is_closed ) {
2019
2017
PyErr_SetString (PyExc_ValueError ,
2020
2018
"Iterator is closed" );
2021
2019
return NULL ;
@@ -2105,7 +2103,7 @@ npyiter_seq_slice(NewNpyArrayIterObject *self,
2105
2103
return NULL ;
2106
2104
}
2107
2105
2108
- if (self -> managed == CLOSED ) {
2106
+ if (self -> is_closed ) {
2109
2107
PyErr_SetString (PyExc_ValueError ,
2110
2108
"Iterator is closed" );
2111
2109
return NULL ;
@@ -2171,7 +2169,7 @@ npyiter_seq_ass_item(NewNpyArrayIterObject *self, Py_ssize_t i, PyObject *v)
2171
2169
return -1 ;
2172
2170
}
2173
2171
2174
- if (self -> managed == CLOSED ) {
2172
+ if (self -> is_closed ) {
2175
2173
PyErr_SetString (PyExc_ValueError ,
2176
2174
"Iterator is closed" );
2177
2175
return -1 ;
@@ -2251,7 +2249,7 @@ npyiter_seq_ass_slice(NewNpyArrayIterObject *self, Py_ssize_t ilow,
2251
2249
return -1 ;
2252
2250
}
2253
2251
2254
- if (self -> managed == CLOSED ) {
2252
+ if (self -> is_closed ) {
2255
2253
PyErr_SetString (PyExc_ValueError ,
2256
2254
"Iterator is closed" );
2257
2255
return -1 ;
@@ -2308,7 +2306,7 @@ npyiter_subscript(NewNpyArrayIterObject *self, PyObject *op)
2308
2306
return NULL ;
2309
2307
}
2310
2308
2311
- if (self -> managed == CLOSED ) {
2309
+ if (self -> is_closed ) {
2312
2310
PyErr_SetString (PyExc_ValueError ,
2313
2311
"Iterator is closed" );
2314
2312
return NULL ;
@@ -2363,7 +2361,7 @@ npyiter_ass_subscript(NewNpyArrayIterObject *self, PyObject *op,
2363
2361
return -1 ;
2364
2362
}
2365
2363
2366
- if (self -> managed == CLOSED ) {
2364
+ if (self -> is_closed ) {
2367
2365
PyErr_SetString (PyExc_ValueError ,
2368
2366
"Iterator is closed" );
2369
2367
return -1 ;
@@ -2403,11 +2401,10 @@ npyiter_enter(NewNpyArrayIterObject *self)
2403
2401
PyErr_SetString (PyExc_RuntimeError , "operation on non-initialized iterator" );
2404
2402
return NULL ;
2405
2403
}
2406
- if (self -> managed == CLOSED ) {
2404
+ if (self -> is_closed ) {
2407
2405
PyErr_SetString (PyExc_ValueError , "cannot reuse closed iterator" );
2408
2406
return NULL ;
2409
2407
}
2410
- self -> managed = CONTEXT_INSIDE ;
2411
2408
Py_INCREF (self );
2412
2409
return (PyObject * )self ;
2413
2410
}
@@ -2421,7 +2418,7 @@ npyiter_close(NewNpyArrayIterObject *self)
2421
2418
Py_RETURN_NONE ;
2422
2419
}
2423
2420
ret = NpyIter_Close (iter );
2424
- self -> managed = CLOSED ;
2421
+ self -> is_closed = 1 ;
2425
2422
if (ret < 0 ) {
2426
2423
return NULL ;
2427
2424
}
@@ -2431,7 +2428,6 @@ npyiter_close(NewNpyArrayIterObject *self)
2431
2428
static PyObject *
2432
2429
npyiter_exit (NewNpyArrayIterObject * self , PyObject * args )
2433
2430
{
2434
- self -> managed = CLOSED ;
2435
2431
/* even if called via exception handling, writeback any data */
2436
2432
return npyiter_close (self );
2437
2433
}
0 commit comments