[Pkg-privacy-commits] [onionshare] 246/256: open files via the 'with' statement, to avoid ResourceWarnings on unclosed files

Ulrike Uhlig ulrike at moszumanska.debian.org
Fri May 26 12:53:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

ulrike pushed a commit to branch master
in repository onionshare.

commit 36de1951e75f9458f06679295018ed21be2ef330
Author: Miguel Jacq <mig at mig5.net>
Date:   Tue May 23 21:22:14 2017 +1000

    open files via the 'with' statement, to avoid ResourceWarnings on unclosed files
---
 onionshare/common.py   | 9 ++++++---
 onionshare/onion.py    | 6 ++++--
 onionshare/settings.py | 5 +++--
 onionshare/strings.py  | 5 +++--
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/onionshare/common.py b/onionshare/common.py
index 232328d..58e7b88 100644
--- a/onionshare/common.py
+++ b/onionshare/common.py
@@ -94,7 +94,9 @@ def get_version():
     """
     Returns the version of OnionShare that is running.
     """
-    return open(get_resource_path('version.txt')).read().strip()
+    with open(get_resource_path('version.txt')) as f:
+      version = f.read().strip()
+      return version
 
 
 def constant_time_compare(val1, val2):
@@ -133,8 +135,9 @@ def build_slug():
     """
     Returns a random string made from two words from the wordlist, such as "deter-trig".
     """
-    wordlist = open(get_resource_path('wordlist.txt')).read().split('\n')
-    wordlist.remove('')
+    with open(get_resource_path('wordlist.txt')) as f:
+      wordlist = f.read().split('\n')
+      wordlist.remove('')
 
     r = SystemRandom()
     return '-'.join(r.choice(wordlist) for x in range(2))
diff --git a/onionshare/onion.py b/onionshare/onion.py
index 4d69ebe..8463971 100644
--- a/onionshare/onion.py
+++ b/onionshare/onion.py
@@ -178,7 +178,8 @@ class Onion(object):
                 self.tor_torrc = os.path.join(self.tor_data_directory.name, 'torrc')
             else:
                 # Linux and Mac can use unix sockets
-                torrc_template = open(common.get_resource_path('torrc_template')).read()
+                with open(common.get_resource_path('torrc_template')) as f:
+                  torrc_template = f.read()
                 self.tor_control_port = None
                 self.tor_control_socket = os.path.join(self.tor_data_directory.name, 'control_socket')
                 self.tor_cookie_auth_file = os.path.join(self.tor_data_directory.name, 'cookie')
@@ -192,7 +193,8 @@ class Onion(object):
             torrc_template = torrc_template.replace('{{geo_ip_file}}',      self.tor_geo_ip_file_path)
             torrc_template = torrc_template.replace('{{geo_ipv6_file}}',    self.tor_geo_ipv6_file_path)
             torrc_template = torrc_template.replace('{{socks_port}}',       str(self.tor_socks_port))
-            open(self.tor_torrc, 'w').write(torrc_template)
+            with open(self.tor_torrc, 'w') as f:
+              f.write(torrc_template)
 
             # Execute a tor subprocess
             start_ts = time.time()
diff --git a/onionshare/settings.py b/onionshare/settings.py
index b5f873a..97c6774 100644
--- a/onionshare/settings.py
+++ b/onionshare/settings.py
@@ -85,8 +85,9 @@ class Settings(object):
         # If the settings file exists, load it
         if os.path.exists(self.filename):
             try:
-                self._settings = json.loads(open(self.filename, 'r').read())
-                self.fill_in_defaults()
+                with open(self.filename, 'r') as f:
+                  self._settings = json.loads(f.read())
+                  self.fill_in_defaults()
             except:
                 pass
 
diff --git a/onionshare/strings.py b/onionshare/strings.py
index 95fcc10..5a499a4 100644
--- a/onionshare/strings.py
+++ b/onionshare/strings.py
@@ -38,8 +38,9 @@ def load_strings(common, default="en"):
         abs_filename = os.path.join(locale_dir, filename)
         lang, ext = os.path.splitext(filename)
         if abs_filename.endswith('.json'):
-            lang_json = open(abs_filename, encoding='utf-8').read()
-            translations[lang] = json.loads(lang_json)
+            with open(abs_filename, encoding='utf-8') as f:
+              lang_json = f.read()
+              translations[lang] = json.loads(lang_json)
 
     strings = translations[default]
     lc, enc = locale.getdefaultlocale()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onionshare.git



More information about the Pkg-privacy-commits mailing list