Skip to content

Commit 9b11ed2

Browse files
committed
Merge pull request #1854 from Ivoz/compat-cleaning
Remove unnecessary imports, move some to six
2 parents 31e5f41 + b7770c5 commit 9b11ed2

File tree

9 files changed

+28
-41
lines changed

9 files changed

+28
-41
lines changed

pip/baseparser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import textwrap
77
from distutils.util import strtobool
88

9-
from pip.compat import ConfigParser, string_types
9+
from pip._vendor.six import string_types
10+
from pip._vendor.six.moves import configparser
1011
from pip.locations import (
1112
default_config_file, default_config_basename, running_under_virtualenv,
1213
)
@@ -128,7 +129,7 @@ class ConfigOptionParser(CustomOptionParser):
128129
configuration files and environmental variables"""
129130

130131
def __init__(self, *args, **kwargs):
131-
self.config = ConfigParser.RawConfigParser()
132+
self.config = configparser.RawConfigParser()
132133
self.name = kwargs.pop('name')
133134
self.files = self.get_config_files()
134135
if self.files:

pip/commands/search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
from pip.basecommand import Command, SUCCESS
55
from pip.util import get_terminal_size
66
from pip.log import logger
7-
from pip.compat import xmlrpclib, reduce, cmp
7+
from pip.compat import reduce, cmp
88
from pip.exceptions import CommandError
99
from pip.status_codes import NO_MATCHES_FOUND
1010
from pip._vendor import pkg_resources
11+
from pip._vendor.six.moves import xmlrpc_client
1112
from distutils.version import StrictVersion, LooseVersion
1213

1314

@@ -48,7 +49,7 @@ def run(self, options, args):
4849
return NO_MATCHES_FOUND
4950

5051
def search(self, query, index_url):
51-
pypi = xmlrpclib.ServerProxy(index_url)
52+
pypi = xmlrpc_client.ServerProxy(index_url)
5253
hits = pypi.search({'name': query, 'summary': query}, 'or')
5354
return hits
5455

pip/compat.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,13 @@ def binary(s):
4444

4545

4646
if sys.version_info >= (3,):
47-
from io import StringIO, BytesIO
47+
from io import StringIO
4848
from functools import reduce
4949
from urllib.error import URLError, HTTPError
50-
from queue import Queue, Empty
5150
from urllib.request import url2pathname, urlretrieve, pathname2url
52-
from email import message as emailmessage
5351
import urllib.parse as urllib
5452
import urllib.request as urllib2
55-
import configparser as ConfigParser
56-
import xmlrpc.client as xmlrpclib
5753
import urllib.parse as urlparse
58-
import http.client as httplib
5954

6055
def cmp(a, b):
6156
return (a > b) - (a < b)
@@ -75,21 +70,13 @@ def console_to_str(s):
7570
def get_http_message_param(http_message, param, default_value):
7671
return http_message.get_param(param, default_value)
7772

78-
bytes = bytes
79-
string_types = (str,)
80-
raw_input = input
8173
else:
8274
from cStringIO import StringIO
8375
from urllib2 import URLError, HTTPError
84-
from Queue import Queue, Empty
8576
from urllib import url2pathname, urlretrieve, pathname2url
86-
from email import Message as emailmessage
8777
import urllib
8878
import urllib2
8979
import urlparse
90-
import ConfigParser
91-
import xmlrpclib
92-
import httplib
9380

9481
def b(s):
9582
return s
@@ -104,12 +91,8 @@ def get_http_message_param(http_message, param, default_value):
10491
result = http_message.getparam(param)
10592
return result or default_value
10693

107-
bytes = str
108-
string_types = (basestring,)
10994
reduce = reduce
11095
cmp = cmp
111-
raw_input = raw_input
112-
BytesIO = StringIO
11396

11497

11598
def get_path_uid(path):

pip/download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import pip
1414

15-
from pip.compat import urllib, urlparse, raw_input
15+
from pip.compat import urllib, urlparse
1616
from pip.exceptions import InstallationError, HashMismatch
1717
from pip.util import (splitext, rmtree, format_size, display_path,
1818
backup_dir, ask_path_exists, unpack_file)
@@ -117,7 +117,7 @@ def handle_401(self, resp, **kwargs):
117117
parsed = urlparse.urlparse(resp.url)
118118

119119
# Prompt the user for a new username and password
120-
username = raw_input("User for %s: " % parsed.netloc)
120+
username = six.moves.input("User for %s: " % parsed.netloc)
121121
password = getpass.getpass("Password: ")
122122

123123
# Store the new username and password to use for future requests

pip/req/req_install.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
import pip.wheel
1212
from pip._vendor import pkg_resources, six
13-
from pip.compat import (
14-
urllib, ConfigParser, string_types,
15-
)
13+
from pip._vendor.six.moves import configparser
14+
from pip.compat import urllib
1615
from pip.download import is_url, url_to_path, path_to_url, is_archive_file
1716
from pip.exceptions import (
1817
InstallationError, UninstallationError, UnsupportedWheel,
@@ -38,7 +37,7 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,
3837
url=None, as_egg=False, update=True, prereleases=None,
3938
editable_options=None, pycompile=True):
4039
self.extras = ()
41-
if isinstance(req, string_types):
40+
if isinstance(req, six.string_types):
4241
req = pkg_resources.Requirement.parse(req)
4342
self.extras = req.extras
4443
self.req = req
@@ -168,7 +167,7 @@ def __str__(self):
168167
if self.satisfied_by is not None:
169168
s += ' in %s' % display_path(self.satisfied_by.location)
170169
if self.comes_from:
171-
if isinstance(self.comes_from, string_types):
170+
if isinstance(self.comes_from, six.string_types):
172171
comes_from = self.comes_from
173172
else:
174173
comes_from = self.comes_from.from_path()
@@ -181,7 +180,7 @@ def from_path(self):
181180
return None
182181
s = str(self.req)
183182
if self.comes_from:
184-
if isinstance(self.comes_from, string_types):
183+
if isinstance(self.comes_from, six.string_types):
185184
comes_from = self.comes_from
186185
else:
187186
comes_from = self.comes_from.from_path()
@@ -619,7 +618,7 @@ def uninstall(self, auto_confirm=False):
619618

620619
# find console_scripts
621620
if dist.has_metadata('entry_points.txt'):
622-
config = ConfigParser.SafeConfigParser()
621+
config = configparser.SafeConfigParser()
623622
config.readfp(
624623
FakeFile(dist.get_metadata_lines('entry_points.txt'))
625624
)

pip/util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111

1212
from pip.exceptions import InstallationError, BadCommand
1313
from pip.compat import(
14-
string_types, raw_input, console_to_str, stdlib_pkgs
14+
console_to_str, stdlib_pkgs
1515
)
1616
from pip.locations import (
1717
site_packages, user_site, running_under_virtualenv, virtualenv_no_global,
1818
write_delete_marker_file
1919
)
2020
from pip.log import logger
21-
from pip._vendor import pkg_resources
21+
from pip._vendor import pkg_resources, six
2222
from pip._vendor.distlib import version
23+
from pip._vendor.six.moves import input
2324

2425
__all__ = ['rmtree', 'display_path', 'backup_dir',
2526
'find_command', 'ask', 'Inf',
@@ -85,7 +86,7 @@ def find_command(cmd, paths=None, pathext=None):
8586
"""Searches the PATH for the given command and returns its path"""
8687
if paths is None:
8788
paths = os.environ.get('PATH', '').split(os.pathsep)
88-
if isinstance(paths, string_types):
89+
if isinstance(paths, six.string_types):
8990
paths = [paths]
9091
# check if there are funny path extensions for executables, e.g. Windows
9192
if pathext is None:
@@ -131,7 +132,7 @@ def ask(message, options):
131132
'No input was expected ($PIP_NO_INPUT set); question: %s' %
132133
message
133134
)
134-
response = raw_input(message)
135+
response = input(message)
135136
response = response.strip().lower()
136137
if response not in options:
137138
print(

pip/vcs/mercurial.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pip.log import logger
88
from pip.vcs import vcs, VersionControl
99
from pip.download import path_to_url
10-
from pip.compat import ConfigParser
10+
from pip._vendor.six.moves import configparser
1111

1212

1313
class Mercurial(VersionControl):
@@ -29,14 +29,14 @@ def export(self, location):
2929

3030
def switch(self, dest, url, rev_options):
3131
repo_config = os.path.join(dest, self.dirname, 'hgrc')
32-
config = ConfigParser.SafeConfigParser()
32+
config = configparser.SafeConfigParser()
3333
try:
3434
config.read(repo_config)
3535
config.set('paths', 'default', url)
3636
config_file = open(repo_config, 'w')
3737
config.write(config_file)
3838
config_file.close()
39-
except (OSError, ConfigParser.NoSectionError) as exc:
39+
except (OSError, configparser.NoSectionError) as exc:
4040
logger.warn(
4141
'Could not switch Mercurial repository to %s: %s'
4242
% (url, exc))

pip/wheel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
from base64 import urlsafe_b64encode
1616
from email.parser import Parser
1717

18-
from pip.compat import ConfigParser, StringIO, binary
18+
from pip.compat import StringIO, binary
1919
from pip.exceptions import InvalidWheelFilename, UnsupportedWheel
2020
from pip.locations import distutils_scheme
2121
from pip.log import logger
2222
from pip import pep425tags
2323
from pip.util import call_subprocess, normalize_path, make_path_relative
2424
from pip._vendor.distlib.scripts import ScriptMaker
2525
from pip._vendor import pkg_resources
26+
from pip._vendor.six.moves import configparser
2627

2728

2829
wheel_ext = '.whl'
@@ -114,7 +115,7 @@ def get_entrypoints(filename):
114115
data.write("\n")
115116
data.seek(0)
116117

117-
cp = ConfigParser.RawConfigParser()
118+
cp = configparser.RawConfigParser()
118119
cp.readfp(data)
119120

120121
console = {}

tests/unit/test_download.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import hashlib
22
import os
3+
from io import BytesIO
34
from shutil import rmtree, copy
45
from tempfile import mkdtemp
56

67
from mock import Mock, patch
78
import pytest
89

910
import pip
10-
from pip.compat import BytesIO, b, pathname2url
11+
from pip.compat import b, pathname2url
1112
from pip.exceptions import HashMismatch
1213
from pip.download import (
1314
PipSession, SafeFileCache, path_to_url, unpack_http_url, url_to_path,

0 commit comments

Comments
 (0)