PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/sys/__init__.py

https://bitbucket.org/pypy/pypy/
Python | 183 lines | 179 code | 3 blank | 1 comment | 0 complexity | 5d65d90c93687982d00d52ea5f965905 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  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. _WIN = sys.platform == 'win32'
  7. class Module(MixedModule):
  8. """Sys Builtin Module. """
  9. _immutable_fields_ = ["defaultencoding?", "debug?"]
  10. def __init__(self, space, w_name):
  11. """NOT_RPYTHON""" # because parent __init__ isn't
  12. if space.config.translating:
  13. del self.__class__.interpleveldefs['pypy_getudir']
  14. super(Module, self).__init__(space, w_name)
  15. self.recursionlimit = 100
  16. self.w_default_encoder = None
  17. self.defaultencoding = "ascii"
  18. self.filesystemencoding = None
  19. self.debug = True
  20. self.track_resources = False
  21. self.dlopenflags = rdynload._dlopen_default_mode()
  22. interpleveldefs = {
  23. '__name__' : '(space.wrap("sys"))',
  24. '__doc__' : '(space.wrap("PyPy sys module"))',
  25. 'platform' : 'space.wrap(sys.platform)',
  26. 'maxint' : 'space.wrap(sys.maxint)',
  27. 'maxsize' : 'space.wrap(sys.maxint)',
  28. 'byteorder' : 'space.wrap(sys.byteorder)',
  29. 'maxunicode' : 'space.wrap(vm.MAXUNICODE)',
  30. 'stdin' : 'state.getio(space).w_stdin',
  31. '__stdin__' : 'state.getio(space).w_stdin',
  32. 'stdout' : 'state.getio(space).w_stdout',
  33. '__stdout__' : 'state.getio(space).w_stdout',
  34. 'stderr' : 'state.getio(space).w_stderr',
  35. '__stderr__' : 'state.getio(space).w_stderr',
  36. 'pypy_objspaceclass' : 'space.wrap(repr(space))',
  37. #'prefix' : # added by pypy_initial_path() when it
  38. #'exec_prefix' : # succeeds, pointing to trunk or /usr
  39. 'path' : 'state.get(space).w_path',
  40. 'modules' : 'state.get(space).w_modules',
  41. 'argv' : 'state.get(space).w_argv',
  42. 'py3kwarning' : 'space.w_False',
  43. 'warnoptions' : 'state.get(space).w_warnoptions',
  44. 'builtin_module_names' : 'space.w_None',
  45. 'pypy_getudir' : 'state.pypy_getudir', # not translated
  46. 'pypy_find_stdlib' : 'initpath.pypy_find_stdlib',
  47. 'pypy_find_executable' : 'initpath.pypy_find_executable',
  48. 'pypy_resolvedirof' : 'initpath.pypy_resolvedirof',
  49. '_getframe' : 'vm._getframe',
  50. '_current_frames' : 'currentframes._current_frames',
  51. 'setrecursionlimit' : 'vm.setrecursionlimit',
  52. 'getrecursionlimit' : 'vm.getrecursionlimit',
  53. 'pypy_set_track_resources' : 'vm.set_track_resources',
  54. 'pypy_get_track_resources' : 'vm.get_track_resources',
  55. 'setcheckinterval' : 'vm.setcheckinterval',
  56. 'getcheckinterval' : 'vm.getcheckinterval',
  57. 'exc_info' : 'vm.exc_info',
  58. 'exc_clear' : 'vm.exc_clear',
  59. 'settrace' : 'vm.settrace',
  60. 'gettrace' : 'vm.gettrace',
  61. 'setprofile' : 'vm.setprofile',
  62. 'getprofile' : 'vm.getprofile',
  63. 'call_tracing' : 'vm.call_tracing',
  64. 'getsizeof' : 'vm.getsizeof',
  65. 'api_version' : 'version.get_api_version(space)',
  66. 'version_info' : 'version.get_version_info(space)',
  67. #'version' : set in startup()
  68. 'pypy_version_info' : 'version.get_pypy_version_info(space)',
  69. 'subversion' : 'version.get_subversion_info(space)',
  70. '_mercurial' : 'version.get_repo_info(space)',
  71. 'hexversion' : 'version.get_hexversion(space)',
  72. 'displayhook' : 'hook.displayhook',
  73. '__displayhook__' : 'hook.__displayhook__',
  74. 'meta_path' : 'space.wrap([])',
  75. 'path_hooks' : 'space.wrap([])',
  76. 'path_importer_cache' : 'space.wrap({})',
  77. 'dont_write_bytecode' : 'space.wrap(space.config.translation.sandbox)',
  78. 'getdefaultencoding' : 'interp_encoding.getdefaultencoding',
  79. 'setdefaultencoding' : 'interp_encoding.setdefaultencoding',
  80. 'getfilesystemencoding' : 'interp_encoding.getfilesystemencoding',
  81. 'float_info' : 'system.get_float_info(space)',
  82. 'long_info' : 'system.get_long_info(space)',
  83. 'float_repr_style' : 'system.get_float_repr_style(space)',
  84. 'getdlopenflags' : 'system.getdlopenflags',
  85. 'setdlopenflags' : 'system.setdlopenflags',
  86. }
  87. if sys.platform == 'win32':
  88. interpleveldefs['winver'] = 'version.get_winver(space)'
  89. interpleveldefs['getwindowsversion'] = 'vm.getwindowsversion'
  90. appleveldefs = {
  91. 'excepthook' : 'app.excepthook',
  92. '__excepthook__' : 'app.excepthook',
  93. 'exit' : 'app.exit',
  94. 'exitfunc' : 'app.exitfunc',
  95. 'callstats' : 'app.callstats',
  96. 'copyright' : 'app.copyright_str',
  97. 'flags' : 'app.null_sysflags',
  98. }
  99. def startup(self, space):
  100. if space.config.translating and not we_are_translated():
  101. # don't get the filesystemencoding at translation time
  102. assert self.filesystemencoding is None
  103. else:
  104. from pypy.module.sys import version
  105. space.setitem(self.w_dict, space.wrap("version"),
  106. space.wrap(version.get_version(space)))
  107. if _WIN:
  108. from pypy.module.sys import vm
  109. w_handle = vm.get_dllhandle(space)
  110. space.setitem(self.w_dict, space.wrap("dllhandle"), w_handle)
  111. def getmodule(self, name):
  112. space = self.space
  113. w_modules = self.get('modules')
  114. try:
  115. return space.getitem(w_modules, space.wrap(name))
  116. except OperationError as e:
  117. if not e.match(space, space.w_KeyError):
  118. raise
  119. return None
  120. def setmodule(self, w_module):
  121. space = self.space
  122. w_name = self.space.getattr(w_module, space.wrap('__name__'))
  123. w_modules = self.get('modules')
  124. self.space.setitem(w_modules, w_name, w_module)
  125. def getdictvalue(self, space, attr):
  126. """ specialize access to dynamic exc_* attributes. """
  127. value = MixedModule.getdictvalue(self, space, attr)
  128. if value is not None:
  129. return value
  130. if attr == 'exc_type':
  131. operror = space.getexecutioncontext().sys_exc_info()
  132. if operror is None:
  133. return space.w_None
  134. else:
  135. return operror.w_type
  136. elif attr == 'exc_value':
  137. operror = space.getexecutioncontext().sys_exc_info()
  138. if operror is None:
  139. return space.w_None
  140. else:
  141. return operror.get_w_value(space)
  142. elif attr == 'exc_traceback':
  143. operror = space.getexecutioncontext().sys_exc_info()
  144. if operror is None:
  145. return space.w_None
  146. else:
  147. return space.wrap(operror.get_traceback())
  148. return None
  149. def get_w_default_encoder(self):
  150. if self.w_default_encoder is not None:
  151. # XXX is this level of caching ok? CPython has some shortcuts
  152. # for common encodings, but as far as I can see it has no general
  153. # cache.
  154. return self.w_default_encoder
  155. else:
  156. from pypy.module.sys.interp_encoding import get_w_default_encoder
  157. return get_w_default_encoder(self.space)
  158. def get_flag(self, name):
  159. space = self.space
  160. return space.int_w(space.getattr(self.get('flags'), space.wrap(name)))
  161. def get_state(self, space):
  162. from pypy.module.sys import state
  163. return state.get(space)