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

scandir doesn't behave like Python 3 version: lacks context manager #103

Closed
oogali opened this issue Jun 5, 2018 · 2 comments
Closed

scandir doesn't behave like Python 3 version: lacks context manager #103

oogali opened this issue Jun 5, 2018 · 2 comments

Comments

@oogali
Copy link

oogali commented Jun 5, 2018

In Python 3.5, I can call scandir via a context manager where as under Python 2.7, I cannot.

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import scandir
>>> with scandir('/etc') as d:
...   for entry in d:
...     print(entry.name)
...
emond.d
ntp-restrict.conf
periodic
manpaths
services~previous
dnsextd.conf
(and many more files)
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from scandir import scandir
>>> with scandir('/etc') as d:
...   for entry in d:
...     print entry.name
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __exit__
>>>
@oogali
Copy link
Author

oogali commented Jun 5, 2018

And to clarify, here's a stack trace from a real program I'm having an issue with:

Traceback (most recent call last):
  File "/Users/oogali/.../venv/bin/forwarder", line 11, in <module>
    load_entry_point('forwarder', 'console_scripts', 'forwarder')()
  File "/Users/oogali/.../forwarder/forwarder/__main__.py", line 12, in main
    forwarder.run()
  File "/Users/oogali/.../forwarder/forwarder/forwarder.py", line 46, in run
    for spool_file in self.gather_files():
  File "/Users/oogali/.../forwarder/forwarder/forwarder.py", line 30, in gather_files
    with scandir(directory) as spool:
AttributeError: __exit__

@benhoyt
Copy link
Owner

benhoyt commented Jun 5, 2018

Yes, you're right. This is a duplicate of #60 so I'll close this one -- basically none of the Python 3.6 additions have been back-ported to the scandir library. I don't have the time to incorporate these now (and it's becoming less and less important as Python 3.x takes over), but I'd welcome quality pull requests.

@benhoyt benhoyt closed this as completed Jun 5, 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