Ticket #22943: regex_py3_wip.diff

File regex_py3_wip.diff, 3.9 KB (added by antialiasis@…, 10 years ago)

Fix Python 3 regex serialization, but a test currently fails (see comment below)

  • django/db/migrations/writer.py

    diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py
    index dbeaae1..f835562 100644
    a b from django.utils.encoding import force_text  
    1818from django.utils.functional import Promise
    1919
    2020
    21 COMPILED_REGEX_TYPE = type(re.compile(''))
     21BASE_REGEX = re.compile('')
     22COMPILED_REGEX_TYPE = type(BASE_REGEX)
    2223
    2324
    2425class SettingsReference(str):
    class MigrationWriter(object):  
    356357            imports.update(pattern_imports)
    357358            imports.update(flag_imports)
    358359            args = [regex_pattern]
    359             if value.flags:
     360            if value.flags != BASE_REGEX.flags:
    360361                args.append(regex_flags)
    361362            return "re.compile(%s)" % ', '.join(args), imports
    362363        # Uh oh.
  • tests/migrations/test_writer.py

    diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
    index 539c201..00da1cb 100644
    a b class WriterTests(TestCase):  
    128128        """
    129129        Make sure compiled regex can be serialized.
    130130        """
     131        regex = re.compile(r'^\w+$', 0)
     132        self.assertSerializedEqual(regex)
     133
    131134        regex = re.compile(r'^\w+$', re.U)
    132135        self.assertSerializedEqual(regex)
    133136
    class WriterTests(TestCase):  
    142145        self.serialize_round_trip(validator)
    143146
    144147        # Test with a compiled regex.
    145         validator = RegexValidator(regex=re.compile(r'^\w+$', re.U))
     148        validator = RegexValidator(regex=re.compile(r'^\w+$'))
    146149        string = MigrationWriter.serialize(validator)[0]
    147         self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$', 32))")
     150        self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$'))")
    148151        self.serialize_round_trip(validator)
    149152
    150153        # Test a string regex with flag
Back to Top