summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@jelmer.uk>2016-04-18 17:25:20 +0000
committerJelmer Vernooij <jelmer@jelmer.uk>2016-04-18 17:25:20 +0000
commitcab83db62d795079a0b88ab05179568142a96ab9 (patch)
tree2866eb6290bfc197c9a15e13be1c19d65ff22259
parent7bcadc85eb177e47383f7335526132e76be20a71 (diff)
downloadpython-fastimport-git-cab83db62d795079a0b88ab05179568142a96ab9.tar.gz
Collapse import statements, remove unnecessary helpers by inlining them.
-rw-r--r--fastimport/commands.py22
-rw-r--r--fastimport/helpers.py38
-rw-r--r--fastimport/parser.py12
-rw-r--r--fastimport/tests/test_commands.py9
4 files changed, 27 insertions, 54 deletions
diff --git a/fastimport/commands.py b/fastimport/commands.py
index c6cbb53..c8fb25e 100644
--- a/fastimport/commands.py
+++ b/fastimport/commands.py
@@ -21,13 +21,13 @@ a fast-import stream.
from __future__ import division
import re
-import sys
import stat
+import sys
-from fastimport.helpers import utf8_bytes_string
-from fastimport.helpers import is_unicode
-from fastimport.helpers import PY2, PY3, PY3_OR_LATER
-from fastimport.helpers import newobject as object
+from fastimport.helpers import (
+ newobject as object,
+ utf8_bytes_string,
+ )
# There is a bug in git 1.5.4.3 and older by which unquoting a string consumes
@@ -69,7 +69,7 @@ class ImportCommand(object):
return repr(self)
def __repr__(self):
- if PY2:
+ if sys.version_info[0] == 2:
return self.__bytes__()
else:
return bytes(self).decode('utf8')
@@ -451,8 +451,8 @@ def check_path(path):
raise ValueError("illegal path '%s'" % path)
if (
- (PY3_OR_LATER and not isinstance(path, bytes)) and
- (PY2 and not isinstance(path, str))
+ (sys.version_info[0] >= 3 and not isinstance(path, bytes)) and
+ (sys.version_info[0] == 2 and not isinstance(path, str))
):
raise TypeError("illegale type for path '%r'" % path)
@@ -490,13 +490,11 @@ def format_who_when(fields):
else:
sep = b' '
- if is_unicode(name):
- name = utf8_bytes_string(name)
+ name = utf8_bytes_string(name)
email = fields[1]
- if is_unicode(email):
- email = utf8_bytes_string(email)
+ email = utf8_bytes_string(email)
result = b'%s%s<%s> %d %s' % (name, sep, email, fields[2], offset_str)
diff --git a/fastimport/helpers.py b/fastimport/helpers.py
index f016a8f..c27c436 100644
--- a/fastimport/helpers.py
+++ b/fastimport/helpers.py
@@ -16,18 +16,6 @@
"""Miscellaneous useful stuff."""
import sys
-# Python 2/3 helpers
-PY3 = sys.version_info[0] == 3
-PY2 = sys.version_info[0] == 2
-PY3_OR_LATER = sys.version_info[0] >= 3
-
-if PY2:
- import itertools
- newmap = itertools.imap
-else:
- import builtins
- newmap = builtins.map
-
def _common_path_and_rest(l1, l2, common=[]):
# From http://code.activestate.com/recipes/208993/
@@ -112,32 +100,18 @@ def is_inside_any(dir_list, fname):
def utf8_bytes_string(s):
"""Convert a string to a bytes string encoded in utf8"""
- utf8_bytes = b''
-
- if PY2:
- utf8_bytes = s.encode('utf8')
+ if sys.version_info[0] == 2:
+ return s.encode('utf8')
else:
if isinstance(s, str):
- utf8_bytes = bytes(s, encoding='utf8')
+ return bytes(s, encoding='utf8')
else:
- utf8_bytes = s
-
- return utf8_bytes
-
-
-def is_unicode(s):
- try:
- return (
- (isinstance(s, str) and PY3_OR_LATER) or
- (isinstance(s, unicode) and PY2)
- )
- except NameError:
- return False # unicode isn't defined in python 3
+ return s
def repr_bytes(obj):
"""Return a bytes representation of the object"""
- if PY2:
+ if sys.version_info[0] == 2:
return repr(obj)
else:
return bytes(obj)
@@ -216,4 +190,4 @@ class newobject(object):
"""
Hook for the future.utils.native() function
"""
- return object(self) \ No newline at end of file
+ return object(self)
diff --git a/fastimport/parser.py b/fastimport/parser.py
index 00aacc3..7fd0a08 100644
--- a/fastimport/parser.py
+++ b/fastimport/parser.py
@@ -169,10 +169,10 @@ from fastimport import (
dates,
errors,
)
-from fastimport.helpers import utf8_bytes_string
-from fastimport.helpers import PY2
-from fastimport.helpers import newmap as map
-from fastimport.helpers import newobject as object
+from fastimport.helpers import (
+ newobject as object,
+ utf8_bytes_string,
+ )
## Stream parsing ##
@@ -597,7 +597,7 @@ class ImportParser(LineBasedParser):
parts[1] = parts[1][1:-1]
elif parts[1].startswith(b'"') or parts[1].endswith(b'"'):
self.abort(errors.BadFormat, '?', '?', s)
- return list(map(_unquote_c_string, parts))
+ return [_unquote_c_string(s) for s in parts]
def _mode(self, s):
"""Check file mode format and parse into an int.
@@ -650,7 +650,7 @@ def _unquote_c_string(s):
codecs.decode(match.group(0), 'unicode-escape')
)
- if not PY2 and isinstance(s, bytes):
+ if sys.version_info[0] >= 3 and isinstance(s, bytes):
return ESCAPE_SEQUENCE_BYTES_RE.sub(decode_match, s)
else:
return ESCAPE_SEQUENCE_RE.sub(decode_match, s)
diff --git a/fastimport/tests/test_commands.py b/fastimport/tests/test_commands.py
index f601146..08fd764 100644
--- a/fastimport/tests/test_commands.py
+++ b/fastimport/tests/test_commands.py
@@ -17,9 +17,10 @@
from unittest import TestCase
-from fastimport.helpers import utf8_bytes_string
-from fastimport.helpers import repr_bytes
-from fastimport.helpers import newmap as map
+from fastimport.helpers import (
+ repr_bytes,
+ utf8_bytes_string,
+ )
from fastimport import (
commands,
@@ -424,7 +425,7 @@ data 10
Test test
""" % {
b'user': b'%s <%s> %d %+05d' % committer,
-}, b''.join(map(repr_bytes, commits)))
+}, b''.join([repr_bytes(s) for s in commits]))
class TestPathChecking(TestCase):