classification
Title: Not matched behavior within printf style bytes formatting
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: docs@python, ethan.furman, python-dev, r.david.murray, serhiy.storchaka, woo yoo
Priority: normal Keywords: patch

Created on 2016-12-17 13:41 by woo yoo, last changed 2017-03-31 16:36 by dstufft. This issue is now closed.

Files
File name Uploaded Description Edit
bytes-format-oct-alt-zero.patch serhiy.storchaka, 2016-12-17 16:27 review
Pull Requests
URL Status Linked Edit
PR 552 closed dstufft, 2017-03-31 16:36
Messages (9)
msg283485 - (view) Author: woo yoo (woo yoo) Date: 2016-12-17 13:41
Per the documentation,"The alternate form causes a leading octal specifier ('0o') to be inserted before the first digit", However the actual behavior didn't conform to the principle.

Code:
>>>b'%#07o' % 34

Output: b'0000o42'
msg283486 - (view) Author: woo yoo (woo yoo) Date: 2016-12-17 13:41
The link is https://docs.python.org/3.5/library/stdtypes.html#printf-style-bytes-formatting
msg283488 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-12-17 14:29
The documentation matches the behavior.  In this context "the first digit" is the 4.  The leading zeros are the pad fill.  Now, whether this is *useful* behavior or not is a separate question :)  And yes, the docs could be clarified on this point either way.
msg283489 - (view) Author: woo yoo (woo yoo) Date: 2016-12-17 14:37
Make a slight change to my code, which becomes `b'%#07x' % 34`, the weird situation appears.
msg283491 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-17 15:18
This is not documentation issue, but a bug in formatting octals in bytes.

>>> '%#07x' % 123
'0x0007b'
>>> b'%#07x' % 123
b'0x0007b'
>>> '%#07o' % 123
'0o00173'
>>> b'%#07o' % 123
b'000o173'
     ^
msg283499 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-17 16:27
Proposed patch fixes this inconsistency.
msg283502 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-12-17 18:06
OK, that makes sense.  Patch looks good to me.
msg283513 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-12-17 20:16
New changeset 96d728c14267 by Serhiy Storchaka in branch '3.5':
Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
https://hg.python.org/cpython/rev/96d728c14267

New changeset 29c9c414c310 by Serhiy Storchaka in branch '3.6':
Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
https://hg.python.org/cpython/rev/29c9c414c310

New changeset 4e55e011dd80 by Serhiy Storchaka in branch 'default':
Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
https://hg.python.org/cpython/rev/4e55e011dd80
msg283514 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-17 20:18
Thanks David.
History
Date User Action Args
2017-03-31 16:36:28dstufftsetpull_requests: + pull_request1017
2016-12-17 20:18:12serhiy.storchakasetstatus: open -> closed
messages: + msg283514

assignee: docs@python -> serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2016-12-17 20:16:48python-devsetnosy: + python-dev
messages: + msg283513
2016-12-17 18:06:35r.david.murraysetmessages: + msg283502
2016-12-17 16:27:23serhiy.storchakasetfiles: + bytes-format-oct-alt-zero.patch
keywords: + patch
messages: + msg283499

stage: needs patch -> patch review
2016-12-17 15:18:22serhiy.storchakasettype: behavior
components: + Interpreter Core, - Documentation
versions: + Python 3.6, Python 3.7
nosy: + serhiy.storchaka, ethan.furman

messages: + msg283491
stage: needs patch
2016-12-17 14:37:49woo yoosetmessages: + msg283489
2016-12-17 14:29:46r.david.murraysetnosy: + r.david.murray
messages: + msg283488
2016-12-17 13:41:41woo yoosetmessages: + msg283486
2016-12-17 13:41:04woo yoocreate