Skip to content

Commit dcdac1f

Browse files
committed
fnmatch: Add ure compatibility.
Removes dependency on re-pcre which is only available on unix port.
1 parent 0c31e0b commit dcdac1f

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

python-stdlib/fnmatch/fnmatch.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
1919

20+
COMPAT = re.__name__ == "ure"
21+
2022

2123
def fnmatch(name, pat):
2224
"""Test whether FILENAME matches PATTERN.
@@ -46,6 +48,11 @@ def _compile_pattern(pat):
4648
res = bytes(res_str, "ISO-8859-1")
4749
else:
4850
res = translate(pat)
51+
if COMPAT:
52+
if res.startswith("(?ms)"):
53+
res = res[5:]
54+
if res.endswith("\\Z"):
55+
res = res[:-2] + "$"
4956
return re.compile(res).match
5057

5158

@@ -104,6 +111,15 @@ def translate(pat):
104111
stuff = "\\" + stuff
105112
res = "%s[%s]" % (res, stuff)
106113
else:
107-
res = res + re.escape(c)
114+
try:
115+
res = res + re.escape(c)
116+
except AttributeError:
117+
# Using ure rather than re-pcre
118+
res = res + re_escape(c)
108119
# Original patterns is undefined, see http://bugs.python.org/issue21464
109120
return "(?ms)" + res + "\Z"
121+
122+
123+
def re_escape(pattern):
124+
# Replacement minimal re.escape for ure compatibility
125+
return re.sub(r"([\^\$\.\|\?\*\+\(\)\[\\])", r"\\\1", pattern)

python-stdlib/fnmatch/metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
srctype = cpython
22
type = module
33
version = 0.5.2
4-
depends = os, os.path, re-pcre
4+
depends = os, os.path

python-stdlib/fnmatch/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
license="Python",
2222
cmdclass={"sdist": sdist_upip.sdist},
2323
py_modules=["fnmatch"],
24-
install_requires=["micropython-os", "micropython-os.path", "micropython-re-pcre"],
24+
install_requires=["micropython-os", "micropython-os.path"],
2525
)

python-stdlib/fnmatch/test_fnmatch.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class FnmatchTestCase(unittest.TestCase):
1010
def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
1111
if should_match:
1212
self.assertTrue(
13-
fn(filename, pattern), "expected %r to match pattern %r" % (filename, pattern)
13+
fn(filename, pattern),
14+
"expected %r to match pattern %r" % (filename, pattern),
1415
)
1516
else:
1617
self.assertTrue(
@@ -80,9 +81,9 @@ def test_filter(self):
8081
self.assertEqual(filter(["a", "b"], "a"), ["a"])
8182

8283

83-
def test_main():
84+
def main():
8485
support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase)
8586

8687

8788
if __name__ == "__main__":
88-
test_main()
89+
main()

0 commit comments

Comments
 (0)