fnmatch: Add ure compatibility.

Removes dependency on re-pcre which is only available on unix port.
pull/490/head
Andrew Leech 2022-03-18 12:41:10 +11:00
rodzic 0c31e0b3d7
commit dcdac1f552
4 zmienionych plików z 23 dodań i 6 usunięć

Wyświetl plik

@ -17,6 +17,8 @@ import re
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"] __all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
COMPAT = re.__name__ == "ure"
def fnmatch(name, pat): def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN. """Test whether FILENAME matches PATTERN.
@ -46,6 +48,11 @@ def _compile_pattern(pat):
res = bytes(res_str, "ISO-8859-1") res = bytes(res_str, "ISO-8859-1")
else: else:
res = translate(pat) res = translate(pat)
if COMPAT:
if res.startswith("(?ms)"):
res = res[5:]
if res.endswith("\\Z"):
res = res[:-2] + "$"
return re.compile(res).match return re.compile(res).match
@ -104,6 +111,15 @@ def translate(pat):
stuff = "\\" + stuff stuff = "\\" + stuff
res = "%s[%s]" % (res, stuff) res = "%s[%s]" % (res, stuff)
else: else:
try:
res = res + re.escape(c) 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 # Original patterns is undefined, see http://bugs.python.org/issue21464
return "(?ms)" + res + "\Z" return "(?ms)" + res + "\Z"
def re_escape(pattern):
# Replacement minimal re.escape for ure compatibility
return re.sub(r"([\^\$\.\|\?\*\+\(\)\[\\])", r"\\\1", pattern)

Wyświetl plik

@ -1,4 +1,4 @@
srctype = cpython srctype = cpython
type = module type = module
version = 0.5.2 version = 0.5.2
depends = os, os.path, re-pcre depends = os, os.path

Wyświetl plik

@ -21,5 +21,5 @@ setup(
license="Python", license="Python",
cmdclass={"sdist": sdist_upip.sdist}, cmdclass={"sdist": sdist_upip.sdist},
py_modules=["fnmatch"], py_modules=["fnmatch"],
install_requires=["micropython-os", "micropython-os.path", "micropython-re-pcre"], install_requires=["micropython-os", "micropython-os.path"],
) )

Wyświetl plik

@ -10,7 +10,8 @@ class FnmatchTestCase(unittest.TestCase):
def check_match(self, filename, pattern, should_match=1, fn=fnmatch): def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
if should_match: if should_match:
self.assertTrue( 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: else:
self.assertTrue( self.assertTrue(
@ -80,9 +81,9 @@ class FilterTestCase(unittest.TestCase):
self.assertEqual(filter(["a", "b"], "a"), ["a"]) self.assertEqual(filter(["a", "b"], "a"), ["a"])
def test_main(): def main():
support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase) support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase)
if __name__ == "__main__": if __name__ == "__main__":
test_main() main()