Skip to content

Commit

Permalink
Fix string weirdness
Browse files Browse the repository at this point in the history
  • Loading branch information
jordemort committed Apr 15, 2022
1 parent 6b15599 commit 4fa2a21
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
33 changes: 4 additions & 29 deletions starlark_go.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>


/* This stuff is in the Go file */
unsigned long NewThread();
void DestroyThread(unsigned long threadId);
Expand All @@ -14,50 +13,26 @@ 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);
}

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);
}

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);
Expand Down Expand Up @@ -290,7 +265,7 @@ PyMODINIT_FUNC PyInit_starlark_go(void) {

StarlarkError = PyErr_NewExceptionWithDoc(
"pystarlark.StarlarkError",
"Unspecified Starlark error",
"Starlark general error",
NULL,
NULL
);
Expand Down
32 changes: 32 additions & 0 deletions tests/test_evalerror.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/test_syntaxerror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "<eval>"
assert e.filename == "<expr>"
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

0 comments on commit 4fa2a21

Please sign in to comment.