[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Ethan Furman

New submission from Ethan Furman:

While `.format()` works fine with enum, %-formatting does not:

-- class AF(enum.IntEnum):
...   IPv4 = 1
...   IPv6 = 2
... 

-- AF.IPv4
AF.IPv4: 1

-- '%s' % AF.IPv4
'AF.IPv4'

-- '%r' % AF.IPv4
'AF.IPv4: 1'

-- '%d' % AF.IPv4
'AF.IPv4'

-- '%i' % AF.IPv4
'AF.IPv4'

-- '%x' % AF.IPv4
'1'

-- '%o' % AF.IPv4
'1'

Hex and octal work, decimal and integer do not.

--
messages: 195160
nosy: barry, eli.bendersky, ethan.furman, serhiy.storchaka
priority: normal
severity: normal
status: open
title: % formatting incomplete for Enum
type: behavior
versions: Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

`.format()` is surprised too.

 '{:}'.format(AF.IPv4)
'AF.IPv4'
 '{:10}'.format(AF.IPv4)
' 1'

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Eric V. Smith

Changes by Eric V. Smith e...@trueblade.com:


--
nosy: +eric.smith

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Eli Bendersky

Eli Bendersky added the comment:

In Objects/unicodeobject.c when it gets into mainformatlong, an IntEnum is 
recognized as an integer (passes PyLong_Check) and goes into formatlong. There, 
in the cases of 'd', 'i', 'u' it has:

case 'u':
/* Special-case boolean: we want 0/1 */
if (PyBool_Check(val))
result = PyNumber_ToBase(val, 10);
else
result = Py_TYPE(val)-tp_str(val);
break;

So tp_str is invoked...

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Ethan Furman

Ethan Furman added the comment:

'{:}'.format(AF.IPv4) is incorrect, but '{:10}'.format(AF.IPv4) behaves as it 
should.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Ethan Furman

Ethan Furman added the comment:

Looks like Objects/unicodeobject.c needs the same enhancement that 
Modules/_json.c received: convert int/float subclasses into actual ints/floats 
before continuing.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Eli Bendersky

Eli Bendersky added the comment:

Ethan, str.format uses __format__. We don't seem to provide a custom one.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18738] % formatting incomplete for Enum

2013-08-14 Thread Eli Bendersky

Eli Bendersky added the comment:

On Wed, Aug 14, 2013 at 6:55 AM, Serhiy Storchaka rep...@bugs.python.orgwrote:


 Serhiy Storchaka added the comment:

 `.format()` is surprised too.

  '{:}'.format(AF.IPv4)
 'AF.IPv4'
  '{:10}'.format(AF.IPv4)
 ' 1'


Oh, this looks like a bug in str.format (and probably the %i behavior too).
Python allows subclassing int and providing custom __str__/__repr__. The
formatting tools should behave sensically in this situation.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18738
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com