diff --git a/starlark_go.c b/starlark_go.c index 4318edd..bf72c74 100644 --- a/starlark_go.c +++ b/starlark_go.c @@ -1,7 +1,6 @@ #define PY_SSIZE_T_CLEAN #include - /* This stuff is in the Go file */ unsigned long NewThread(); void DestroyThread(unsigned long threadId); @@ -14,23 +13,10 @@ static PyObject *StarlarkError = NULL; static PyObject *SyntaxError = NULL; static PyObject *EvalError = NULL; -static inline PyObject *PyUnicode_Copy(const char *str) { - int length = strlen(str); - - PyObject *src = PyUnicode_FromString(str); - PyObject *dst = PyUnicode_New(length, 1114111); - PyUnicode_CopyCharacters(dst, 0, src, 0, length); - Py_DECREF(src); - - return dst; -} /* Helpers to raise custom exceptions from Go */ void Raise_StarlarkError(const char *error, const char *error_type) { PyGILState_STATE gilstate = PyGILState_Ensure(); - PyObject *exc_args = PyTuple_New(2); - - PyTuple_SetItem(exc_args, 0, PyUnicode_Copy(error)); - PyTuple_SetItem(exc_args, 1, PyUnicode_Copy(error_type)); + PyObject *exc_args = Py_BuildValue("ss", error, error_type); PyErr_SetObject(StarlarkError, exc_args); Py_DECREF(exc_args); PyGILState_Release(gilstate); @@ -38,14 +24,7 @@ void Raise_StarlarkError(const char *error, const char *error_type) { void Raise_SyntaxError(const char *error, const char *error_type, const char *msg, const char *filename, const long line, const long column) { PyGILState_STATE gilstate = PyGILState_Ensure(); - PyObject *exc_args = PyTuple_New(6); - - PyTuple_SetItem(exc_args, 0, PyUnicode_Copy(error)); - PyTuple_SetItem(exc_args, 1, PyUnicode_Copy(error_type)); - PyTuple_SetItem(exc_args, 2, PyUnicode_Copy(msg)); - PyTuple_SetItem(exc_args, 3, PyUnicode_Copy(filename)); - PyTuple_SetItem(exc_args, 4, PyLong_FromLong(line)); - PyTuple_SetItem(exc_args, 5, PyLong_FromLong(column)); + PyObject *exc_args = Py_BuildValue("ssssll", error, error_type, msg, filename, line, column); PyErr_SetObject(SyntaxError, exc_args); Py_DECREF(exc_args); PyGILState_Release(gilstate); @@ -53,11 +32,7 @@ void Raise_SyntaxError(const char *error, const char *error_type, const char *ms void Raise_EvalError(const char *error, const char *error_type, const char *backtrace) { PyGILState_STATE gilstate = PyGILState_Ensure(); - PyObject *exc_args = PyTuple_New(3); - - PyTuple_SetItem(exc_args, 0, PyUnicode_Copy(error)); - PyTuple_SetItem(exc_args, 1, PyUnicode_Copy(error_type)); - PyTuple_SetItem(exc_args, 2, PyUnicode_Copy(backtrace)); + PyObject *exc_args = Py_BuildValue("sss", error, error_type, backtrace); PyErr_SetObject(EvalError, exc_args); Py_DECREF(exc_args); PyGILState_Release(gilstate); @@ -290,7 +265,7 @@ PyMODINIT_FUNC PyInit_starlark_go(void) { StarlarkError = PyErr_NewExceptionWithDoc( "pystarlark.StarlarkError", - "Unspecified Starlark error", + "Starlark general error", NULL, NULL ); diff --git a/tests/test_evalerror.py b/tests/test_evalerror.py new file mode 100644 index 0000000..f74bfed --- /dev/null +++ b/tests/test_evalerror.py @@ -0,0 +1,32 @@ +import pytest + +from pystarlark import EvalError, Starlark + + +def test_raises_evalerror(): + s = Starlark() + + with pytest.raises(EvalError): + s.eval('1 + "2"') + + with pytest.raises(EvalError): + s.exec('1 + "2"') + + +def test_eval_attrs(): + s = Starlark() + raised = False + + try: + s.eval('1 + "2"') + except EvalError as e: + assert hasattr(e, "error") + assert isinstance(e.error, str) + assert hasattr(e, "error_type") + assert isinstance(e.error_type, str) + assert e.error_type == "*starlark.EvalError" + assert hasattr(e, "backtrace") + assert isinstance(e.backtrace, str) + raised = True + + assert raised diff --git a/tests/test_syntaxerror.py b/tests/test_syntaxerror.py index 63ed806..78c4444 100644 --- a/tests/test_syntaxerror.py +++ b/tests/test_syntaxerror.py @@ -26,18 +26,18 @@ def test_syntaxerror_attrs(): assert isinstance(e.error, str) assert hasattr(e, "error_type") assert isinstance(e.error_type, str) - # assert e.error_type == "syntax.Error" + assert e.error_type == "*syntax.Error" assert hasattr(e, "msg") assert isinstance(e.msg, str) assert hasattr(e, "filename") assert isinstance(e.filename, str) - # assert e.filename == "" + assert e.filename == "" assert hasattr(e, "line") assert isinstance(e.line, int) - # assert e.line == 1 + assert e.line == 1 assert hasattr(e, "column") assert isinstance(e.column, int) - # assert e.column == 2 + assert e.column == 2 raised = True assert raised