Commit 865cd35c authored by Claude Paroz's avatar Claude Paroz
Browse files

Made more extensive usage of context managers with open.

parent ec5423df
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -22,7 +22,8 @@ def unique_messages():
                cmd = 'msguniq "%s.po"' % pf
                stdout = os.popen(cmd)
                msg = stdout.read()
                open('%s.po' % pf, 'w').write(msg)
                with open('%s.po' % pf, 'w') as fp:
                    fp.write(msg)

if __name__ == "__main__":
    unique_messages()
+1 −4
Original line number Diff line number Diff line
@@ -120,12 +120,9 @@ class SpatiaLiteCreation(DatabaseCreation):

            # Opening up the SpatiaLite SQL initialization file and executing
            # as a script.
            sql_fh = open(spatialite_sql, 'r')
            try:
            with open(spatialite_sql, 'r') as sql_fh:
                cur = self.connection._cursor()
                cur.executescript(sql_fh.read())
            finally:
                sql_fh.close()

    def spatialite_init_file(self):
        # SPATIALITE_SQL may be placed in settings to tell GeoDjango
+4 −4
Original line number Diff line number Diff line
@@ -7,9 +7,9 @@ def fromfile(file_h):
    """
    # If given a file name, get a real handle.
    if isinstance(file_h, basestring): 
        file_h = open(file_h, 'rb')

    # Reading in the file's contents,
        with open(file_h, 'rb') as file_h:
            buf = file_h.read()
    else:
        buf = file_h.read()

    # If we get WKB need to wrap in buffer(), so run through regexes.
+8 −11
Original line number Diff line number Diff line
@@ -47,8 +47,7 @@ class SessionStore(SessionBase):
    def load(self):
        session_data = {}
        try:
            session_file = open(self._key_to_file(), "rb")
            try:
            with open(self._key_to_file(), "rb") as session_file:
                file_data = session_file.read()
            # Don't fail if there is no data in the session file.
            # We may have opened the empty placeholder file.
@@ -57,8 +56,6 @@ class SessionStore(SessionBase):
                    session_data = self.decode(file_data)
                except (EOFError, SuspiciousOperation):
                    self.create()
            finally:
                session_file.close()
        except IOError:
            self.create()
        return session_data
+9 −18
Original line number Diff line number Diff line
@@ -31,16 +31,13 @@ class FileBasedCache(BaseCache):

        fname = self._key_to_file(key)
        try:
            f = open(fname, 'rb')
            try:
            with open(fname, 'rb') as f:
                exp = pickle.load(f)
                now = time.time()
                if exp < now:
                    self._delete(fname)
                else:
                    return pickle.load(f)
            finally:
                f.close()
        except (IOError, OSError, EOFError, pickle.PickleError):
            pass
        return default
@@ -61,13 +58,10 @@ class FileBasedCache(BaseCache):
            if not os.path.exists(dirname):
                os.makedirs(dirname)

            f = open(fname, 'wb')
            try:
            with open(fname, 'wb') as f:
                now = time.time()
                pickle.dump(now + timeout, f, pickle.HIGHEST_PROTOCOL)
                pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
            finally:
                f.close()
        except (IOError, OSError):
            pass

@@ -94,8 +88,7 @@ class FileBasedCache(BaseCache):
        self.validate_key(key)
        fname = self._key_to_file(key)
        try:
            f = open(fname, 'rb')
            try:
            with open(fname, 'rb') as f:
                exp = pickle.load(f)
            now = time.time()
            if exp < now:
@@ -103,8 +96,6 @@ class FileBasedCache(BaseCache):
                return False
            else:
                return True
            finally:
                f.close()
        except (IOError, OSError, EOFError, pickle.PickleError):
            return False

Loading