| 1 |
from pypy.interpreter.mixedmodule import MixedModule |
| 2 |
from pypy.interpreter.error import OperationError |
| 3 |
from rpython.rlib.objectmodel import we_are_translated |
| 4 |
from rpython.rlib import rdynload |
| 5 |
import sys |
| 6 |
|
| 7 |
_WIN = sys.platform == 'win32' |
| 8 |
|
| 9 |
class Module(MixedModule): |
| 10 |
"""Sys Builtin Module. """ |
| 11 |
_immutable_fields_ = ["defaultencoding", "debug?", "filesystemencoding?"] |
| 12 |
|
| 13 |
def __init__(self, space, w_name): |
| 14 |
"""NOT_RPYTHON""" |
| 15 |
if space.config.translating: |
| 16 |
del self.__class__.interpleveldefs['pypy_getudir'] |
| 17 |
del self.__class__.appleveldefs['stdin'] |
| 18 |
del self.__class__.appleveldefs['__stdin__'] |
| 19 |
del self.__class__.appleveldefs['stdout'] |
| 20 |
del self.__class__.appleveldefs['__stdout__'] |
| 21 |
del self.__class__.appleveldefs['stderr'] |
| 22 |
del self.__class__.appleveldefs['__stderr__'] |
| 23 |
|
| 24 |
super(Module, self).__init__(space, w_name) |
| 25 |
self.recursionlimit = 1000 |
| 26 |
self.defaultencoding = "utf-8" |
| 27 |
self.filesystemencoding = None |
| 28 |
self.track_resources = False |
| 29 |
self.finalizing = False |
| 30 |
self.dlopenflags = rdynload._dlopen_default_mode() |
| 31 |
|
| 32 |
interpleveldefs = { |
| 33 |
'__name__' : '(space.newtext("sys"))', |
| 34 |
'__doc__' : '(space.newtext("PyPy sys module"))', |
| 35 |
|
| 36 |
'platform' : 'space.newtext(system.PLATFORM)', |
| 37 |
'maxsize' : 'space.newint(sys.maxint)', |
| 38 |
'byteorder' : 'space.newtext(sys.byteorder)', |
| 39 |
'maxunicode' : 'space.newint(vm.MAXUNICODE)', |
| 40 |
'pypy_objspaceclass' : 'space.newtext(repr(space))', |
| 41 |
'prefix' : 'state.get(space).w_initial_prefix', |
| 42 |
'exec_prefix' : 'state.get(space).w_initial_prefix', |
| 43 |
'path' : 'state.get(space).w_path', |
| 44 |
'modules' : 'state.get(space).w_modules', |
| 45 |
'argv' : 'state.get(space).w_argv', |
| 46 |
'orig_argv' : 'state.get(space).w_orig_argv', |
| 47 |
'warnoptions' : 'state.get(space).w_warnoptions', |
| 48 |
'abiflags' : 'space.wrap("")', |
| 49 |
'_framework': "space.newtext('')", |
| 50 |
'builtin_module_names' : 'space.w_None', |
| 51 |
'stdlib_module_names' : 'space.w_None', |
| 52 |
'pypy_getudir' : 'state.pypy_getudir', |
| 53 |
'pypy_find_stdlib' : 'initpath.pypy_find_stdlib', |
| 54 |
'pypy_find_executable' : 'initpath.pypy_find_executable', |
| 55 |
'pypy_resolvedirof' : 'initpath.pypy_resolvedirof', |
| 56 |
'pypy_initfsencoding' : 'initpath.pypy_initfsencoding', |
| 57 |
'set_int_max_str_digits': 'state.set_int_max_str_digits', |
| 58 |
'get_int_max_str_digits': 'state.get_int_max_str_digits', |
| 59 |
|
| 60 |
'_getframe' : 'vm._getframe', |
| 61 |
'_current_frames' : 'threadmappings._current_frames', |
| 62 |
'_current_exceptions' : 'threadmappings._current_exceptions', |
| 63 |
'setrecursionlimit' : 'vm.setrecursionlimit', |
| 64 |
'getrecursionlimit' : 'vm.getrecursionlimit', |
| 65 |
'setcheckinterval' : 'vm.setcheckinterval', |
| 66 |
'getcheckinterval' : 'vm.getcheckinterval', |
| 67 |
'setswitchinterval' : 'vm.setswitchinterval', |
| 68 |
'getswitchinterval' : 'vm.getswitchinterval', |
| 69 |
'exc_info' : 'vm.exc_info', |
| 70 |
'exception' : 'vm.exception', |
| 71 |
'settrace' : 'vm.settrace', |
| 72 |
'gettrace' : 'vm.gettrace', |
| 73 |
'setprofile' : 'vm.setprofile', |
| 74 |
'getprofile' : 'vm.getprofile', |
| 75 |
'call_tracing' : 'vm.call_tracing', |
| 76 |
'getsizeof' : 'vm.getsizeof', |
| 77 |
'intern' : 'vm.intern', |
| 78 |
|
| 79 |
'api_version' : 'version.get_api_version(space)', |
| 80 |
'version_info' : 'version.get_version_info(space)', |
| 81 |
|
| 82 |
'pypy_version_info' : 'version.get_pypy_version_info(space)', |
| 83 |
'_git' : 'version.get_git_info(space)', |
| 84 |
'hexversion' : 'version.get_hexversion(space)', |
| 85 |
|
| 86 |
'meta_path' : 'space.newlist([])', |
| 87 |
'path_hooks' : 'space.newlist([])', |
| 88 |
'path_importer_cache' : 'space.newdict()', |
| 89 |
'dont_write_bytecode' : 'space.newbool(space.config.translation.sandbox)', |
| 90 |
|
| 91 |
'getdefaultencoding' : 'interp_encoding.getdefaultencoding', |
| 92 |
'getfilesystemencoding' : 'interp_encoding.getfilesystemencoding', |
| 93 |
'getfilesystemencodeerrors': 'interp_encoding.getfilesystemencodeerrors', |
| 94 |
|
| 95 |
'float_info' : 'system.get_float_info(space)', |
| 96 |
'int_info' : 'system.get_int_info(space)', |
| 97 |
'hash_info' : 'system.get_hash_info(space)', |
| 98 |
'float_repr_style' : 'system.get_float_repr_style(space)', |
| 99 |
|
| 100 |
'get_asyncgen_hooks' : 'vm.get_asyncgen_hooks', |
| 101 |
'set_asyncgen_hooks' : 'vm.set_asyncgen_hooks', |
| 102 |
|
| 103 |
'is_finalizing' : 'vm.is_finalizing', |
| 104 |
|
| 105 |
'audit' : 'vm.audit', |
| 106 |
'addaudithook' : 'vm.addaudithook', |
| 107 |
|
| 108 |
'pycache_prefix' : 'space.w_None', |
| 109 |
|
| 110 |
'get_coroutine_origin_tracking_depth': 'vm.get_coroutine_origin_tracking_depth', |
| 111 |
'set_coroutine_origin_tracking_depth': 'vm.set_coroutine_origin_tracking_depth', |
| 112 |
|
| 113 |
'unraisablehook' : 'vm.unraisablehook', |
| 114 |
'__unraisablehook__' : 'vm.unraisablehook', |
| 115 |
|
| 116 |
'_pypy_prepare_shutdown' : 'vm._pypy_prepare_shutdown', |
| 117 |
} |
| 118 |
|
| 119 |
if sys.platform == 'win32': |
| 120 |
interpleveldefs['winver'] = 'version.get_winver(space)' |
| 121 |
interpleveldefs['getwindowsversion'] = 'vm.getwindowsversion' |
| 122 |
interpleveldefs['_vpath'] = 'space.newtext(r"..\..")' |
| 123 |
else: |
| 124 |
interpleveldefs['getdlopenflags'] = 'system.getdlopenflags' |
| 125 |
interpleveldefs['setdlopenflags'] = 'system.setdlopenflags' |
| 126 |
|
| 127 |
appleveldefs = { |
| 128 |
'excepthook' : 'app.excepthook', |
| 129 |
'__excepthook__' : 'app.excepthook', |
| 130 |
'breakpointhook' : 'app.breakpointhook', |
| 131 |
'__breakpointhook__' : 'app.breakpointhook', |
| 132 |
'displayhook' : 'app.displayhook', |
| 133 |
'__displayhook__' : 'app.__displayhook__', |
| 134 |
'exit' : 'app.exit', |
| 135 |
'callstats' : 'app.callstats', |
| 136 |
'copyright' : 'app.copyright_str', |
| 137 |
'flags' : 'app.null_sysflags', |
| 138 |
'_xoptions' : 'app.null__xoptions', |
| 139 |
'implementation' : 'app.implementation', |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
'stdin' : 'std_test.stdin', |
| 144 |
'__stdin__' : 'std_test.stdin', |
| 145 |
'stdout' : 'std_test.stdout', |
| 146 |
'__stdout__' : 'std_test.stdout', |
| 147 |
'stderr' : 'std_test.stderr', |
| 148 |
'__stderr__' : 'std_test.stderr', |
| 149 |
} |
| 150 |
|
| 151 |
def startup(self, space): |
| 152 |
if space.config.translating: |
| 153 |
assert self.filesystemencoding is None |
| 154 |
|
| 155 |
if not space.config.translating or we_are_translated(): |
| 156 |
from pypy.module.sys import version |
| 157 |
space.setitem(self.w_dict, space.newtext("version"), |
| 158 |
version.get_version(space)) |
| 159 |
if _WIN: |
| 160 |
from pypy.module.sys import vm |
| 161 |
w_handle = vm.get_dllhandle(space) |
| 162 |
space.setitem(self.w_dict, space.newtext("dllhandle"), w_handle) |
| 163 |
|
| 164 |
from pypy.module.sys import system |
| 165 |
thread_info = system.get_thread_info(space) |
| 166 |
if thread_info is not None: |
| 167 |
space.setitem(self.w_dict, space.newtext('thread_info'), thread_info) |
| 168 |
|
| 169 |
def setup_after_space_initialization(self): |
| 170 |
"NOT_RPYTHON" |
| 171 |
space = self.space |
| 172 |
|
| 173 |
if not space.config.translating: |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
from pypy.module.sys.interp_encoding import base_encoding |
| 183 |
self.filesystemencoding = base_encoding |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
from pypy import pypydir |
| 188 |
import os |
| 189 |
rootdir = os.path.dirname(pypydir) |
| 190 |
for attr in ['prefix', 'exec_prefix', 'base_prefix', 'base_exec_prefix']: |
| 191 |
space.setitem(self.w_dict, space.wrap(attr), space.wrap(rootdir)) |
| 192 |
space.setitem(self.w_dict, space.wrap("platlibdir"), space.wrap(space.config.objspace.platlibdir)) |
| 193 |
|
| 194 |
|
| 195 |
def flush_std_files(self, space): |
| 196 |
w_stdout = space.sys.getdictvalue(space, 'stdout') |
| 197 |
w_stderr = space.sys.getdictvalue(space, 'stderr') |
| 198 |
ret = 0 |
| 199 |
for w_file in [w_stdout, w_stderr]: |
| 200 |
if not (space.is_none(w_file) or |
| 201 |
self._file_is_closed(space, w_file)): |
| 202 |
try: |
| 203 |
space.call_method(w_file, 'flush') |
| 204 |
except OperationError as e: |
| 205 |
if w_file is w_stdout: |
| 206 |
e.write_unraisable(space, '', w_file) |
| 207 |
ret = -1 |
| 208 |
return ret |
| 209 |
|
| 210 |
def _file_is_closed(self, space, w_file): |
| 211 |
try: |
| 212 |
w_closed = space.getattr(w_file, space.newtext('closed')) |
| 213 |
except OperationError: |
| 214 |
return False |
| 215 |
return space.bool_w(w_closed) |
| 216 |
|
| 217 |
def getmodule(self, name): |
| 218 |
space = self.space |
| 219 |
w_modules = self.get('modules') |
| 220 |
try: |
| 221 |
return space.getitem(w_modules, space.newtext(name)) |
| 222 |
except OperationError as e: |
| 223 |
if not e.match(space, space.w_KeyError): |
| 224 |
raise |
| 225 |
return None |
| 226 |
|
| 227 |
def setmodule(self, w_module): |
| 228 |
space = self.space |
| 229 |
w_name = self.space.getattr(w_module, space.newtext('__name__')) |
| 230 |
w_modules = self.get('modules') |
| 231 |
self.space.setitem(w_modules, w_name, w_module) |
| 232 |
|
| 233 |
def get_flag(self, name): |
| 234 |
space = self.space |
| 235 |
return space.int_w(space.getattr(self.get('flags'), space.newtext(name))) |
| 236 |
|
| 237 |
def get_state(self, space): |
| 238 |
from pypy.module.sys import state |
| 239 |
return state.get(space) |
| 240 |
|
| 241 |
def get_optimize(self): |
| 242 |
try: |
| 243 |
return self.get_flag('optimize') |
| 244 |
except RuntimeError: |
| 245 |
return 0 |