New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Py3k] line number is wrong after encoding declaration #46637
Comments
# This issue inherits from bpo-2301. If there is "# coding: ????" is in source code and Please look into Parser/tokenizer.c. In this case, So # coding: ascii outputs C:\Documents and Settings\WhiteRabbit>py3k ascii.py Traceback (most recent call last):
File "ascii.py", line 6, in <module>
# 4
RuntimeError: a
[22821 refs] One line shifted because line number wrongly +1 And # dummy outputs C:\Documents and Settings\WhiteRabbit>py3k ascii.py Traceback (most recent call last):
File "ascii.py", line 8, in <module>
# 5
RuntimeError: a
[22821 refs] Two lines shifted because line number wrongly +2 |
Following dirty hack workarounds this bug. Comment of this function Index: Parser/tokenizer.c --- Parser/tokenizer.c (revision 61632)
+++ Parser/tokenizer.c (working copy)
@@ -464,6 +464,7 @@
Py_XDECREF(tok->decoding_readline);
readline = PyObject_GetAttrString(stream, "readline");
tok->decoding_readline = readline;
+ tok->lineno = -1; /* dirty hack */
cleanup:
Py_XDECREF(stream); But if multibyte character is in line like this, its line will not be # coding: cp932 C:\Documents and Settings\WhiteRabbit>py3k cp932.py
Traceback (most recent call last):
File "cp932.py", line 3, in <module>
[22819 refs] This is because Python/trackeback.c 's tb_displayline() assumes # http://mail.python.org/pipermail/python-3000/2008-March/012546.html |
I've written testcase for lineno problem. |
This is a bug and not a new feature, so it could go in after beta. I'm |
While this is a bug, it's not serious enough to hold up the release. |
Py3.0b2. This bug seems to be quite annoying. Especially when one works --------- def fb():
i = 1
j = 0
r = i / j # -- coding: cp1252 -- import modb
def fa():
modb.fb() # -- coding: cp1252 -- import moda
def main():
moda.fa()
if __name__ == '__main__':
main() Running main.py leads to an >c:\python30\pythonw -u "main.py"
(Traceback (most recent call last):
File "main.py", line 11, in <module>
File "main.py", line 8, in main
File "C:\jm\jmpy3\moda.py", line 8, in fa
File "C:\jm\jmpy3\modb.py", line 8, in fb
ZeroDivisionError: int division or modulo by zero
>Exit code: 1 |
Could "-- coding: ascii --" and other equivalent encodings be fixed, |
Python 3.0rc1 If the lines are now displayed correctly, I think there is still a Python 2.5.2 # -- coding: cp1252 -- <<<< line 1, first line s = 'abc'
import dummy
s = 'def' >pythonw -u "testpy2.py"
Traceback (most recent call last):
File "testpy2.py", line 4, in <module>
import dummy
ImportError: No module named dummy
>Exit code: 1 Python 3.0rc1 # -- coding: cp1252 -- s = 'abc'
import dummy
s = 'def' >c:\python30\pythonw -u "testpy3.py"
Traceback (most recent call last):
File "testpy3.py", line 5, in <module>
s = 'def'
ImportError: No module named dummy
>Exit code: 1 |
bpo-3973 is a duplicate. |
By setting lineto to 1 (as proposed ocean-city), ASCII tests (test1 --- test1 --- --- test2 --- I don't know how to test a UTF-16 file. Can someone write a testcase? |
ocean-city testcase is invalid: it uses subprocess.call() which |
Hum, about the empty line error using a multibyte charset, the issue |
Here is a patch fixing this issue: it's quite the same that ocean-city About the truncated traceback for multibyte charset: see the new |
Oh! My patch breaks "python -m". The problem is maybe no in the token Python 3.0 trunk unpatched: $ ./python test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
$ ./python -m test
Traceback (most recent call last):
File "/home/haypo/prog/py3k/Lib/runpy.py", line 121, in
_run_module_as_main
"__main__", fname, loader, pkg_name)
File "/home/haypo/prog/py3k/Lib/runpy.py", line 34, in _run_code
exec(code, run_globals)
File "/home/haypo/prog/py3k/test.py", line 2, in <module>
raise Exception("line 2")
Exception: line 2 Python 3.0 trunk + tokenizer-coding.patch: marge$ ./python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
raise Exception("line 2")
Exception: line 2
Traceback (most recent call last):
File "/home/haypo/prog/py3k/Lib/runpy.py", line 121, in
_run_module_as_main
"__main__", fname, loader, pkg_name)
File "/home/haypo/prog/py3k/Lib/runpy.py", line 34, in _run_code
exec(code, run_globals)
File "/home/haypo/prog/py3k/test.py", line 1, in <module>
# coding: ASCII
Exception: line 2 |
Victor, this is fp_setreadl's problem, so if put "tok->lineno = -1" r = set_readline(tok, cs);
if (r) {
/* 1 */
tok->encoding = cs;
tok->decoding_state = STATE_NORMAL; At /* 1 */, set_readline could be buf_setreadl(), and fp_setreadl is |
@Ocean-City: Oops, sorry. Using your patch (set lineno in The new patch includes your fix for tokenizer.c and a new version of the |
bpo-2832 is a duplicate. |
benjamin was afraid by the comment /* dirty hack */ in my previous |
Your patch does the correct thing, however an explanation of the -1
Or we could change the place of the tok->lineno++ in tok_nextc() so that Then, I think that your test is not correct: What is the meaning of the |
@Amaury: Ok, I added your long comment in tokenizer.c. You're also Since tokenizer.c is hard to understand, I don't wnat to change the |
This issue depends on bpo-3975 to properly display tracebacks from python |
Committed r66867. I had to considerably change the unit tests, because the subprocess |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: