PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/eval.py

https://github.com/lalitjsraks/pypy
Python | 132 lines | 97 code | 10 blank | 25 comment | 9 complexity | 5e7369928f79652f0dd39052ebce5a01 MD5 | raw file
  1. from pypy.interpreter.error import OperationError
  2. from pypy.rpython.lltypesystem import rffi, lltype
  3. from pypy.module.cpyext.api import (
  4. cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, fread, feof, Py_ssize_tP)
  5. from pypy.module.cpyext.pyobject import PyObject, borrow_from
  6. from pypy.module.cpyext.pyerrors import PyErr_SetFromErrno
  7. from pypy.module.__builtin__ import compiling
  8. @cpython_api([PyObject, PyObject, PyObject], PyObject)
  9. def PyEval_CallObjectWithKeywords(space, w_obj, w_arg, w_kwds):
  10. return space.call(w_obj, w_arg, w_kwds)
  11. @cpython_api([], PyObject)
  12. def PyEval_GetBuiltins(space):
  13. """Return a dictionary of the builtins in the current execution
  14. frame, or the interpreter of the thread state if no frame is
  15. currently executing."""
  16. caller = space.getexecutioncontext().gettopframe_nohidden()
  17. if caller is not None:
  18. w_globals = caller.w_globals
  19. w_builtins = space.getitem(w_globals, space.wrap('__builtins__'))
  20. if not space.isinstance_w(w_builtins, space.w_dict):
  21. w_builtins = w_builtins.getdict()
  22. else:
  23. w_builtins = space.builtin.getdict()
  24. return borrow_from(None, w_builtins)
  25. @cpython_api([], PyObject, error=CANNOT_FAIL)
  26. def PyEval_GetLocals(space):
  27. """Return a dictionary of the local variables in the current execution
  28. frame, or NULL if no frame is currently executing."""
  29. caller = space.getexecutioncontext().gettopframe_nohidden()
  30. if caller is None:
  31. return None
  32. return borrow_from(None, caller.getdictscope())
  33. @cpython_api([], PyObject, error=CANNOT_FAIL)
  34. def PyEval_GetGlobals(space):
  35. """Return a dictionary of the global variables in the current execution
  36. frame, or NULL if no frame is currently executing."""
  37. caller = space.getexecutioncontext().gettopframe_nohidden()
  38. if caller is None:
  39. return None
  40. return borrow_from(None, caller.w_globals)
  41. @cpython_api([PyObject, PyObject], PyObject)
  42. def PyObject_CallObject(space, w_obj, w_arg):
  43. """
  44. Call a callable Python object callable_object, with arguments given by the
  45. tuple args. If no arguments are needed, then args may be NULL. Returns
  46. the result of the call on success, or NULL on failure. This is the equivalent
  47. of the Python expression apply(callable_object, args) or
  48. callable_object(*args)."""
  49. return space.call(w_obj, w_arg)
  50. @cpython_api([PyObject, PyObject, PyObject], PyObject)
  51. def PyObject_Call(space, w_obj, w_args, w_kw):
  52. """
  53. Call a callable Python object, with arguments given by the
  54. tuple args, and named arguments given by the dictionary kw. If no named
  55. arguments are needed, kw may be NULL. args must not be NULL, use an
  56. empty tuple if no arguments are needed. Returns the result of the call on
  57. success, or NULL on failure. This is the equivalent of the Python expression
  58. apply(callable_object, args, kw) or callable_object(*args, **kw)."""
  59. return space.call(w_obj, w_args, w_kw)
  60. # These constants are also defined in include/eval.h
  61. Py_single_input = 256
  62. Py_file_input = 257
  63. Py_eval_input = 258
  64. def run_string(space, source, filename, start, w_globals, w_locals):
  65. w_source = space.wrap(source)
  66. start = rffi.cast(lltype.Signed, start)
  67. if start == Py_file_input:
  68. mode = 'exec'
  69. elif start == Py_eval_input:
  70. mode = 'eval'
  71. elif start == Py_single_input:
  72. mode = 'single'
  73. else:
  74. raise OperationError(space.w_ValueError, space.wrap(
  75. "invalid mode parameter for PyRun_String"))
  76. w_code = compiling.compile(space, w_source, filename, mode)
  77. return compiling.eval(space, w_code, w_globals, w_locals)
  78. @cpython_api([CONST_STRING, rffi.INT_real,PyObject, PyObject], PyObject)
  79. def PyRun_String(space, source, start, w_globals, w_locals):
  80. """This is a simplified interface to PyRun_StringFlags() below, leaving
  81. flags set to NULL."""
  82. source = rffi.charp2str(source)
  83. filename = "<string>"
  84. return run_string(space, source, filename, start, w_globals, w_locals)
  85. @cpython_api([FILEP, CONST_STRING, rffi.INT_real, PyObject, PyObject], PyObject)
  86. def PyRun_File(space, fp, filename, start, w_globals, w_locals):
  87. """This is a simplified interface to PyRun_FileExFlags() below, leaving
  88. closeit set to 0 and flags set to NULL."""
  89. BUF_SIZE = 8192
  90. source = ""
  91. filename = rffi.charp2str(filename)
  92. buf = lltype.malloc(rffi.CCHARP.TO, BUF_SIZE, flavor='raw')
  93. try:
  94. while True:
  95. count = fread(buf, 1, BUF_SIZE, fp)
  96. source += rffi.charpsize2str(buf, count)
  97. if count < BUF_SIZE:
  98. if feof(fp):
  99. break
  100. PyErr_SetFromErrno(space, space.w_IOError)
  101. finally:
  102. lltype.free(buf, flavor='raw')
  103. return run_string(space, source, filename, start, w_globals, w_locals)
  104. # Undocumented function!
  105. @cpython_api([PyObject, Py_ssize_tP], rffi.INT_real, error=0)
  106. def _PyEval_SliceIndex(space, w_obj, pi):
  107. """Extract a slice index from a PyInt or PyLong or an object with the
  108. nb_index slot defined, and store in *pi.
  109. Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
  110. and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
  111. Return 0 on error, 1 on success.
  112. Note: If v is NULL, return success without storing into *pi. This
  113. is because_PyEval_SliceIndex() is called by apply_slice(), which can be
  114. called by the SLICE opcode with v and/or w equal to NULL.
  115. """
  116. if w_obj is not None:
  117. pi[0] = space.getindex_w(w_obj, None)
  118. return 1