classification
Title: PyUnicode_FromFormatV() must fail if the format string is invalid
Type: Stage:
Components: Versions: Python 3.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: skrah, vstinner
Priority: normal Keywords: patch

Created on 2012-10-06 21:56 by vstinner, last changed 2013-04-09 22:19 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
invalid_format.patch vstinner, 2012-10-06 21:56 review
invalid_format-2.patch vstinner, 2012-10-07 21:06 review
Messages (6)
msg172250 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-10-06 21:56
If the format string is invalid, PyUnicode_FromFormatV() formats the valid arguments and then copies the raw format string. Errors are silently ignored.

I propose to raise a ValueError if the format string is invalid. This change may "break" existing applications, but I hope that nobody relies on this "bug" :-)

Attached patch implements my proposition.
msg172251 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-10-06 21:57
The full test suite pass with the patch, so hopefully, CPython doesn't rely on this bug :-)
msg172261 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-10-06 23:01
Hum, my patch is incomplete: "%.s" is not seen as an invalid format.
msg172287 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-10-07 09:58
I don't know the reason for the previous behavior, but it's documented:

http://docs.python.org/dev/c-api/unicode.html?highlight=pyunicode_fromformatv#PyUnicode_FromFormat


"An unrecognized format character causes all the rest of the format string to be copied as-is to the result string, and any extra arguments discarded."
msg172345 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-10-07 21:06
Updated patch: detect also invalid "%.s" format and update the documentation.

> I don't know the reason for the previous behavior, but it's documented:

Oh, I missed the doc. Fixed in the new patch.
msg186461 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-04-09 22:19
Barry wrote on python-dev mailing list:
"If it's documented to behave that way, why would you still consider it a bug?
The current behavior is clearly intentional, the function is working as
intended, and there may be code out there that depends on this documented
functionality, or at least, it won't be prepared to handle the new exception."
http://mail.python.org/pipermail/python-dev/2012-October/122122.html

So I'm closing the issue.
History
Date User Action Args
2013-04-09 22:19:26vstinnersetstatus: open -> closed
resolution: wont fix
messages: + msg186461
2012-10-07 21:06:37vstinnersetfiles: + invalid_format-2.patch

messages: + msg172345
2012-10-07 09:58:53skrahsetnosy: + skrah
messages: + msg172287
2012-10-06 23:01:43vstinnersetmessages: + msg172261
2012-10-06 21:57:55vstinnersetmessages: + msg172251
2012-10-06 21:56:20vstinnercreate