summaryrefslogtreecommitdiff
path: root/git/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'git/config.py')
-rw-r--r--git/config.py60
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: