Eric V. Smith added the comment:
Or:
{:{:s}{:d}s}.format(str(self.pcs), self.format_align, self.max_length)
You're trying to apply the string format specifier (the stuff after the first
colon through the final s, as expanded) to an object that's not always a
string: sometimes it's None. So
HCT added the comment:
I use lots of complicated format such as the following
{:{:s}{:d}s}.format( self.pcs,self.format_align, self.max_length )
it looks like the way to do it from now on will be
{!s:{:s}{:d}}.format( self.pcs,self.format_align, self.max_length )
--
HCT added the comment:
unlike NoneType, datetime doesn't throw exception. is returning the format
specifier the intended behaviour of this fix?
import datetime
a=datetime.datetime(1999,7,7)
str(a)
'1999-07-07 00:00:00'
{:s}.format(a)
's'
{:7s}.format(a)
'7s'
{!s}.format(a)
'1999-07-07
R. David Murray added the comment:
Yes. It is not returning the format specifier, it is filling in the strftime
template s from the datetime...which equals s, since it consists of just
that constant string.
Try {:%Y-%m-%d}, for example.
--
___
R. David Murray added the comment:
Which, by the way, has been the behavior all along, it is not something
affected by this fix, because datetime *does* have a __format__ method.
--
___
Python tracker rep...@bugs.python.org
HCT added the comment:
None does have __format__, but it raises exception
dir(None)
['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__le__', '__lt__', '__ne__', '__new__', '__reduce__',
Mark Lawrence added the comment:
That's not an exception, you've not actually called the function.
None.__format__('')
'None'
--
nosy: +BreamoreBoy
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7994
Eric V. Smith added the comment:
David is correct.
It's often easiest to think about the builtin format() instead of str.format().
Notice below that the format specifier has to make sense for the object being
formatted:
import datetime
now = datetime.datetime.now()
format('somestring',
R. David Murray added the comment:
NoneType is a subclass of object.
class Foo(object):
...pass
...
f = Foo()
f.__format__
built-in method __format__ of Foo object at 0xb71543b4
ie: the exception is being raised by object.__format__, as provided for by this
issue.
--
Eric V. Smith added the comment:
BreamoreBoy:
This is basically the definition of object.__format__:
def __format__(self, specifier):
if len(specifier) == 0:
return str(self)
raise TypeError('non-empty format string passed to object.__format__')
Which is why it works for an empty
HCT added the comment:
I think was confused as I forgot that I was doing str.format where {} being
format of str. confusion cleared
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7994
___
HCT added the comment:
just found out about this change in the latest official stable release and it's
breaking my code all over the place. something like {:s}.format( self.pc )
used to work in 3.3.4 and prior releases now raise exception rather then return
a string 'None' when self.pc was
Changes by R. David Murray rdmur...@bitdance.com:
--
nosy: +r.david.murray
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7994
___
___
Eric V. Smith added the comment:
I think the best we could do is have None.__format__ be:
def __format__(self, fmt):
return str(self).__format__(fmt)
Or its logical equivalent.
But this seems more like papering over a bug, instead of actually fixing a
problem. My suggestion is to use:
Roundup Robot added the comment:
New changeset f56b98143792 by R David Murray in branch 'default':
whatsnew: object.__format__ raises TypeError on non-empty string.
http://hg.python.org/cpython/rev/f56b98143792
--
nosy: +python-dev
___
Python tracker
Florent Xicluna florent.xicl...@gmail.com added the comment:
now the PendingDeprecationWarnings are checked in the test suite, with r84772
(for 2.7).
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7994
Eric Smith e...@trueblade.com added the comment:
Manually merged to py3k in r84790. I'll leave this open until I create the 3.3
issue to change it to a DeprecationWarning.
--
keywords: -needs review
versions: -Python 3.3
___
Python tracker
Florent Xicluna florent.xicl...@gmail.com added the comment:
This should be merged before 3.2 beta.
--
nosy: +flox
resolution: - accepted
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7994
Changes by Ezio Melotti ezio.melo...@gmail.com:
--
nosy: +ezio.melotti
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7994
___
___
Python-bugs-list
Eric Smith e...@trueblade.com added the comment:
Committed in trunk in r79596. I'll leave this open until I port to py3k, check
the old tests for this usage, and create the issue to make it a
DeprecationWarning.
--
stage: patch review - committed/rejected
Eric Smith e...@trueblade.com added the comment:
Meador: Your patch (-3) looks identical to mine (-2), unless I'm making some
mistake. Could you check? I'd like to get this applied in the next few days,
before 2.7b1.
Thanks!
--
___
Python tracker
Meador Inge mead...@gmail.com added the comment:
Hi Eric,
(-2) and (-3) are different. The changes that I made, however, are pretty
minor. Also, they are all in 'test_builtin.py'.
--
___
Python tracker rep...@bugs.python.org
Eric Smith e...@trueblade.com added the comment:
I haven't looked at the patch, but:
Thanks for the the additional tests. Missing unicode was definitely a mistake.
str(w[0].message) is an improvement.
The PEP is out of date in many respects. I think it's best to note that in the
PEP and
Meador Inge mead...@gmail.com added the comment:
The patch looks reasonable. I built on it with the following changes:
1. Added some extra test cases to cover Unicode format strings,
since the code was changed to handle these as well.
2. Changed test_builtin.py by
Eric Smith e...@trueblade.com added the comment:
This version of the patch adds support for classic classes and adds tests.
Documentation still needs to be written.
Again, this diff is against trunk.
If anyone wants to review this, in particular the tests that exercise
New submission from Eric Smith e...@trueblade.com:
Background:
format(obj, fmt) eventually calls object.__format__(obj, fmt) if obj (or one of
its bases) does not implement __format__. The behavior of object.__format__ is
basically:
def __format__(self, fmt):
return
26 matches
Mail list logo