Table of Contents

Class SyntaxError

Namespace
NumSharp
Assembly
NumSharp.dll

Exception that corresponds to Python's SyntaxError: text that is not valid Python source. Catch it where a string argument is parsed as Python literals and the caller wants to tell malformed text from a well-formed value that is merely not allowed (that one is a ValueError).

public class SyntaxError : NumSharpException, ISerializable, INumSharpException
Inheritance
SyntaxError
Implements
Derived
Inherited Members

Remarks

Raised by the string form of matrix(string, DType, bool) / asmatrix(string, DType), where NumPy's _convert_from_string hands every whitespace-separated token to ast.literal_eval. A token that does not parse ("1 + 2" splits into the lone token +, "1L" is not a number) is CPython's SyntaxError there, with CPython's own text (invalid syntax (<unknown>, line 1), invalid decimal literal (<unknown>, line 1)).

It is NOT a ValueError, exactly as in Python, so a catch (ValueError) written for "Rows not the same size." does not swallow a typo in the literal.

Like Python's, it carries the location: msg, filename, lineno, offset, text, end_lineno and end_offset, filled by SyntaxError(string, string, int?, int?, string, int?, int?) (Python's SyntaxError(msg, (filename, lineno, offset, text, end_lineno, end_offset))), whose Message is CPython's str(): invalid syntax (c.py, line 3). Python's subclasses IndentationError and TabError derive from it.

Constructors

SyntaxError()

Creates the error with Python's text for a bare raise SyntaxError: "None", as str(SyntaxError()) is 'None' (CPython's SyntaxError.str prints its msg attribute, which is None without arguments), unlike the other Python errors' empty text.

public SyntaxError()

Remarks

Prefer the overload that carries the parser's text: a bare SyntaxError tells the user nothing about the token. Every location attribute, msg included, is null.

SyntaxError(string)

Creates the error with the parser's message, which is also msg; the location attributes stay null.

public SyntaxError(string message)

Parameters

message string

CPython's message, including its (<unknown>, line 1) location suffix where CPython prints one.

Remarks

Python's SyntaxError('m'): msg is 'm' and the text is m, nothing parsed out of it, so a message that already holds a location suffix keeps it in msg too.

SyntaxError(string, Exception)

Creates the error with the parser's message and the exception that caused it: Python's raise SyntaxError(message) from innerException, the cause kept in InnerException as Python keeps it in cause; msg is the message and the location attributes stay null.

public SyntaxError(string message, Exception innerException)

Parameters

message string

CPython's message, verbatim.

innerException Exception

The exception that led to this one (a tokenizer's own failure), or null for none.

SyntaxError(string, string, int?, int?, string, int?, int?)

Python's SyntaxError(msg, (filename, lineno, offset, text[, end_lineno, end_offset])): the message and where the error is. Message is CPython's str() of the error: msg (file, line N), msg (file) without a line number, msg (line N) without a file name, msg alone without either; the file is its base name.

public SyntaxError(string msg, string filename, int? lineno, int? offset, string text, int? end_lineno = null, int? end_offset = null)

Parameters

msg string

The parser's message without the location (invalid syntax), or null (printed None).

filename string

The source's name (<unknown> for ast.literal_eval, a path), or null for none.

lineno int?

The 1-based line, or null for none.

offset int?

The 1-based column (CPython reports 0 for some tokenizer errors), or null for none.

text string

The source line, or null for none.

end_lineno int?

The line the error ends on, or null for none.

end_offset int?

The column it ends at, or null for none; required when end_lineno is given.

Remarks

The base name follows the operating system NumSharp runs on, as CPython's does (its my_basename splits at the platform's SEP): on Windows C:\a\b\c.py reads c.py and a/b/c.py stays whole; elsewhere a/b/c.py reads c.py and C:\a\b\c.py stays whole. A name that ends in the separator reads as empty (m (, line 3)), as in CPython.

Exceptions

TypeError

end_lineno is given without end_offset (CPython: end_offset must be provided when end_lineno is provided).

Properties

end_lineno

The line the error ends on (Python's end_lineno), or null when not given.

public int? end_lineno { get; }

Property Value

int?

end_offset

The column the error ends at (Python's end_offset), or null when not given.

public int? end_offset { get; }

Property Value

int?

filename

The name of the source the error is in (Python's filename), or null when not given.

public string filename { get; }

Property Value

string

Remarks

The full name as given; only Message shortens it to the base name.

lineno

The 1-based line of the error (Python's lineno), or null when not given.

public int? lineno { get; }

Property Value

int?

msg

The parser's message without the location (Python's msg), or null after the bare constructor.

public string msg { get; }

Property Value

string

offset

The column of the error (Python's offset), or null when not given.

public int? offset { get; }

Property Value

int?

text

The source line holding the error (Python's text), or null when not given.

public string text { get; }

Property Value

string