[pypy-svn] r24365 - in pypy/dist/pypy: bin interpreter module/sys objspace objspace/std

ac at codespeak.net ac at codespeak.net
Tue Mar 14 18:53:34 CET 2006


Author: ac
Date: Tue Mar 14 18:53:32 2006
New Revision: 24365

Modified:
   pypy/dist/pypy/bin/py.py
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/main.py
   pypy/dist/pypy/interpreter/mixedmodule.py
   pypy/dist/pypy/interpreter/pyopcode.py
   pypy/dist/pypy/module/sys/__init__.py
   pypy/dist/pypy/objspace/descroperation.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/objspace/std/typeobject.py
   pypy/dist/pypy/objspace/std/typetype.py
Log:
(pedronis, arre) Refactor to avoid unwrapping and re-wrapping
of strings when accessing attributes.



Modified: pypy/dist/pypy/bin/py.py
==============================================================================
--- pypy/dist/pypy/bin/py.py	(original)
+++ pypy/dist/pypy/bin/py.py	Tue Mar 14 18:53:32 2006
@@ -130,7 +130,7 @@
             exit_status = 0
     finally:
         # call the sys.exitfunc()
-        w_exitfunc = space.sys.getdictvalue(space, 'exitfunc')
+        w_exitfunc = space.sys.getdictvalue_w(space, 'exitfunc')
         if w_exitfunc is not None:
             def doit():
                 space.call_function(w_exitfunc)

Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Tue Mar 14 18:53:32 2006
@@ -16,10 +16,13 @@
     def getdict(self):
         return None
 
-    def getdictvalue(self, space, attr):
+    def getdictvalue_w(self, space, attr):
+        return self.getdictvalue(space, space.wrap(attr))
+
+    def getdictvalue(self, space, w_attr):
         w_dict = self.getdict()
         if w_dict is not None:
-            return space.finditem(w_dict, space.wrap(attr))
+            return space.finditem(w_dict, w_attr)
         return None
 
     def setdict(self, space, w_dict):
@@ -363,6 +366,10 @@
         """shortcut for space.int_w(space.hash(w_obj))"""
         return self.int_w(self.hash(w_obj))
 
+    def set_str_keyed_item(self, w_obj, w_key, w_value):
+        w_strkey = self.wrap(self.str_w(w_key)) # Force the key to a space.w_str
+        return self.setitem(w_obj, w_strkey, w_value)
+    
     def finditem(self, w_obj, w_key):
         try:
             return self.getitem(w_obj, w_key)
@@ -533,7 +540,7 @@
         w_type = self.type(w_obj)
         w_mro = self.getattr(w_type, self.wrap("__mro__"))
         for w_supertype in self.unpackiterable(w_mro):
-            w_value = w_supertype.getdictvalue(self, name)
+            w_value = w_supertype.getdictvalue_w(self, name)
             if w_value is not None:
                 return w_value
         return None

Modified: pypy/dist/pypy/interpreter/main.py
==============================================================================
--- pypy/dist/pypy/interpreter/main.py	(original)
+++ pypy/dist/pypy/interpreter/main.py	Tue Mar 14 18:53:32 2006
@@ -126,11 +126,11 @@
                           w_traceback)
 
             # call sys.excepthook if present
-            w_hook = space.sys.getdictvalue(space, 'excepthook')
+            w_hook = space.sys.getdictvalue_w(space, 'excepthook')
             if w_hook is not None:
                 # hack: skip it if it wasn't modified by the user,
                 #       to do instead the faster verbose/nonverbose thing below
-                w_original = space.sys.getdictvalue(space, '__excepthook__')
+                w_original = space.sys.getdictvalue_w(space, '__excepthook__')
                 if w_original is None or not space.is_w(w_hook, w_original):
                     space.call_function(w_hook, w_type, w_value, w_traceback)
                     return False   # done

Modified: pypy/dist/pypy/interpreter/mixedmodule.py
==============================================================================
--- pypy/dist/pypy/interpreter/mixedmodule.py	(original)
+++ pypy/dist/pypy/interpreter/mixedmodule.py	Tue Mar 14 18:53:32 2006
@@ -21,7 +21,7 @@
 
     def get(self, name):
         space = self.space
-        w_value = self.getdictvalue(space, name) 
+        w_value = self.getdictvalue_w(space, name) 
         if w_value is None: 
             raise OperationError(space.w_AttributeError, space.wrap(name))
         return w_value 
@@ -30,10 +30,11 @@
         w_builtin = self.get(name) 
         return self.space.call_function(w_builtin, *args_w)
 
-    def getdictvalue(self, space, name):
-        w_name = space.new_interned_str(name)
+    def getdictvalue(self, space, w_name):
         w_value = space.finditem(self.w_dict, w_name)
         if self.lazy and w_value is None:
+            name = space.str_w(w_name)
+            w_name = space.new_interned_str(name)
             try: 
                 loader = self.loaders[name]
             except KeyError: 

Modified: pypy/dist/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyopcode.py	(original)
+++ pypy/dist/pypy/interpreter/pyopcode.py	Tue Mar 14 18:53:32 2006
@@ -472,9 +472,9 @@
         w_value = f.space.finditem(f.w_globals, w_varname)
         if w_value is None:
             # not in the globals, now look in the built-ins
-            varname = f.getname_u(nameindex)
-            w_value = f.builtin.getdictvalue(f.space, varname)
+            w_value = f.builtin.getdictvalue(f.space, w_varname)
             if w_value is None:
+                varname = f.getname_u(nameindex)
                 message = "global name '%s' is not defined" % varname
                 raise OperationError(f.space.w_NameError,
                                      f.space.wrap(message))
@@ -559,7 +559,7 @@
         w_modulename = f.getname_w(nameindex)
         modulename = f.space.str_w(w_modulename)
         w_fromlist = f.valuestack.pop()
-        w_import = f.builtin.getdictvalue(f.space, '__import__')
+        w_import = f.builtin.getdictvalue_w(f.space, '__import__')
         if w_import is None:
             raise OperationError(space.w_ImportError,
                                  space.wrap("__import__ not found"))

Modified: pypy/dist/pypy/module/sys/__init__.py
==============================================================================
--- pypy/dist/pypy/module/sys/__init__.py	(original)
+++ pypy/dist/pypy/module/sys/__init__.py	Tue Mar 14 18:53:32 2006
@@ -96,11 +96,12 @@
         w_modules = self.get('modules')
         self.space.setitem(w_modules, w_name, w_module)
 
-    def getdictvalue(self, space, attr): 
+    def getdictvalue(self, space, w_attr): 
         """ specialize access to dynamic exc_* attributes. """ 
-        value = MixedModule.getdictvalue(self, space, attr) 
+        value = MixedModule.getdictvalue(self, space, w_attr) 
         if value is not None: 
-            return value 
+            return value
+        attr = space.str_w(w_attr)
         if attr == 'exc_type':
             operror = space.getexecutioncontext().sys_exc_info()
             if operror is None:

Modified: pypy/dist/pypy/objspace/descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/descroperation.py	(original)
+++ pypy/dist/pypy/objspace/descroperation.py	Tue Mar 14 18:53:32 2006
@@ -20,7 +20,7 @@
         if w_descr is not None:
             if space.is_data_descr(w_descr):
                 return space.get(w_descr, w_obj)
-        w_value = w_obj.getdictvalue(space, name)
+        w_value = w_obj.getdictvalue(space, w_name)
         if w_value is not None:
             return w_value
         if w_descr is not None:
@@ -39,7 +39,7 @@
             # note: don't use w_name as a key in w_dict directly -- the expected
             # result of setattr() is that it never stores subclasses of 'str'
             # in the __dict__
-            space.setitem(w_dict, space.wrap(name), w_value)
+            space.set_str_keyed_item(w_dict, w_name, w_value)
             return
         raiseattrerror(space, w_obj, name, w_descr)
 

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Tue Mar 14 18:53:32 2006
@@ -436,6 +436,15 @@
         else:
             return ObjSpace.finditem(self, w_obj, w_key)
 
+    def set_str_keyed_item(self, w_obj, w_key, w_value):
+        # performance shortcut to avoid creating the OperationError(KeyError)
+        if type(w_key) is not W_StringObject:
+            w_key = self.new_interned_str(self.str_w(w_key))
+        if type(w_obj) is W_DictObject:
+            w_obj.content[w_key] = w_value
+        else:
+            self.setitem(w_obj, w_key, w_value)
+
     # support for the deprecated __getslice__, __setslice__, __delslice__
 
     def getslice(self, w_obj, w_start, w_stop):

Modified: pypy/dist/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/typeobject.py	Tue Mar 14 18:53:32 2006
@@ -168,6 +168,8 @@
                     else:
                         # create member
                         slot_name = _mangle(slot_name, name)
+                        # Force interning of slot names.
+                        slot_name = space.str_w(space.new_interned_str(slot_name))
                         w_self.dict_w[slot_name] = space.wrap(Member(nslots, slot_name, w_self))
                         nslots += 1
 
@@ -212,12 +214,16 @@
             if isinstance(w_new, Function):
                 w_self.dict_w['__new__'] = StaticMethod(w_new)
 
-    def getdictvalue(w_self, space, attr):
+    def getdictvalue(w_self, space, w_attr):
+        return w_self.getdictvalue_w(space, space.str_w(w_attr))
+    
+    def getdictvalue_w(w_self, space, attr):
         try:
             return w_self.dict_w[attr]
         except KeyError:
             if w_self.lazyloaders:
                 if attr in w_self.lazyloaders:
+                    w_attr = space.new_interned_str(attr)
                     loader = w_self.lazyloaders[attr]
                     del w_self.lazyloaders[attr]
                     w_value = loader()
@@ -230,7 +236,7 @@
         # note that this doesn't call __get__ on the result at all
         space = w_self.space
         for w_class in w_self.mro_w:
-            w_value = w_class.getdictvalue(space, key)
+            w_value = w_class.getdictvalue_w(space, key)
             if w_value is not None:
                 return w_value
         return None
@@ -240,7 +246,7 @@
         # attribute was found
         space = w_self.space
         for w_class in w_self.mro_w:
-            w_value = w_class.getdictvalue(space, key)
+            w_value = w_class.getdictvalue_w(space, key)
             if w_value is not None:
                 return w_class, w_value
         return None, None
@@ -265,7 +271,7 @@
         "NOT_RPYTHON.  Forces the lazy attributes to be computed."
         if 'lazyloaders' in w_self.__dict__:
             for attr in w_self.lazyloaders.keys():
-                w_self.getdictvalue(w_self.space, attr)
+                w_self.getdictvalue_w(w_self.space, attr)
             del w_self.lazyloaders
         return False
 

Modified: pypy/dist/pypy/objspace/std/typetype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/typetype.py	(original)
+++ pypy/dist/pypy/objspace/std/typetype.py	Tue Mar 14 18:53:32 2006
@@ -103,7 +103,7 @@
         return space.wrap("""type(object) -> the object's type
 type(name, bases, dict) -> a new type""")
     w_type = _check(space, w_type)
-    w_result = w_type.getdictvalue(space, '__doc__')
+    w_result = w_type.getdictvalue_w(space, '__doc__')
     if w_result is None:
         return space.w_None
     else:



More information about the Pypy-commit mailing list