Skip to content

Remove Python 2 compatibility shims #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ff57c04
Remove str import from builtins
Harmon758 Feb 7, 2020
e0bf255
Remove unnecessary check for sys.getfilesystemencoding
Harmon758 Feb 7, 2020
142c779
Remove and replace compat.FileType
Harmon758 Feb 7, 2020
584ab08
Remove compat.byte_ord
Harmon758 Feb 7, 2020
e564c2f
Remove and replace compat.bchr
Harmon758 Feb 7, 2020
5444787
Remove and replace compat.mviter
Harmon758 Feb 7, 2020
3f21cb1
Remove compat.range
Harmon758 Feb 7, 2020
d0d2a86
Remove and replace compat.xrange
Harmon758 Feb 7, 2020
91e91b2
Remove and replace compat.unicode
Harmon758 Feb 7, 2020
c30880d
Remove Python 2 check for compat.defenc
Harmon758 Feb 7, 2020
2c4d556
Remove and replace compat.binary_type
Harmon758 Feb 7, 2020
8e55323
Remove and replace compat._unichr
Harmon758 Feb 7, 2020
18fc6b2
Remove and replace compat.bytes_chr
Harmon758 Feb 7, 2020
9615ada
Remove surrogateescape error handler for Python 2
Harmon758 Feb 7, 2020
5549ffe
Remove and replace compat.UnicodeMixin
Harmon758 Feb 7, 2020
60c8dc2
Remove checks for Python 2 and/or 3
Harmon758 Feb 7, 2020
6005b89
Remove Python 2 test
Harmon758 Feb 7, 2020
952eaad
Remove compat.PY3
Harmon758 Feb 7, 2020
266187b
Remove and replace compat.MAXSIZE
Harmon758 Feb 7, 2020
8a8b24e
Remove and replace compat.izip
Harmon758 Feb 7, 2020
07df7c9
Remove and replace compat.string_types
Harmon758 Feb 7, 2020
2f31261
Remove and replace compat.text_type
Harmon758 Feb 7, 2020
369de3d
Remove no longer used compat imports
Harmon758 Feb 7, 2020
92348df
Remove no longer used imports in tests
Harmon758 Feb 7, 2020
ebcdb8b
Remove attempt to import ConfigParser for Python 2
Harmon758 Feb 7, 2020
7f250ca
Remove check for Python 2.7
Harmon758 Feb 7, 2020
21d56e2
Remove unnecessary check for logging.NullHandler for Python 2.6
Harmon758 Feb 7, 2020
d96688f
Improve setup.py python_requires
Harmon758 Feb 7, 2020
d0cd5bf
Remove unnecessary check for PermissionError for Python < 3.3
Harmon758 Feb 7, 2020
a611adc
Add to AUTHORS
Harmon758 Feb 7, 2020
d0899a0
Fix requirements.txt formatting
Harmon758 Feb 7, 2020
c5f5911
Remove now unused is_invoking_git variable in test
Harmon758 Feb 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove checks for Python 2 and/or 3
  • Loading branch information
Harmon758 committed Feb 7, 2020
commit 60c8dc2bd98ad1c8dc3a575e4b5e29b277bdd636
7 changes: 0 additions & 7 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
string_types,
defenc,
force_bytes,
PY3,
safe_decode,
is_posix,
is_win,
Expand Down Expand Up @@ -916,18 +915,12 @@ def transform_kwargs(self, split_single_char_options=True, **kwargs):
@classmethod
def __unpack_args(cls, arg_list):
if not isinstance(arg_list, (list, tuple)):
# This is just required for unicode conversion, as subprocess can't handle it
# However, in any other case, passing strings (usually utf-8 encoded) is totally fine
if not PY3 and isinstance(arg_list, str):
return [arg_list.encode(defenc)]
return [str(arg_list)]

outlist = []
for arg in arg_list:
if isinstance(arg_list, (list, tuple)):
outlist.extend(cls.__unpack_args(arg))
elif not PY3 and isinstance(arg_list, str):
outlist.append(arg_list.encode(defenc))
# END recursion
else:
outlist.append(str(arg))
Expand Down
4 changes: 0 additions & 4 deletions git/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,5 @@ class metaclass(meta):
def __new__(cls, name, nbases, d):
if nbases is None:
return type.__new__(cls, name, (), d)
# There may be clients who rely on this attribute to be set to a reasonable value, which is why
# we set the __metaclass__ attribute explicitly
if not PY3 and '___metaclass__' not in d:
d['__metaclass__'] = meta
return meta(name, bases, d)
return metaclass(meta.__name__ + 'Helper', None, {})
5 changes: 1 addition & 4 deletions git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
defenc,
force_text,
with_metaclass,
PY3,
is_win,
)
from git.util import LockFile
Expand Down Expand Up @@ -372,9 +371,7 @@ def string_decode(v):
v = v[:-1]
# end cut trailing escapes to prevent decode error

if PY3:
return v.encode(defenc).decode('unicode_escape')
return v.decode('string_escape')
return v.encode(defenc).decode('unicode_escape')
# end
# end

Expand Down
12 changes: 2 additions & 10 deletions git/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import re

from git.cmd import handle_process_output
from git.compat import (
defenc,
PY3
)
from git.compat import defenc
from git.util import finalize_process, hex_to_bin

from .objects.blob import Blob
Expand All @@ -27,10 +24,7 @@
def _octal_repl(matchobj):
value = matchobj.group(1)
value = int(value, 8)
if PY3:
value = bytes(bytearray((value,)))
else:
value = chr(value)
value = bytes(bytearray((value,)))
return value


Expand Down Expand Up @@ -369,8 +363,6 @@ def __str__(self):
# Python2 silliness: have to assure we convert our likely to be unicode object to a string with the
# right encoding. Otherwise it tries to convert it using ascii, which may fail ungracefully
res = h + msg
if not PY3:
res = res.encode(defenc)
# end
return res

Expand Down
3 changes: 1 addition & 2 deletions git/index/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from git.cmd import PROC_CREATIONFLAGS, handle_process_output
from git.compat import (
PY3,
defenc,
force_text,
force_bytes,
Expand Down Expand Up @@ -73,7 +72,7 @@ def run_commit_hook(name, index, *args):
return

env = os.environ.copy()
env['GIT_INDEX_FILE'] = safe_decode(index.path) if PY3 else safe_encode(index.path)
env['GIT_INDEX_FILE'] = safe_decode(index.path)
env['GIT_EDITOR'] = ':'
try:
cmd = subprocess.Popen([hp] + list(args),
Expand Down
5 changes: 1 addition & 4 deletions git/objects/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
tree_to_stream
)

from git.compat import PY3

if PY3:
cmp = lambda a, b: (a > b) - (a < b)
cmp = lambda a, b: (a > b) - (a < b)

__all__ = ("TreeModifier", "Tree")

Expand Down
8 changes: 1 addition & 7 deletions git/refs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import time

from git.compat import (
PY3,
string_types,
defenc
)
Expand Down Expand Up @@ -35,12 +34,7 @@ class RefLogEntry(tuple):

def __repr__(self):
"""Representation of ourselves in git reflog format"""
res = self.format()
if PY3:
return res
# repr must return a string, which it will auto-encode from unicode using the default encoding.
# This usually fails, so we encode ourselves
return res.encode(defenc)
return self.format()

def format(self):
""":return: a string suitable to be placed in a reflog file"""
Expand Down
8 changes: 2 additions & 6 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from git.compat import (
text_type,
defenc,
PY3,
safe_decode,
is_win,
)
Expand Down Expand Up @@ -691,11 +690,8 @@ def _get_untracked_files(self, *args, **kwargs):
# Special characters are escaped
if filename[0] == filename[-1] == '"':
filename = filename[1:-1]
if PY3:
# WHATEVER ... it's a mess, but works for me
filename = filename.encode('ascii').decode('unicode_escape').encode('latin1').decode(defenc)
else:
filename = filename.decode('string_escape').decode(defenc)
# WHATEVER ... it's a mess, but works for me
filename = filename.encode('ascii').decode('unicode_escape').encode('latin1').decode(defenc)
untracked_files.append(filename)
finalize_process(proc)
return untracked_files
Expand Down
1 change: 0 additions & 1 deletion git/test/test_fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def test_tree_entries_from_data_with_failing_name_decode_py2(self):
r = tree_entries_from_data(b'100644 \x9f\0aaa')
assert r == [('aaa', 33188, u'\udc9f')], r

@skipIf(not PY3, 'odd types returned ... maybe figure it out one day')
def test_tree_entries_from_data_with_failing_name_decode_py3(self):
r = tree_entries_from_data(b'100644 \x9f\0aaa')
assert r == [(b'aaa', 33188, '\udc9f')], r
12 changes: 3 additions & 9 deletions git/test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Repo,
cmd
)
from git.compat import PY3, is_darwin
from git.compat import is_darwin
from git.test.lib import (
TestBase,
patch,
Expand Down Expand Up @@ -61,18 +61,12 @@ def test_call_process_calls_execute(self, git):

def test_call_unpack_args_unicode(self):
args = Git._Git__unpack_args(u'Unicode€™')
if PY3:
mangled_value = 'Unicode\u20ac\u2122'
else:
mangled_value = 'Unicode\xe2\x82\xac\xe2\x84\xa2'
mangled_value = 'Unicode\u20ac\u2122'
assert_equal(args, [mangled_value])

def test_call_unpack_args(self):
args = Git._Git__unpack_args(['git', 'log', '--', u'Unicode€™'])
if PY3:
mangled_value = 'Unicode\u20ac\u2122'
else:
mangled_value = 'Unicode\xe2\x82\xac\xe2\x84\xa2'
mangled_value = 'Unicode\u20ac\u2122'
assert_equal(args, ['git', 'log', '--', mangled_value])

@raises(GitCommandError)
Expand Down
6 changes: 1 addition & 5 deletions git/test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
GitCommandError,
CheckoutError,
)
from git.compat import string_types, is_win, PY3
from git.compat import string_types, is_win
from git.exc import (
HookExecutionError,
InvalidGitRepositoryError
Expand Down Expand Up @@ -821,10 +821,6 @@ def test_index_bare_add(self, rw_bare_repo):
asserted = True
assert asserted, "Adding using a filename is not correctly asserted."

@skipIf(HIDE_WINDOWS_KNOWN_ERRORS and not PY3, r"""
FIXME: File "C:\projects\gitpython\git\util.py", line 125, in to_native_path_linux
return path.replace('\\', '/')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)""")
@with_rw_directory
def test_add_utf8P_path(self, rw_dir):
# NOTE: fp is not a Unicode object in python 2 (which is the source of the problem)
Expand Down
6 changes: 0 additions & 6 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
GitCommandError
)
from git.compat import (
PY3,
is_win,
string_types,
win_encode,
Expand Down Expand Up @@ -526,11 +525,6 @@ def test_untracked_files(self, rwrepo):
num_test_untracked += join_path_native(base, utfile) in files
self.assertEqual(len(files), num_test_untracked)

if is_win and not PY3 and is_invoking_git:
## On Windows, shell needed when passing unicode cmd-args.
#
repo_add = fnt.partial(repo_add, shell=True)
untracked_files = [win_encode(f) for f in untracked_files]
repo_add(untracked_files)
self.assertEqual(len(rwrepo.untracked_files), (num_recently_untracked - len(files)))
# end for each run
Expand Down
6 changes: 1 addition & 5 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@

from .compat import (
MAXSIZE,
defenc,
PY3
defenc
)
from .exc import InvalidGitRepositoryError

Expand Down Expand Up @@ -592,9 +591,6 @@ def _main_actor(cls, env_name, env_email, config_reader=None):
('email', env_email, cls.conf_email, default_email)):
try:
val = os.environ[evar]
if not PY3:
val = val.decode(defenc)
# end assure we don't get 'invalid strings'
setattr(actor, attr, val)
except KeyError:
if config_reader is not None:
Expand Down