Skip to content

Commit

Permalink
Create io.BytesIO() in destructor of test_io.py
Browse files Browse the repository at this point in the history
WrapperTest.test_create_at_shutdown_with_encoding fails with something
like:

```
Exception ignored in: <function C.__del__ at 0x101895ae0>
Traceback (most recent call last):
  File "x.py", line 10, in __del__
  File "/Users/sgross/Projects/nogil/Lib/_pyio.py", line 2030, in __init__
  File "/Users/sgross/Projects/nogil/Lib/_pyio.py", line 1021, in seekable
ValueError: I/O operation on closed file.
```

The problem is that the GC calls the finalizer on `self.buf` before the
`__del__` on `class C`. I don't think Python guarantees any ordering on
destructors.
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent dd9b784 commit a595fc1
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3542,10 +3542,8 @@ def _check_create_at_shutdown(self, **kwargs):
codecs.lookup('utf-8')
class C:
def __init__(self):
self.buf = io.BytesIO()
def __del__(self):
io.TextIOWrapper(self.buf, **{kwargs})
io.TextIOWrapper(io.BytesIO(), **{kwargs})
print("ok")
c = C()
""".format(iomod=iomod, kwargs=kwargs)
Expand Down

0 comments on commit a595fc1

Please sign in to comment.