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

Created on 2009-07-18 00:27 by kaizhu, last changed 2009-11-04 22:39 by pitrou.

Files
File name Uploaded Description Edit Remove
parse_template.patch pitrou, 2009-11-04 22:39
Messages (3)
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) Date: 2009-11-04 22:39
Here is a patch.
History
Date User Action Args
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