Rev 5257: (lifeless) Explicitly close various file objects used in bzrlib (Martin [gz]) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed May 26 12:24:34 BST 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5257 [merge]
revision-id: pqm at pqm.ubuntu.com-20100526112426-r041wtw03oqi4hj1
parent: pqm at pqm.ubuntu.com-20100526084351-ari52tpazd7t61z3
parent: gzlist at googlemail.com-20100525172752-amm089xcikv968sw
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2010-05-26 12:24:26 +0100
message:
  (lifeless) Explicitly close various file objects used in bzrlib (Martin [gz])
modified:
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/bundle/bundle_data.py   read_changeset.py-20050619171944-c0d95aa685537640
  bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/diff.py                 diff.py-20050309040759-26944fbbf2ebbf36
  bzrlib/hashcache.py            hashcache.py-20050706091756-fe3a8cc1143ff24f
  bzrlib/index.py                index.py-20070712131115-lolkarso50vjr64s-1
  bzrlib/merge.py                merge.py-20050513021216-953b65a438527106
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/store/text.py           text.py-20050928201105-c26468dcb5d9b18b
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/transform.py            transform.py-20060105172343-dd99e54394d91687
  bzrlib/tree.py                 tree.py-20050309040759-9d5f2496be663e77
  bzrlib/weavefile.py            weavefile.py-20050629135233-2ffe0200f103f6c2
  bzrlib/workingtree.py          workingtree.py-20050511021032-29b6ec0a681e02e3
  bzrlib/xml_serializer.py       xml.py-20050309040759-57d51586fdec365d
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2010-05-19 02:58:05 +0000
+++ b/bzrlib/builtins.py	2010-05-25 17:27:52 +0000
@@ -3145,8 +3145,11 @@
         def get_message(commit_obj):
             """Callback to get commit message"""
             if file:
-                my_message = codecs.open(
-                    file, 'rt', osutils.get_user_encoding()).read()
+                f = codecs.open(file, 'rt', osutils.get_user_encoding())
+                try:
+                    my_message = f.read()
+                finally:
+                    f.close()
             elif message is not None:
                 my_message = message
             else:

=== modified file 'bzrlib/bundle/bundle_data.py'
--- a/bzrlib/bundle/bundle_data.py	2009-05-06 05:36:28 +0000
+++ b/bzrlib/bundle/bundle_data.py	2009-09-20 22:12:36 +0000
@@ -278,7 +278,11 @@
         if rev.revision_id != revision_id:
             raise AssertionError()
         if sha1 != rev.inventory_sha1:
-            open(',,bogus-inv', 'wb').write(s)
+            f = open(',,bogus-inv', 'wb')
+            try:
+                f.write(s)
+            finally:
+                f.close()
             warning('Inventory sha hash mismatch for revision %s. %s'
                     ' != %s' % (revision_id, sha1, rev.inventory_sha1))
 

=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2010-05-20 18:23:17 +0000
+++ b/bzrlib/bzrdir.py	2010-05-25 17:27:52 +0000
@@ -2873,7 +2873,11 @@
             self.revisions[rev_id] = rev
 
     def _load_old_inventory(self, rev_id):
-        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
+        f = self.branch.repository.inventory_store.get(rev_id)
+        try:
+            old_inv_xml = f.read()
+        finally:
+            f.close()
         inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
         inv.revision_id = rev_id
         rev = self.revisions[rev_id]
@@ -2957,8 +2961,11 @@
                 ie.revision = previous_ie.revision
                 return
         if ie.has_text():
-            text = self.branch.repository._text_store.get(ie.text_id)
-            file_lines = text.readlines()
+            f = self.branch.repository._text_store.get(ie.text_id)
+            try:
+                file_lines = f.readlines()
+            finally:
+                f.close()
             w.add_lines(rev_id, previous_revisions, file_lines)
             self.text_count += 1
         else:

=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-05-07 06:35:34 +0000
+++ b/bzrlib/config.py	2010-05-25 17:27:52 +0000
@@ -477,6 +477,14 @@
     def _get_nickname(self):
         return self.get_user_option('nickname')
 
+    def _write_config_file(self):
+        f = file(self._get_filename(), "wb")
+        try:
+            osutils.copy_ownership_from_path(f.name)
+            self._get_parser().write(f)
+        finally:
+            f.close()
+
 
 class GlobalConfig(IniBasedConfig):
     """The configuration that should be used for a specific location."""
@@ -518,13 +526,6 @@
         self._get_parser().setdefault(section, {})[option] = value
         self._write_config_file()
 
-    def _write_config_file(self):
-        path = self._get_filename()
-        f = open(path, 'wb')
-        osutils.copy_ownership_from_path(path)
-        self._get_parser().write(f)
-        f.close()
-
 
 class LocationConfig(IniBasedConfig):
     """A configuration object that gives the policy for a location."""
@@ -664,7 +665,7 @@
         self._get_parser()[location][option]=value
         # the allowed values of store match the config policies
         self._set_option_policy(location, option, store)
-        self._get_parser().write(file(self._get_filename(), 'wb'))
+        self._write_config_file()
 
 
 class BranchConfig(Config):
@@ -991,7 +992,11 @@
         """Save the config file, only tests should use it for now."""
         conf_dir = os.path.dirname(self._filename)
         ensure_config_dir_exists(conf_dir)
-        self._get_config().write(file(self._filename, 'wb'))
+        f = file(self._filename, 'wb')
+        try:
+            self._get_config().write(f)
+        finally:
+            f.close()
 
     def _set_option(self, section_name, option_name, value):
         """Set an authentication configuration option"""
@@ -1445,7 +1450,11 @@
             return StringIO()
 
     def _get_configobj(self):
-        return ConfigObj(self._get_config_file(), encoding='utf-8')
+        f = self._get_config_file()
+        try:
+            return ConfigObj(f, encoding='utf-8')
+        finally:
+            f.close()
 
     def _set_configobj(self, configobj):
         out_file = StringIO()

=== modified file 'bzrlib/diff.py'
--- a/bzrlib/diff.py	2010-04-30 11:35:43 +0000
+++ b/bzrlib/diff.py	2010-05-25 17:27:52 +0000
@@ -706,7 +706,7 @@
         """
         def _get_text(tree, file_id, path):
             if file_id is not None:
-                return tree.get_file(file_id, path).readlines()
+                return tree.get_file_lines(file_id, path)
             else:
                 return []
         try:

=== modified file 'bzrlib/hashcache.py'
--- a/bzrlib/hashcache.py	2009-05-06 05:36:28 +0000
+++ b/bzrlib/hashcache.py	2009-09-20 22:12:36 +0000
@@ -289,6 +289,9 @@
 
             self._cache[path] = (sha1, fp)
 
+        # GZ 2009-09-20: Should really use a try/finally block to ensure close
+        inf.close()
+
         self.needs_write = False
 
     def _cutoff_time(self):

=== modified file 'bzrlib/index.py'
--- a/bzrlib/index.py	2010-04-14 05:06:53 +0000
+++ b/bzrlib/index.py	2010-05-25 17:27:52 +0000
@@ -462,6 +462,7 @@
         trailers = 0
         pos = stream.tell()
         lines = stream.read().split('\n')
+        # GZ 2009-09-20: Should really use a try/finally block to ensure close
         stream.close()
         del lines[-1]
         _, _, _, trailers = self._parse_lines(lines, pos)

=== modified file 'bzrlib/merge.py'
--- a/bzrlib/merge.py	2010-05-03 04:08:50 +0000
+++ b/bzrlib/merge.py	2010-05-25 17:27:52 +0000
@@ -1455,7 +1455,7 @@
     def get_lines(self, tree, file_id):
         """Return the lines in a file, or an empty list."""
         if tree.has_id(file_id):
-            return tree.get_file(file_id).readlines()
+            return tree.get_file_lines(file_id)
         else:
             return []
 

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2010-05-13 17:32:55 +0000
+++ b/bzrlib/osutils.py	2010-05-25 17:27:52 +0000
@@ -2078,9 +2078,11 @@
     base = dirname(bzrlib.__file__)
     if getattr(sys, 'frozen', None):    # bzr.exe
         base = abspath(pathjoin(base, '..', '..'))
-    filename = pathjoin(base, resource_relpath)
-    return open(filename, 'rU').read()
-
+    f = file(pathjoin(base, resource_relpath), "rU")
+    try:
+        return f.read()
+    finally:
+        f.close()
 
 def file_kind_from_stat_mode_thunk(mode):
     global file_kind_from_stat_mode

=== modified file 'bzrlib/store/text.py'
--- a/bzrlib/store/text.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/store/text.py	2010-05-25 16:45:12 +0000
@@ -118,7 +118,9 @@
         # so buffer them in a StringIO instead
         if getattr(f, 'tell', None) is not None:
             return gzip.GzipFile(mode='rb', fileobj=f)
-        else:
+        try:
             from cStringIO import StringIO
             sio = StringIO(f.read())
             return gzip.GzipFile(mode='rb', fileobj=sio)
+        finally:
+            f.close()

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2010-05-20 18:23:17 +0000
+++ b/bzrlib/tests/__init__.py	2010-05-25 17:27:52 +0000
@@ -2482,7 +2482,11 @@
 
     def check_file_contents(self, filename, expect):
         self.log("check contents of file %s" % filename)
-        contents = file(filename, 'r').read()
+        f = file(filename)
+        try:
+            contents = f.read()
+        finally:
+            f.close()
         if contents != expect:
             self.log("expected: %r" % expect)
             self.log("actually: %r" % contents)

=== modified file 'bzrlib/transform.py'
--- a/bzrlib/transform.py	2010-05-13 17:32:55 +0000
+++ b/bzrlib/transform.py	2010-05-25 17:27:52 +0000
@@ -1792,7 +1792,7 @@
         parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
                        self._iter_parent_trees()]
         vf.add_lines((file_id, tree_revision), parent_keys,
-                     self.get_file(file_id).readlines())
+                     self.get_file_lines(file_id))
         repo = self._get_repository()
         base_vf = repo.texts
         if base_vf not in vf.fallback_versionedfiles:
@@ -2460,8 +2460,12 @@
     if entry.kind == "directory":
         return True
     if entry.kind == "file":
-        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
-            return True
+        f = file(target_path, 'rb')
+        try:
+            if tree.get_file_text(file_id) == f.read():
+                return True
+        finally:
+            f.close()
     elif entry.kind == "symlink":
         if tree.get_symlink_target(file_id) == os.readlink(target_path):
             return True

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2010-05-11 08:36:16 +0000
+++ b/bzrlib/tree.py	2010-05-25 17:27:52 +0000
@@ -520,7 +520,7 @@
             parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
                 self._iter_parent_trees()]
             vf.add_lines((file_id, last_revision), parent_keys,
-                         self.get_file(file_id).readlines())
+                         self.get_file_lines(file_id))
             repo = self.branch.repository
             base_vf = repo.texts
         else:

=== modified file 'bzrlib/weavefile.py'
--- a/bzrlib/weavefile.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/weavefile.py	2009-09-20 22:12:36 +0000
@@ -118,7 +118,10 @@
 
     from weave import WeaveFormatError
 
-    lines = iter(f.readlines())
+    try:
+        lines = iter(f.readlines())
+    finally:
+        f.close()
 
     try:
         l = lines.next()

=== modified file 'bzrlib/workingtree.py'
--- a/bzrlib/workingtree.py	2010-05-17 10:04:05 +0000
+++ b/bzrlib/workingtree.py	2010-05-25 17:27:52 +0000
@@ -527,7 +527,7 @@
 
         # Now we have the parents of this content
         annotator = self.branch.repository.texts.get_annotator()
-        text = self.get_file(file_id).read()
+        text = self.get_file_text(file_id)
         this_key =(file_id, default_revision)
         annotator.add_special_text(this_key, file_parent_keys, text)
         annotations = [(key[-1], line)

=== modified file 'bzrlib/xml_serializer.py'
--- a/bzrlib/xml_serializer.py	2010-04-30 11:03:59 +0000
+++ b/bzrlib/xml_serializer.py	2010-05-25 17:27:52 +0000
@@ -83,8 +83,11 @@
 
     def read_inventory(self, f, revision_id=None):
         try:
-            return self._unpack_inventory(self._read_element(f),
-                revision_id=None)
+            try:
+                return self._unpack_inventory(self._read_element(f),
+                    revision_id=None)
+            finally:
+                f.close()
         except ParseError, e:
             raise errors.UnexpectedInventoryFormat(e)
 




More information about the bazaar-commits mailing list