Skip to content

Commit 3b7d2ee

Browse files
authored
Merge pull request #7061 from chrahunt/refactor/move-code-out-of-init
Move code out of pip._internal.__init__
2 parents b926290 + 3e98ee8 commit 3b7d2ee

File tree

7 files changed

+54
-47
lines changed

7 files changed

+54
-47
lines changed

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def find_version(*file_paths):
7171
},
7272
entry_points={
7373
"console_scripts": [
74-
"pip=pip._internal:main",
75-
"pip%s=pip._internal:main" % sys.version_info[:1],
76-
"pip%s.%s=pip._internal:main" % sys.version_info[:2],
74+
"pip=pip._internal.main:main",
75+
"pip%s=pip._internal.main:main" % sys.version_info[:1],
76+
"pip%s.%s=pip._internal.main:main" % sys.version_info[:2],
7777
],
7878
},
7979

src/pip/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
path = os.path.dirname(os.path.dirname(__file__))
1414
sys.path.insert(0, path)
1515

16-
from pip._internal import main as _main # isort:skip # noqa
16+
from pip._internal.main import main as _main # isort:skip # noqa
1717

1818
if __name__ == '__main__':
1919
sys.exit(_main())

src/pip/_internal/__init__.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env python
22
from __future__ import absolute_import
33

4-
import locale
5-
import logging
6-
import os
7-
import sys
84
import warnings
95

106
# We ignore certain warnings from urllib3, since they are not relevant to pip's
@@ -15,45 +11,10 @@
1511
)
1612

1713
import pip._internal.utils.inject_securetransport # noqa
18-
from pip._internal.cli.autocompletion import autocomplete
19-
from pip._internal.cli.main_parser import parse_command
20-
from pip._internal.commands import create_command
21-
from pip._internal.exceptions import PipError
22-
from pip._internal.utils import deprecation
2314

2415
# Raised when using --trusted-host.
2516
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
2617
# Raised since socks support depends on PySocks, which may not be installed.
2718
# Barry Warsaw noted (on 2016-06-17) that this should be done before
2819
# importing pip.vcs, which has since moved to pip._internal.vcs.
2920
warnings.filterwarnings("ignore", category=DependencyWarning)
30-
31-
logger = logging.getLogger(__name__)
32-
33-
34-
def main(args=None):
35-
if args is None:
36-
args = sys.argv[1:]
37-
38-
# Configure our deprecation warnings to be sent through loggers
39-
deprecation.install_warning_logger()
40-
41-
autocomplete()
42-
43-
try:
44-
cmd_name, cmd_args = parse_command(args)
45-
except PipError as exc:
46-
sys.stderr.write("ERROR: %s" % exc)
47-
sys.stderr.write(os.linesep)
48-
sys.exit(1)
49-
50-
# Needed for locale.getpreferredencoding(False) to work
51-
# in pip._internal.utils.encoding.auto_decode
52-
try:
53-
locale.setlocale(locale.LC_ALL, '')
54-
except locale.Error as e:
55-
# setlocale can apparently crash if locale are uninitialized
56-
logger.debug("Ignoring error %s when setting locale", e)
57-
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
58-
59-
return command.main(cmd_args)

src/pip/_internal/main.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Primary application entrypoint.
2+
"""
3+
from __future__ import absolute_import
4+
5+
import locale
6+
import logging
7+
import os
8+
import sys
9+
10+
from pip._internal.cli.autocompletion import autocomplete
11+
from pip._internal.cli.main_parser import parse_command
12+
from pip._internal.commands import create_command
13+
from pip._internal.exceptions import PipError
14+
from pip._internal.utils import deprecation
15+
16+
logger = logging.getLogger(__name__)
17+
18+
19+
def main(args=None):
20+
if args is None:
21+
args = sys.argv[1:]
22+
23+
# Configure our deprecation warnings to be sent through loggers
24+
deprecation.install_warning_logger()
25+
26+
autocomplete()
27+
28+
try:
29+
cmd_name, cmd_args = parse_command(args)
30+
except PipError as exc:
31+
sys.stderr.write("ERROR: %s" % exc)
32+
sys.stderr.write(os.linesep)
33+
sys.exit(1)
34+
35+
# Needed for locale.getpreferredencoding(False) to work
36+
# in pip._internal.utils.encoding.auto_decode
37+
try:
38+
locale.setlocale(locale.LC_ALL, '')
39+
except locale.Error as e:
40+
# setlocale can apparently crash if locale are uninitialized
41+
logger.debug("Ignoring error %s when setting locale", e)
42+
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
43+
44+
return command.main(cmd_args)

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import six
1111
from setuptools.wheel import Wheel
1212

13-
import pip._internal
13+
from pip._internal.main import main as pip_entry_point
1414
from tests.lib import DATA_DIR, SRC_DIR, TestData
1515
from tests.lib.path import Path
1616
from tests.lib.scripttest import PipTestEnvironment
@@ -342,7 +342,7 @@ def pip(self, *args):
342342
stdout = io.BytesIO()
343343
sys.stdout = stdout
344344
try:
345-
returncode = pip._internal.main(list(args))
345+
returncode = pip_entry_point(list(args))
346346
except SystemExit as e:
347347
returncode = e.code or 0
348348
finally:

tests/functional/test_completion.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def setup_completion(script, words, cword, cwd=None):
7878

7979
# expect_error is True because autocomplete exists with 1 status code
8080
result = script.run(
81-
'python', '-c', 'import pip._internal;pip._internal.autocomplete()',
81+
'python', '-c',
82+
'from pip._internal.cli.autocompletion import autocomplete;'
83+
'autocomplete()',
8284
expect_error=True,
8385
cwd=cwd,
8486
)

tests/unit/test_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import pytest
55

66
import pip._internal.configuration
7-
from pip._internal import main
87
from pip._internal.commands import create_command
98
from pip._internal.exceptions import PipError
9+
from pip._internal.main import main
1010
from tests.lib.options_helpers import AddFakeCommandMixin
1111

1212

0 commit comments

Comments
 (0)