From dcdac1f552d2684751ff0f75d20030b12e97407f Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 18 Mar 2022 12:41:10 +1100 Subject: [PATCH] fnmatch: Add ure compatibility. Removes dependency on re-pcre which is only available on unix port. --- python-stdlib/fnmatch/fnmatch.py | 18 +++++++++++++++++- python-stdlib/fnmatch/metadata.txt | 2 +- python-stdlib/fnmatch/setup.py | 2 +- python-stdlib/fnmatch/test_fnmatch.py | 7 ++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/python-stdlib/fnmatch/fnmatch.py b/python-stdlib/fnmatch/fnmatch.py index 93b5d521..f573d75c 100644 --- a/python-stdlib/fnmatch/fnmatch.py +++ b/python-stdlib/fnmatch/fnmatch.py @@ -17,6 +17,8 @@ import re __all__ = ["filter", "fnmatch", "fnmatchcase", "translate"] +COMPAT = re.__name__ == "ure" + def fnmatch(name, pat): """Test whether FILENAME matches PATTERN. @@ -46,6 +48,11 @@ def _compile_pattern(pat): res = bytes(res_str, "ISO-8859-1") else: res = translate(pat) + if COMPAT: + if res.startswith("(?ms)"): + res = res[5:] + if res.endswith("\\Z"): + res = res[:-2] + "$" return re.compile(res).match @@ -104,6 +111,15 @@ def translate(pat): stuff = "\\" + stuff res = "%s[%s]" % (res, stuff) else: - res = res + re.escape(c) + try: + res = res + re.escape(c) + except AttributeError: + # Using ure rather than re-pcre + res = res + re_escape(c) # Original patterns is undefined, see http://bugs.python.org/issue21464 return "(?ms)" + res + "\Z" + + +def re_escape(pattern): + # Replacement minimal re.escape for ure compatibility + return re.sub(r"([\^\$\.\|\?\*\+\(\)\[\\])", r"\\\1", pattern) diff --git a/python-stdlib/fnmatch/metadata.txt b/python-stdlib/fnmatch/metadata.txt index faa83214..5eb297d4 100644 --- a/python-stdlib/fnmatch/metadata.txt +++ b/python-stdlib/fnmatch/metadata.txt @@ -1,4 +1,4 @@ srctype = cpython type = module version = 0.5.2 -depends = os, os.path, re-pcre +depends = os, os.path diff --git a/python-stdlib/fnmatch/setup.py b/python-stdlib/fnmatch/setup.py index 415804c0..5a9ac632 100644 --- a/python-stdlib/fnmatch/setup.py +++ b/python-stdlib/fnmatch/setup.py @@ -21,5 +21,5 @@ setup( license="Python", cmdclass={"sdist": sdist_upip.sdist}, py_modules=["fnmatch"], - install_requires=["micropython-os", "micropython-os.path", "micropython-re-pcre"], + install_requires=["micropython-os", "micropython-os.path"], ) diff --git a/python-stdlib/fnmatch/test_fnmatch.py b/python-stdlib/fnmatch/test_fnmatch.py index 1ddf8a60..4eaeec63 100644 --- a/python-stdlib/fnmatch/test_fnmatch.py +++ b/python-stdlib/fnmatch/test_fnmatch.py @@ -10,7 +10,8 @@ class FnmatchTestCase(unittest.TestCase): def check_match(self, filename, pattern, should_match=1, fn=fnmatch): if should_match: self.assertTrue( - fn(filename, pattern), "expected %r to match pattern %r" % (filename, pattern) + fn(filename, pattern), + "expected %r to match pattern %r" % (filename, pattern), ) else: self.assertTrue( @@ -80,9 +81,9 @@ class FilterTestCase(unittest.TestCase): self.assertEqual(filter(["a", "b"], "a"), ["a"]) -def test_main(): +def main(): support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase) if __name__ == "__main__": - test_main() + main()