[New-bugs-announce] [issue42475] wrongly cache pattern by re.compile

ProFatXuanAll report at bugs.python.org
Thu Nov 26 13:29:35 EST 2020


New submission from ProFatXuanAll <ProFatXuanAll at gmail.com>:

When I run the following code, I expected to get output result `['i am next line with [unk]']`, but instead I get the original list in `data`.

Code snippet

```py
import re

data = [
    '= hello =',
    'i am next line with <unk>',
]

pttn = re.compile(r'=.*=')
samples = filter(lambda sample: not pttn.match(sample), data)

pttn = re.compile(r'<unk>')
samples = map(lambda sample: pttn.sub('[unk]', sample), samples)

print(list(samples))
```

I suspect that is the cache provide by `re.compile` cause the problem.
The `sub` function in `map` is somehow begin link to the first `pttn`.

If I instead rename the second `pttn` to `pttn2`, then it work magically, but this is not expected.

----------
components: Regular Expressions
files: rebug.py
messages: 381907
nosy: ProFatXuanAll, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: wrongly cache pattern by re.compile
type: behavior
versions: Python 3.8
Added file: https://bugs.python.org/file49625/rebug.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42475>
_______________________________________


More information about the New-bugs-announce mailing list