with self.assertRaises(ValueError):
compile(tree, '<string>', 'exec')
+ def test_compilation_of_ast_nodes_with_default_end_position_values(self):
+ tree = ast.Module(body=[
+ ast.Import(names=[ast.alias(name='builtins', lineno=1, col_offset=0)], lineno=1, col_offset=0),
+ ast.Import(names=[ast.alias(name='traceback', lineno=0, col_offset=0)], lineno=0, col_offset=1)
+ ], type_ignores=[])
+
+ # Check that compilation doesn't crash. Note: this may crash explicitly only on debug mode.
+ compile(tree, "<string>", "exec")
def test_slice(self):
slc = ast.parse("x[::]").body[0].value.slice
--- /dev/null
+Ensure that custom :mod:`ast` nodes without explicit end positions can be
+compiled. Patch by Pablo Galindo.
class Obj2ModVisitor(PickleVisitor):
+
+ attribute_special_defaults = {
+ "end_lineno": "lineno",
+ "end_col_offset": "col_offset",
+ }
+
@contextmanager
def recursive_call(self, node, level):
self.emit('if (_Py_EnterRecursiveCall(" while traversing \'%s\' node")) {' % node, level, reflow=False)
self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
self.emit("Py_CLEAR(tmp);", depth+1)
if self.isNumeric(field):
- self.emit("%s = 0;" % field.name, depth+1)
+ if field.name in self.attribute_special_defaults:
+ self.emit(
+ "%s = %s;" % (field.name, self.attribute_special_defaults[field.name]),
+ depth+1,
+ )
+ else:
+ self.emit("%s = 0;" % field.name, depth+1)
elif not self.isSimpleType(field):
self.emit("%s = NULL;" % field.name, depth+1)
else:
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_lineno = 0;
+ end_lineno = lineno;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_col_offset = 0;
+ end_col_offset = col_offset;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_lineno = 0;
+ end_lineno = lineno;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_col_offset = 0;
+ end_col_offset = col_offset;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_lineno = 0;
+ end_lineno = lineno;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_col_offset = 0;
+ end_col_offset = col_offset;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_lineno = 0;
+ end_lineno = lineno;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_col_offset = 0;
+ end_col_offset = col_offset;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_lineno = 0;
+ end_lineno = lineno;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_col_offset = 0;
+ end_col_offset = col_offset;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_lineno = 0;
+ end_lineno = lineno;
}
else {
int res;
}
if (tmp == NULL || tmp == Py_None) {
Py_CLEAR(tmp);
- end_col_offset = 0;
+ end_col_offset = col_offset;
}
else {
int res;