Skip to content

Commit 14def5e

Browse files
committed
Handle slice at end of list as append
Fixes jython#356
1 parent 38fe45b commit 14def5e

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

Lib/test/test_ast_jy.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,33 @@ def test_empty_init(self):
146146
#ast.stmt()
147147
#ast.unaryop()
148148

149+
class TestAstList(unittest.TestCase):
150+
"""Supplementary tests for org.python.core.AstList"""
151+
152+
def test_concat(self):
153+
# Issue gh-356 Java IndexOOB error on append
154+
body = ast.parse('x=1').body # _ast.AstList
155+
p = [ast.Pass()] # PyList
156+
x = body + p
157+
self.assertEqual(2, len(x))
158+
self.assertIsInstance(x[1], ast.Pass)
159+
160+
def test_append(self):
161+
# Issue gh-356 Java IndexOOB error on append
162+
body = ast.parse('x=1').body # _ast.AstList
163+
p = [ast.Pass()] # PyList
164+
body += p
165+
self.assertEqual(2, len(body))
166+
self.assertIsInstance(body[1], ast.Pass)
167+
168+
149169
#==============================================================================
150170

151171
def test_main(verbose=None):
152-
test_classes = [TestCompile]
172+
test_classes = [
173+
TestCompile,
174+
TestAstList
175+
]
153176
test_support.run_unittest(*test_classes)
154177

155178
if __name__ == "__main__":

src/org/python/core/AstList.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,14 @@ protected void setslicePySequence(int start, int stop, int step, PySequence valu
493493
}
494494
value = newseq;
495495
}
496-
int n = value.__len__();
496+
int size = __len__(), n = value.__len__();
497497
for (int i = 0, j = start; i < n; i++, j += step) {
498-
pyset(j, value.pyget(i));
498+
PyObject item = value.pyget(i);
499+
if (j < size) {
500+
pyset(j, item);
501+
} else {
502+
pyadd(item);
503+
}
499504
}
500505
}
501506
}

0 commit comments

Comments
 (0)