Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67bc05e

Browse files
authored
Merge pull request #811 from BoboTiG/fix-resources-warnings
Fix several ResourceWarning: unclosed file
2 parents ff73f6f + 174d3bf commit 67bc05e

13 files changed

Lines changed: 101 additions & 90 deletions

pre_commit/commands/autoupdate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def _update_repo(repo_config, store, tags_only):
7272

7373

7474
def _write_new_config_file(path, output):
75-
original_contents = open(path).read()
75+
with open(path) as f:
76+
original_contents = f.read()
7677
output = remove_defaults(output, CONFIG_SCHEMA)
7778
new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS)
7879

pre_commit/commands/install_uninstall.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def _hook_paths(git_root, hook_type):
3838
def is_our_script(filename):
3939
if not os.path.exists(filename):
4040
return False
41-
contents = io.open(filename).read()
41+
with io.open(filename) as f:
42+
contents = f.read()
4243
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
4344

4445

pre_commit/git.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def get_conflicted_files():
7070
logger.info('Checking merge-conflict files only.')
7171
# Need to get the conflicted files from the MERGE_MSG because they could
7272
# have resolved the conflict by choosing one side or the other
73-
merge_msg = open(os.path.join(get_git_dir('.'), 'MERGE_MSG'), 'rb').read()
73+
with open(os.path.join(get_git_dir('.'), 'MERGE_MSG'), 'rb') as f:
74+
merge_msg = f.read()
7475
merge_conflict_filenames = parse_merge_msg_for_conflicts(merge_msg)
7576

7677
# This will get the rest of the changes made after the merge.

pre_commit/repository.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def _read_state(prefix, venv):
4343
if not os.path.exists(filename):
4444
return None
4545
else:
46-
return json.loads(io.open(filename).read())
46+
with io.open(filename) as f:
47+
return json.loads(f.read())
4748

4849

4950
def _write_state(prefix, venv, state):

testing/fixtures.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def modify_manifest(path):
4040
.pre-commit-hooks.yaml.
4141
"""
4242
manifest_path = os.path.join(path, C.MANIFEST_FILE)
43-
manifest = ordered_load(io.open(manifest_path).read())
43+
with io.open(manifest_path) as f:
44+
manifest = ordered_load(f.read())
4445
yield manifest
4546
with io.open(manifest_path, 'w') as manifest_file:
4647
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
@@ -55,7 +56,8 @@ def modify_config(path='.', commit=True):
5556
.pre-commit-config.yaml
5657
"""
5758
config_path = os.path.join(path, C.CONFIG_FILE)
58-
config = ordered_load(io.open(config_path).read())
59+
with io.open(config_path) as f:
60+
config = ordered_load(f.read())
5961
yield config
6062
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
6163
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
@@ -100,7 +102,8 @@ def make_config_from_repo(repo_path, rev=None, hooks=None, check=True):
100102

101103
def read_config(directory, config_file=C.CONFIG_FILE):
102104
config_path = os.path.join(directory, config_file)
103-
config = ordered_load(io.open(config_path).read())
105+
with io.open(config_path) as f:
106+
config = ordered_load(f.read())
104107
return config
105108

106109

tests/commands/autoupdate_test.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ def test_autoupdate_up_to_date_repo(up_to_date_repo, in_tmpdir, store):
4242
config = make_config_from_repo(up_to_date_repo, check=False)
4343
write_config('.', config)
4444

45-
before = open(C.CONFIG_FILE).read()
45+
with open(C.CONFIG_FILE) as f:
46+
before = f.read()
4647
assert '^$' not in before
4748
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
48-
after = open(C.CONFIG_FILE).read()
49+
with open(C.CONFIG_FILE) as f:
50+
after = f.read()
4951
assert ret == 0
5052
assert before == after
5153

@@ -68,9 +70,11 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
6870

6971
config['rev'] = rev
7072
write_config('.', config)
71-
before = open(C.CONFIG_FILE).read()
73+
with open(C.CONFIG_FILE) as f:
74+
before = f.read()
7275
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
73-
after = open(C.CONFIG_FILE).read()
76+
with open(C.CONFIG_FILE) as f:
77+
after = f.read()
7478
assert ret == 0
7579
assert before != after
7680
assert update_rev in after
@@ -106,9 +110,11 @@ def test_autoupdate_out_of_date_repo(out_of_date_repo, in_tmpdir, store):
106110
)
107111
write_config('.', config)
108112

109-
before = open(C.CONFIG_FILE).read()
113+
with open(C.CONFIG_FILE) as f:
114+
before = f.read()
110115
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
111-
after = open(C.CONFIG_FILE).read()
116+
with open(C.CONFIG_FILE) as f:
117+
after = f.read()
112118
assert ret == 0
113119
assert before != after
114120
# Make sure we don't add defaults
@@ -128,10 +134,12 @@ def test_autoupdate_out_of_date_repo_with_correct_repo_name(
128134
write_config('.', config)
129135

130136
runner = Runner('.', C.CONFIG_FILE)
131-
before = open(C.CONFIG_FILE).read()
137+
with open(C.CONFIG_FILE) as f:
138+
before = f.read()
132139
repo_name = 'file://{}'.format(out_of_date_repo.path)
133140
ret = autoupdate(runner, store, tags_only=False, repos=(repo_name,))
134-
after = open(C.CONFIG_FILE).read()
141+
with open(C.CONFIG_FILE) as f:
142+
after = f.read()
135143
assert ret == 0
136144
assert before != after
137145
assert out_of_date_repo.head_rev in after
@@ -148,10 +156,12 @@ def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
148156
write_config('.', config)
149157

150158
runner = Runner('.', C.CONFIG_FILE)
151-
before = open(C.CONFIG_FILE).read()
159+
with open(C.CONFIG_FILE) as f:
160+
before = f.read()
152161
# It will not update it, because the name doesn't match
153162
ret = autoupdate(runner, store, tags_only=False, repos=('dne',))
154-
after = open(C.CONFIG_FILE).read()
163+
with open(C.CONFIG_FILE) as f:
164+
after = f.read()
155165
assert ret == 0
156166
assert before == after
157167

@@ -171,7 +181,8 @@ def test_does_not_reformat(in_tmpdir, out_of_date_repo, store):
171181
f.write(config)
172182

173183
autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
174-
after = open(C.CONFIG_FILE).read()
184+
with open(C.CONFIG_FILE) as f:
185+
after = f.read()
175186
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_rev)
176187
assert after == expected
177188

@@ -200,7 +211,8 @@ def test_loses_formatting_when_not_detectable(
200211
f.write(config)
201212

202213
autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
203-
after = open(C.CONFIG_FILE).read()
214+
with open(C.CONFIG_FILE) as f:
215+
after = f.read()
204216
expected = (
205217
'repos:\n'
206218
'- repo: {}\n'
@@ -225,7 +237,8 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
225237

226238
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
227239
assert ret == 0
228-
assert 'v1.2.3' in open(C.CONFIG_FILE).read()
240+
with open(C.CONFIG_FILE) as f:
241+
assert 'v1.2.3' in f.read()
229242

230243

231244
@pytest.fixture
@@ -243,7 +256,8 @@ def test_autoupdate_tags_only(tagged_repo_with_more_commits, in_tmpdir, store):
243256

244257
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=True)
245258
assert ret == 0
246-
assert 'v1.2.3' in open(C.CONFIG_FILE).read()
259+
with open(C.CONFIG_FILE) as f:
260+
assert 'v1.2.3' in f.read()
247261

248262

249263
@pytest.fixture
@@ -282,9 +296,11 @@ def test_autoupdate_hook_disappearing_repo(
282296
)
283297
write_config('.', config)
284298

285-
before = open(C.CONFIG_FILE).read()
299+
with open(C.CONFIG_FILE) as f:
300+
before = f.read()
286301
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
287-
after = open(C.CONFIG_FILE).read()
302+
with open(C.CONFIG_FILE) as f:
303+
after = f.read()
288304
assert ret == 1
289305
assert before == after
290306

tests/commands/install_uninstall_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ def test_replace_old_commit_script(tempdir_factory, store):
415415
runner = Runner(path, C.CONFIG_FILE)
416416

417417
# Install a script that looks like our old script
418-
pre_commit_contents = io.open(resource_filename('hook-tmpl')).read()
418+
with io.open(resource_filename('hook-tmpl')) as f:
419+
pre_commit_contents = f.read()
419420
new_contents = pre_commit_contents.replace(
420421
CURRENT_HASH, PRIOR_HASHES[-1],
421422
)

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@
2121
from testing.util import cwd
2222

2323

24+
@pytest.fixture(autouse=True)
25+
def no_warnings(recwarn):
26+
yield
27+
warnings = []
28+
for warning in recwarn: # pragma: no cover
29+
message = str(warning.message)
30+
# ImportWarning: Not importing directory '...' missing __init__(.py)
31+
if not (
32+
isinstance(warning.message, ImportWarning)
33+
and message.startswith('Not importing directory ')
34+
and ' missing __init__' in message
35+
):
36+
warnings.append('{}:{} {}'.format(
37+
warning.filename,
38+
warning.lineno,
39+
message,
40+
))
41+
assert not warnings
42+
43+
2444
@pytest.fixture
2545
def tempdir_factory(tmpdir):
2646
class TmpdirFactory(object):

tests/error_handler_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ def test_log_and_exit(cap_out, mock_store_dir):
8787
)
8888

8989
assert os.path.exists(log_file)
90-
contents = io.open(log_file).read()
91-
assert contents == (
92-
'msg: FatalError: hai\n'
93-
"I'm a stacktrace\n"
94-
)
90+
with io.open(log_file) as f:
91+
assert f.read() == (
92+
'msg: FatalError: hai\n'
93+
"I'm a stacktrace\n"
94+
)
9595

9696

9797
def test_error_handler_non_ascii_exception(mock_store_dir):

tests/languages/all_test.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
from __future__ import unicode_literals
22

3+
import functools
34
import inspect
45

56
import pytest
7+
import six
68

79
from pre_commit.languages.all import all_languages
810
from pre_commit.languages.all import languages
911

1012

13+
if six.PY2: # pragma: no cover
14+
ArgSpec = functools.partial(
15+
inspect.ArgSpec, varargs=None, keywords=None, defaults=None,
16+
)
17+
getargspec = inspect.getargspec
18+
else: # pragma: no cover
19+
ArgSpec = functools.partial(
20+
inspect.FullArgSpec, varargs=None, varkw=None, defaults=None,
21+
kwonlyargs=[], kwonlydefaults=None, annotations={},
22+
)
23+
getargspec = inspect.getfullargspec
24+
25+
1126
@pytest.mark.parametrize('language', all_languages)
1227
def test_install_environment_argspec(language):
13-
expected_argspec = inspect.ArgSpec(
28+
expected_argspec = ArgSpec(
1429
args=['prefix', 'version', 'additional_dependencies'],
15-
varargs=None, keywords=None, defaults=None,
1630
)
17-
argspec = inspect.getargspec(languages[language].install_environment)
31+
argspec = getargspec(languages[language].install_environment)
1832
assert argspec == expected_argspec
1933

2034

@@ -25,28 +39,20 @@ def test_ENVIRONMENT_DIR(language):
2539

2640
@pytest.mark.parametrize('language', all_languages)
2741
def test_run_hook_argpsec(language):
28-
expected_argspec = inspect.ArgSpec(
29-
args=['prefix', 'hook', 'file_args'],
30-
varargs=None, keywords=None, defaults=None,
31-
)
32-
argspec = inspect.getargspec(languages[language].run_hook)
42+
expected_argspec = ArgSpec(args=['prefix', 'hook', 'file_args'])
43+
argspec = getargspec(languages[language].run_hook)
3344
assert argspec == expected_argspec
3445

3546

3647
@pytest.mark.parametrize('language', all_languages)
3748
def test_get_default_version_argspec(language):
38-
expected_argspec = inspect.ArgSpec(
39-
args=[], varargs=None, keywords=None, defaults=None,
40-
)
41-
argspec = inspect.getargspec(languages[language].get_default_version)
49+
expected_argspec = ArgSpec(args=[])
50+
argspec = getargspec(languages[language].get_default_version)
4251
assert argspec == expected_argspec
4352

4453

4554
@pytest.mark.parametrize('language', all_languages)
4655
def test_healthy_argspec(language):
47-
expected_argspec = inspect.ArgSpec(
48-
args=['prefix', 'language_version'],
49-
varargs=None, keywords=None, defaults=None,
50-
)
51-
argspec = inspect.getargspec(languages[language].healthy)
56+
expected_argspec = ArgSpec(args=['prefix', 'language_version'])
57+
argspec = getargspec(languages[language].healthy)
5258
assert argspec == expected_argspec

0 commit comments

Comments
 (0)