twolfson / restructuredtext-lint

reStructuredText linter
The Unlicense
171 stars 20 forks source link

Use a with statement to read the file #28

Closed asottile closed 8 years ago

asottile commented 8 years ago

For exception safety

twolfson commented 8 years ago

Can you elaborate on exception safety? Won't the same errors be raised in both scenarios?

asottile commented 8 years ago

If read() (for whatever reason) were to throw an exception, you'd leave an open file descriptor (.close() would not be called). Though this is unlikely, it's best to write the (simpler) code which does this for you. with statements are a best practice in python when dealing with file objects (or other closables).

You can read more about them at their PEP (introduced in py25, standard in py26): https://www.python.org/dev/peps/pep-0343/

twolfson commented 8 years ago

Ah, right. Good catch :+1: I will land this now.

twolfson commented 8 years ago

This has been merged/released in 0.14.3. Thanks for the fix!

asottile commented 8 years ago

No problem :) Thanks for the merge :D