diff options
| author | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-01 03:50:12 +0200 | 
|---|---|---|
| committer | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-01 04:15:25 +0200 | 
| commit | d84b960982b5bad0b3c78c4a680638824924004b (patch) | |
| tree | 0699e2cd45403b34d0f52dbed45881f3607cd2d2 /git/config.py | |
| parent | b114f3bbe50f50477778a0a13cf99c0cfee1392a (diff) | |
| download | gitpython-d84b960982b5bad0b3c78c4a680638824924004b.tar.gz | |
cfg_TCs, #519: FIX config resource leaks
+ Modify lock/read-config-file code to ansure files closed
+ Use `with GitConfigarser()` more systematically in TCs.
+ Clear any locks left hanging from pev Tcs 
Diffstat (limited to 'git/config.py')
| -rw-r--r-- | git/config.py | 60 | 
1 files changed, 23 insertions, 37 deletions
diff --git a/git/config.py b/git/config.py index 5bd10975..ad6192ff 100644 --- a/git/config.py +++ b/git/config.py @@ -388,23 +388,18 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje          while files_to_read:              file_path = files_to_read.pop(0)              fp = file_path -            close_fp = False +            file_ok = False -            # assume a path if it is not a file-object -            if not hasattr(fp, "seek"): +            if hasattr(fp, "seek"): +                self._read(fp, fp.name) +            else: +                # assume a path if it is not a file-object                  try: -                    fp = open(file_path, 'rb') -                    close_fp = True +                    with open(file_path, 'rb') as fp: +                        file_ok = True +                        self._read(fp, fp.name)                  except IOError:                      continue -            # END fp handling - -            try: -                self._read(fp, fp.name) -            finally: -                if close_fp: -                    fp.close() -            # END read-handling              # Read includes and append those that we didn't handle yet              # We expect all paths to be normalized and absolute (and will assure that is the case) @@ -413,7 +408,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje                      if include_path.startswith('~'):                          include_path = os.path.expanduser(include_path)                      if not os.path.isabs(include_path): -                        if not close_fp: +                        if not file_ok:                              continue                          # end ignore relative paths if we don't know the configuration file path                          assert os.path.isabs(file_path), "Need absolute paths to be sure our cycle checks will work" @@ -477,34 +472,25 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje          # end          fp = self._file_or_files -        close_fp = False          # we have a physical file on disk, so get a lock -        if isinstance(fp, string_types + (FileType, )): +        is_file_lock = isinstance(fp, string_types + (FileType, )) +        if is_file_lock:              self._lock._obtain_lock() -        # END get lock for physical files - -        if not hasattr(fp, "seek"): -            fp = open(self._file_or_files, "wb") -            close_fp = True -        else: -            fp.seek(0) -            # make sure we do not overwrite into an existing file -            if hasattr(fp, 'truncate'): -                fp.truncate() -            # END -        # END handle stream or file - -        # WRITE DATA          try: -            self._write(fp) +            if not hasattr(fp, "seek"): +                with open(self._file_or_files, "wb") as fp: +                    self._write(fp) +            else: +                fp.seek(0) +                # make sure we do not overwrite into an existing file +                if hasattr(fp, 'truncate'): +                    fp.truncate() +                self._write(fp)          finally: -            if close_fp: -                fp.close() -        # END data writing - -        # we do not release the lock - it will be done automatically once the -        # instance vanishes +            # we release the lock - it will not vanish automatically in PY3.5+ +            if is_file_lock: +                self._lock._release_lock()      def _assure_writable(self, method_name):          if self.read_only:  | 
