~ubuntu-branches/debian/experimental/nuitka/experimental

« back to all changes in this revision

Viewing changes to nuitka/SyntaxErrors.py

  • Committer: Package Import Robot
  • Author(s): Kay Hayen
  • Date: 2015-04-06 17:20:44 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20150406172044-grhy9sk7g0whu2ag
Tags: 0.5.12+ds-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#     Copyright 2015, Kay Hayen, mailto:kay.hayen@gmail.com
2
 
#
3
 
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
4
 
#     integrates with CPython, but also works on its own.
5
 
#
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
9
 
#
10
 
#        http://www.apache.org/licenses/LICENSE-2.0
11
 
#
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.
17
 
#
18
 
""" Handling of syntax errors.
19
 
 
20
 
Format SyntaxError/IndentationError exception for output, as well as
21
 
raise it for the given source code reference.
22
 
"""
23
 
 
24
 
def formatOutput(e):
25
 
    if len(e.args) > 1:
26
 
        reason, (filename, lineno, colno, message) = e.args
27
 
 
28
 
        if message is None and colno is not None:
29
 
            colno = None
30
 
    else:
31
 
        reason, = e.args
32
 
 
33
 
        filename = None
34
 
        lineno = None
35
 
        colno = None
36
 
        message = None
37
 
 
38
 
    # On CPython3.4 at least, this attribute appears to override reason for
39
 
    # SyntaxErrors at least.
40
 
    if hasattr(e, "msg"):
41
 
        reason = e.msg
42
 
 
43
 
    if colno is not None:
44
 
        colno = colno - len(message) + len(message.lstrip())
45
 
 
46
 
        return """\
47
 
  File "%s", line %d
48
 
    %s
49
 
    %s^
50
 
%s: %s""" % (
51
 
            filename,
52
 
            lineno,
53
 
            message.strip(),
54
 
            ' ' * (colno-1) if colno is not None else "",
55
 
            e.__class__.__name__,
56
 
            reason
57
 
         )
58
 
    elif message is not None:
59
 
        return """\
60
 
  File "%s", line %d
61
 
    %s
62
 
%s: %s""" % (
63
 
            filename,
64
 
            lineno,
65
 
            message.strip(),
66
 
            e.__class__.__name__,
67
 
            reason
68
 
         )
69
 
    elif filename is not None:
70
 
        return """\
71
 
  File "%s", line %s
72
 
%s: %s""" % (
73
 
            filename,
74
 
            lineno,
75
 
            e.__class__.__name__,
76
 
            reason
77
 
         )
78
 
    else:
79
 
        return """\
80
 
%s: %s""" % (
81
 
            e.__class__.__name__,
82
 
            reason
83
 
         )
84
 
 
85
 
 
86
 
def raiseSyntaxError(reason, source_ref, col_offset = None, display_file = True,
87
 
                     display_line = True, source_line = None):
88
 
    def readSource():
89
 
        import linecache
90
 
        return linecache.getline(
91
 
            filename = source_ref.getFilename(),
92
 
            lineno   = source_ref.getLineNumber()
93
 
        )
94
 
 
95
 
    if display_file and display_line:
96
 
        if source_line is None:
97
 
            source_line = readSource()
98
 
 
99
 
        raise SyntaxError(
100
 
            reason,
101
 
            (
102
 
                source_ref.getFilename(),
103
 
                source_ref.getLineNumber(),
104
 
                col_offset,
105
 
                source_line
106
 
            )
107
 
        )
108
 
    else:
109
 
        if source_ref is not None:
110
 
            if source_line is None and display_line:
111
 
                source_line = readSource()
112
 
 
113
 
            raise SyntaxError(
114
 
                reason,
115
 
                (
116
 
                    source_ref.getFilename(),
117
 
                    source_ref.getLineNumber(),
118
 
                    None,
119
 
                    source_line
120
 
                )
121
 
            )
122
 
        else:
123
 
            raise SyntaxError(
124
 
                reason,
125
 
                (
126
 
                    None,
127
 
                    None,
128
 
                    None,
129
 
                    None
130
 
                )
131
 
            )