Issue 12705: Make compile('1\n2\n', '', 'single') raise an exception instead of silently truncating? (original) (raw)

Issue12705

Created on 2011-08-07 01:39 by Devin Jeanpierre, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue12705-0.patch meador.inge,2012-01-17 15:52 Patch against tip (3.3.0a0) review
Messages (11)
msg141735 - (view) Author: Devin Jeanpierre (Devin Jeanpierre) * Date: 2011-08-07 01:39
(this is copy-pasted from http://mail.python.org/pipermail/python-ideas/2011-July/010787.html ) compile('1\n2\n', '','single') == compile('1\n', '','single'). That is, it ignores the second statement ('2\n'), without offering a way for the caller to detect this. Considering that 'single' is primarily used to emulate the behaviour of the Python interpreter, most of the time, giving it multiple statements is an impossibility, and so that case doesn't matter and could raise an exception without affecting existing code. For example, the code module meets this description, as do debuggers and such. However, in cases where it _is_ possible to give the compiler multiple statements, the user should be warned that his input isn't valid, somehow. For example, the following doctest will mysteriously fail, because it was written incorrectly (doctest uses 'single'): >>> import sys ... sys.stdout.write('foo\n') foo This is because the second statement in the doctest was silently discarded by compile(). It might not always be clear to users how to fix this, and I think this kind of non-obvious error would exist in any use of 'single' that can in theory involve multiple statements, through user error or program bug. So I'd appreciate it if compile() raised an exception in this case. Perhaps SyntaxError or ValueError.
msg141977 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-08-12 17:46
Pending an argument against, I agree with the change. I think SyntaxError would be best. ValueError (etc) is for runtime (though this is compile during runtime). What would you have for the error message? My first idea is "Cannot compile multiple statements as a single statement." This should be clear enough when calling compile(...'single'). It should at least give a hint in the doctest case.
msg151319 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-01-16 04:56
IMO this is a bug and should be fixed in stable versions too.
msg151373 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-01-16 15:25
I think an exception is fine, but it should only happen in 3.3.
msg151461 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-01-17 15:52
The attached patch fixes this be checking what is left in the input buffer after parsing. Anything but trailing whitespace and comments triggers the exception. Ignoring trailing whitespace and comments makes sense, but it is also somewhat required. Trailing comments are common in doctests: >>> doctest.testfile('test_doctest.txt', raise_on_error=True) ... # doctest: +ELLIPSIS and trailing whitespace is used by the codeop heuristics. Here is an example of the new exception: >>> compile('1 + 2\n3 + 4\n', '','single') Traceback (most recent call last): File "", line 1, in File "", line 1 1 + 2 ^ SyntaxError: multiple statements found while compiling a single statement Tested on Fedora 15; no regressions.
msg151475 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-01-17 16:56
I don’t understand why some two-liners are allowed (like "class X:\n pass"). The doc says “a single interactive statement”.
msg151481 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-01-17 19:20
On Tue, Jan 17, 2012 at 10:56 AM, Éric Araujo <report@bugs.python.org> wrote: > I don’t understand why some two-liners are allowed (like "class X:\n pass").  The doc says “a single interactive statement”. Because a single statement can be multiple lines (as is the case for compound statements). Look at the grammar for a single input statement (interactive_input): http://docs.python.org/dev/reference/toplevel_components.html#interactive-input. This issue is really about multiple statements and not multiple lines.
msg151482 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-17 19:25
Because a class statement is one statement (it is a compound statement, but still just one). The docs don't talk about lines :) A "single interactive statement" is what you can enter at the interactive prompt in one line, or more than one line with only secondary prompts from the second line on.
msg151500 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-01-18 00:06
Thank you both for fixing my incomplete understanding. Meador, unless you want a review from another core dev, I think you can just go ahead.
msg151609 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-01-19 07:11
New changeset 2bd7f40108b4 by Meador Inge in branch 'default': Issue #12705: Raise SyntaxError when compiling multiple statements as single interactive statement http://hg.python.org/cpython/rev/2bd7f40108b4
msg151610 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-01-19 07:12
Fixed in 3.3.
History
Date User Action Args
2022-04-11 14:57:20 admin set github: 56914
2012-01-19 07:12:14 meador.inge set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2012-01-19 07:11:11 python-dev set nosy: + python-devmessages: +
2012-01-18 00:06:11 eric.araujo set messages: +
2012-01-17 19:25:20 georg.brandl set nosy: + georg.brandlmessages: +
2012-01-17 19:20:33 meador.inge set messages: +
2012-01-17 16:56:51 eric.araujo set messages: +
2012-01-17 15:52:07 meador.inge set files: + issue12705-0.patchkeywords: + patchmessages: + stage: needs patch -> patch review
2012-01-16 18:00:09 meador.inge set nosy: + meador.inge
2012-01-16 15:25:13 benjamin.peterson set messages: +
2012-01-16 12:23:25 pitrou set nosy: + benjamin.petersonstage: test needed -> needs patch
2012-01-16 04:56:42 eric.araujo set nosy: + eric.araujomessages: +
2011-08-12 17:46:22 terry.reedy set nosy: + terry.reedymessages: + type: enhancementstage: test needed
2011-08-07 01:39:51 Devin Jeanpierre create