Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a52168

Browse files
committed
io,#519: ALL open() --> with open()
+ Some cases had restructuring of code.
1 parentbdf1e68 commit9a52168

File tree

14 files changed

+92
-89
lines changed

14 files changed

+92
-89
lines changed

‎doc/source/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
# built documents.
5151
#
5252
# The short X.Y version.
53-
VERSION=open(os.path.join(os.path.dirname(__file__),"..","..",'VERSION')).readline().strip()
53+
withopen(os.path.join(os.path.dirname(__file__),"..","..",'VERSION'))asfd:
54+
VERSION=fd.readline().strip()
5455
version=VERSION
5556
# The full version, including alpha/beta/rc tags.
5657
release=VERSION

‎git/objects/submodule/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
854854
self._clear_cache()
855855
wtd=mod.working_tree_dir
856856
del(mod)# release file-handles (windows)
857+
importgc
858+
gc.collect()
857859
rmtree(wtd)
858860
# END delete tree if possible
859861
# END handle force

‎git/refs/symbolic.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ def _get_ref_info(cls, repo, ref_path):
134134
point to, or None"""
135135
tokens=None
136136
try:
137-
fp=open(join(repo.git_dir,ref_path),'rt')
138-
value=fp.read().rstrip()
139-
fp.close()
137+
withopen(join(repo.git_dir,ref_path),'rt')asfp:
138+
value=fp.read().rstrip()
140139
# Don't only split on spaces, but on whitespace, which allows to parse lines like
141140
# 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo
142141
tokens=value.split()
@@ -313,13 +312,17 @@ def set_reference(self, ref, logmsg=None):
313312

314313
lfd=LockedFD(fpath)
315314
fd=lfd.open(write=True,stream=True)
316-
fd.write(write_value.encode('ascii')+b'\n')
317-
lfd.commit()
318-
315+
ok=True
316+
try:
317+
fd.write(write_value.encode('ascii')+b'\n')
318+
lfd.commit()
319+
ok=True
320+
finally:
321+
ifnotok:
322+
lfd.rollback()
319323
# Adjust the reflog
320324
iflogmsgisnotNone:
321325
self.log_append(oldbinsha,logmsg)
322-
# END handle reflog
323326

324327
returnself
325328

@@ -422,40 +425,36 @@ def delete(cls, repo, path):
422425
# check packed refs
423426
pack_file_path=cls._get_packed_refs_path(repo)
424427
try:
425-
reader=open(pack_file_path,'rb')
426-
except (OSError,IOError):
427-
pass# it didnt exist at all
428-
else:
429-
new_lines=list()
430-
made_change=False
431-
dropped_last_line=False
432-
forlineinreader:
433-
# keep line if it is a comment or if the ref to delete is not
434-
# in the line
435-
# If we deleted the last line and this one is a tag-reference object,
436-
# we drop it as well
437-
line=line.decode(defenc)
438-
if (line.startswith('#')orfull_ref_pathnotinline)and \
439-
(notdropped_last_lineordropped_last_lineandnotline.startswith('^')):
440-
new_lines.append(line)
441-
dropped_last_line=False
442-
continue
443-
# END skip comments and lines without our path
444-
445-
# drop this line
446-
made_change=True
447-
dropped_last_line=True
448-
# END for each line in packed refs
449-
reader.close()
428+
withopen(pack_file_path,'rb')asreader:
429+
new_lines=list()
430+
made_change=False
431+
dropped_last_line=False
432+
forlineinreader:
433+
# keep line if it is a comment or if the ref to delete is not
434+
# in the line
435+
# If we deleted the last line and this one is a tag-reference object,
436+
# we drop it as well
437+
line=line.decode(defenc)
438+
if (line.startswith('#')orfull_ref_pathnotinline)and \
439+
(notdropped_last_lineordropped_last_lineandnotline.startswith('^')):
440+
new_lines.append(line)
441+
dropped_last_line=False
442+
continue
443+
# END skip comments and lines without our path
444+
445+
# drop this line
446+
made_change=True
447+
dropped_last_line=True
450448

451449
# write the new lines
452450
ifmade_change:
453451
# write-binary is required, otherwise windows will
454452
# open the file in text mode and change LF to CRLF !
455-
open(pack_file_path,'wb').writelines(l.encode(defenc)forlinnew_lines)
456-
# END write out file
457-
# END open exception handling
458-
# END handle deletion
453+
withopen(pack_file_path,'wb')asfd:
454+
fd.writelines(l.encode(defenc)forlinnew_lines)
455+
456+
except (OSError,IOError):
457+
pass# it didnt exist at all
459458

460459
# delete the reflog
461460
reflog_path=RefLog.path(cls(repo,full_ref_path))
@@ -484,7 +483,8 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
484483
target_data=target.path
485484
ifnotresolve:
486485
target_data="ref: "+target_data
487-
existing_data=open(abs_ref_path,'rb').read().decode(defenc).strip()
486+
withopen(abs_ref_path,'rb')asfd:
487+
existing_data=fd.read().decode(defenc).strip()
488488
ifexisting_data!=target_data:
489489
raiseOSError("Reference at %r does already exist, pointing to %r, requested was %r"%
490490
(full_ref_path,existing_data,target_data))
@@ -549,7 +549,11 @@ def rename(self, new_path, force=False):
549549
ifisfile(new_abs_path):
550550
ifnotforce:
551551
# if they point to the same file, its not an error
552-
ifopen(new_abs_path,'rb').read().strip()!=open(cur_abs_path,'rb').read().strip():
552+
withopen(new_abs_path,'rb')asfd1:
553+
f1=fd1.read().strip()
554+
withopen(cur_abs_path,'rb')asfd2:
555+
f2=fd2.read().strip()
556+
iff1!=f2:
553557
raiseOSError("File at path %r already exists"%new_abs_path)
554558
# else: we could remove ourselves and use the otherone, but
555559
# but clarity we just continue as usual

‎git/remote.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,8 @@ def _get_fetch_info_from_stderr(self, proc, progress):
638638
finalize_process(proc,stderr=stderr_text)
639639

640640
# read head information
641-
fp=open(join(self.repo.git_dir,'FETCH_HEAD'),'rb')
642-
fetch_head_info= [l.decode(defenc)forlinfp.readlines()]
643-
fp.close()
641+
withopen(join(self.repo.git_dir,'FETCH_HEAD'),'rb')asfp:
642+
fetch_head_info= [l.decode(defenc)forlinfp.readlines()]
644643

645644
l_fil=len(fetch_info_lines)
646645
l_fhi=len(fetch_head_info)

‎git/test/fixtures/cat_file.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
importsys
22

3-
forlineinopen(sys.argv[1]).readlines():
4-
sys.stdout.write(line)
5-
sys.stderr.write(line)
3+
withopen(sys.argv[1])asfd:
4+
forlineinfd.readlines():
5+
sys.stdout.write(line)
6+
sys.stderr.write(line)

‎git/test/lib/helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def fixture_path(name):
3939

4040

4141
deffixture(name):
42-
returnopen(fixture_path(name),'rb').read()
42+
withopen(fixture_path(name),'rb')asfd:
43+
returnfd.read()
4344

4445

4546
defabsolute_project_path():
@@ -373,7 +374,6 @@ def _make_file(self, rela_path, data, repo=None):
373374
"""
374375
repo=repoorself.rorepo
375376
abs_path=os.path.join(repo.working_tree_dir,rela_path)
376-
fp=open(abs_path,"w")
377-
fp.write(data)
378-
fp.close()
377+
withopen(abs_path,"w")asfp:
378+
fp.write(data)
379379
returnabs_path

‎git/test/test_base.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ def test_base_object(self):
7777
assertdata
7878

7979
tmpfilename=tempfile.mktemp(suffix='test-stream')
80-
tmpfile=open(tmpfilename,'wb+')
81-
assertitem==item.stream_data(tmpfile)
82-
tmpfile.seek(0)
83-
asserttmpfile.read()==data
84-
tmpfile.close()
80+
withopen(tmpfilename,'wb+')astmpfile:
81+
assertitem==item.stream_data(tmpfile)
82+
tmpfile.seek(0)
83+
asserttmpfile.read()==data
8584
os.remove(tmpfilename)
86-
# END stream to file directly
8785
# END for each object type to create
8886

8987
# each has a unique sha
@@ -133,7 +131,8 @@ def test_add_unicode(self, rw_repo):
133131
fromnoseimportSkipTest
134132
raiseSkipTest("Environment doesn't support unicode filenames")
135133

136-
open(file_path,"wb").write(b'something')
134+
withopen(file_path,"wb")asfp:
135+
fp.write(b'something')
137136

138137
ifis_win:
139138
# on windows, there is no way this works, see images on

‎git/test/test_commit.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,16 @@ def test_serialization_unicode_support(self):
313313

314314
deftest_invalid_commit(self):
315315
cmt=self.rorepo.commit()
316-
cmt._deserialize(open(fixture_path('commit_invalid_data'),'rb'))
316+
withopen(fixture_path('commit_invalid_data'),'rb')asfd:
317+
cmt._deserialize(fd)
317318

318319
self.assertEqual(cmt.author.name,u'E.Azer Ko�o�o�oculu',cmt.author.name)
319320
self.assertEqual(cmt.author.email,'azer@kodfabrik.com',cmt.author.email)
320321

321322
deftest_gpgsig(self):
322323
cmt=self.rorepo.commit()
323-
cmt._deserialize(open(fixture_path('commit_with_gpgsig'),'rb'))
324+
withopen(fixture_path('commit_with_gpgsig'),'rb')asfd:
325+
cmt._deserialize(fd)
324326

325327
fixture_sig="""-----BEGIN PGP SIGNATURE-----
326328
Version: GnuPG v1.4.11 (GNU/Linux)

‎git/test/test_docs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def test_init_repo_object(self, rw_dir):
5353
# ![5-test_init_repo_object]
5454

5555
# [6-test_init_repo_object]
56-
repo.archive(open(join(rw_dir,'repo.tar'),'wb'))
56+
withopen(join(rw_dir,'repo.tar'),'wb')asfp:
57+
repo.archive(fp)
5758
# ![6-test_init_repo_object]
5859

5960
# repository paths

‎git/test/test_git.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ def test_it_executes_git_to_shell_and_returns_result(self):
9393

9494
deftest_it_accepts_stdin(self):
9595
filename=fixture_path("cat_file_blob")
96-
fh=open(filename,'r')
97-
assert_equal("70c379b63ffa0795fdbfbc128e5a2818397b7ef8",
98-
self.git.hash_object(istream=fh,stdin=True))
99-
fh.close()
96+
withopen(filename,'r')asfh:
97+
assert_equal("70c379b63ffa0795fdbfbc128e5a2818397b7ef8",
98+
self.git.hash_object(istream=fh,stdin=True))
10099

101100
@patch.object(Git,'execute')
102101
deftest_it_ignores_false_kwargs(self,git):
@@ -200,10 +199,9 @@ def test_environment(self, rw_dir):
200199
self.assertEqual(self.git.environment(), {})
201200

202201
path=os.path.join(rw_dir,'failing-script.sh')
203-
stream=open(path,'wt')
204-
stream.write("#!/usr/bin/env sh\n"+
205-
"echo FOO\n")
206-
stream.close()
202+
withopen(path,'wt')asstream:
203+
stream.write("#!/usr/bin/env sh\n"
204+
"echo FOO\n")
207205
os.chmod(path,0o777)
208206

209207
rw_repo=Repo.init(os.path.join(rw_dir,'repo'))

‎git/test/test_remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def tearDown(self):
105105
gc.collect()
106106

107107
def_print_fetchhead(self,repo):
108-
fp=open(os.path.join(repo.git_dir,"FETCH_HEAD"))
109-
fp.close()
108+
withopen(os.path.join(repo.git_dir,"FETCH_HEAD")):
109+
pass
110110

111111
def_do_test_fetch_result(self,results,remote):
112112
# self._print_fetchhead(remote.repo)

‎git/test/test_repo.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,16 @@ def test_git_file(self, rwrepo):
781781
real_path_abs=os.path.abspath(join_path_native(rwrepo.working_tree_dir,'.real'))
782782
os.rename(rwrepo.git_dir,real_path_abs)
783783
git_file_path=join_path_native(rwrepo.working_tree_dir,'.git')
784-
open(git_file_path,'wb').write(fixture('git_file'))
784+
withopen(git_file_path,'wb')asfp:
785+
fp.write(fixture('git_file'))
785786

786787
# Create a repo and make sure it's pointing to the relocated .git directory.
787788
git_file_repo=Repo(rwrepo.working_tree_dir)
788789
self.assertEqual(os.path.abspath(git_file_repo.git_dir),real_path_abs)
789790

790791
# Test using an absolute gitdir path in the .git file.
791-
open(git_file_path,'wb').write(('gitdir: %s\n'%real_path_abs).encode('ascii'))
792+
withopen(git_file_path,'wb')asfp:
793+
fp.write(('gitdir: %s\n'%real_path_abs).encode('ascii'))
792794
git_file_repo=Repo(rwrepo.working_tree_dir)
793795
self.assertEqual(os.path.abspath(git_file_repo.git_dir),real_path_abs)
794796

‎git/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def _obtain_lock_or_raise(self):
576576
try:
577577
flags=os.O_WRONLY|os.O_CREAT|os.O_EXCL
578578
ifis_win:
579-
flags|=getattr(os,'O_SHORT_LIVED')
579+
flags|=os.O_SHORT_LIVED
580580
fd=os.open(lock_file,flags,0)
581581
os.close(fd)
582582
exceptOSErrorase:

‎setup.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
importsys
1616
fromosimportpath
1717

18-
v=open(path.join(path.dirname(__file__),'VERSION'))
19-
VERSION=v.readline().strip()
20-
v.close()
18+
withopen(path.join(path.dirname(__file__),'VERSION'))asv:
19+
VERSION=v.readline().strip()
2120

2221
withopen('requirements.txt')asreqs_file:
2322
requirements=reqs_file.read().splitlines()
@@ -50,22 +49,18 @@ def make_release_tree(self, base_dir, files):
5049
def_stamp_version(filename):
5150
found,out=False,list()
5251
try:
53-
f=open(filename,'r')
52+
withopen(filename,'r')asf:
53+
forlineinf:
54+
if'__version__ ='inline:
55+
line=line.replace("'git'","'%s'"%VERSION)
56+
found=True
57+
out.append(line)
5458
except (IOError,OSError):
5559
print("Couldn't find file %s to stamp version"%filename,file=sys.stderr)
56-
return
57-
# END handle error, usually happens during binary builds
58-
forlineinf:
59-
if'__version__ ='inline:
60-
line=line.replace("'git'","'%s'"%VERSION)
61-
found=True
62-
out.append(line)
63-
f.close()
6460

6561
iffound:
66-
f=open(filename,'w')
67-
f.writelines(out)
68-
f.close()
62+
withopen(filename,'w')asf:
63+
f.writelines(out)
6964
else:
7065
print("WARNING: Couldn't find version line in file %s"%filename,file=sys.stderr)
7166

@@ -109,8 +104,7 @@ def _stamp_version(filename):
109104
install_requires=install_requires,
110105
test_requirements=test_requires+install_requires,
111106
zip_safe=False,
112-
long_description="""\
113-
GitPython is a python library used to interact with Git repositories""",
107+
long_description="""GitPython is a python library used to interact with Git repositories""",
114108
classifiers=[
115109
# Picked from
116110
# http://pypi.python.org/pypi?:action=list_classifiers

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp