Skip to content

Commit d70ba69

Browse files
committedDec 12, 2023
Extract all "import gc" to module level
The gc module was already imported at module scope in git/repo/base.py, since f1a82e4 (#555). Importing the top-level git module or any submodule of it runs that import statement. Because the gc module is already imported, reimporting it is fast. Imports that there is no specific reason to do locally should be at module scope. Having them local decreased readability, in part because of how black inserts a black line between them and gc.collect() calls they are imported to allow. An alternative to this change would be to remove the preexisting top-level "import gc" (there is also another one in the test suite) and replace it with a local import as well. I am unsure if that would affect performance and, if so, whether the effect would be good or bad, since the small delay of the import might potentially be less desirable to an applicaion if it occurs while the work of the application is already in progress. If a gc.collect() call runs as a consequence of a finally block or __del__ method being called during interpreter shutdown, then in (very) rare cases the variable may have been set to None. But this does not appear to have been the intent behind making the imports local. More importantly, a local import should not be expected to succeed, or the imported module usable, in such a situation.
1 parent 68272aa commit d70ba69

File tree

10 files changed

+11
-20
lines changed

10 files changed

+11
-20
lines changed
 

‎git/objects/submodule/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This module is part of GitPython and is released under the
22
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33

4+
import gc
45
from io import BytesIO
56
import logging
67
import os
@@ -1079,8 +1080,6 @@ def remove(
10791080
self._clear_cache()
10801081
wtd = mod.working_tree_dir
10811082
del mod # Release file-handles (Windows).
1082-
import gc
1083-
10841083
gc.collect()
10851084
rmtree(str(wtd))
10861085
# END delete tree if possible

‎test/performance/test_commit.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"""Performance tests for commits (iteration, traversal, and serialization)."""
77

8+
import gc
89
from io import BytesIO
910
from time import time
1011
import sys
@@ -17,8 +18,6 @@
1718

1819
class TestPerformance(TestBigRepoRW, TestCommitSerialization):
1920
def tearDown(self):
20-
import gc
21-
2221
gc.collect()
2322

2423
# ref with about 100 commits in its history.

‎test/performance/test_streams.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Performance tests for data streaming."""
55

6+
import gc
67
import os
78
import subprocess
89
import sys
@@ -92,8 +93,6 @@ def test_large_data_streaming(self, rwrepo):
9293

9394
# del db file so git has something to do.
9495
ostream = None
95-
import gc
96-
9796
gc.collect()
9897
os.remove(db_file)
9998

‎test/test_base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6+
import gc
67
import os
78
import sys
89
import tempfile
@@ -20,8 +21,6 @@
2021

2122
class TestBase(_TestBase):
2223
def tearDown(self):
23-
import gc
24-
2524
gc.collect()
2625

2726
type_tuples = (

‎test/test_docs.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6+
import gc
67
import os
78
import sys
89

@@ -16,8 +17,6 @@
1617

1718
class Tutorials(TestBase):
1819
def tearDown(self):
19-
import gc
20-
2120
gc.collect()
2221

2322
# ACTUALLY skipped by git.util.rmtree (in local onerror function), from the last call to it via

‎test/test_git.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6+
import gc
67
import inspect
78
import logging
89
import os
@@ -34,8 +35,6 @@ def setUpClass(cls):
3435
cls.git = Git(cls.rorepo.working_dir)
3536

3637
def tearDown(self):
37-
import gc
38-
3938
gc.collect()
4039

4140
def _assert_logged_for_popen(self, log_watcher, name, value):

‎test/test_quick_doc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# This module is part of GitPython and is released under the
22
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33

4+
import gc
5+
46
from test.lib import TestBase
57
from test.lib.helper import with_rw_directory
68

79

810
class QuickDoc(TestBase):
911
def tearDown(self):
10-
import gc
11-
1212
gc.collect()
1313

1414
@with_rw_directory

‎test/test_remote.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6+
import gc
67
import os
78
import os.path as osp
89
from pathlib import Path
@@ -105,8 +106,6 @@ def assert_received_message(self):
105106

106107
class TestRemote(TestBase):
107108
def tearDown(self):
108-
import gc
109-
110109
gc.collect()
111110

112111
def _print_fetchhead(self, repo):

‎test/test_repo.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6+
import gc
67
import glob
78
import io
89
from io import BytesIO
@@ -72,8 +73,6 @@ def tearDown(self):
7273
if osp.isfile(lfp):
7374
raise AssertionError("Previous TC left hanging git-lock file: {}".format(lfp))
7475

75-
import gc
76-
7776
gc.collect()
7877

7978
def test_new_should_raise_on_invalid_repo_location(self):

‎test/test_submodule.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33

44
import contextlib
5+
import gc
56
import os
67
import os.path as osp
78
from pathlib import Path
@@ -61,8 +62,6 @@ def update(self, op, cur_count, max_count, message=""):
6162

6263
class TestSubmodule(TestBase):
6364
def tearDown(self):
64-
import gc
65-
6665
gc.collect()
6766

6867
k_subm_current = "c15a6e1923a14bc760851913858a3942a4193cdb"

0 commit comments

Comments
 (0)