[pypy-issue] Issue #2758: strange behavior in re.sub (pypy/pypy)

Joseph Joseph issues-reply at bitbucket.org
Sat Feb 24 02:38:33 EST 2018


New issue 2758: strange behavior in re.sub
https://bitbucket.org/pypy/pypy/issues/2758/strange-behavior-in-resub

Joseph Joseph:

### Environment ###
```
Python 2.7.13 (84a2f3e6a7f88f2fe698e473998755b3bd1a12e2, Oct 05 2017, 16:31:59)
[PyPy 5.9.0 with GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
```

Given the following code:
```
#!python

import re


class Proxy:
    def __init__(self):
        self.matches = []

    def repl(self, match):
        self.matches.append(match)
        return ""


def main():
    pat = "\$\{(\w*)\}"
    value = "the name is ${joseph}, the name is ${alice}"

    proxy = Proxy()
    compiled_pat = re.compile(pat)
    compiled_pat.sub(proxy.repl, value)
    for match in proxy.matches:
        print(match.groups())


if __name__ == "__main__":
    main()

```
The result of execution is:

```
(None,)
(None,)
```
The expected result is:
```
('joseph',)
('alice',)
```

But if we changed a little bit in Proxy.repl as following, the result will be correct.

```
#!python

class Proxy:
    def __init__(self):
        self.matches = []

    def repl(self, match):
        self.matches.append(match)
        print match.groups() .   # <----------- added this line
        return ""

```

this bug also exists in
``` 
Python 3.5.3 (d72f9800a42b46a8056951b1da2426d2c2d8d502, Oct 07 2017, 08:21:37)
[PyPy 5.9.0-beta0 with GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
```




More information about the pypy-issue mailing list