mpenning / ciscoconfparse

Parse, Audit, Query, Build, and Modify Arista / Cisco / Juniper / Palo Alto / F5 configurations.
http://www.pennington.net/py/ciscoconfparse/
GNU General Public License v3.0
793 stars 220 forks source link

ResourceWarning: unclosed file <_io.TextIOWrapper #122

Closed kimoldfield closed 5 years ago

kimoldfield commented 5 years ago

Using ciscoconfparse under python 3.4 with unittest gets the warning:

.../virtualenv/lib/python3.4/site-packages/ciscoconfparse/ciscoconfparse.py:66: ResourceWarning: unclosed file <_io.TextIOWrapper name='.../virtualenv/lib/python3.4/site-packages/ciscoconfparse/version' mode='r' encoding='UTF-8'>
  'version')).read().strip()
kimoldfield commented 5 years ago

The warning can be removed with the following patch:

--- ciscoconfparse.py.old       2018-11-07 18:36:39.599368903 +1100
+++ ciscoconfparse.py   2018-11-07 18:23:25.680051450 +1100
@@ -62,8 +62,9 @@
 """

 ## Docstring props: http://stackoverflow.com/a/1523456/667301
-__version__ = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 
-    'version')).read().strip()
+with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'version')) \
+        as fh:
+    __version__ = fh.read().strip()
 __email__ = "mike /at\ pennington [dot] net"
 __author__ = "David Michael Pennington <{0}>".format(__email__)
 __copyright__ = "2007-{0}, {1}".format(time.strftime('%Y'), __author__)
mpenning commented 5 years ago

This should be fixed in version 1.3.22

jobec commented 5 years ago

This isn't fixed, or it's a regression in version 1.3.39.

/home/vagrant/.venv/lib/python3.6/site-packages/ciscoconfparse/ciscoconfparse.py:77: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/vagrant/.venv/lib/python3.6/site-packages/ciscoconfparse/version' mode='r' encoding='UTF-8'>
  __version__ = open(versionfilepath).read().strip()

https://github.com/mpenning/ciscoconfparse/blob/15890e6a3d84eb8fb9d3f411f4157702261362e6/ciscoconfparse/ciscoconfparse.py#L77

mpenning commented 5 years ago

@jobec version 1.3.40 should help