[3.10] gh-155292: Don't consider Unicode codepoint attributes outside RFC 3454 (GH-155293) (GH-156020) by encukou · Pull Request #156932 · python/cpython · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
463 changes: 315 additions & 148 deletions Lib/stringprep.py

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Lib/test/test_codecs.py
34 changes: 28 additions & 6 deletions Lib/test/test_unicodedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class UnicodeDatabaseTest(unittest.TestCase):
db = unicodedata

class UnicodeFunctionsTest(UnicodeDatabaseTest):
old = False

# Update this if the database changes. Make sure to do a full rebuild
# (e.g. 'make distclean && make') to get the correct checksum.
Expand All @@ -94,7 +95,8 @@ def test_function_checksum(self):
]
h.update(''.join(data).encode("ascii"))
result = h.hexdigest()
self.assertEqual(result, self.expectedchecksum)
if not self.old:
self.assertEqual(result, self.expectedchecksum)

@requires_resource('cpu')
def test_name_inverse_lookup(self):
Expand All @@ -120,9 +122,13 @@ def test_numeric(self):
self.assertEqual(self.db.numeric('9'), 9)
self.assertEqual(self.db.numeric('\u215b'), 0.125)
self.assertEqual(self.db.numeric('\u2468'), 9.0)
self.assertEqual(self.db.numeric('\ua627'), 7.0)
# New in 5.1.0
self.assertEqual(self.db.numeric('\ua627', None),
None if self.old else 7.0)
self.assertEqual(self.db.numeric('\U00020000', None), None)
self.assertEqual(self.db.numeric('\U0001012A'), 9000)
# New in 4.1.0
self.assertEqual(self.db.numeric('\U0001012A', None),
None if self.old else 9000)

self.assertRaises(TypeError, self.db.numeric)
self.assertRaises(TypeError, self.db.numeric, 'xx')
Expand All @@ -145,7 +151,8 @@ def test_category(self):
self.assertEqual(self.db.category('a'), 'Ll')
self.assertEqual(self.db.category('A'), 'Lu')
self.assertEqual(self.db.category('\U00020000'), 'Lo')
self.assertEqual(self.db.category('\U0001012A'), 'No')
self.assertEqual(self.db.category('\U0001012A'),
'Cn' if self.old else 'No')

self.assertRaises(TypeError, self.db.category)
self.assertRaises(TypeError, self.db.category, 'xx')
Expand All @@ -159,6 +166,15 @@ def test_bidirectional(self):
self.assertRaises(TypeError, self.db.bidirectional)
self.assertRaises(TypeError, self.db.bidirectional, 'xx')

def test_bidirectional_unassigned(self):
self.assertEqual(self.db.bidirectional('\u0378'), '')
self.assertEqual(self.db.bidirectional('\u077F'), '' if self.old else 'AL')
self.assertEqual(self.db.bidirectional('\u20CF'), '')
self.assertEqual(self.db.bidirectional('\u0590'), '')
self.assertEqual(self.db.bidirectional('\uFFFF'), '')
self.assertEqual(self.db.bidirectional('\U0001FFFE'), '')
self.assertEqual(self.db.bidirectional('\U00010D01'), '' if self.old else 'AL')

def test_decomposition(self):
self.assertEqual(self.db.decomposition('\uFFFE'),'')
self.assertEqual(self.db.decomposition('\u00bc'), '<fraction> 0031 2044 0034')
Expand Down Expand Up @@ -257,8 +273,14 @@ def test_east_asian_width(self):
self.assertEqual(eaw('\U00020000'), 'W')

def test_east_asian_width_9_0_changes(self):
self.assertEqual(self.db.ucd_3_2_0.east_asian_width('\u231a'), 'N')
self.assertEqual(self.db.east_asian_width('\u231a'), 'W')
self.assertEqual(self.db.east_asian_width('\u231a'),
'N' if self.old else 'W')


class Unicode_3_2_0_FunctionsTest(UnicodeFunctionsTest):
db = unicodedata.ucd_3_2_0
old = True


class UnicodeMiscTest(UnicodeDatabaseTest):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change the :mod:`stringprep` module and :mod:`encodings.idna` codec to not
consider Unicode codepoint attributes beyond those defined in :rfc:`3454`.
17 changes: 16 additions & 1 deletion Tools/unicode/makeunicodedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@

import dataclasses
import os
import subprocess
import sys
import zipfile

from functools import partial
from textwrap import dedent
from typing import Iterator, List, Optional, Set, Tuple

SCRIPT = sys.argv[0]
SCRIPT = os.path.normpath(sys.argv[0])
VERSION = "3.3"

# The Unicode Database
Expand Down Expand Up @@ -128,6 +129,7 @@ def maketables(trace=0):
makeunicodename(unicode, trace)
makeunicodedata(unicode, trace)
makeunicodetype(unicode, trace)
makestringprep()


# --------------------------------------------------------------------
Expand Down Expand Up @@ -792,6 +794,19 @@ def word_key(a):
fprint('};')



def makestringprep():
FILE = "Lib/stringprep.py"

print("--- Preparing", FILE, "...")

MKSTRINGPREP = "Tools/unicode/mkstringprep.py"

with open(FILE, "w") as f:
f.truncate()
subprocess.check_call([sys.executable, MKSTRINGPREP], stdout=f)


def merge_old_version(version, new, old):
# Changes to exclusion file not implemented yet
if old.exclusions != new.exclusions:
Expand Down
93 changes: 63 additions & 30 deletions Tools/unicode/mkstringprep.py
Loading