[Python-bugs-list] [Bug #125808] Fiddling builtin str flips out re.sub
noreply@sourceforge.net
noreply@sourceforge.net
Thu, 14 Dec 2000 09:58:54 -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 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