mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Allow changing the filename associated with eval()
This commit is contained in:
parent
810f9ac2f9
commit
ced206da0f
@ -87,19 +87,21 @@ static void DukContext_dealloc(DukContext *self)
|
||||
|
||||
static PyObject *DukContext_eval(DukContext *self, PyObject *args, PyObject *kw)
|
||||
{
|
||||
const char *code;
|
||||
const char *code, *fname = "<eval>";
|
||||
int noresult = 0, ret = 0;
|
||||
PyObject *result = NULL, *temp = NULL;
|
||||
|
||||
static char *keywords[] = {"code", "noreturn", NULL};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "s|O:eval", keywords,
|
||||
&code, &temp)) {
|
||||
static char *keywords[] = {"code", "noreturn", "fname", NULL};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "s|Os:eval", keywords,
|
||||
&code, &temp, &fname)) {
|
||||
return NULL;
|
||||
}
|
||||
if (temp && PyObject_IsTrue(temp)) noresult = 1;
|
||||
|
||||
self->py_thread_state = PyEval_SaveThread(); // Release GIL
|
||||
ret = (noresult) ? duk_peval_string_noresult(self->ctx, code) : duk_peval_string(self->ctx, code);
|
||||
ret = DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | ((noresult) ? DUK_COMPILE_NORESULT : 0);
|
||||
duk_push_string(self->ctx, fname);
|
||||
ret = duk_eval_raw(self->ctx, code, 0, ret);
|
||||
PyEval_RestoreThread(self->py_thread_state); // Acquire GIL
|
||||
self->py_thread_state = NULL;
|
||||
if (ret != 0) {
|
||||
|
@ -141,15 +141,17 @@ class EvalTests(unittest.TestCase):
|
||||
except JSError as e:
|
||||
e = e.args[0]
|
||||
self.assertEqual('SyntaxError', e.name)
|
||||
self.assertEqual('<eval>', e.fileName)
|
||||
self.assertEqual(1, e.lineNumber)
|
||||
self.assertIn('line 1', e.toString())
|
||||
|
||||
try:
|
||||
self.ctx.eval('\na()')
|
||||
self.ctx.eval('\na()', fname='xxx')
|
||||
self.assert_('No error raised for malformed js')
|
||||
except JSError as e:
|
||||
e = e.args[0]
|
||||
self.assertEqual('ReferenceError', e.name)
|
||||
self.assertEqual('xxx', e.fileName)
|
||||
self.assertEqual(2, e.lineNumber)
|
||||
|
||||
def test_eval_multithreading(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user