You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Async file objects implement trio’s AsyncResource interface: you close them by calling aclose() instead of close (!!), and they can be used as async context managers.
but:
$ cat ~/test.py
import trio
async def dostuff():
async with trio.open_file('foo.txt', 'w') as fh:
await foo.write('hello, world')
trio.run(dostuff)
$ python3 ~/test.py
Traceback (most recent call last):
File "/home/nikratio/test.py", line 7, in <module>
trio.run(dostuff)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 1225, in run
return result.unwrap()
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_result.py", line 119, in unwrap
raise self.error
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 1334, in run_impl
msg = task.coro.send(next_send)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 923, in init
self.entry_queue.spawn()
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_util.py", line 109, in __aexit__
await self._agen.asend(None)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/async_generator/_impl.py", line 274, in asend
return await self._do_it(self._it.send, value)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/async_generator/_impl.py", line 290, in _do_it
return await ANextIter(self._it, start_fn, *args)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/async_generator/_impl.py", line 202, in send
return self._invoke(self._it.send, value)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/async_generator/_impl.py", line 209, in _invoke
result = fn(*args)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 318, in open_nursery
await nursery._nested_child_finished(nested_child_exc)
File "/home/nikratio/.local/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 203, in open_cancel_scope
yield scope
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_multierror.py", line 144, in __exit__
raise filtered_exc
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 203, in open_cancel_scope
yield scope
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 318, in open_nursery
await nursery._nested_child_finished(nested_child_exc)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 427, in _nested_child_finished
raise MultiError(self._pending_excs)
File "/home/nikratio/consulting/iofabric/venv/lib/python3.6/site-packages/trio/_core/_run.py", line 1334, in run_impl
msg = task.coro.send(next_send)
File "/home/nikratio/test.py", line 4, in dostuff
async with trio.open_file('foo.txt', 'w') as fh:
AttributeError: __aexit__
The text was updated successfully, but these errors were encountered:
Unfortunately it's very difficult to give a better error message here... #79 documents our attempts so far.
You ought to have at least gotten a "Warning: coroutine 'open_file' was never awaited" though. I don't know why you didn't! I don't get that warning here either. The lack of a warning seems like a CPython bug...
The docs say:
but:
The text was updated successfully, but these errors were encountered: