mirror of
https://github.com/python/cpython.git
synced 2025-10-02 13:22:19 +00:00
bpo-32775: Fix regular expression warnings in fnmatch. (GH-5583) (GH-5596)
fnmatch.translate() no longer produces patterns which contain set
operations.
Sets starting with '[' or containing '--', '&&', '~~' or '||' will
be interpreted differently in regular expressions in future versions.
Currently they emit warnings. fnmatch.translate() now avoids producing
patterns containing such sets by accident.
(cherry picked from commit 23cdbfa744
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
105fcbfd6a
commit
0e361730b0
3 changed files with 38 additions and 2 deletions
|
@ -97,11 +97,30 @@ def translate(pat):
|
||||||
if j >= n:
|
if j >= n:
|
||||||
res = res + '\\['
|
res = res + '\\['
|
||||||
else:
|
else:
|
||||||
stuff = pat[i:j].replace('\\','\\\\')
|
stuff = pat[i:j]
|
||||||
|
if '--' not in stuff:
|
||||||
|
stuff = stuff.replace('\\', r'\\')
|
||||||
|
else:
|
||||||
|
chunks = []
|
||||||
|
k = i+2 if pat[i] == '!' else i+1
|
||||||
|
while True:
|
||||||
|
k = pat.find('-', k, j)
|
||||||
|
if k < 0:
|
||||||
|
break
|
||||||
|
chunks.append(pat[i:k])
|
||||||
|
i = k+1
|
||||||
|
k = k+3
|
||||||
|
chunks.append(pat[i:j])
|
||||||
|
# Escape backslashes and hyphens for set difference (--).
|
||||||
|
# Hyphens that create ranges shouldn't be escaped.
|
||||||
|
stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')
|
||||||
|
for s in chunks)
|
||||||
|
# Escape set operations (&&, ~~ and ||).
|
||||||
|
stuff = re.sub(r'([&~|])', r'\\\1', stuff)
|
||||||
i = j+1
|
i = j+1
|
||||||
if stuff[0] == '!':
|
if stuff[0] == '!':
|
||||||
stuff = '^' + stuff[1:]
|
stuff = '^' + stuff[1:]
|
||||||
elif stuff[0] == '^':
|
elif stuff[0] in ('^', '['):
|
||||||
stuff = '\\' + stuff
|
stuff = '\\' + stuff
|
||||||
res = '%s[%s]' % (res, stuff)
|
res = '%s[%s]' % (res, stuff)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import os
|
import os
|
||||||
|
import warnings
|
||||||
|
|
||||||
from fnmatch import fnmatch, fnmatchcase, translate, filter
|
from fnmatch import fnmatch, fnmatchcase, translate, filter
|
||||||
|
|
||||||
|
@ -83,6 +84,17 @@ class FnmatchTestCase(unittest.TestCase):
|
||||||
check('usr/bin', 'usr\\bin', normsep)
|
check('usr/bin', 'usr\\bin', normsep)
|
||||||
check('usr\\bin', 'usr\\bin')
|
check('usr\\bin', 'usr\\bin')
|
||||||
|
|
||||||
|
def test_warnings(self):
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter('error', Warning)
|
||||||
|
check = self.check_match
|
||||||
|
check('[', '[[]')
|
||||||
|
check('&', '[a&&b]')
|
||||||
|
check('|', '[a||b]')
|
||||||
|
check('~', '[a~~b]')
|
||||||
|
check(',', '[a-z+--A-Z]')
|
||||||
|
check('.', '[a-z--/A-Z]')
|
||||||
|
|
||||||
|
|
||||||
class TranslateTestCase(unittest.TestCase):
|
class TranslateTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
:func:`fnmatch.translate()` no longer produces patterns which contain set
|
||||||
|
operations. Sets starting with '[' or containing '--', '&&', '~~' or '||'
|
||||||
|
will be interpreted differently in regular expressions in future versions.
|
||||||
|
Currently they emit warnings. fnmatch.translate() now avoids producing
|
||||||
|
patterns containing such sets by accident.
|
Loading…
Add table
Add a link
Reference in a new issue