| 1 |
import py |
| 2 |
|
| 3 |
import os, sys, subprocess |
| 4 |
from os.path import join, dirname |
| 5 |
|
| 6 |
import pypy |
| 7 |
from pypy.interpreter import gateway |
| 8 |
from pypy.interpreter.error import OperationError |
| 9 |
from pypy.tool.ann_override import PyPyAnnotatorPolicy |
| 10 |
from rpython.config.config import to_optparse, make_dict, SUPPRESS_USAGE |
| 11 |
from rpython.config.config import ConflictConfigError |
| 12 |
from rpython.rlib import rlocale |
| 13 |
from pypy.tool.option import make_objspace |
| 14 |
from pypy import pypydir |
| 15 |
from rpython.rlib import rthread |
| 16 |
from pypy.module.thread import os_thread |
| 17 |
from pypy.module.sys.version import CPYTHON_VERSION |
| 18 |
|
| 19 |
thisdir = py.path.local(__file__).dirpath() |
| 20 |
|
| 21 |
try: |
| 22 |
this_dir = dirname(__file__) |
| 23 |
except NameError: |
| 24 |
this_dir = dirname(sys.argv[0]) |
| 25 |
|
| 26 |
def debug(msg): |
| 27 |
try: |
| 28 |
os.write(2, "debug: " + msg + '\n') |
| 29 |
except OSError: |
| 30 |
pass |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
def create_entry_point(space, w_dict): |
| 36 |
if w_dict is not None: |
| 37 |
w_entry_point = space.getitem(w_dict, space.newtext('entry_point')) |
| 38 |
w_run_toplevel = space.getitem(w_dict, space.newtext('run_toplevel')) |
| 39 |
w_initstdio = space.getitem(w_dict, space.newtext('initstdio')) |
| 40 |
withjit = space.config.objspace.usemodules.pypyjit |
| 41 |
hashfunc = space.config.objspace.hash |
| 42 |
else: |
| 43 |
w_initstdio = space.appexec([], """(): |
| 44 |
return lambda unbuffered: None |
| 45 |
""") |
| 46 |
|
| 47 |
def entry_point(argv): |
| 48 |
if withjit: |
| 49 |
from rpython.jit.backend.hlinfo import highleveljitinfo |
| 50 |
highleveljitinfo.sys_executable = argv[0] |
| 51 |
|
| 52 |
if hashfunc == "siphash24": |
| 53 |
from rpython.rlib import rsiphash |
| 54 |
rsiphash.enable_siphash24() |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if len(argv) > 2 and argv[1] == '--heapsize': |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
from rpython.rlib import rgc |
| 66 |
rgc.set_max_heap_size(int(argv[2])) |
| 67 |
argv = argv[:1] + argv[3:] |
| 68 |
try: |
| 69 |
try: |
| 70 |
space.startup() |
| 71 |
if rlocale.HAVE_LANGINFO: |
| 72 |
try: |
| 73 |
rlocale.setlocale(rlocale.LC_CTYPE, '') |
| 74 |
except rlocale.LocaleError: |
| 75 |
pass |
| 76 |
w_executable = space.newfilename(argv[0]) |
| 77 |
w_argv = space.newlist([space.newfilename(s) |
| 78 |
for s in argv[1:]]) |
| 79 |
w_bargv = space.newlist([space.newbytes(s) |
| 80 |
for s in argv[1:]]) |
| 81 |
w_exitcode = space.call_function(w_entry_point, w_executable, w_bargv, w_argv) |
| 82 |
exitcode = space.int_w(w_exitcode) |
| 83 |
except OperationError as e: |
| 84 |
debug("OperationError:") |
| 85 |
debug(" operror-type: " + e.w_type.getname(space)) |
| 86 |
debug(" operror-value: " + space.text_w(space.str(e.get_w_value(space)))) |
| 87 |
return 1 |
| 88 |
finally: |
| 89 |
try: |
| 90 |
|
| 91 |
if space.finish() < 0: |
| 92 |
|
| 93 |
|
| 94 |
exitcode = 120 |
| 95 |
except OperationError as e: |
| 96 |
debug("OperationError:") |
| 97 |
debug(" operror-type: " + e.w_type.getname(space)) |
| 98 |
debug(" operror-value: " + space.text_w(space.str(e.get_w_value(space)))) |
| 99 |
return 1 |
| 100 |
return exitcode |
| 101 |
|
| 102 |
return entry_point, get_additional_entrypoints(space, w_initstdio) |
| 103 |
|
| 104 |
|
| 105 |
def get_additional_entrypoints(space, w_initstdio): |
| 106 |
|
| 107 |
|
| 108 |
from rpython.rlib.entrypoint import entrypoint_highlevel |
| 109 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 110 |
|
| 111 |
if space.config.objspace.disable_entrypoints: |
| 112 |
return {} |
| 113 |
|
| 114 |
@entrypoint_highlevel('main', [rffi.CCHARP, rffi.INT], |
| 115 |
c_name='pypy_setup_home') |
| 116 |
def pypy_setup_home(ll_home, verbose): |
| 117 |
from pypy.module.sys.initpath import pypy_find_stdlib |
| 118 |
verbose = rffi.cast(lltype.Signed, verbose) |
| 119 |
if ll_home and ord(ll_home[0]): |
| 120 |
home1 = rffi.charp2str(ll_home) |
| 121 |
home = join(home1, 'x') |
| 122 |
|
| 123 |
else: |
| 124 |
home1 = "pypy's shared library location" |
| 125 |
home = '*' |
| 126 |
w_path = pypy_find_stdlib(space, home) |
| 127 |
if space.is_none(w_path): |
| 128 |
if verbose: |
| 129 |
debug("pypy_setup_home: directories 'lib-python' and 'lib_pypy'" |
| 130 |
" not found in %s or in any parent directory" % home1) |
| 131 |
return rffi.cast(rffi.INT, 1) |
| 132 |
space.startup() |
| 133 |
must_leave = space.threadlocals.try_enter_thread(space) |
| 134 |
try: |
| 135 |
|
| 136 |
|
| 137 |
space.appexec([w_path, space.newfilename(home), w_initstdio], |
| 138 |
r"""(path, home, initstdio): |
| 139 |
import sys |
| 140 |
# don't import anything more above this: sys.path is not set |
| 141 |
sys.path[:] = path |
| 142 |
sys.executable = home |
| 143 |
initstdio(unbuffered=True) |
| 144 |
import os # don't move it to the first line of this function! |
| 145 |
_MACOSX = sys.platform == 'darwin' |
| 146 |
if _MACOSX: |
| 147 |
# __PYVENV_LAUNCHER__, used by CPython on macOS, should be ignored |
| 148 |
# since it (possibly) results in a wrong sys.prefix and |
| 149 |
# sys.exec_prefix (and consequently sys.path). |
| 150 |
old_pyvenv_launcher = os.environ.pop('__PYVENV_LAUNCHER__', None) |
| 151 |
try: |
| 152 |
import site |
| 153 |
except Exception as e: |
| 154 |
sys.stderr.write("'import site' failed:\n") |
| 155 |
import traceback |
| 156 |
traceback.print_exc() |
| 157 |
if _MACOSX and old_pyvenv_launcher: |
| 158 |
os.environ['__PYVENV_LAUNCHER__'] = old_pyvenv_launcher |
| 159 |
""") |
| 160 |
return rffi.cast(rffi.INT, 0) |
| 161 |
except OperationError as e: |
| 162 |
if verbose: |
| 163 |
debug("OperationError:") |
| 164 |
debug(" operror-type: " + e.w_type.getname(space)) |
| 165 |
debug(" operror-value: " + space.text_w(space.str(e.get_w_value(space)))) |
| 166 |
return rffi.cast(rffi.INT, -1) |
| 167 |
finally: |
| 168 |
if must_leave: |
| 169 |
space.threadlocals.leave_thread(space) |
| 170 |
|
| 171 |
@entrypoint_highlevel('main', [rffi.CCHARP], c_name='pypy_execute_source') |
| 172 |
def pypy_execute_source(ll_source): |
| 173 |
return pypy_execute_source_ptr(ll_source, 0) |
| 174 |
|
| 175 |
@entrypoint_highlevel('main', [rffi.CCHARP, lltype.Signed], |
| 176 |
c_name='pypy_execute_source_ptr') |
| 177 |
def pypy_execute_source_ptr(ll_source, ll_ptr): |
| 178 |
source = rffi.charp2str(ll_source) |
| 179 |
res = _pypy_execute_source(source, ll_ptr) |
| 180 |
return rffi.cast(rffi.INT, res) |
| 181 |
|
| 182 |
@entrypoint_highlevel('main', [], c_name='pypy_init_threads') |
| 183 |
def pypy_init_threads(): |
| 184 |
if not space.config.objspace.usemodules.thread: |
| 185 |
return |
| 186 |
os_thread.setup_threads(space) |
| 187 |
|
| 188 |
@entrypoint_highlevel('main', [], c_name='pypy_thread_attach') |
| 189 |
def pypy_thread_attach(): |
| 190 |
if not space.config.objspace.usemodules.thread: |
| 191 |
return |
| 192 |
os_thread.setup_threads(space) |
| 193 |
os_thread.bootstrapper.acquire(space, None, None) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
rthread.gc_thread_start() |
| 198 |
os_thread.bootstrapper.nbthreads += 1 |
| 199 |
os_thread.bootstrapper.release() |
| 200 |
|
| 201 |
def _pypy_execute_source(source, c_argument): |
| 202 |
try: |
| 203 |
w_globals = space.newdict(module=True) |
| 204 |
space.setitem(w_globals, space.newtext('__builtins__'), |
| 205 |
space.builtin_modules['builtins']) |
| 206 |
space.setitem(w_globals, space.newtext('c_argument'), |
| 207 |
space.newint(c_argument)) |
| 208 |
space.appexec([space.newtext(source), w_globals], """(src, glob): |
| 209 |
import sys |
| 210 |
stmt = compile(src, 'c callback', 'exec') |
| 211 |
if not hasattr(sys, '_pypy_execute_source'): |
| 212 |
sys._pypy_execute_source = [] |
| 213 |
sys._pypy_execute_source.append(glob) |
| 214 |
exec(stmt, glob) |
| 215 |
""") |
| 216 |
except OperationError as e: |
| 217 |
debug("OperationError:") |
| 218 |
debug(" operror-type: " + e.w_type.getname(space)) |
| 219 |
debug(" operror-value: " + space.text_w(space.str(e.get_w_value(space)))) |
| 220 |
return -1 |
| 221 |
return 0 |
| 222 |
|
| 223 |
return {'pypy_execute_source': pypy_execute_source, |
| 224 |
'pypy_execute_source_ptr': pypy_execute_source_ptr, |
| 225 |
'pypy_init_threads': pypy_init_threads, |
| 226 |
'pypy_thread_attach': pypy_thread_attach, |
| 227 |
'pypy_setup_home': pypy_setup_home} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
class PyPyTarget(object): |
| 235 |
|
| 236 |
usage = SUPPRESS_USAGE |
| 237 |
|
| 238 |
take_options = True |
| 239 |
space = None |
| 240 |
|
| 241 |
def opt_parser(self, config): |
| 242 |
parser = to_optparse(config, useoptions=["objspace.*"], |
| 243 |
parserkwargs={'usage': self.usage}) |
| 244 |
return parser |
| 245 |
|
| 246 |
def handle_config(self, config, translateconfig): |
| 247 |
if (not translateconfig.help and |
| 248 |
translateconfig._cfgimpl_value_owners['opt'] == 'default'): |
| 249 |
raise Exception("You have to specify the --opt level.\n" |
| 250 |
"Try --opt=2 or --opt=jit, or equivalently -O2 or -Ojit .") |
| 251 |
self.translateconfig = translateconfig |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
from pypy.config.pypyoption import set_pypy_opt_level |
| 260 |
set_pypy_opt_level(config, translateconfig.opt) |
| 261 |
|
| 262 |
def print_help(self, config): |
| 263 |
self.opt_parser(config).print_help() |
| 264 |
|
| 265 |
def get_additional_config_options(self): |
| 266 |
from pypy.config.pypyoption import pypy_optiondescription |
| 267 |
return pypy_optiondescription |
| 268 |
|
| 269 |
def target(self, driver, args): |
| 270 |
driver.exe_name = 'pypy%d.%d-%%(backend)s' % CPYTHON_VERSION[:2] |
| 271 |
|
| 272 |
config = driver.config |
| 273 |
parser = self.opt_parser(config) |
| 274 |
|
| 275 |
parser.parse_args(args) |
| 276 |
|
| 277 |
|
| 278 |
global space, entry_point |
| 279 |
|
| 280 |
if config.objspace.allworkingmodules: |
| 281 |
from pypy.config.pypyoption import enable_allworkingmodules |
| 282 |
enable_allworkingmodules(config) |
| 283 |
if config.objspace.translationmodules: |
| 284 |
from pypy.config.pypyoption import enable_translationmodules |
| 285 |
enable_translationmodules(config) |
| 286 |
|
| 287 |
config.translation.suggest(check_str_without_nul=True) |
| 288 |
config.translation.suggest(shared=True) |
| 289 |
config.translation.suggest(icon=join(this_dir, 'pypy.ico')) |
| 290 |
config.translation.suggest(manifest=join(this_dir, 'python.manifest')) |
| 291 |
if config.translation.shared: |
| 292 |
if config.translation.output is not None: |
| 293 |
raise Exception("Cannot use the --output option with PyPy " |
| 294 |
"when --shared is on (it is by default). " |
| 295 |
"See issue #1971.") |
| 296 |
|
| 297 |
|
| 298 |
if config.translation.profopt: |
| 299 |
if config.translation.profoptargs is None: |
| 300 |
config.translation.profoptargs = "$(RPYDIR)/../lib-python/2.7/test/regrtest.py --pgo -x test_asyncore test_gdb test_multiprocessing test_subprocess || true" |
| 301 |
elif config.translation.profoptargs is not None: |
| 302 |
raise Exception("Cannot use --profoptargs without specifying --profopt as well") |
| 303 |
|
| 304 |
if sys.platform == 'win32': |
| 305 |
libdir = thisdir.join('..', '..', 'libs') |
| 306 |
libdir.ensure(dir=1) |
| 307 |
pythonlib = "python{0[0]}{0[1]}.lib".format(CPYTHON_VERSION) |
| 308 |
config.translation.libname = str(libdir.join(pythonlib)) |
| 309 |
|
| 310 |
if config.translation.thread: |
| 311 |
config.objspace.usemodules.thread = True |
| 312 |
elif config.objspace.usemodules.thread: |
| 313 |
try: |
| 314 |
config.translation.thread = True |
| 315 |
except ConflictConfigError: |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
config.objspace.usemodules.thread = False |
| 323 |
|
| 324 |
if config.translation.continuation: |
| 325 |
config.objspace.usemodules._continuation = True |
| 326 |
elif config.objspace.usemodules._continuation: |
| 327 |
try: |
| 328 |
config.translation.continuation = True |
| 329 |
except ConflictConfigError: |
| 330 |
|
| 331 |
|
| 332 |
config.objspace.usemodules._continuation = False |
| 333 |
|
| 334 |
if not config.translation.rweakref: |
| 335 |
config.objspace.usemodules._weakref = False |
| 336 |
|
| 337 |
if config.translation.jit: |
| 338 |
config.objspace.usemodules.pypyjit = True |
| 339 |
elif config.objspace.usemodules.pypyjit: |
| 340 |
config.translation.jit = True |
| 341 |
|
| 342 |
if config.translation.sandbox: |
| 343 |
assert 0, ("--sandbox is not tested nor maintained. If you " |
| 344 |
"really want to try it anyway, remove this line in " |
| 345 |
"pypy/goal/targetpypystandalone.py.") |
| 346 |
|
| 347 |
if config.objspace.usemodules.cpyext: |
| 348 |
if config.translation.reverse_debugger: |
| 349 |
assert config.translation.gc == 'boehm' |
| 350 |
|
| 351 |
|
| 352 |
elif config.translation.gc != 'incminimark': |
| 353 |
raise Exception("The 'cpyext' module requires the default" |
| 354 |
" 'incminimark' GC. You need 'targetpypystandalone.py" |
| 355 |
" --withoutmod-cpyext' with other GCs. (It almost" |
| 356 |
" works with Boehm, the fix should be quick)") |
| 357 |
|
| 358 |
config.translating = True |
| 359 |
|
| 360 |
import translate |
| 361 |
translate.log_config(config.objspace, "PyPy config object") |
| 362 |
|
| 363 |
|
| 364 |
from pypy.module.sys.moduledef import Module as SysModule |
| 365 |
options = make_dict(config) |
| 366 |
wrapstr = 'space.wrap(%r)' % (options) |
| 367 |
SysModule.interpleveldefs['pypy_translation_info'] = wrapstr |
| 368 |
|
| 369 |
if 'compile' in driver._disabled: |
| 370 |
driver.default_goal = 'source' |
| 371 |
elif config.objspace.usemodules._cffi_backend: |
| 372 |
self.hack_for_cffi_modules(driver) |
| 373 |
else: |
| 374 |
driver.default_goal = 'compile' |
| 375 |
return self.get_entry_point(config) |
| 376 |
|
| 377 |
def hack_for_cffi_modules(self, driver): |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
from rpython.tool.runsubprocess import run_subprocess |
| 383 |
from rpython.translator.driver import taskdef |
| 384 |
import types |
| 385 |
|
| 386 |
compile_goal, = driver.backend_select_goals(['compile']) |
| 387 |
@taskdef([compile_goal], "Create cffi bindings for modules") |
| 388 |
def task_build_cffi_imports(self): |
| 389 |
''' Use cffi to compile cffi interfaces to modules''' |
| 390 |
filename = join(pypydir, '..', 'lib_pypy', 'pypy_tools', |
| 391 |
'build_cffi_imports.py') |
| 392 |
argv = [filename,] |
| 393 |
exe_name = py.path.local(driver.c_entryp) |
| 394 |
status, out, err = run_subprocess(str(exe_name), argv) |
| 395 |
sys.stdout.write(out) |
| 396 |
sys.stderr.write(err) |
| 397 |
driver.task_build_cffi_imports = types.MethodType(task_build_cffi_imports, driver) |
| 398 |
driver.tasks['build_cffi_imports'] = driver.task_build_cffi_imports, [compile_goal] |
| 399 |
driver.default_goal = 'build_cffi_imports' |
| 400 |
|
| 401 |
|
| 402 |
def jitpolicy(self, driver): |
| 403 |
from pypy.module.pypyjit.policy import PyPyJitPolicy |
| 404 |
from pypy.module.pypyjit.hooks import pypy_hooks |
| 405 |
return PyPyJitPolicy(pypy_hooks) |
| 406 |
|
| 407 |
def get_gchooks(self): |
| 408 |
from pypy.module.gc.hook import LowLevelGcHooks |
| 409 |
if self.space is None: |
| 410 |
raise Exception("get_gchooks must be called after get_entry_point") |
| 411 |
return self.space.fromcache(LowLevelGcHooks) |
| 412 |
|
| 413 |
def get_entry_point(self, config): |
| 414 |
self.space = make_objspace(config) |
| 415 |
|
| 416 |
|
| 417 |
filename = join(pypydir, 'interpreter', 'app_main.py') |
| 418 |
app = gateway.applevel(open(filename).read(), 'app_main.py', 'app_main') |
| 419 |
app.hidden_applevel = False |
| 420 |
w_dict = app.getwdict(self.space) |
| 421 |
entry_point, _ = create_entry_point(self.space, w_dict) |
| 422 |
|
| 423 |
return entry_point, None, PyPyAnnotatorPolicy(self.space) |
| 424 |
|
| 425 |
def interface(self, ns): |
| 426 |
for name in ['take_options', 'handle_config', 'print_help', 'target', |
| 427 |
'jitpolicy', 'get_entry_point', |
| 428 |
'get_additional_config_options']: |
| 429 |
ns[name] = getattr(self, name) |
| 430 |
ns['get_gchooks'] = self.get_gchooks |
| 431 |
|
| 432 |
PyPyTarget().interface(globals()) |
| 433 |
|