Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

The code should speak for itself:

$ python
Python 3.3.0 (default, Dec 22 2012, 21:02:07) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> '{}'.format(np.bytes_(b'Hello'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded while calling a Python object
>>> np.version.version
'1.7.0'

Both str and repr return "b'Hello'" on np.bytes_(b'Hello'), and I can print(np.bytes_(b'Hello')) just fine, but in a format string it falls into a recursion loop.

Am I being stupid or is it indeed what it appears to be, i.e. a problem in numpy? Even if it is, I don't quite understand what is happening. Can someone please explain?

I haven't reproduced it with Python 2.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The behaviour of {} is to call np.bytes_(b'Hello').__format__(). It seems there is a bug where __format__ is calling itself. See this related ticket

Here is a workaround.

Python 3.2.3 (default, Oct 19 2012, 19:53:57) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> '{}'.format(np.bytes_(b'Hello'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded while calling a Python object
>>> '{!s}'.format(np.bytes_(b'Hello'))
"b'Hello'"
>>> '{!r}'.format(np.bytes_(b'Hello'))
"b'Hello'"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.