classification
Title: Fill Character cannot be \0
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: builtin __format__ methods cannot fill with \x00 char
View: 12546
Assigned To: eric.smith Nosy List: Jason.Michalski, Julian, eric.smith, mark.dickinson, skrah
Priority: normal Keywords: patch

Created on 2013-04-12 16:54 by Julian, last changed 2014-04-03 16:15 by skrah. This issue is now closed.

Files
File name Uploaded Description Edit
cpython-3.3-17705.patch Jason.Michalski, 2013-04-13 20:57 review
Messages (6)
msg186653 - (view) Author: Julian Berman (Julian) * Date: 2013-04-12 16:54
The docs say:

"The fill character can be any character other than ‘{‘ or ‘}’."

http://docs.python.org/dev/library/string.html#format-specification-mini-language

But:

>>> "{0:\x01>8.2f}".format(12)
'\x01\x01\x0112.00'

whereas:

>>> "{0:\x00>8.2f}".format(12)
'   12.00'
msg186724 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-04-13 14:47
Unsurprisingly (libmpdec is a C library) this also does not work in _decimal. I could add a special case in _decimal.c at the cost of
two additional if statements for all regular use cases.


Is padding with NUL a legitimate use case? IOW, is the slowdown justified?
msg186726 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-04-13 14:51
> Is padding with NUL a legitimate use case?

I don't see a good reason to disallow it, and it seems like a fairly plausible need.  Numpy, for example, pads strings will NUL bytes when placing a short string in long fixed-width field.
msg186731 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-04-13 15:13
Mark Dickinson <report@bugs.python.org> wrote:
> Numpy, for example, pads strings will NUL bytes when placing a short
> string in long fixed-width field.

I was hoping to escape the work, but that's quite convincing. ;)

Changing libmpdec doesn't look very appealing, so probably I'll use
"{" as a placeholder for NUL and then rewrite the result.
msg186842 - (view) Author: Jason Michalski (Jason.Michalski) * Date: 2013-04-13 20:57
I worked on a patch that allows NUL padding for longs, floats and complex numbers. It seems NUL was being used as a sentinel in the format string parsing when no padding character is given. It was then replaced with a space in each call to fill_padding if it was NUL.
msg215456 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2014-04-03 16:14
This looks like a duplicate of #12546.
History
Date User Action Args
2014-04-03 16:15:23skrahsetsuperseder: builtin __format__ methods cannot fill with \x00 char
2014-04-03 16:14:29skrahsetstatus: open -> closed
versions: + Python 3.5, - Python 2.7, Python 3.3
messages: + msg215456

resolution: duplicate
stage: patch review -> resolved
2013-05-04 20:31:38pitrousetstage: patch review
2013-04-19 18:57:24terry.reedysetversions: - Python 3.2
2013-04-13 23:24:10eric.smithsetassignee: eric.smith
2013-04-13 20:57:04Jason.Michalskisetfiles: + cpython-3.3-17705.patch

nosy: + Jason.Michalski
messages: + msg186842

keywords: + patch
2013-04-13 15:13:26skrahsetmessages: + msg186731
2013-04-13 14:51:55mark.dickinsonsetnosy: + mark.dickinson
messages: + msg186726
2013-04-13 14:47:32skrahsetnosy: + skrah
messages: + msg186724
2013-04-12 22:34:57eric.smithsetnosy: + eric.smith
2013-04-12 16:54:55Juliancreate