This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re.py - encounter unexpected str-object
Type: behavior Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: effbot, ezio.melotti, kaizhu, mrabarnett, pitrou
Priority: critical Keywords: patch

Created on 2009-07-18 00:27 by kaizhu, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
parse_template.patch pitrou, 2009-11-04 22:39
Messages (5)
msg90650 - (view) Author: kai zhu (kaizhu) Date: 2009-07-18 00:27
>>> import re
>>> compiled = re.compile(b"a(\w)")
>>> s = b"aa"
>>> s = compiled.sub(b"a\\1", s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.1/re.py", line 303, in filter
    return sre_parse.expand_template(template, match)
  File ".../lib/python3.1/sre_parse.py", line 810, in expand_template
    return sep.join(literals)
TypeError: sequence item 0: expected bytes, str found
msg90651 - (view) Author: kai zhu (kaizhu) Date: 2009-07-18 01:40
traced culprit to sre_parse.py <line 711> (where literal is always str):

...
def parse_template(source, pattern):
    # parse 're' replacement string into list of literals and
    # group references
    s = Tokenizer(source)
    sget = s.get
    p = []
    a = p.append
    def literal(literal, p=p, pappend=a):
        if p and p[-1][0] is LITERAL:
            p[-1] = LITERAL, p[-1][1] + literal
        else:
            pappend((LITERAL, literal))
...

a possible hack-around is <line 717>:

...
    a = p.append
    def literal(literal, p=p, pappend=a):
        if isinstance(source, (bytes, bytearray)): # hack
            literal = literal.encode()             # hack str->bytes
        if p and p[-1][0] is LITERAL:
...
msg94907 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-11-04 22:39
Here is a patch.
msg100511 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-03-06 02:16
The patch looks OK.
msg100534 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-03-06 15:33
Fixed in r78729 (py3k) and r78730 (release31-maint). I also added a test for callbacks.
History
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50758
2010-03-06 15:33:31ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg100534

stage: patch review -> resolved
2010-03-06 02:16:58ezio.melottisetmessages: + msg100511
2010-02-27 09:34:09ezio.melottisetnosy: + mrabarnett
2009-11-04 22:39:42pitrousetfiles: + parse_template.patch

nosy: + effbot
messages: + msg94907

keywords: + patch
stage: patch review
2009-07-19 13:45:27pitrousetnosy: + pitrou

versions: + Python 3.2
2009-07-18 10:27:00georg.brandlsetpriority: normal -> critical
2009-07-18 08:27:26ezio.melottisetpriority: normal
nosy: + ezio.melotti
2009-07-18 01:40:53kaizhusetmessages: + msg90651
2009-07-18 00:28:27kaizhusetcomponents: + Regular Expressions
2009-07-18 00:27:24kaizhucreate