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"]
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)

Wyświetl plik

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

Wyświetl plik

@ -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"],
)

Wyświetl plik

@ -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()