matiasb / python-unrar

A ctypes wrapper for UnRAR library, plus a rarfile module on top of it.
http://python-unrar.readthedocs.org/en/latest/
GNU General Public License v3.0
74 stars 30 forks source link

Implement 'with' control-flow structure requirements #17

Closed ahokinson closed 5 years ago

ahokinson commented 5 years ago

Since the documentation lists zipfile as inspiration, to be more in line with the ZipFile built-in class, I suggest that the __enter__ and __exit__ methods be implemented.

While working with this library, I tried the following:

if file.endswith(".cbz"):
    with ZipFile("{}/temp/{}".format(project_root, file)) as zipped:
...
if file.endswith(".cbr"):
    with RarFile("{}/temp/{}".format(project_root, file)) as rarred:
...

only to find that the __enter__ and __exit__ methods weren't implemented.

I borrowed from the ZipFile class methods, but it's worth noting that the file is not left open in __init__ so it should not be closed in __exit__ as it is in the ZipFile class.