[pypy-svn] r9264 - in pypy/branch/dist-interpapp/pypy: interpreter module/sys2
hpk at codespeak.net
hpk at codespeak.net
Thu Feb 17 10:49:00 CET 2005
Author: hpk
Date: Thu Feb 17 10:49:00 2005
New Revision: 9264
Added:
pypy/branch/dist-interpapp/pypy/module/sys2/
pypy/branch/dist-interpapp/pypy/module/sys2/__init__.py
pypy/branch/dist-interpapp/pypy/module/sys2/app.py
- copied, changed from r9258, pypy/dist/pypy/module/sysmodule.py
pypy/branch/dist-interpapp/pypy/module/sys2/state.py
- copied, changed from r9258, pypy/dist/pypy/module/sysinterp.py
pypy/branch/dist-interpapp/pypy/module/sys2/vm.py
- copied, changed from r9258, pypy/dist/pypy/module/sysinterp.py
Modified:
pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py
Log:
a careful port (preserving history as feasible)
of the module/sys*.py module parts to the new
module definition structure.
Currently, when you startup "py.py" then you
you will have 'builtin' and 'sys2' automatically
in your namespace so that you can play a bit
with the two converted modules. accessing
their dict completely un-lazifies them.
Modified: pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py (original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py Thu Feb 17 10:49:00 2005
@@ -90,6 +90,9 @@
from pypy.module.builtin import Module
w_name = self.wrap('builtin')
self.setitem(self.w_builtins, w_name, Module(self, w_name))
+ from pypy.module.sys2 import Module
+ w_name = self.wrap('sys2')
+ self.setitem(self.w_builtins, w_name, Module(self, w_name))
def make_sys(self):
"NOT_RPYTHON: only for initializing the space."
Added: pypy/branch/dist-interpapp/pypy/module/sys2/__init__.py
==============================================================================
--- (empty file)
+++ pypy/branch/dist-interpapp/pypy/module/sys2/__init__.py Thu Feb 17 10:49:00 2005
@@ -0,0 +1,54 @@
+from pypy.interpreter.newmodule import ExtModule
+
+class Module(ExtModule):
+ """Sys Builtin Module. """
+ interpleveldefs = {
+ 'pypy_objspaceclass' : '(space.wrap(space.__class__.__name__))',
+
+ '__name__' : '(space.wrap("sys"))',
+ '__doc__' : '(space.wrap("PyPy sys module"))',
+
+ 'platform' : 'space.wrap(sys.platform)',
+ 'maxint' : 'space.wrap(sys.maxint)',
+ 'byteorder' : 'space.wrap(sys.byteorder)',
+ 'exec_prefix' : 'space.wrap(sys.exec_prefix)',
+ 'prefix' : 'space.wrap(sys.prefix)',
+ 'maxunicode' : 'space.wrap(sys.maxunicode)',
+ 'maxint' : 'space.wrap(sys.maxint)',
+ 'stdin' : 'space.wrap(sys.stdin)',
+ 'stdout' : 'space.wrap(sys.stdout)',
+ 'stderr' : 'space.wrap(sys.stderr)',
+ 'pypy_objspaceclass' : 'space.wrap(space.__class__.__name__)',
+
+ 'path' : 'state.get(space).w_path',
+ 'modules' : 'state.get(space).w_modules',
+ 'argv' : 'state.get(space).w_argv',
+ 'warnoptions' : 'state.get(space).w_warnoptions',
+ 'builtin_module_names' : 'state.get(space).w_builtin_module_names',
+ 'pypy_getudir' : 'state.pypy_getudir',
+
+ 'getdefaultencoding' : 'space.wrap(sys.getdefaultencoding())',
+ 'getrefcount' : 'vm.getrefcount',
+ '_getframe' : 'vm._getframe',
+ 'setrecursionlimit' : 'vm.setrecursionlimit',
+ 'getrecursionlimit' : 'vm.getrecursionlimit',
+ 'setcheckinterval' : 'vm.setcheckinterval',
+ 'getcheckinterval' : 'vm.getcheckinterval',
+ 'exc_info' : 'vm.exc_info',
+ 'exc_clear' : 'vm.exc_clear',
+
+ 'executable' : 'space.wrap("py.py")',
+ 'copyright' : 'space.wrap("MIT-License")',
+ 'version_info' : 'space.wrap((2,3,4, "pypy1"))',
+ 'version' : 'space.wrap("2.3.4 (pypy1 build)")',
+ 'hexversion' : 'space.wrap(0x020304a0)',
+ 'ps1' : 'space.wrap(">>>>")',
+ 'ps2' : 'space.wrap("....")',
+ }
+ appleveldefs = {
+ 'displayhook' : 'app.displayhook',
+ '__displayhook__' : 'app.__displayhook__',
+ 'excepthook' : 'app.excepthook',
+ '__excepthook__' : 'app.__excepthook__',
+ 'exit' : 'app.exit',
+ }
Copied: pypy/branch/dist-interpapp/pypy/module/sys2/app.py (from r9258, pypy/dist/pypy/module/sysmodule.py)
==============================================================================
--- pypy/dist/pypy/module/sysmodule.py (original)
+++ pypy/branch/dist-interpapp/pypy/module/sys2/app.py Thu Feb 17 10:49:00 2005
@@ -2,36 +2,7 @@
The 'sys' module.
"""
-__interplevel__execfile('sysinterp.py')
-
-# Common data structures
-from __interplevel__ import initialpath as path
-from __interplevel__ import modules, argv
-from __interplevel__ import warnoptions, builtin_module_names
-from __interplevel__ import maxunicode
-
-# Objects from interpreter-level
-from __interplevel__ import stdin, stdout, stderr, maxint
-from __interplevel__ import platform, byteorder
-from __interplevel__ import pypy_objspaceclass
-from __interplevel__ import exec_prefix, prefix
-
-# Functions from interpreter-level
-from __interplevel__ import _getframe, exc_info, exc_clear, pypy_getudir
-from __interplevel__ import getrecursionlimit, setrecursionlimit
-from __interplevel__ import getcheckinterval, setcheckinterval
-from __interplevel__ import getdefaultencoding, getrefcount
-
-# Dummy
-executable = 'py.py'
-api_version = 0
-copyright = ''
-version = '2.3a0 (pypy build)'
-version_info = (2, 3, 0, 'alpha', 0)
-hexversion = 0x020300a0
-ps1 = '>>>> '
-ps2 = '.... '
-
+import sys
# XXX not called by the core yet
def excepthook(exctype, value, traceback):
@@ -46,6 +17,10 @@
# in normalize_exception, which is exactlylike CPython.
raise SystemExit, exitcode
+def setmodule(module):
+ """ put a module into the modules dict """
+ sys.modules[module.__name__] = module
+
def displayhook(obj):
if obj is not None:
__builtins__['_'] = obj
@@ -56,14 +31,13 @@
__displayhook__ = displayhook # this is exactly like in CPython
def getfilesystemencoding():
- """getfilesystemencoding() -> string
-
-Return the encoding used to convert Unicode filenames in
-operating system filenames."""
-
- if platform == "win32":
+ """ getfilesystemencoding() -> string
+ Return the encoding used to convert Unicode filenames in
+ operating system filenames.
+ """
+ if sys.platform == "win32":
encoding = "mbcs"
- elif platform == "darwin":
+ elif sys.platform == "darwin":
encoding = "utf-8"
else:
encoding = None
Copied: pypy/branch/dist-interpapp/pypy/module/sys2/state.py (from r9258, pypy/dist/pypy/module/sysinterp.py)
==============================================================================
--- pypy/dist/pypy/module/sysinterp.py (original)
+++ pypy/branch/dist-interpapp/pypy/module/sys2/state.py Thu Feb 17 10:49:00 2005
@@ -3,30 +3,19 @@
"""
#from pypy.interpreter.module import Module
from pypy.interpreter.error import OperationError
+from pypy.tool.cache import Cache
-import sys as cpy_sys
+import sys, os
def hack_cpython_module(modname):
"NOT_RPYTHON. Steal a module from CPython."
cpy_module = __import__(modname, globals(), locals(), None)
return cpy_module
-## # to build the dictionary of the module, we get all the objects
-## # accessible as 'self.xxx'. Methods are bound.
-## contents = []
-## for name in cpy_module.__dict__:
-## # ignore names in '_xyz'
-## if not name.startswith('_') or name.endswith('_'):
-## value = cpy_module.__dict__[name]
-## contents.append((space.wrap(name), space.wrap(value)))
-## w_contents = space.newdict(contents)
-## return Module(space, space.wrap(modname), w_contents)
# ____________________________________________________________
#
-# List of built-in modules.
-# It should contain the name of all the files 'module/*module.py'.
-builtin_module_names = ['__builtin__', 'sys', 'exceptions'
- ]
+
+builtin_module_names = ['__builtin__', 'sys', 'exceptions']
# Create the builtin_modules dictionary, mapping names to Module instances
builtin_modules = {}
@@ -47,156 +36,38 @@
else:
builtin_module_names.append(fn)
-# ____________________________________________________________
-#
-# Common data structures
-w_modules = space.newdict([])
-w_warnoptions = space.newlist([])
-w_argv = space.newlist([])
-builtin_module_names.sort()
-w_builtin_module_names = space.newtuple([space.wrap(fn)
- for fn in builtin_module_names])
-
-# Initialize the default path
-import os
-from pypy.interpreter import autopath
-srcdir = os.path.dirname(autopath.pypydir)
-python_std_lib = os.path.normpath(
- os.path.join(autopath.pypydir, os.pardir,'lib-python-2.3.4'))
-pypy_override_lib = os.path.join(autopath.pypydir, 'lib')
-assert os.path.exists(python_std_lib)
-del os, autopath # XXX for the translator. Something is very wrong around here.
-
-w_initialpath = space.newlist([space.wrap(''),
- space.wrap(pypy_override_lib),
- space.wrap(python_std_lib),
- ] +
- [space.wrap(p) for p in cpy_sys.path if p!= srcdir])
-
-# XXX - Replace with appropriate PyPy version numbering
-w_platform = space.wrap(cpy_sys.platform)
-w_maxint = space.wrap(cpy_sys.maxint)
-w_byteorder = space.wrap(cpy_sys.byteorder)
-w_exec_prefix = space.wrap(cpy_sys.exec_prefix)
-w_prefix = space.wrap(cpy_sys.prefix)
-w_maxunicode = space.wrap(cpy_sys.maxunicode)
-w_stdin = space.wrap(cpy_sys.stdin)
-w_stdout = space.wrap(cpy_sys.stdout)
-w_stderr = space.wrap(cpy_sys.stderr)
-
-w_pypy_objspaceclass = space.wrap(space.__class__.__name__)
+builtin_module_names.sort()
-# ____________________________________________________________
-
-def setmodule(w_module):
- """ put a module into the modules dict """
- w_name = space.getattr(w_module, space.wrap('__name__'))
- space.setitem(w_modules, w_name, w_module)
-
-def setbuiltinmodule(w_module, name):
- """ put a module into the modules builtin_modules dicts """
- if builtin_modules[name] is None:
- builtin_modules[name] = space.unwrap(w_module)
- else:
- assert builtin_modules[name] is space.unwrap(w_module), (
- "trying to change the builtin-in module %r" % (name,))
- space.setitem(w_modules, space.wrap(name), w_module)
-
-##def displayhook(w_x):
-## w = space.wrap
-## if not space.is_true(space.is_(w_x, space.w_None)):
-## w_stdout = space.getattr(space.w_sys, space.wrap('stdout'))
-## try:
-## w_repr = space.repr(w_x)
-## except OperationError:
-## w_repr = space.wrap("! __repr__ raised an exception")
-## w_repr = space.add(w_repr, space.wrap("\n"))
-## space.call_method(w_stdout, 'write',
-## space.setitem(space.w_builtins, w('_'), w_x)
-
-def _getframe(w_depth=0):
- depth = space.int_w(w_depth)
- try:
- f = space.getexecutioncontext().framestack.top(depth)
- except IndexError:
- raise OperationError(space.w_ValueError,
- space.wrap("call stack is not deep enough"))
- except ValueError:
- raise OperationError(space.w_ValueError,
- space.wrap("frame index must not be negative"))
- return space.wrap(f)
-
-# directly from the C code in ceval.c, might be moved somewhere else.
-
-def setrecursionlimit(w_new_limit):
- """setrecursionlimit(n)
-
-Set the maximum depth of the Python interpreter stack to n. This
-limit prevents infinite recursion from causing an overflow of the C
-stack and crashing Python. The highest possible limit is platform
-dependent."""
- new_limit = space.int_w(w_new_limit)
- if new_limit <= 0:
- raise OperationError(space.w_ValueError,
- space.wrap("recursion limit must be positive"))
- # global recursion_limit
- # we need to do it without writing globals.
- space.recursion_limit = new_limit
-
-def getrecursionlimit():
- """getrecursionlimit()
-
-Return the current value of the recursion limit, the maximum depth
-of the Python interpreter stack. This limit prevents infinite
-recursion from causing an overflow of the C stack and crashing Python."""
-
- return space.newint(space.recursion_limit)
-
-checkinterval = 100
-
-def setcheckinterval(w_interval):
- """setcheckinterval(n)
-
-Tell the Python interpreter to check for asynchronous events every
-n instructions. This also affects how often thread switches occur."""
-
- space.sys.checkinterval = space.int_w(w_interval)
-
-def getcheckinterval():
- """getcheckinterval() -> current check interval; see setcheckinterval()."""
- return space.newint(checkinterval)
-
-
-def exc_info():
- operror = space.getexecutioncontext().sys_exc_info()
- if operror is None:
- return space.newtuple([space.w_None,space.w_None,space.w_None])
- else:
- return space.newtuple([operror.w_type,operror.w_value,
- space.wrap(operror.application_traceback)])
-
-def exc_clear():
- operror = space.getexecutioncontext().sys_exc_info()
- if operror is not None:
- operror.clear(space)
+class State:
+ def __init__(self, space, stuff=None):
+ self.space = space
+ self.w_builtin_module_names = space.newlist(
+ [space.wrap(fn) for fn in builtin_module_names])
+ self.w_modules = space.newdict([])
+ self.w_warnoptions = space.newlist([])
+ self.w_argv = space.newlist([])
+ self.setinitialpath(space)
+
+ def setinitialpath(self, space):
+ # Initialize the default path
+ from pypy.interpreter import autopath
+ srcdir = os.path.dirname(autopath.pypydir)
+ python_std_lib = os.path.normpath(
+ os.path.join(autopath.pypydir, os.pardir,'lib-python-2.3.4'))
+ pypy_override_lib = os.path.join(autopath.pypydir, 'lib')
+ assert os.path.exists(python_std_lib)
+ self.w_path = space.newlist([space.wrap(''),
+ space.wrap(pypy_override_lib),
+ space.wrap(python_std_lib),
+ ] +
+ [space.wrap(p) for p in sys.path if p!= srcdir])
+
+statecache = Cache()
+def get(space):
+ return space.loadfromcache(space, State, statecache)
-def pypy_getudir():
+def pypy_getudir(space):
"""NOT_RPYTHON"""
from pypy.tool.udir import udir
return space.wrap(str(udir))
-def getdefaultencoding():
- """getdefaultencoding() -> return the default encoding used for UNICODE"""
- return space.wrap(cpy_sys.getdefaultencoding())
-
-def getrefcount(w_obj):
- """getrefcount(object) -> integer
-
-Return the reference count of object. The count returned is generally
-one higher than you might expect, because it includes the (temporary)
-reference as an argument to getrefcount().
-"""
- # From the results i get when using this i need to apply a fudge
- # value of 6 to get results comparable to cpythons. /Arre
- return space.wrap(cpy_sys.getrefcount(w_obj) - 6)
-
Copied: pypy/branch/dist-interpapp/pypy/module/sys2/vm.py (from r9258, pypy/dist/pypy/module/sysinterp.py)
==============================================================================
--- pypy/dist/pypy/module/sysinterp.py (original)
+++ pypy/branch/dist-interpapp/pypy/module/sys2/vm.py Thu Feb 17 10:49:00 2005
@@ -1,97 +1,10 @@
"""
Implementation of interpreter-level 'sys' routines.
"""
-#from pypy.interpreter.module import Module
from pypy.interpreter.error import OperationError
-
-import sys as cpy_sys
-
-def hack_cpython_module(modname):
- "NOT_RPYTHON. Steal a module from CPython."
- cpy_module = __import__(modname, globals(), locals(), None)
- return cpy_module
-## # to build the dictionary of the module, we get all the objects
-## # accessible as 'self.xxx'. Methods are bound.
-## contents = []
-## for name in cpy_module.__dict__:
-## # ignore names in '_xyz'
-## if not name.startswith('_') or name.endswith('_'):
-## value = cpy_module.__dict__[name]
-## contents.append((space.wrap(name), space.wrap(value)))
-## w_contents = space.newdict(contents)
-## return Module(space, space.wrap(modname), w_contents)
-
-# ____________________________________________________________
-#
-# List of built-in modules.
-# It should contain the name of all the files 'module/*module.py'.
-builtin_module_names = ['__builtin__', 'sys', 'exceptions'
- ]
-
-# Create the builtin_modules dictionary, mapping names to Module instances
-builtin_modules = {}
-for fn in builtin_module_names:
- builtin_modules[fn] = None
-
-# The following built-in modules are not written in PyPy, so we
-# steal them from Python.
-for fn in ['posix', 'nt', 'os2', 'mac', 'ce', 'riscos',
- 'math', '_codecs', 'array',
- '_random', '_sre', 'time', '_socket', 'errno',
- 'marshal', 'binascii', 'parser']:
- if fn not in builtin_modules:
- try:
- builtin_modules[fn] = hack_cpython_module(fn)
- except ImportError:
- pass
- else:
- builtin_module_names.append(fn)
+import sys
# ____________________________________________________________
-#
-# Common data structures
-w_modules = space.newdict([])
-w_warnoptions = space.newlist([])
-w_argv = space.newlist([])
-builtin_module_names.sort()
-w_builtin_module_names = space.newtuple([space.wrap(fn)
- for fn in builtin_module_names])
-
-# Initialize the default path
-import os
-from pypy.interpreter import autopath
-srcdir = os.path.dirname(autopath.pypydir)
-python_std_lib = os.path.normpath(
- os.path.join(autopath.pypydir, os.pardir,'lib-python-2.3.4'))
-pypy_override_lib = os.path.join(autopath.pypydir, 'lib')
-assert os.path.exists(python_std_lib)
-del os, autopath # XXX for the translator. Something is very wrong around here.
-
-w_initialpath = space.newlist([space.wrap(''),
- space.wrap(pypy_override_lib),
- space.wrap(python_std_lib),
- ] +
- [space.wrap(p) for p in cpy_sys.path if p!= srcdir])
-
-# XXX - Replace with appropriate PyPy version numbering
-w_platform = space.wrap(cpy_sys.platform)
-w_maxint = space.wrap(cpy_sys.maxint)
-w_byteorder = space.wrap(cpy_sys.byteorder)
-w_exec_prefix = space.wrap(cpy_sys.exec_prefix)
-w_prefix = space.wrap(cpy_sys.prefix)
-w_maxunicode = space.wrap(cpy_sys.maxunicode)
-w_stdin = space.wrap(cpy_sys.stdin)
-w_stdout = space.wrap(cpy_sys.stdout)
-w_stderr = space.wrap(cpy_sys.stderr)
-
-w_pypy_objspaceclass = space.wrap(space.__class__.__name__)
-
-# ____________________________________________________________
-
-def setmodule(w_module):
- """ put a module into the modules dict """
- w_name = space.getattr(w_module, space.wrap('__name__'))
- space.setitem(w_modules, w_name, w_module)
def setbuiltinmodule(w_module, name):
""" put a module into the modules builtin_modules dicts """
@@ -102,19 +15,7 @@
"trying to change the builtin-in module %r" % (name,))
space.setitem(w_modules, space.wrap(name), w_module)
-##def displayhook(w_x):
-## w = space.wrap
-## if not space.is_true(space.is_(w_x, space.w_None)):
-## w_stdout = space.getattr(space.w_sys, space.wrap('stdout'))
-## try:
-## w_repr = space.repr(w_x)
-## except OperationError:
-## w_repr = space.wrap("! __repr__ raised an exception")
-## w_repr = space.add(w_repr, space.wrap("\n"))
-## space.call_method(w_stdout, 'write',
-## space.setitem(space.w_builtins, w('_'), w_x)
-
-def _getframe(w_depth=0):
+def _getframe(space, w_depth=0):
depth = space.int_w(w_depth)
try:
f = space.getexecutioncontext().framestack.top(depth)
@@ -128,7 +29,7 @@
# directly from the C code in ceval.c, might be moved somewhere else.
-def setrecursionlimit(w_new_limit):
+def setrecursionlimit(space, w_new_limit):
"""setrecursionlimit(n)
Set the maximum depth of the Python interpreter stack to n. This
@@ -141,62 +42,54 @@
space.wrap("recursion limit must be positive"))
# global recursion_limit
# we need to do it without writing globals.
- space.recursion_limit = new_limit
+ space.sys.recursion_limit = new_limit
-def getrecursionlimit():
+def getrecursionlimit(space):
"""getrecursionlimit()
+ Return the current value of the recursion limit, the maximum depth
+ of the Python interpreter stack. This limit prevents infinite
+ recursion from causing an overflow of the C stack and crashing Python.
+ """
-Return the current value of the recursion limit, the maximum depth
-of the Python interpreter stack. This limit prevents infinite
-recursion from causing an overflow of the C stack and crashing Python."""
-
- return space.newint(space.recursion_limit)
+ return space.wrap(space.sys.recursion_limit)
checkinterval = 100
-def setcheckinterval(w_interval):
+def setcheckinterval(space, w_interval):
"""setcheckinterval(n)
+ Tell the Python interpreter to check for asynchronous events every
+ n instructions. This also affects how often thread switches occur."""
+ space.sys.checkinterval = space.int_w(w_interval)
-Tell the Python interpreter to check for asynchronous events every
-n instructions. This also affects how often thread switches occur."""
-
- space.sys.checkinterval = space.int_w(w_interval)
-
-def getcheckinterval():
+def getcheckinterval(space):
"""getcheckinterval() -> current check interval; see setcheckinterval()."""
- return space.newint(checkinterval)
+ return space.wrap(space.sys.checkinterval)
-
-def exc_info():
+def exc_info(space):
operror = space.getexecutioncontext().sys_exc_info()
if operror is None:
return space.newtuple([space.w_None,space.w_None,space.w_None])
else:
- return space.newtuple([operror.w_type,operror.w_value,
+ return space.newtuple([operror.w_type, operror.w_value,
space.wrap(operror.application_traceback)])
-def exc_clear():
+def exc_clear(space):
operror = space.getexecutioncontext().sys_exc_info()
if operror is not None:
operror.clear(space)
-def pypy_getudir():
+def pypy_getudir(space):
"""NOT_RPYTHON"""
from pypy.tool.udir import udir
return space.wrap(str(udir))
-def getdefaultencoding():
- """getdefaultencoding() -> return the default encoding used for UNICODE"""
- return space.wrap(cpy_sys.getdefaultencoding())
-
-def getrefcount(w_obj):
+def getrefcount(space, w_obj):
"""getrefcount(object) -> integer
-
-Return the reference count of object. The count returned is generally
-one higher than you might expect, because it includes the (temporary)
-reference as an argument to getrefcount().
-"""
+ Return the reference count of object. The count returned is generally
+ one higher than you might expect, because it includes the (temporary)
+ reference as an argument to getrefcount().
+ """
# From the results i get when using this i need to apply a fudge
# value of 6 to get results comparable to cpythons. /Arre
- return space.wrap(cpy_sys.getrefcount(w_obj) - 6)
+ return space.wrap(sys.getrefcount(w_obj) - 6)
More information about the Pypy-commit
mailing list