Skip to content

Commit eafaeee

Browse files
committed
Never modify sys.path
This removes the logic that, under some (most) circumstances when not using a version from PyPI, would insert the location of the gitdb git-submodule near the front of sys.path. (As noted in #1717, the specific way this was being done was not causing the git-submodule's version of gitdb to actually be used. But it was still modifying sys.path, which this now prevents.) The installation test, which had verified the insertion into sys.path, is modified accordingly, except that for now the check that the very first sys.path entry is undisturbed is kept in place.
1 parent 44102f3 commit eafaeee

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

git/__init__.py

-28
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,12 @@
77
# @PydevCodeAnalysisIgnore
88
from git.exc import * # @NoMove @IgnorePep8
99
import inspect
10-
import os
11-
import sys
12-
import os.path as osp
1310

1411
from typing import Optional
1512
from git.types import PathLike
1613

1714
__version__ = "git"
1815

19-
20-
# { Initialization
21-
def _init_externals() -> None:
22-
"""Initialize external projects by putting them into the path"""
23-
if __version__ == "git" and "PYOXIDIZER" not in os.environ:
24-
sys.path.insert(1, osp.join(osp.dirname(__file__), "ext", "gitdb"))
25-
26-
try:
27-
import gitdb
28-
except ImportError as e:
29-
raise ImportError("'gitdb' could not be found in your PYTHONPATH") from e
30-
# END verify import
31-
32-
33-
# } END initialization
34-
35-
36-
#################
37-
_init_externals()
38-
#################
39-
40-
# { Imports
41-
4216
try:
4317
from git.config import GitConfigParser # @NoMove @IgnorePep8
4418
from git.objects import * # @NoMove @IgnorePep8
@@ -59,8 +33,6 @@ def _init_externals() -> None:
5933
except GitError as _exc:
6034
raise ImportError("%s: %s" % (_exc.__class__.__name__, _exc)) from _exc
6135

62-
# } END imports
63-
6436
# __all__ must be statically defined by py.typed support
6537
# __all__ = [name for name, obj in locals().items() if not (name.startswith("_") or inspect.ismodule(obj))]
6638
__all__ = [

test/test_installation.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def setUp_venv(self, rw_dir):
2525
@with_rw_directory
2626
def test_installation(self, rw_dir):
2727
self.setUp_venv(rw_dir)
28+
2829
result = subprocess.run(
2930
[self.pip, "install", "."],
3031
stdout=subprocess.PIPE,
@@ -35,12 +36,32 @@ def test_installation(self, rw_dir):
3536
result.returncode,
3637
msg=result.stderr or result.stdout or "Can't install project",
3738
)
38-
result = subprocess.run([self.python, "-c", "import git"], stdout=subprocess.PIPE, cwd=self.sources)
39+
40+
result = subprocess.run(
41+
[self.python, "-c", "import git"],
42+
stdout=subprocess.PIPE,
43+
cwd=self.sources,
44+
)
3945
self.assertEqual(
4046
0,
4147
result.returncode,
4248
msg=result.stderr or result.stdout or "Selftest failed",
4349
)
50+
51+
result = subprocess.run(
52+
[self.python, "-c", "import gitdb; import smmap"],
53+
stdout=subprocess.PIPE,
54+
cwd=self.sources,
55+
)
56+
self.assertEqual(
57+
0,
58+
result.returncode,
59+
msg=result.stderr or result.stdout or "Dependencies not installed",
60+
)
61+
62+
# Even IF gitdb or any other dependency is supplied during development
63+
# by inserting its location into PYTHONPATH or otherwise patched into
64+
# sys.path, make sure it is not wrongly inserted as the *first* entry.
4465
result = subprocess.run(
4566
[self.python, "-c", "import sys;import git; print(sys.path)"],
4667
stdout=subprocess.PIPE,
@@ -53,4 +74,3 @@ def test_installation(self, rw_dir):
5374
syspath[0],
5475
msg="Failed to follow the conventions for https://docs.python.org/3/library/sys.html#sys.path",
5576
)
56-
self.assertTrue(syspath[1].endswith("gitdb"), msg="Failed to add gitdb to sys.path")

0 commit comments

Comments
 (0)