1
# Copyright 2015, Kay Hayen, mailto:kay.hayen@gmail.com
3
# Part of "Nuitka", an optimizing Python compiler that is compatible and
4
# integrates with CPython, but also works on its own.
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
18
""" Handling of syntax errors.
20
Format SyntaxError/IndentationError exception for output, as well as
21
raise it for the given source code reference.
26
reason, (filename, lineno, colno, message) = e.args
28
if message is None and colno is not None:
38
# On CPython3.4 at least, this attribute appears to override reason for
39
# SyntaxErrors at least.
44
colno = colno - len(message) + len(message.lstrip())
54
' ' * (colno-1) if colno is not None else "",
58
elif message is not None:
69
elif filename is not None:
86
def raiseSyntaxError(reason, source_ref, col_offset = None, display_file = True,
87
display_line = True, source_line = None):
90
return linecache.getline(
91
filename = source_ref.getFilename(),
92
lineno = source_ref.getLineNumber()
95
if display_file and display_line:
96
if source_line is None:
97
source_line = readSource()
102
source_ref.getFilename(),
103
source_ref.getLineNumber(),
109
if source_ref is not None:
110
if source_line is None and display_line:
111
source_line = readSource()
116
source_ref.getFilename(),
117
source_ref.getLineNumber(),