[New-bugs-announce] [issue11145] '%o' % user-defined instance

Armin Rigo report at bugs.python.org
Mon Feb 7 19:46:34 CET 2011

New submission from Armin Rigo <arigo at users.sourceforge.net>:

The expression  '%o' % x,  where x is a user-defined instance, usually ignores a user-defined __oct__() method.  I suppose that's fine; assuming this is the expected behavior, then the present issue is about the "usually" in my previous sentence.

If 'x' is an instance of a subclass of 'long', then the __oct__() is called.  It must be specifically a 'long' -- not, say, 'int' or 'str' or 'object'.  Moreover, there is a test in test_format.py (class Foobar) that checks that if this __oct__() returns a non-string, then we get a nice TypeError.  That's already strange -- why is __oct__() called in the first place?  But trying out more I get (CPython 2.7.1):

>>> class X(long):
...   def __oct__(self):
...     return 'abc'
>>> '%o' % X()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: ../trunk/Objects/stringobject.c:4035: bad argument to internal function

components: Interpreter Core
messages: 128148
nosy: arigo
priority: normal
severity: normal
status: open
title: '%o' % user-defined instance
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list