Skip to content

Commit 5beb28b

Browse files
committed
More efficient resolution of joiner contexts
In some pathological cases, this would out eligibility under CONTEXTJ rules much faster.
1 parent 1b12148 commit 5beb28b

File tree

3 files changed

+2164
-62
lines changed

3 files changed

+2164
-62
lines changed

idna/core.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ def valid_contextj(label: str, pos: int) -> bool:
150150
joining_type = idnadata.joining_types.get(ord(label[i]))
151151
if joining_type == ord('T'):
152152
continue
153-
if joining_type in [ord('L'), ord('D')]:
153+
elif joining_type in [ord('L'), ord('D')]:
154154
ok = True
155155
break
156+
else:
157+
break
156158

157159
if not ok:
158160
return False
@@ -162,9 +164,11 @@ def valid_contextj(label: str, pos: int) -> bool:
162164
joining_type = idnadata.joining_types.get(ord(label[i]))
163165
if joining_type == ord('T'):
164166
continue
165-
if joining_type in [ord('R'), ord('D')]:
167+
elif joining_type in [ord('R'), ord('D')]:
166168
ok = True
167169
break
170+
else:
171+
break
168172
return ok
169173

170174
if cp_value == 0x200d:
@@ -236,12 +240,8 @@ def check_label(label: Union[str, bytes, bytearray]) -> None:
236240
if intranges_contain(cp_value, idnadata.codepoint_classes['PVALID']):
237241
continue
238242
elif intranges_contain(cp_value, idnadata.codepoint_classes['CONTEXTJ']):
239-
try:
240-
if not valid_contextj(label, pos):
241-
raise InvalidCodepointContext('Joiner {} not allowed at position {} in {}'.format(
242-
_unot(cp_value), pos+1, repr(label)))
243-
except ValueError:
244-
raise IDNAError('Unknown codepoint adjacent to joiner {} at position {} in {}'.format(
243+
if not valid_contextj(label, pos):
244+
raise InvalidCodepointContext('Joiner {} not allowed at position {} in {}'.format(
245245
_unot(cp_value), pos+1, repr(label)))
246246
elif intranges_contain(cp_value, idnadata.codepoint_classes['CONTEXTO']):
247247
if not valid_contexto(label, pos):

0 commit comments

Comments
 (0)