Skip to content

Commit 391a767

Browse files
committed
io: Wrap (probably) allconfig_writers in with blocks
1 parent c7f459f commit 391a767

File tree

10 files changed

+172
-198
lines changed

10 files changed

+172
-198
lines changed

doc/source/tutorial.rst

Lines changed: 64 additions & 60 deletions
Large diffs are not rendered by default.

git/objects/submodule/base.py

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -398,24 +398,20 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
398398
# otherwise there is a '-' character in front of the submodule listing
399399
# a38efa84daef914e4de58d1905a500d8d14aaf45 mymodule (v0.9.0-1-ga38efa8)
400400
# -a38efa84daef914e4de58d1905a500d8d14aaf45 submodules/intermediate/one
401-
writer = sm.repo.config_writer()
402-
writer.set_value(sm_section(name), 'url', url)
403-
writer.release()
401+
with sm.repo.config_writer() as writer:
402+
writer.set_value(sm_section(name), 'url', url)
404403

405404
# update configuration and index
406405
index = sm.repo.index
407-
writer = sm.config_writer(index=index, write=False)
408-
writer.set_value('url', url)
409-
writer.set_value('path', path)
410-
411-
sm._url = url
412-
if not branch_is_default:
413-
# store full path
414-
writer.set_value(cls.k_head_option, br.path)
415-
sm._branch_path = br.path
416-
# END handle path
417-
writer.release()
418-
del(writer)
406+
with sm.config_writer(index=index, write=False) as writer:
407+
writer.set_value('url', url)
408+
writer.set_value('path', path)
409+
410+
sm._url = url
411+
if not branch_is_default:
412+
# store full path
413+
writer.set_value(cls.k_head_option, br.path)
414+
sm._branch_path = br.path
419415

420416
# we deliberatly assume that our head matches our index !
421417
sm.binsha = mrepo.head.commit.binsha
@@ -542,9 +538,8 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
542538
# the default implementation will be offended and not update the repository
543539
# Maybe this is a good way to assure it doesn't get into our way, but
544540
# we want to stay backwards compatible too ... . Its so redundant !
545-
writer = self.repo.config_writer()
546-
writer.set_value(sm_section(self.name), 'url', self.url)
547-
writer.release()
541+
with self.repo.config_writer() as writer:
542+
writer.set_value(sm_section(self.name), 'url', self.url)
548543
# END handle dry_run
549544
# END handle initalization
550545

@@ -731,11 +726,9 @@ def move(self, module_path, configuration=True, module=True):
731726
# END handle submodule doesn't exist
732727

733728
# update configuration
734-
writer = self.config_writer(index=index) # auto-write
735-
writer.set_value('path', module_checkout_path)
736-
self.path = module_checkout_path
737-
writer.release()
738-
del(writer)
729+
with self.config_writer(index=index) as writer: # auto-write
730+
writer.set_value('path', module_checkout_path)
731+
self.path = module_checkout_path
739732
# END handle configuration flag
740733
except Exception:
741734
if renamed_module:
@@ -898,13 +891,11 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
898891

899892
# now git config - need the config intact, otherwise we can't query
900893
# information anymore
901-
writer = self.repo.config_writer()
902-
writer.remove_section(sm_section(self.name))
903-
writer.release()
894+
with self.repo.config_writer() as writer:
895+
writer.remove_section(sm_section(self.name))
904896

905-
writer = self.config_writer()
906-
writer.remove_section()
907-
writer.release()
897+
with self.config_writer() as writer:
898+
writer.remove_section()
908899
# END delete configuration
909900

910901
return self
@@ -995,18 +986,15 @@ def rename(self, new_name):
995986
return self
996987

997988
# .git/config
998-
pw = self.repo.config_writer()
999-
# As we ourselves didn't write anything about submodules into the parent .git/config, we will not require
1000-
# it to exist, and just ignore missing entries
1001-
if pw.has_section(sm_section(self.name)):
1002-
pw.rename_section(sm_section(self.name), sm_section(new_name))
1003-
# end
1004-
pw.release()
989+
with self.repo.config_writer() as pw:
990+
# As we ourselves didn't write anything about submodules into the parent .git/config,
991+
# we will not require it to exist, and just ignore missing entries.
992+
if pw.has_section(sm_section(self.name)):
993+
pw.rename_section(sm_section(self.name), sm_section(new_name))
1005994

1006995
# .gitmodules
1007-
cw = self.config_writer(write=True).config
1008-
cw.rename_section(sm_section(self.name), sm_section(new_name))
1009-
cw.release()
996+
with self.config_writer(write=True) as cw:
997+
cw.config.rename_section(sm_section(self.name), sm_section(new_name))
1010998

1011999
self._name = new_name
10121000

git/refs/head.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,15 @@ def set_tracking_branch(self, remote_reference):
133133
raise ValueError("Incorrect parameter type: %r" % remote_reference)
134134
# END handle type
135135

136-
writer = self.config_writer()
137-
if remote_reference is None:
138-
writer.remove_option(self.k_config_remote)
139-
writer.remove_option(self.k_config_remote_ref)
140-
if len(writer.options()) == 0:
141-
writer.remove_section()
142-
# END handle remove section
143-
else:
144-
writer.set_value(self.k_config_remote, remote_reference.remote_name)
145-
writer.set_value(self.k_config_remote_ref, Head.to_full_path(remote_reference.remote_head))
146-
# END handle ref value
147-
writer.release()
136+
with self.config_writer() as writer:
137+
if remote_reference is None:
138+
writer.remove_option(self.k_config_remote)
139+
writer.remove_option(self.k_config_remote_ref)
140+
if len(writer.options()) == 0:
141+
writer.remove_section()
142+
else:
143+
writer.set_value(self.k_config_remote, remote_reference.remote_name)
144+
writer.set_value(self.k_config_remote_ref, Head.to_full_path(remote_reference.remote_head))
148145

149146
return self
150147

git/repo/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,8 @@ def _clone(cls, git, url, path, odb_default_type, progress, **kwargs):
924924
# sure
925925
repo = cls(os.path.abspath(path), odbt=odbt)
926926
if repo.remotes:
927-
writer = repo.remotes[0].config_writer
928-
writer.set_value('url', repo.remotes[0].url.replace("\\\\", "\\").replace("\\", "/"))
929-
# PY3: be sure cleanup is performed and lock is released
930-
writer.release()
927+
with repo.remotes[0].config_writer as writer:
928+
writer.set_value('url', repo.remotes[0].url.replace("\\\\", "\\").replace("\\", "/"))
931929
# END handle remote repo
932930
return repo
933931

git/test/lib/helper.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,13 @@ def remote_repo_creator(self):
237237
rw_remote_repo.daemon_export = True
238238

239239
# this thing is just annoying !
240-
crw = rw_remote_repo.config_writer()
241-
section = "daemon"
242-
try:
243-
crw.add_section(section)
244-
except Exception:
245-
pass
246-
crw.set(section, "receivepack", True)
247-
# release lock
248-
crw.release()
249-
del(crw)
240+
with rw_remote_repo.config_writer() as crw:
241+
section = "daemon"
242+
try:
243+
crw.add_section(section)
244+
except Exception:
245+
pass
246+
crw.set(section, "receivepack", True)
250247

251248
# initialize the remote - first do it as local remote and pull, then
252249
# we change the url to point to the daemon. The daemon should be started
@@ -255,7 +252,8 @@ def remote_repo_creator(self):
255252
d_remote.fetch()
256253
remote_repo_url = "git://localhost:%s%s" % (GIT_DAEMON_PORT, remote_repo_dir)
257254

258-
d_remote.config_writer.set('url', remote_repo_url)
255+
with d_remote.config_writer as cw:
256+
cw.set('url', remote_repo_url)
259257

260258
temp_dir = osp(_mktemp())
261259
gd = launch_git_daemon(temp_dir, '127.0.0.1', GIT_DAEMON_PORT)

git/test/test_docs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def test_init_repo_object(self, rw_dir):
3939

4040
# [3-test_init_repo_object]
4141
repo.config_reader() # get a config reader for read-only access
42-
cw = repo.config_writer() # get a config writer to change configuration
43-
cw.release() # call release() to be sure changes are written and locks are released
42+
with repo.config_writer(): # get a config writer to change configuration
43+
pass # call release() to be sure changes are written and locks are released
4444
# ![3-test_init_repo_object]
4545

4646
# [4-test_init_repo_object]
@@ -398,9 +398,8 @@ def test_references_and_objects(self, rw_dir):
398398

399399
# [26-test_references_and_objects]
400400
assert origin.url == repo.remotes.origin.url
401-
cw = origin.config_writer
402-
cw.set("pushurl", "other_url")
403-
cw.release()
401+
with origin.config_writer as cw:
402+
cw.set("pushurl", "other_url")
404403

405404
# Please note that in python 2, writing origin.config_writer.set(...) is totally safe.
406405
# In py3 __del__ calls can be delayed, thus not writing changes in time.

git/test/test_index.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,9 @@ def test_index_mutation(self, rw_repo):
412412

413413
uname = u"Thomas Müller"
414414
umail = "sd@company.com"
415-
writer = rw_repo.config_writer()
416-
writer.set_value("user", "name", uname)
417-
writer.set_value("user", "email", umail)
418-
writer.release()
415+
with rw_repo.config_writer() as writer:
416+
writer.set_value("user", "name", uname)
417+
writer.set_value("user", "email", umail)
419418
self.assertEqual(writer.get_value("user", "name"), uname)
420419

421420
# remove all of the files, provide a wild mix of paths, BaseIndexEntries,

git/test/test_refs.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,13 @@ def test_heads(self, rwrepo):
101101
assert prev_object == cur_object # represent the same git object
102102
assert prev_object is not cur_object # but are different instances
103103

104-
writer = head.config_writer()
105-
tv = "testopt"
106-
writer.set_value(tv, 1)
107-
assert writer.get_value(tv) == 1
108-
writer.release()
104+
with head.config_writer() as writer:
105+
tv = "testopt"
106+
writer.set_value(tv, 1)
107+
assert writer.get_value(tv) == 1
109108
assert head.config_reader().get_value(tv) == 1
110-
writer = head.config_writer()
111-
writer.remove_option(tv)
112-
writer.release()
109+
with head.config_writer() as writer:
110+
writer.remove_option(tv)
113111

114112
# after the clone, we might still have a tracking branch setup
115113
head.set_tracking_branch(None)
@@ -175,7 +173,7 @@ def test_is_valid(self):
175173

176174
def test_orig_head(self):
177175
assert type(self.rorepo.head.orig_head()) == SymbolicReference
178-
176+
179177
@with_rw_repo('0.1.6')
180178
def test_head_checkout_detached_head(self, rw_repo):
181179
res = rw_repo.remotes.origin.refs.master.checkout()

git/test/test_remote.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ def get_info(res, remote, name):
267267

268268
# put origin to git-url
269269
other_origin = other_repo.remotes.origin
270-
other_origin.config_writer.set("url", remote_repo_url)
270+
with other_origin.config_writer as cw:
271+
cw.set("url", remote_repo_url)
271272
# it automatically creates alternates as remote_repo is shared as well.
272273
# It will use the transport though and ignore alternates when fetching
273274
# assert not other_repo.alternates # this would fail
@@ -416,13 +417,12 @@ def test_base(self, rw_repo, remote_repo):
416417
self.failUnlessRaises(IOError, reader.set, opt, "test")
417418

418419
# change value
419-
writer = remote.config_writer
420-
new_val = "myval"
421-
writer.set(opt, new_val)
422-
assert writer.get(opt) == new_val
423-
writer.set(opt, val)
424-
assert writer.get(opt) == val
425-
del(writer)
420+
with remote.config_writer as writer:
421+
new_val = "myval"
422+
writer.set(opt, new_val)
423+
assert writer.get(opt) == new_val
424+
writer.set(opt, val)
425+
assert writer.get(opt) == val
426426
assert getattr(remote, opt) == val
427427
# END for each default option key
428428

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy