| 1 | import sys |
| 2 | from rpython.rlib.rarithmetic import widen |
| 3 | from rpython.rtyper.lltypesystem import rffi |
| 4 | from pypy.interpreter.error import oefmt |
| 5 | from pypy.objspace.std.sliceobject import W_SliceObject |
| 6 | from pypy.module.__builtin__ import compiling |
| 7 | from pypy.module._hpy_universal.apiset import API |
| 8 | from pypy.module._hpy_universal import llapi |
| 9 | |
| 10 | @API.func("HPy HPy_EvalCode(HPyContext *ctx, HPy code, HPy globals, HPy locals)") |
| 11 | def HPy_EvalCode(space, handles, ctx, h_code, h_globals, h_locals): |
| 12 | w_code = handles.deref(h_code) |
| 13 | if not h_globals: |
| 14 | w_globals = space.w_None |
| 15 | else: |
| 16 | w_globals = handles.deref(h_globals) |
| 17 | if not h_locals: |
| 18 | w_locals = space.w_None |
| 19 | else: |
| 20 | w_locals = handles.deref(h_locals) |
| 21 | w_ret = compiling.eval(space, w_code, w_globals, w_locals) |
| 22 | return handles.new(w_ret) |
| 23 | |
| 24 | @API.func("HPy HPy_Compile_s(HPyContext *ctx, const char *utf8_source, const char *utf8_filename, int kind)") |
| 25 | def HPy_Compile_s(space, handles, ctx, source, filename, kind): |
| 26 | """ |
| 27 | Evaluating Python statements/expressions */ |
| 28 | |
| 29 | Parse and compile the Python source code. |
| 30 | |
| 31 | :param ctx: |
| 32 | The execution context. |
| 33 | :param utf8_source: |
| 34 | Python source code given as UTF-8 encoded C string (must not be ``NULL``). |
| 35 | :param utf8_filename: |
| 36 | The filename (UTF-8 encoded C string) to use for construction of the code |
| 37 | object. It may appear in tracebacks or in ``SyntaxError`` exception |
| 38 | messages. |
| 39 | :param kind: |
| 40 | The source kind which tells the parser if a single expression, statement, |
| 41 | or a whole file should be parsed (see enum :c:enum:`HPy_SourceKind`). |
| 42 | |
| 43 | :returns: |
| 44 | A Python code object resulting from the parsed and compiled Python source |
| 45 | code or ``HPy_NULL`` in case of errors. |
| 46 | """ |
| 47 | Kinds = llapi.cts.gettype("HPy_SourceKind") |
| 48 | w_source = API.ccharp2text(space, source) |
| 49 | filename = rffi.constcharp2str(filename) |
| 50 | kind = widen(kind) |
| 51 | if kind == Kinds.HPy_SourceKind_Expr: |
| 52 | mode = "eval" |
| 53 | elif kind == Kinds.HPy_SourceKind_File: |
| 54 | mode = "exec" |
| 55 | elif kind == Kinds.HPy_SourceKind_Single: |
| 56 | mode = "single" |
| 57 | else: |
| 58 | raise oefmt(space.w_SystemError, "invalid source kind") |
| 59 | flags = 0 |
| 60 | feature_version = -1 |
| 61 | w_result = compiling.compile(space, w_source, |
| 62 | filename, mode, flags, |
| 63 | _feature_version=feature_version) |
| 64 | return handles.new(w_result) |