oracle / graalpython

A high-performance embeddable Python 3 runtime for Java
https://www.graalvm.org/python/
Other
1.22k stars 104 forks source link

"TypeError: sequence item 1: expected str instance, NoneType found" when trying to `re.sub` with a function returning `None` #385

Closed masklinn closed 7 months ago

masklinn commented 7 months ago

The use case is a bit debatable as it's ill-defined, but I caught that because an (old) test suite unwittingly made use of that property: if an re.sub callback returns a None, the behaviour is (apparently) the same as returning an empty string. However GraalPython raises a TypeError instead.

# cpython 3.12
>>> import re
>>> re.sub(r'\d', lambda m: None, "f1")
'f'
# pypy 3.10
>>>> import re
>>>> re.sub(r'\d', lambda m: None, "f1")
'f'
# graalpy 23.1.2
>>> re.sub(r'\d', lambda m: None, "f1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/masklinn/.pyenv/versions/graalpy-23.1.2/lib/python3.10/re.py", line 209, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: sequence item 1: expected str instance, NoneType found