Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trio.open_file doesn't return context manager #425

Closed
Nikratio opened this issue Jan 28, 2018 · 4 comments
Closed

trio.open_file doesn't return context manager #425

Nikratio opened this issue Jan 28, 2018 · 4 comments

Comments

@Nikratio
Copy link

The docs say:

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__


@Nikratio
Copy link
Author

$ python3 -c 'import trio; print(trio.version)'
0.3.0

@njsmith
Copy link
Member

njsmith commented Jan 28, 2018

You want async with await open_file.

@njsmith
Copy link
Member

njsmith commented Jan 29, 2018

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...

@njsmith
Copy link
Member

njsmith commented Jan 29, 2018

Well, I filed bpo-32703; I think that's all we can do here for now.

@njsmith njsmith closed this as completed Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants