| 1 |
from rpython.rlib.objectmodel import we_are_translated, specialize |
| 2 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 3 |
from pypy.interpreter.error import OperationError, oefmt |
| 4 |
from pypy.interpreter import executioncontext |
| 5 |
from rpython.rtyper.annlowlevel import llhelper |
| 6 |
from rpython.rlib.rdynload import DLLHANDLE |
| 7 |
from rpython.rlib import rawrefcount, rgil |
| 8 |
import sys |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
executioncontext.ExecutionContext.cpyext_operror = None |
| 14 |
|
| 15 |
|
| 16 |
class State: |
| 17 |
def __init__(self, space): |
| 18 |
self.space = space |
| 19 |
self.reset() |
| 20 |
self.programname = lltype.nullptr(rffi.CWCHARP.TO) |
| 21 |
self.version = lltype.nullptr(rffi.CCHARP.TO) |
| 22 |
self.builder = None |
| 23 |
self.C = CNamespace() |
| 24 |
self.static_memory_error = OperationError(space.w_MemoryError, space.w_None) |
| 25 |
|
| 26 |
def reset(self): |
| 27 |
from pypy.module.cpyext.modsupport import PyMethodDef |
| 28 |
ec = self.space.getexecutioncontext() |
| 29 |
ec.cpyext_operror = None |
| 30 |
self.new_method_def = lltype.nullptr(PyMethodDef) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
self.package_context = None, None |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
self.extensions = {} |
| 45 |
|
| 46 |
self.datetimeAPI = [] |
| 47 |
self.threadstate_count = 100 |
| 48 |
|
| 49 |
self.cpyext_is_imported = False |
| 50 |
|
| 51 |
def make_sure_cpyext_is_imported(self): |
| 52 |
if not self.cpyext_is_imported: |
| 53 |
self.space.getbuiltinmodule("cpyext") |
| 54 |
self.cpyext_is_imported = True |
| 55 |
|
| 56 |
def set_exception(self, operror): |
| 57 |
self.clear_exception() |
| 58 |
ec = self.space.getexecutioncontext() |
| 59 |
ec.cpyext_operror = operror |
| 60 |
|
| 61 |
def clear_exception(self): |
| 62 |
"""Clear the current exception state, and return the operror.""" |
| 63 |
ec = self.space.getexecutioncontext() |
| 64 |
operror = ec.cpyext_operror |
| 65 |
ec.cpyext_operror = None |
| 66 |
return operror |
| 67 |
|
| 68 |
def get_exception(self): |
| 69 |
ec = self.space.getexecutioncontext() |
| 70 |
return ec.cpyext_operror |
| 71 |
|
| 72 |
@specialize.arg(1) |
| 73 |
def check_and_raise_exception(self, always=False): |
| 74 |
ec = self.space.getexecutioncontext() |
| 75 |
operror = ec.cpyext_operror |
| 76 |
if operror: |
| 77 |
self.clear_exception() |
| 78 |
operror.normalize_exception(self.space) |
| 79 |
raise operror |
| 80 |
if always: |
| 81 |
raise oefmt(self.space.w_SystemError, |
| 82 |
"Function returned an error result without setting an " |
| 83 |
"exception") |
| 84 |
|
| 85 |
def setup_rawrefcount(self): |
| 86 |
space = self.space |
| 87 |
if not self.space.config.translating: |
| 88 |
def dealloc_trigger(): |
| 89 |
from pypy.module.cpyext.pyobject import PyObject, decref |
| 90 |
print 'dealloc_trigger...' |
| 91 |
while True: |
| 92 |
ob = rawrefcount.next_dead(PyObject) |
| 93 |
if not ob: |
| 94 |
break |
| 95 |
print 'deallocating PyObject', ob |
| 96 |
decref(space, ob) |
| 97 |
print 'dealloc_trigger DONE' |
| 98 |
return "RETRY" |
| 99 |
rawrefcount.init(dealloc_trigger) |
| 100 |
else: |
| 101 |
if space.config.translation.gc == "boehm": |
| 102 |
action = BoehmPyObjDeallocAction(space) |
| 103 |
space.actionflag.register_periodic_action(action, |
| 104 |
use_bytecode_counter=True) |
| 105 |
else: |
| 106 |
pyobj_dealloc_action = PyObjDeallocAction(space) |
| 107 |
self.dealloc_trigger = lambda: pyobj_dealloc_action.fire() |
| 108 |
|
| 109 |
def build_api(self): |
| 110 |
"""NOT_RPYTHON |
| 111 |
This function is called when at object space creation, |
| 112 |
and drives the compilation of the cpyext library |
| 113 |
""" |
| 114 |
self.setup_rawrefcount() |
| 115 |
from pypy.module.cpyext import api |
| 116 |
if not self.space.config.translating: |
| 117 |
self.api_lib = str(api.build_bridge(self.space)) |
| 118 |
else: |
| 119 |
api.setup_library(self.space) |
| 120 |
|
| 121 |
def install_dll(self, eci): |
| 122 |
"""NOT_RPYTHON |
| 123 |
Called when the dll has been compiled""" |
| 124 |
if sys.platform == 'win32': |
| 125 |
self.get_pythonapi_handle = rffi.llexternal( |
| 126 |
'pypy_get_pythonapi_handle', [], DLLHANDLE, |
| 127 |
compilation_info=eci) |
| 128 |
|
| 129 |
def startup(self, space): |
| 130 |
"This function is called when the program really starts" |
| 131 |
from pypy.module.cpyext.typeobject import setup_new_method_def |
| 132 |
from pypy.module.cpyext.api import INIT_FUNCTIONS |
| 133 |
|
| 134 |
if we_are_translated(): |
| 135 |
if space.config.translation.gc != "boehm": |
| 136 |
|
| 137 |
|
| 138 |
rawrefcount.init( |
| 139 |
llhelper(rawrefcount.RAWREFCOUNT_DEALLOC_TRIGGER, |
| 140 |
self.dealloc_trigger)) |
| 141 |
self.builder.attach_all(space) |
| 142 |
|
| 143 |
setup_new_method_def(space) |
| 144 |
|
| 145 |
for func in INIT_FUNCTIONS: |
| 146 |
func(space) |
| 147 |
self.check_and_raise_exception() |
| 148 |
|
| 149 |
|
| 150 |
if space.config.translation.thread: |
| 151 |
from pypy.module.thread import os_thread |
| 152 |
if not os_thread.threads_initialized(space): |
| 153 |
os_thread.setup_threads(space) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
def get_programname(self): |
| 158 |
if not self.programname: |
| 159 |
space = self.space |
| 160 |
argv = space.sys.get('argv') |
| 161 |
if space.len_w(argv): |
| 162 |
argv0 = space.getitem(argv, space.newint(0)) |
| 163 |
progname = space.utf8_w(argv0) |
| 164 |
lgt = space.len_w(argv0) |
| 165 |
else: |
| 166 |
progname = "pypy3" |
| 167 |
lgt = len(progname) |
| 168 |
self.programname = rffi.utf82wcharp(progname, lgt) |
| 169 |
lltype.render_immortal(self.programname) |
| 170 |
return self.programname |
| 171 |
|
| 172 |
def get_version(self): |
| 173 |
if not self.version: |
| 174 |
space = self.space |
| 175 |
w_version = space.sys.get('version') |
| 176 |
version = space.text_w(w_version) |
| 177 |
self.version = rffi.str2charp(version) |
| 178 |
lltype.render_immortal(self.version) |
| 179 |
return self.version |
| 180 |
foo = self.import_module(name='foo', init=init) |
| 181 |
|
| 182 |
def find_extension(self, name, path): |
| 183 |
from pypy.module.cpyext.import_ import PyImport_AddModule |
| 184 |
from pypy.interpreter.module import Module |
| 185 |
try: |
| 186 |
w_dict = self.extensions[path] |
| 187 |
except KeyError: |
| 188 |
return None |
| 189 |
with rffi.scoped_str2charp(name) as ll_name: |
| 190 |
w_mod = PyImport_AddModule(self.space, ll_name) |
| 191 |
assert isinstance(w_mod, Module) |
| 192 |
w_mdict = w_mod.getdict(self.space) |
| 193 |
self.space.call_method(w_mdict, 'update', w_dict) |
| 194 |
return w_mod |
| 195 |
|
| 196 |
def fixup_extension(self, w_mod, name, path): |
| 197 |
from pypy.interpreter.module import Module |
| 198 |
space = self.space |
| 199 |
w_modules = space.sys.get('modules') |
| 200 |
space.setitem_str(w_modules, name, w_mod) |
| 201 |
w_dict = w_mod.getdict(space) |
| 202 |
w_copy = space.call_method(w_dict, 'copy') |
| 203 |
self.extensions[path] = w_copy |
| 204 |
return w_mod |
| 205 |
|
| 206 |
@specialize.arg(1) |
| 207 |
def ccall(self, name, *args): |
| 208 |
return getattr(self.C, name)(*args) |
| 209 |
|
| 210 |
|
| 211 |
class CNamespace: |
| 212 |
def _freeze_(self): |
| 213 |
return True |
| 214 |
|
| 215 |
|
| 216 |
def _rawrefcount_perform(space): |
| 217 |
from pypy.module.cpyext.pyobject import PyObject, decref |
| 218 |
while True: |
| 219 |
py_obj = rawrefcount.next_dead(PyObject) |
| 220 |
if not py_obj: |
| 221 |
break |
| 222 |
decref(space, py_obj) |
| 223 |
|
| 224 |
class PyObjDeallocAction(executioncontext.AsyncAction): |
| 225 |
"""An action that invokes _Py_Dealloc() on the dying PyObjects. |
| 226 |
""" |
| 227 |
def perform(self, executioncontext, frame): |
| 228 |
_rawrefcount_perform(self.space) |
| 229 |
|
| 230 |
class BoehmPyObjDeallocAction(executioncontext.PeriodicAsyncAction): |
| 231 |
|
| 232 |
|
| 233 |
def perform(self, executioncontext, frame): |
| 234 |
if we_are_translated(): |
| 235 |
_rawrefcount_perform(self.space) |