[Jython-checkins] jython: Respect AST.lineno in compiled code (fixes #2635).
jeff.allen
jython-checkins at python.org
Thu Apr 4 04:12:25 EDT 2019
https://hg.python.org/jython/rev/23ded47db8ca
changeset: 8234:23ded47db8ca
user: Jim Peterson <elkniwt at gmail.com>
date: Thu Apr 04 08:06:04 2019 +0100
summary:
Respect AST.lineno in compiled code (fixes #2635).
This change makes getLine() and getCharPositionInLine() protected, adding
getLine() and getCol_offset() to PythonTree, which calls the protected methods.
All references outside the PythonTree descendents call getLineno() and
getCol_offset(), instead.
files:
ACKNOWLEDGMENTS | 1 +
Lib/test/test_ast.py | 15 ++++++++-
NEWS | 1 +
src/org/python/antlr/ParseException.java | 2 +-
src/org/python/antlr/PythonTree.java | 12 ++++++-
src/org/python/compiler/CodeCompiler.java | 16 +++++-----
src/org/python/compiler/Module.java | 2 +-
src/org/python/compiler/ScopesCompiler.java | 4 +-
src/org/python/core/ParserFacade.java | 4 +-
9 files changed, 40 insertions(+), 17 deletions(-)
diff --git a/ACKNOWLEDGMENTS b/ACKNOWLEDGMENTS
--- a/ACKNOWLEDGMENTS
+++ b/ACKNOWLEDGMENTS
@@ -184,6 +184,7 @@
Raymond Ferguson
yishenggudou (愧僧)
Andrew Kuchling
+ Jim Peterson
Local Variables:
mode: indented-text
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1,4 +1,4 @@
-import sys, itertools, unittest
+import sys, itertools, unittest, traceback
from test import test_support
import ast
@@ -290,6 +290,19 @@
self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
self.assertRaises(ValueError, ast.literal_eval, 'foo()')
+ def test_uses_lineno(self):
+ node = ast.Module(body=[ast.Expr(value=ast.Name(id='x', ctx=ast.Load()), lineno=42)])
+ node = ast.fix_missing_locations(node)
+ exc = None
+ try:
+ exec compile(node,'<test_uses_lineno>','exec')
+ except:
+ exc = traceback.format_tb(sys.exc_info()[2])
+ assert exc is not None
+
+ # can only check the last line of the stack trace:
+ self.assertEqual(exc[-1],' File "<test_uses_lineno>", line 42, in <module>\n')
+
def test_main():
#XXX: AST pickling is left as a TODO for now.
diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@
Development tip
Bugs fixed
+ - [ 2635 ] AST.lineno ignored by compile
- [ 2744 ] Support buffer type in marshal.dump(s)
- [ 2077 ] marshal doesn't raise error when fed unmarshalable object
- [ 2445 ] Eclipse's DelegatingFeatureMap has MRO conflict
diff --git a/src/org/python/antlr/ParseException.java b/src/org/python/antlr/ParseException.java
--- a/src/org/python/antlr/ParseException.java
+++ b/src/org/python/antlr/ParseException.java
@@ -35,7 +35,7 @@
* n must not be null to use this constructor
*/
public ParseException(String message, PythonTree n) {
- this(message, n.getLine(), n.getCharPositionInLine());
+ this(message, n.getLineno(), n.getCol_offset());
this.node = n;
this.token = n.getToken();
}
diff --git a/src/org/python/antlr/PythonTree.java b/src/org/python/antlr/PythonTree.java
--- a/src/org/python/antlr/PythonTree.java
+++ b/src/org/python/antlr/PythonTree.java
@@ -78,7 +78,7 @@
return node.getText();
}
- public int getLine() {
+ protected int getLine() {
if (node.getToken()==null || node.getToken().getLine()==0) {
if ( getChildCount()>0 ) {
return getChild(0).getLine();
@@ -88,7 +88,7 @@
return node.getToken().getLine();
}
- public int getCharPositionInLine() {
+ protected int getCharPositionInLine() {
Token token = node.getToken();
if (token==null || token.getCharPositionInLine()==-1) {
if (getChildCount()>0) {
@@ -105,6 +105,14 @@
return token.getCharPositionInLine();
}
+ public int getLineno() {
+ return getLine();
+ }
+
+ public int getCol_offset() {
+ return getCharPositionInLine();
+ }
+
public int getTokenStartIndex() {
return node.getTokenStartIndex();
}
diff --git a/src/org/python/compiler/CodeCompiler.java b/src/org/python/compiler/CodeCompiler.java
--- a/src/org/python/compiler/CodeCompiler.java
+++ b/src/org/python/compiler/CodeCompiler.java
@@ -183,7 +183,7 @@
}
public void setline(PythonTree node) throws Exception {
- setline(node.getLine());
+ setline(node.getLineno());
}
public void set(PythonTree node) throws Exception {
@@ -509,7 +509,7 @@
scope.setup_closure();
scope.dump();
module.codeConstant(new Suite(node, node.getInternalBody()), name, true, className, false,
- false, node.getLine(), scope, cflags).get(code);
+ false, node.getLineno(), scope, cflags).get(code);
Str docStr = getDocStr(node.getInternalBody());
if (docStr != null) {
@@ -2168,7 +2168,7 @@
code.ldc("append");
code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class));
- String tmp_append = "_[" + node.getLine() + "_" + node.getCharPositionInLine() + "]";
+ String tmp_append = "_[" + node.getLineno() + "_" + node.getCol_offset() + "]";
java.util.List<expr> args = new ArrayList<expr>();
args.add(node.getInternalElt());
@@ -2312,7 +2312,7 @@
scope.setup_closure();
scope.dump();
- module.codeConstant(retSuite, name, true, className, false, false, node.getLine(), scope,
+ module.codeConstant(retSuite, name, true, className, false, false, node.getLineno(), scope,
cflags).get(code);
if (!makeClosure(scope)) {
@@ -2387,8 +2387,8 @@
// Make code object out of suite
module.codeConstant(new Suite(node, node.getInternalBody()), name, false, name,
- getDocStr(node.getInternalBody()), true, false, node.getLine(), scope, cflags).get(
- code);
+ getDocStr(node.getInternalBody()), true, false, node.getLineno(), scope, cflags)
+ .get(code);
// Make class out of name, bases, and code
if (!makeClosure(scope)) {
@@ -2608,7 +2608,7 @@
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
module.codeConstant(new Suite(node, bod), "<genexpr>", true, className, false, false,
- node.getLine(), scope, cflags).get(code);
+ node.getLineno(), scope, cflags).get(code);
code.aconst_null();
if (!makeClosure(scope)) {
@@ -2681,7 +2681,7 @@
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
module.codeConstant(new Suite(node, bod), "<genexpr>", true, className, false, false,
- node.getLine(), scope, cflags).get(code);
+ node.getLineno(), scope, cflags).get(code);
code.aconst_null();
if (!makeClosure(scope)) {
diff --git a/src/org/python/compiler/Module.java b/src/org/python/compiler/Module.java
--- a/src/org/python/compiler/Module.java
+++ b/src/org/python/compiler/Module.java
@@ -685,7 +685,7 @@
if (!err) {
try {
Py.warning(Py.SyntaxWarning, msg, (sfilename != null) ? sfilename : "?",
- node.getLine(), null, Py.None);
+ node.getLineno(), null, Py.None);
return;
} catch (PyException e) {
if (!e.match(Py.SyntaxWarning)) {
diff --git a/src/org/python/compiler/ScopesCompiler.java b/src/org/python/compiler/ScopesCompiler.java
--- a/src/org/python/compiler/ScopesCompiler.java
+++ b/src/org/python/compiler/ScopesCompiler.java
@@ -285,7 +285,7 @@
@Override
public Object visitListComp(ListComp node) throws Exception {
- String tmp = "_[" + node.getLine() + "_" + node.getCharPositionInLine()
+ String tmp = "_[" + node.getLineno() + "_" + node.getCol_offset()
+ "]";
cur.addBound(tmp);
traverse(node);
@@ -328,7 +328,7 @@
visit(generators.get(0).getInternalIter());
}
String bound_exp = "_(x)";
- String tmp = "_(" + node.getLine() + "_" + node.getCharPositionInLine()
+ String tmp = "_(" + node.getLineno() + "_" + node.getCol_offset()
+ ")";
def(tmp);
ArgListCompiler ac = new ArgListCompiler();
diff --git a/src/org/python/core/ParserFacade.java b/src/org/python/core/ParserFacade.java
--- a/src/org/python/core/ParserFacade.java
+++ b/src/org/python/core/ParserFacade.java
@@ -84,8 +84,8 @@
int line=e.line;
int col=e.charPositionInLine;
if (node != null) {
- line = node.getLine();
- col = node.getCharPositionInLine();
+ line = node.getLineno();
+ col = node.getCol_offset();
}
String text= getLine(reader, line);
String msg = e.getMessage();
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list