[Python-bugs-list] [Bug #125808] Fiddling builtin str flips out re.sub

noreply@sourceforge.net noreply@sourceforge.net
Thu, 14 Dec 2000 10:11:02 -0800


Bug #125808, was updated on 2000-Dec-14 09:40
Here is a current snapshot of the bug.

Project: Python
Category: None
Status: Closed
Resolution: Invalid
Bug Group: Not a Bug
Priority: 5
Submitted by: montanaro
Assigned to : gvanrossum
Summary: Fiddling builtin str flips out re.sub

Details: Something about replacing __builtin__.str seems to cause re.sub to fail when trying to replace control characters in a string.  Given the following PYTHONSTARTUP file:

import pprint, __builtin__
class Writer:
    def __init__(self):
        self.pp = pprint.PrettyPrinter()

    def str(self, obj):
        return self.pp.pformat(obj)
__builtin__.str = Writer().str


executing the following at an interactive Python prompt:

    import re ; re.sub(r"[\000-\037\177]", "", "\000")

causes the substitution to fail.  This is with a version of Python compiled from the latest CVS tree.  It also fails with 2.0c1, but not 1.5.2.


Follow-Ups:

Date: 2000-Dec-14 10:11
By: tim_one

Comment:
Heh -- that time he beat me by 2(!) seconds.
-------------------------------------------------------

Date: 2000-Dec-14 10:09
By: tim_one

Comment:
Oops!  Guido & I added comments at the same time, but he committed 6 seconds(!) before I did, so my screen undid his.  Undoing mine, to restore his.  Let's hope he's not doing the same thing at the same time <0.9 wink>.

-------------------------------------------------------

Date: 2000-Dec-14 10:07
By: gvanrossum

Comment:
Closed again (Tim's update somehow reopened it).

-------------------------------------------------------

Date: 2000-Dec-14 10:04
By: tim_one

Comment:
Assigned to Fredrik, but I gotta say I've got little sympathy, Skip -- given how much of Python's libraries are written in Python, *of course* you'll break things if you replace __builtin__ functions.

In particular, the function _class_escape in sre_parse.py ("handle escape code inside character class") uses the builtin str().  Perhaps /F can think of an easy way to use some other method there, but you're playing with fire regardless.
-------------------------------------------------------

Date: 2000-Dec-14 09:58
By: gvanrossum

Comment:
That'll teach you to mess with builtins!

Your str() implementation does not preserve the important property that for any string s, str(s)==s.  Your str() behaves more like repr():

>>> # Builtin str
>>> str('abc')
'abc'
>>> # Your str
>>> str('abc')
"'abc'"
>>> 

-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=125808&group_id=5470