| 1 | from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 | from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL |
| 3 | from pypy.module.cpyext.state import State |
| 4 | from pypy.module.cpyext.pyobject import PyObject |
| 5 | from pypy.module.cpyext.pyerrors import PyErr_SetNone |
| 6 | |
| 7 | @cpython_api([], rffi.INT_real, error=CANNOT_FAIL) |
| 8 | def Py_IsInitialized(space): |
| 9 | return 1 |
| 10 | |
| 11 | @cpython_api([], rffi.CWCHARP, error=CANNOT_FAIL) |
| 12 | def Py_GetProgramName(space): |
| 13 | """ |
| 14 | Return the program name set with Py_SetProgramName(), or the default. |
| 15 | The returned string points into static storage; the caller should not modify its |
| 16 | value.""" |
| 17 | return space.fromcache(State).get_programname() |
| 18 | |
| 19 | @cpython_api([], rffi.CCHARP) |
| 20 | def Py_GetVersion(space): |
| 21 | """Return the version of this Python interpreter. This is a |
| 22 | string that looks something like |
| 23 | |
| 24 | "1.5 (\#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]" |
| 25 | |
| 26 | The first word (up to the first space character) is the current |
| 27 | Python version; the first three characters are the major and minor |
| 28 | version separated by a period. The returned string points into |
| 29 | static storage; the caller should not modify its value. The value |
| 30 | is available to Python code as sys.version.""" |
| 31 | return space.fromcache(State).get_version() |
| 32 | |
| 33 | @cpython_api([lltype.Ptr(lltype.FuncType([], lltype.Void))], rffi.INT_real, error=-1) |
| 34 | def Py_AtExit(space, func_ptr): |
| 35 | """Register a cleanup function to be called by Py_Finalize(). The cleanup |
| 36 | function will be called with no arguments and should return no value. At |
| 37 | most 32 cleanup functions can be registered. When the registration is |
| 38 | successful, Py_AtExit() returns 0; on failure, it returns -1. The cleanup |
| 39 | function registered last is called first. Each cleanup function will be |
| 40 | called at most once. Since Python's internal finalization will have |
| 41 | completed before the cleanup function, no Python APIs should be called by |
| 42 | func.""" |
| 43 | from pypy.module.cpyext.moduledef import Module |
| 44 | w_module = space.getbuiltinmodule('cpyext') |
| 45 | module = space.interp_w(Module, w_module) |
| 46 | try: |
| 47 | module.register_atexit(func_ptr) |
| 48 | except ValueError: |
| 49 | return -1 |
| 50 | return 0 |
| 51 | |
| 52 | @cpython_api([], PyObject, error=CANNOT_FAIL) |
| 53 | def PyThread_exit_thread(space): |
| 54 | PyErr_SetNone(space, space.w_SystemExit) |