[Python-Dev] Re: [Python-checkins] python/dist/src/Objects stringobject.c,2.171,2.172

Neal Norwitz neal@metaslash.com
Sun, 28 Jul 2002 12:29:33 -0400


Neal Norwitz wrote:
> 
> Fredrik Lundh wrote:
> >
> > > SF patch #577031, remove PyArg_Parse() since it's deprecated
> >
> > > ! v = PyNumber_Float(v);
> > > ! if (!v)
> > >   return -1;
> >
> > > v = PyNumber_Int(v);
> > > ! if (!v)
> > >   return -1;
> >
> > umm.
> >
> > doesn't PyNumber_Float and PyNumber_Int convert its argument to
> > a float/integer, if it's not already the right type?
> 
> Yes.
> 
> > in earlier versions of Python, "%g" % "1.0" raised a TypeError.  does
> > it still do that with this patch in place?
> 
> No. :-(  That wasn't an intentional change.  The intent was
> to convert an int/long to a double in the case of '%g' et al and
> from a double to an int in the case of '%d'.
> 
> What is the best way to fix this?  

To answer my own question, it appears that I should use 
PyFloat_AsDouble() and PyInt_AsLong() and check for an error.
I don't know why I didn't do this before.  This restores the
original behaviour.

I'll check this in later.  Let me know if I screwed up again.

I'll also update the tests to check for the exception.

Neal