Skip to content

Commit c24ab65

Browse files
committed
Fixed minor issues
1 parent 0f6e635 commit c24ab65

6 files changed

Lines changed: 28 additions & 12 deletions

File tree

codext/__common__.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@
1919
from six import binary_type, string_types, text_type, BytesIO
2020
from string import *
2121
from types import FunctionType, ModuleType
22-
try: # Python 2
22+
try: # Python2
2323
import __builtin__ as builtins
24-
from inspect import getargspec as getfullargspec
25-
from string import maketrans
26-
except ImportError: # Python 3
24+
except ImportError:
2725
import builtins
28-
from importlib import reload
26+
try: # Python2
2927
from inspect import getfullargspec
28+
except ImportError:
29+
from inspect import getargspec as getfullargspec
30+
try: # Python2
31+
from string import maketrans
32+
except ImportError:
3033
maketrans = str.maketrans
34+
try: # Python3
35+
from importlib import reload
36+
except ImportError:
37+
pass
3138

3239

3340
__all__ = ["add", "add_macro", "add_map", "b", "clear", "codecs", "decode", "encode", "ensure_str", "examples", "guess",
34-
"isb", "generate_strings_from_regex", "get_alphabet_from_mask", "handle_error", "is_native",
41+
"isb", "generate_strings_from_regex", "get_alphabet_from_mask", "handle_error", "i2s", "is_native",
3542
"list_categories", "list_encodings", "list_macros", "lookup", "maketrans", "os", "rank", "re", "register",
3643
"remove", "reset", "s2i", "search", "stopfunc", "BytesIO", "_input", "_stripl", "CodecMacro",
3744
"DARWIN", "LANG", "LINUX", "MASKS", "PY3", "UNIX", "WINDOWS"]
@@ -75,6 +82,11 @@
7582
exc_name = lambda e: "".join(t.capitalize() for t in re.split(r"[-_+]", e))
7683

7784

85+
def i2s(input):
86+
h = hex(input)[2:].rstrip("eL")
87+
return codecs.decode(h.zfill(len(h) + len(h) % 2), "hex")
88+
89+
7890
class CodecMacro(tuple):
7991
"""Macro details when looking up the codec registry. """
8092
def __new__(cls, name):
@@ -170,11 +182,11 @@ def __stdin_pipe():
170182

171183
def _input(infile):
172184
# handle input file or stdin
185+
c = b("")
173186
if infile:
174187
with open(infile, 'rb') as f:
175188
c = f.read()
176189
else:
177-
c = b("")
178190
for line in __stdin_pipe():
179191
c += line
180192
return c

codext/common/cases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
slugify = lambda i, e="strict", d="-": (re.sub(r"[^0-9a-z]+", d, i.lower()).strip(d), len(i))
3030
add("slugify", lambda i, e="strict": slugify(i, e), None, r"^(?:slug(?:ify)?|kebab(?:[-_]?case)?)$")
31-
add("snakecase", lambda i, e="strict": slugify(i, e, "_"), None, r"^snake(?:[-_]?case)$")
31+
add("snakecase", lambda i, e="strict": slugify(i, e, "_"), None, r"^snake(?:[-_]?case)?$")
3232

3333
swapcase = lambda i, e="strict": (i.swapcase(), len(i))
3434
add("swapcase", swapcase, swapcase, r"^(?:swap(?:[-_]?case)?|invert(?:case)?)$")

codext/others/html.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,6 @@ def htmlentity_decode(text, errors="strict"):
287287
return s, len(text)
288288

289289

290-
add("html", htmlentity_encode, htmlentity_decode, r"^html(?:[-_]?entit(?:y|ies))?$")
290+
add("html", htmlentity_encode, htmlentity_decode, r"^html(?:[-_]?entit(?:y|ies))?$",
291+
extra_exceptions=["HtmlEntityDecodeError"])
291292

codext/others/letters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def encode(text, errors="strict"):
6060
try:
6161
s += encmap[c]
6262
except KeyError:
63-
s += handle_error(letters + "_indices", errors)(c, i)
63+
s += handle_error("letter-indices", errors)(c, i)
6464
return "".join(encmap.get(c.upper(), c) for c in text), len(text)
6565
return encode
6666

@@ -81,7 +81,7 @@ def decode(text, errors="strict"):
8181
except (IndexError, KeyError):
8282
pass
8383
if err:
84-
s += handle_error(letters + "_indices", errors, decode=True)(text[i], i)
84+
s += handle_error("letter-indices", errors, decode=True)(text[i], i)
8585
return s, len(text)
8686
return decode
8787

tests/test_common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def test_reset_codecs(self):
123123
self.assertIsNotNone(codext.encode("test", "morse"))
124124
self.assertRaises(LookupError, codext.encode, "test", "dummy")
125125
self.assertTrue(len(CODECS_OVERWRITTEN) > 0)
126+
self.assertIsNotNone(str(CODECS_OVERWRITTEN[0]))
126127

127128
def test_search_codecs(self):
128129
self.assertIsNotNone(codext.search("morse"))

tests/test_generated.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ def _template(self):
108108
with codecs.open(tfile, 'wb', encoding=ename) as f:
109109
f.write(b(s1))
110110
with codecs.open(tfile, 'rb', encoding=ename) as f:
111-
s = f.read() if PY3 else f.read().rstrip("\x00")
111+
s = f.read()
112+
if not PY3 and re.search("[^\x00]\x00$", s):
113+
s = s[:-1]
112114
self.assertEqual(b(icdec(f2(s2, ename))), b(icdec(s)))
113115
os.remove(tfile)
114116
return _template

0 commit comments

Comments
 (0)