Skip to content

Commit

Permalink
Never modify sys.path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliahKagan committed Oct 20, 2023
1 parent df777fd commit 00aee49
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
29 changes: 0 additions & 29 deletions git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,11 @@
# flake8: noqa
# @PydevCodeAnalysisIgnore
from git.exc import * # @NoMove @IgnorePep8
import os
import os.path as osp
import sys

from typing import List, Optional, Sequence, Tuple, Union, TYPE_CHECKING
from git.types import PathLike

__version__ = "git"


# { Initialization
def _init_externals() -> None:
"""Initialize external projects by putting them into the path"""
if __version__ == "git" and "PYOXIDIZER" not in os.environ:
sys.path.insert(1, osp.join(osp.dirname(__file__), "ext", "gitdb"))

try:
import gitdb
except ImportError as e:
raise ImportError("'gitdb' could not be found in your PYTHONPATH") from e
# END verify import


# } END initialization


#################
_init_externals()
#################

# { Imports

from gitdb.util import to_hex_sha

try:
Expand All @@ -62,8 +35,6 @@ def _init_externals() -> None:
except GitError as _exc:
raise ImportError("%s: %s" % (_exc.__class__.__name__, _exc)) from _exc

# } END imports

# __all__ must be statically defined by py.typed support
# __all__ = [name for name, obj in locals().items() if not (name.startswith("_") or inspect.ismodule(obj))]
__all__ = [
Expand Down
24 changes: 22 additions & 2 deletions test/test_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def setUp_venv(self, rw_dir):
@with_rw_directory
def test_installation(self, rw_dir):
self.setUp_venv(rw_dir)

result = subprocess.run(
[self.pip, "install", "."],
stdout=subprocess.PIPE,
Expand All @@ -35,12 +36,32 @@ def test_installation(self, rw_dir):
result.returncode,
msg=result.stderr or result.stdout or "Can't install project",
)
result = subprocess.run([self.python, "-c", "import git"], stdout=subprocess.PIPE, cwd=self.sources)

result = subprocess.run(
[self.python, "-c", "import git"],
stdout=subprocess.PIPE,
cwd=self.sources,
)
self.assertEqual(
0,
result.returncode,
msg=result.stderr or result.stdout or "Selftest failed",
)

result = subprocess.run(
[self.python, "-c", "import gitdb; import smmap"],
stdout=subprocess.PIPE,
cwd=self.sources,
)
self.assertEqual(
0,
result.returncode,
msg=result.stderr or result.stdout or "Dependencies not installed",
)

# Even IF gitdb or any other dependency is supplied during development
# by inserting its location into PYTHONPATH or otherwise patched into
# sys.path, make sure it is not wrongly inserted as the *first* entry.
result = subprocess.run(
[self.python, "-c", "import sys;import git; print(sys.path)"],
stdout=subprocess.PIPE,
Expand All @@ -53,4 +74,3 @@ def test_installation(self, rw_dir):
syspath[0],
msg="Failed to follow the conventions for https://docs.python.org/3/library/sys.html#sys.path",
)
self.assertTrue(syspath[1].endswith("gitdb"), msg="Failed to add gitdb to sys.path")

0 comments on commit 00aee49

Please sign in to comment.