Skip to content

Commit 4792a99

Browse files
committed
Fixed minor bugs
1 parent 281ca1b commit 4792a99

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

codext/__common__.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ def __new__(cls, name):
109109
for action, examples in (self.codecs[0].parameters.get('examples', {}) or {'enc-dec(': ["T3st str!"]}).items():
110110
if re.match(r"enc(-dec)?\(", action):
111111
for e in (examples.keys() if action.startswith("enc(") else examples or []):
112-
rd = re.match(r"\@random(?:\{(\d+(?:,(\d+))*?)\})?$", e)
112+
rd = re.match(r"\@(i?)random(?:\{(\d+(?:,(\d+))*?)\})?$", e)
113113
if rd:
114-
for n in (rd.group(1) or "512").split(","):
115-
self.encode("".join(chr(randint(0, 255)) for i in range(int(n))))
114+
for n in (rd.group(2) or "512").split(","):
115+
s = "".join(chr(randint(0, 255)) for i in range(int(n)))
116+
self.encode(s.lower() if rd.group(1) else s)
116117
continue
117118
self.encode(e)
118119

@@ -1276,10 +1277,9 @@ def __make_encodings_dict(include, exclude):
12761277
def _develop(d, keep=True):
12771278
d = d or {}
12781279
for k, v in d.items():
1279-
l, cc = [], [e for e in v if e in CODECS_CATEGORIES]
1280+
l, cc, sc = [], [e for e in v if e in CODECS_CATEGORIES], [e for e in v if e not in CODECS_CATEGORIES]
12801281
# list from in-scope categories and then everything that is not a category
1281-
for enc in ((list_encodings(*cc) if len(cc) > 0 or keep else []) + \
1282-
[e for e in v if e not in CODECS_CATEGORIES]):
1282+
for enc in ((list_encodings(*cc) if (len(cc) > 0 or keep) and len(sc) == 0 else []) + sc):
12831283
g = []
12841284
for e in (search(enc, False) or [enc]):
12851285
try:
@@ -1293,8 +1293,8 @@ def _develop(d, keep=True):
12931293
l.extend(g)
12941294
d[k] = list(set(l))
12951295
return d
1296-
exclude = _develop(exclude, False)
1297-
return {k: [x for x in v if x not in exclude.get(k, [])] for k, v in _develop(include).items()}
1296+
_excl, _incl = _develop(exclude, False), _develop(include)
1297+
return {k: [x for x in v if x not in _excl.get(k, [])] for k, v in _incl.items()}
12981298

12991299

13001300
def __rank(prev_input, input, prev_encoding, encodings, heuristic=False, extended=False, yield_score=False):
@@ -1304,7 +1304,10 @@ def __rank(prev_input, input, prev_encoding, encodings, heuristic=False, extende
13041304
try:
13051305
codec = CODECS_CACHE[e]
13061306
except KeyError:
1307-
CODECS_CACHE[e] = codec = lookup(e, False)
1307+
try:
1308+
CODECS_CACHE[e] = codec = lookup(e, False)
1309+
except LookupError:
1310+
continue
13081311
t = __score(prev_input, input, prev_encoding, e, codec, heuristic, extended)
13091312
if t:
13101313
ranking[e] = t
@@ -1321,7 +1324,7 @@ def __init__(self, text, pad_char=None):
13211324
pad_char, last_char = (chr(pad_char), chr(c)) if isinstance(c, int) else (pad_char, c)
13221325
self.padding = pad_char is not None and last_char == pad_char
13231326
if self.padding:
1324-
text = text.rstrip(pad_char)
1327+
text = text.rstrip(b(pad_char) if isinstance(text, bytes) else pad_char)
13251328
self.len = len(self.text)
13261329
self.lcharset = len(set(self.text))
13271330
self.printables = float(len([c for c in self.text if c in printable])) / self.len
@@ -1501,7 +1504,8 @@ def rank(input, extended=False, limit=-1, include=None, exclude=None):
15011504
:param include: inclusion list with category, codec or encoding names (nothing means include every encoding)
15021505
:param exclude: exclusion list with category, codec or encoding names (nothing means exclude no encoding)
15031506
"""
1504-
encodings = __make_encodings_dict({-1: include or CODECS_CATEGORIES}, {-1: exclude or []})
1507+
encodings = __make_encodings_dict(include if isinstance(include, dict) else {-1: include or CODECS_CATEGORIES},
1508+
exclude if isinstance(exclude, dict) else {-1: exclude or []})
15051509
r = list(__rank(None, input, "", encodings[-1], True, extended, True))
15061510
return r[:limit] if len(r) > 1 else r
15071511
codecs.rank = rank

tests/test_generated.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def _template(self):
3636
for ename in m.groups():
3737
if ename is None:
3838
continue
39+
# buggy generated encoding names
40+
try:
41+
lookup(ename)
42+
except LookupError:
43+
continue
3944
# erroneous encoding name test
4045
if examples is None:
4146
self.assertRaises(LookupError, f1, "test", ename)
@@ -72,11 +77,12 @@ def _template(self):
7277
# examples validation tests
7378
if k.startswith("enc-dec") and isinstance(examples, list):
7479
for e in examples[:]:
75-
rd = re.match(r"\@random(?:\{(\d+(?:,(\d+))*?)\})?$", e)
80+
rd = re.match(r"\@(i?)random(?:\{(\d+(?:,(\d+))*?)\})?$", e)
7681
if rd:
7782
examples.remove(e)
78-
for n in (rd.group(1) or "512").split(","):
79-
examples.append("".join(chr(randint(0, 255)) for i in range(int(n))))
83+
for n in (rd.group(2) or "512").split(","):
84+
s = "".join(chr(randint(0, 255)) for i in range(int(n)))
85+
examples.append(s.lower() if rd.group(1) else s)
8086
for s in [""] + examples:
8187
self.assertEqual(icdec(f2(icenc(f1(s, ename)), ename)), icdec(s))
8288
self.assertEqual(icdec(f2(icenc(f1(b(s), ename)), ename)), b(icdec(s)))

0 commit comments

Comments
 (0)