[Python-bugs-list] [ python-Bugs-666958 ] repr.repr not always safe

SourceForge.net noreply@sourceforge.net
Mon, 20 Jan 2003 06:11:56 -0800


Bugs item #666958, was opened at 2003-01-12 23:17
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=666958&group_id=5470

Category: Python Library
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Bob Ippolito (etrepum)
Assigned to: Nobody/Anonymous (nobody)
Summary: repr.repr not always safe

Initial Comment:
>>> class A:
...   def __repr__(self):
...     raise Exception
...   def someMethod(self):
...     pass
... 
>>> A()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in __repr__
Exception
>>> repr.repr(A())
'<A instance at 237360>'
>>> repr.repr(A().someMethod)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/repr.py", line 15, in repr
    return self.repr1(x, self.maxlevel)
  File "/usr/lib/python2.2/repr.py", line 24, in repr1
    s = `x`
  File "<stdin>", line

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

>Comment By: Bob Ippolito (etrepum)
Date: 2003-01-20 09:11

Message:
Logged In: YES 
user_id=139309

It's not my module, it comes with Python (at least in my 2.2, and 2.3 CVS HEAD).  Look for yourself!

The issue has nothing to do with the fact that it has the same name as the builtin.

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

Comment By: Christos Georgiou (tzot)
Date: 2003-01-20 00:28

Message:
Logged In: YES 
user_id=539787

Did you try to change the module name to, say, repr2 and 
then run it again?  Any specific reason you named the 
module as an internal function?
BTW, what is repr.repr supposed to do?  Please supply the 
repr.py script, along with the full traceback.

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

Comment By: Bob Ippolito (etrepum)
Date: 2003-01-13 09:54

Message:
Logged In: YES 
user_id=139309

I say it's a bug because it's inconsistent behavior.  You
can repr the class instance that throws during a __repr__
just fine ('<A instance at 237360>' ), but you can't
__repr__ a bound method of the class instance without
throwing an exception.  In fact, if the __repr__ of the
class instance returns anything but a string, say it returns
None for example, then the exception will bubble all the way
up and you get no useful output from repr.repr.

It's very annoying if you're doing something equivalent to,
but more useful than:
repr.repr([1, 2, 3, 4, A().someMethod, 6, 7, 8, A()])

Note that repr.repr([1, 2, 3, 4, A(), 6, 7, 8]) does not
throw any exceptions.

There should be some sort of graceful way to do it, no?  I
think it ought to handle the bound method broken instance
case just like it handles the broken instance case directly.

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

Comment By: Tim Peters (tim_one)
Date: 2003-01-12 23:34

Message:
Logged In: YES 
user_id=31435

Why do say this is a bug?  If you raise an exception, yes, 
you get an exception.  Most __repr__ *implementations* 
try hard not to raise any exceptions, but it's never 
guaranteed that they won't.  For example, just about any 
__repr__ implementation may run out of memory while 
trying to build its result string, so MemoryError is almost 
always a possible exception.

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=666958&group_id=5470