Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c53971

Browse files
committed
add PyErr_SyntaxLocationEx, to support adding a column offset
1 parent 26d64ae commit 2c53971

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

Doc/c-api/exceptions.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ in various ways. There is a separate error indicator for each thread.
294294
parameter specifying the exception type to be raised. Availability: Windows.
295295

296296

297+
.. cfunction:: void PyErr_SyntaxLocationEx(char *filename, int lineno, int col_offset)
298+
299+
Set file, line, and offset information for the current exception. If the
300+
current exception is not a :exc:`SyntaxError`, then it sets additional
301+
attributes, which make the exception printing subsystem think the exception
302+
is a :exc:`SyntaxError`.
303+
304+
305+
.. cfunction:: void PyErr_SyntaxLocation(char *filename, int lineno)
306+
307+
Like :cfunc:`PyErr_SyntaxLocationExc`, but the col_offset parameter is
308+
omitted.
309+
310+
297311
.. cfunction:: void PyErr_BadInternalCall()
298312

299313
This is a shorthand for ``PyErr_SetString(PyExc_SystemError, message)``,

Include/pyerrors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ int PySignal_SetWakeupFd(int fd);
229229

230230
/* Support for adding program text to SyntaxErrors */
231231
PyAPI_FUNC(void) PyErr_SyntaxLocation(const char *, int);
232+
PyAPI_FUNC(void) PyErr_SyntaxLocationEx(const char *, int, int);
232233
PyAPI_FUNC(PyObject *) PyErr_ProgramText(const char *, int);
233234

234235
/* The following functions are used to create and modify unicode

Python/errors.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,12 +780,18 @@ PyErr_WriteUnraisable(PyObject *obj)
780780
extern PyObject *PyModule_GetWarningsModule(void);
781781

782782

783+
void
784+
PyErr_SyntaxLocation(const char *filename, int lineno) {
785+
PyErr_SyntaxLocationEx(filename, lineno, -1);
786+
}
787+
788+
783789
/* Set file and line information for the current exception.
784790
If the exception is not a SyntaxError, also sets additional attributes
785791
to make printing of exceptions believe it is a syntax error. */
786792

787793
void
788-
PyErr_SyntaxLocation(const char *filename, int lineno)
794+
PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
789795
{
790796
PyObject *exc, *v, *tb, *tmp;
791797

@@ -802,6 +808,16 @@ PyErr_SyntaxLocation(const char *filename, int lineno)
802808
PyErr_Clear();
803809
Py_DECREF(tmp);
804810
}
811+
if (col_offset >= 0) {
812+
tmp = PyLong_FromLong(col_offset);
813+
if (tmp == NULL)
814+
PyErr_Clear();
815+
else {
816+
if (PyObject_SetAttrString(v, "offset", tmp))
817+
PyErr_Clear();
818+
Py_DECREF(tmp);
819+
}
820+
}
805821
if (filename != NULL) {
806822
tmp = PyUnicode_FromString(filename);
807823
if (tmp == NULL)

0 commit comments

Comments
 (0)