classification
Title: No float formatting in PyString_FromFormat
Type: enhancement Stage: patch review
Components: Extension Modules, Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: BreamoreBoy, MrJean1, duaneb, eric.smith, facundobatista, mark.dickinson, riq
Priority: normal Keywords: patch

Created on 2008-05-10 21:09 by duaneb, last changed 2012-10-28 10:35 by mark.dickinson. This issue is now closed.

Files
File name Uploaded Description Edit
stringobject_format_f.patch MrJean1, 2008-06-21 23:34 support for %f in PyString_FromFormat*
stringobject_format_Lf.patch MrJean1, 2008-06-23 06:08 support for %f and %Lf in PyString_FromFormat*
stringobject_format_xLf.patch MrJean1, 2008-06-24 15:47 support for %#zx, %f, %Lf in PyString_FromFormat*
Messages (17)
msg66586 - (view) Author: duane Bailey (duaneb) Date: 2008-05-10 21:09
There appears to be most of the formatting options in the *printf 
family... except for the obvious %f. Why is this crucial option missing?
msg68509 - (view) Author: Ricardo Quesada (riq) Date: 2008-06-21 16:04
Do you expect to have support for float, double and long double ?
Also, do you expect to have support for precision-modifiers ?
msg68523 - (view) Author: duane Bailey (duaneb) Date: 2008-06-21 17:42
Well, precision modifiers would be nice, but I don't think that they're
necessarily needed. General floating point support, however, is needed.
msg68552 - (view) Author: Jean Brouwers (MrJean1) Date: 2008-06-21 23:21
Attached is a patch attempting to provide support for %f with precision in PyString_FromFormat* based on file Objects/stringobject.c of Python 2.6b1.

Note, only %f is support but %lf is not and silently ignored.
msg68596 - (view) Author: Jean Brouwers (MrJean1) Date: 2008-06-22 23:47
Attached is another patch for file Objects/stringobject.c(relative to 
Python 2.6b1) which supports the double %f and long double %LF formats.  

However, %Lf is included only if symbol  LDBL_DIG is defined in header 
file <float.h> on the underlying platform.

This patch also handles the long and size_t flags correctly in the first 
loop to determine the size of the buffer under
    ...
    case 'd': case 'u': case 'i': case 'x':
    ...

Btw, it does not make sense to handle float since there is not formnat 
specification for floats.
msg68597 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-06-22 23:58
Jean, you can increase *hugely* the possibility of this being accepted
if you submit a comprehensive suite test for this.

Thanks!!
msg68609 - (view) Author: Jean Brouwers (MrJean1) Date: 2008-06-23 05:24
Another rev of the patch now including updates to 2 documentation files: 
Doc/c-api/exceptions.rst and Doc/c-api/string.rst.  As before the patch is 
relative to the source of Python 2.6b1.

Where should test cases for the new formats in the PyString_FromFormat* 
and PyErr_Format functions be added?
msg68610 - (view) Author: Jean Brouwers (MrJean1) Date: 2008-06-23 06:08
The tests for PyString_FromFormat are in file Modules/_testcapimodule.c.

Attached is yet another update of the patch, this one also includes 3 
tests for the new %f format specification added to file 
Modules/_testcapimodule.c.
msg68691 - (view) Author: Jean Brouwers (MrJean1) Date: 2008-06-24 15:47
Here is another patch for PyString_FromFormat* adding support for 'l' and 
'z' type specifications for the '%x' format and an optional '#' to prepend 
'0x' to the hex result.  In addition, the '%p' format is handled as '%#x'.

Updates for the c-api tests and the documentation for exceptions and 
string are also included in the patch.

Like before, the patch is a forward delta to the files of Python 2.6b1.
msg87617 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-05-12 12:26
From the code:

/* assume %f produces at most (L)DBL_DIG 
   digits before and after the decimal
   point, plus the latter plus a sign */

This is not correct. DBL_DIG is 15 on my x86 Linux machine.    
printf("%.*f\n", DBL_DIG, 1e20) produces a string that's 37 characters
long. It might be best to use PyOS_double_to_string instead of snprintf,
although we don't support long double there. I'm not sure long double
support is all that important in practice (for Python), though.

It would be easier to review this if the %p change weren't also
included. That's looks like a good change, but it's a separate issue.

unicodeobject.c should also be modified. In 3.x, it's unicodeobject.c
and bytesobject.c that need to work.
msg87671 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-05-13 10:01
I also think it would be good to use PyOS_double_to_string here.  That
does make it impossible to format long doubles, though, except by doing
a possibly lossy conversion to double first.

As far as I can see, Python doesn't use long double anywhere outside the
ctypes module, so I'm not sure that long double support really matters
right now.
msg110633 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-18 11:15
Jean, do you intend taking this issue any further or can it be closed?
msg110634 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-18 11:17
I'll take a look at this.
msg110668 - (view) Author: Jean Brouwers (MrJean1) Date: 2010-07-18 17:25
Mark L,

No, I do not intend to take this issue further.  Rather the opposite, it should probably be paired down based on the recent comments.  Specifically, use PyOS_double_to_string, remove support for long double and perhaps more.

Also, the patches I created are more than 2 years old and will need to be updated against the current stringobject.c code.  There are quite a few changes in the 2.7 version of that file since early 2.6.
msg110673 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-18 19:24
Closing as Jean has no interest in keeping it open.
msg110715 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-19 06:56
Jean may have no interest in moving this forward, but I do.  (Which is why I assigned the issue to me.)
msg174027 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-10-28 10:35
Okay, I've failed to take this forward.  Reclosing as out of date.
History
Date User Action Args
2012-10-28 10:35:12mark.dickinsonsetstatus: open -> closed
resolution: out of date
messages: + msg174027
2011-11-19 16:35:46mark.dickinsonsetversions: + Python 3.3, - Python 2.7, Python 3.2
2010-07-19 06:56:47mark.dickinsonsetstatus: closed -> open
resolution: out of date -> (no value)
messages: + msg110715
2010-07-18 19:24:30BreamoreBoysetstatus: open -> closed
resolution: out of date
messages: + msg110673
2010-07-18 17:25:23MrJean1setmessages: + msg110668
2010-07-18 11:17:29mark.dickinsonsetassignee: mark.dickinson
messages: + msg110634
2010-07-18 11:15:26BreamoreBoysetnosy: + BreamoreBoy
messages: + msg110633
2009-05-13 10:01:47mark.dickinsonsetmessages: + msg87671
2009-05-12 12:26:35eric.smithsetnosy: + mark.dickinson
messages: + msg87617
2009-05-12 04:03:05ajaksu2setpriority: normal
nosy: + eric.smith
versions: + Python 2.7, Python 3.2, - Python 2.6, Python 2.5, Python 2.4, Python 2.3, Python 2.2.3, Python 2.2.2, Python 2.2.1, Python 2.2, Python 2.1.2, Python 2.1.1, Python 3.0

stage: patch review
2008-06-24 15:47:17MrJean1setfiles: + stringobject_format_xLf.patch
messages: + msg68691
2008-06-23 06:08:54MrJean1setfiles: - stringobject_format_Lf.patch
2008-06-23 06:08:31MrJean1setfiles: + stringobject_format_Lf.patch
messages: + msg68610
2008-06-23 05:24:58MrJean1setfiles: - stringobject_format_Lf.patch
2008-06-23 05:24:38MrJean1setfiles: + stringobject_format_Lf.patch
messages: + msg68609
2008-06-22 23:58:07facundobatistasetnosy: + facundobatista
messages: + msg68597
2008-06-22 23:52:24MrJean1setfiles: + stringobject_format_Lf.patch
2008-06-22 23:50:12MrJean1setfiles: - stringobject_format_Lf.patch
2008-06-22 23:47:26MrJean1setfiles: + stringobject_format_Lf.patch
messages: + msg68596
2008-06-21 23:34:26MrJean1setfiles: + stringobject_format_f.patch
2008-06-21 23:33:29MrJean1setfiles: - stringobject_format_f.patch
2008-06-21 23:21:43MrJean1setfiles: + stringobject_format_f.patch
keywords: + patch
messages: + msg68552
nosy: + MrJean1
2008-06-21 17:42:16duanebsetmessages: + msg68523
2008-06-21 16:04:56riqsetnosy: + riq
messages: + msg68509
2008-05-10 21:09:06duanebcreate