Skip to content

fnmatch: Add ure compatibility. #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions python-stdlib/fnmatch/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@
The function translate(PATTERN) returns a regular expression
corresponding to PATTERN. (It does not compile it.)
"""
import os
import os.path
import re

# import functools
try:
from os.path import normcase
except ImportError:

def normcase(s):
"""
From os.path.normcase
Normalize the case of a pathname. On Windows, convert all characters
in the pathname to lowercase, and also convert forward slashes to
backward slashes. On other operating systems, return the path unchanged.
"""
return s


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

COMPAT = re.__name__ == "ure"


def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN.
Expand All @@ -33,8 +45,8 @@ def fnmatch(name, pat):
if the operating system requires it.
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
"""
name = os.path.normcase(name)
pat = os.path.normcase(pat)
name = normcase(name)
pat = normcase(pat)
return fnmatchcase(name, pat)


Expand All @@ -46,16 +58,21 @@ 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


def filter(names, pat):
"""Return the subset of the list NAMES that match PAT."""
result = []
pat = os.path.normcase(pat)
pat = normcase(pat)
match = _compile_pattern(pat)
for name in names:
if match(os.path.normcase(name)):
if match(normcase(name)):
result.append(name)
return result

Expand Down Expand Up @@ -104,6 +121,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)
3 changes: 1 addition & 2 deletions python-stdlib/fnmatch/metadata.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
srctype = cpython
type = module
version = 0.5.2
depends = os, os.path, re-pcre
version = 0.6.0
3 changes: 1 addition & 2 deletions python-stdlib/fnmatch/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="micropython-fnmatch",
version="0.5.2",
version="0.6.0",
description="CPython fnmatch module ported to MicroPython",
long_description="This is a module ported from CPython standard library to be compatible with\nMicroPython interpreter. Usually, this means applying small patches for\nfeatures not supported (yet, or at all) in MicroPython. Sometimes, heavier\nchanges are required. Note that CPython modules are written with availability\nof vast resources in mind, and may not work for MicroPython ports with\nlimited heap. If you are affected by such a case, please help reimplement\nthe module from scratch.",
url="https://github.com/micropython/micropython-lib",
Expand All @@ -21,5 +21,4 @@
license="Python",
cmdclass={"sdist": sdist_upip.sdist},
py_modules=["fnmatch"],
install_requires=["micropython-os", "micropython-os.path", "micropython-re-pcre"],
)
7 changes: 4 additions & 3 deletions python-stdlib/fnmatch/test_fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -80,9 +81,9 @@ def test_filter(self):
self.assertEqual(filter(["a", "b"], "a"), ["a"])


def test_main():
def main():
support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase)


if __name__ == "__main__":
test_main()
main()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy