[pypy-svn] pypy default: Split the '__flags__' interp-level field of W_TypeObject

arigo commits-noreply at bitbucket.org
Sun Mar 13 15:04:01 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r42557:bd3c564c333b
Date: 2011-03-13 08:26 -0400
http://bitbucket.org/pypy/pypy/changeset/bd3c564c333b/

Log:	Split the '__flags__' interp-level field of W_TypeObject into
	individual boolean fields, which looks better anyway in (R)Python
	code. This allows a fix: _ABSTRACT is not an immutable flag,
	whereas the others are.

diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -32,6 +32,7 @@
 from pypy.rlib.objectmodel import specialize
 from pypy.module.__builtin__.abstractinst import abstract_issubclass_w
 from pypy.module.__builtin__.interp_classobj import W_ClassObject
+from pypy.rlib import jit
 
 WARN_ABOUT_MISSING_SLOT_FUNCTIONS = False
 
@@ -267,6 +268,7 @@
         PyMember_SetOne(space, w_self, self.member, w_value)
 
 class W_PyCTypeObject(W_TypeObject):
+    @jit.dont_look_inside
     def __init__(self, space, pto):
         bases_w = space.fixedview(from_ref(space, pto.c_tp_bases))
         dict_w = {}
@@ -285,7 +287,7 @@
 
         W_TypeObject.__init__(self, space, extension_name,
             bases_w or [space.w_object], dict_w)
-        self.__flags__ = _CPYTYPE
+        self.flag_cpytype = True
 
 @bootstrap_function
 def init_typeobject(space):

diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -10,13 +10,9 @@
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.rlib.objectmodel import current_object_addr_as_int, compute_hash
 from pypy.rlib.jit import hint, purefunction_promote, we_are_jitted
-from pypy.rlib.jit import purefunction
+from pypy.rlib.jit import purefunction, dont_look_inside
 from pypy.rlib.rarithmetic import intmask, r_uint
 
-from copy_reg import _HEAPTYPE
-_CPYTYPE = 1 # used for non-heap types defined in C
-_ABSTRACT = 1 << 20
-
 # from compiler/misc.py
 
 MANGLE_LEN = 256 # magic constant from compile.c
@@ -80,7 +76,9 @@
     # other changes to the type (e.g. the name) leave it unchanged
     _version_tag = None
 
-    _immutable_fields_ = ["__flags__",
+    _immutable_fields_ = ["flag_heaptype",
+                          "flag_cpytype",
+                          #  flag_abstract is not immutable
                           'needsdel',
                           'weakrefable',
                           'hasdict',
@@ -98,6 +96,7 @@
     # of the __new__ is an instance of the type
     w_bltin_new = None
 
+    @dont_look_inside
     def __init__(w_self, space, name, bases_w, dict_w,
                  overridetypedef=None):
         w_self.space = space
@@ -110,7 +109,9 @@
         w_self.weakrefable = False
         w_self.w_doc = space.w_None
         w_self.weak_subclasses = []
-        w_self.__flags__ = 0           # or _HEAPTYPE or _CPYTYPE
+        w_self.flag_heaptype = False
+        w_self.flag_cpytype = False
+        w_self.flag_abstract = False
         w_self.instancetypedef = overridetypedef
 
         if overridetypedef is not None:
@@ -368,19 +369,16 @@
         raise UnwrapError(w_self)
 
     def is_heaptype(w_self):
-        return w_self.__flags__ & _HEAPTYPE
+        return w_self.flag_heaptype
 
     def is_cpytype(w_self):
-        return w_self.__flags__ & _CPYTYPE
+        return w_self.flag_cpytype
 
     def is_abstract(w_self):
-        return w_self.__flags__ & _ABSTRACT
+        return w_self.flag_abstract
 
     def set_abstract(w_self, abstract):
-        if abstract:
-            w_self.__flags__ |= _ABSTRACT
-        else:
-            w_self.__flags__ &= ~_ABSTRACT
+        w_self.flag_abstract = bool(abstract)
 
     def issubtype(w_self, w_type):
         w_self = hint(w_self, promote=True)
@@ -633,11 +631,12 @@
         w_self.bases_w = [w_self.space.w_object]
     w_bestbase = check_and_find_best_base(w_self.space, w_self.bases_w)
     w_self.instancetypedef = w_bestbase.instancetypedef
-    w_self.__flags__ = _HEAPTYPE
+    w_self.flag_heaptype = True
     for w_base in w_self.bases_w:
         if not isinstance(w_base, W_TypeObject):
             continue
-        w_self.__flags__ |= w_base.__flags__
+        w_self.flag_cpytype |= w_base.flag_cpytype
+        w_self.flag_abstract |= w_base.flag_abstract
 
     hasoldstylebase = copy_flags_from_bases(w_self, w_bestbase)
     create_all_slots(w_self, hasoldstylebase)

diff --git a/pypy/module/_stackless/interp_coroutine.py b/pypy/module/_stackless/interp_coroutine.py
--- a/pypy/module/_stackless/interp_coroutine.py
+++ b/pypy/module/_stackless/interp_coroutine.py
@@ -320,21 +320,22 @@
     return space.newtuple(items)
 
 def makeStaticMethod(module, classname, funcname):
+    "NOT_RPYTHON"
     space = module.space
     w_klass = space.getattr(space.wrap(module), space.wrap(classname))
     # HACK HACK HACK
     # make the typeobject mutable for a while
-    from pypy.objspace.std.typeobject import _HEAPTYPE, W_TypeObject
+    from pypy.objspace.std.typeobject import W_TypeObject
     assert isinstance(w_klass, W_TypeObject)
-    old_flags = w_klass.__flags__
-    w_klass.__flags__ |= _HEAPTYPE
+    old_flag = w_klass.flag_heaptype
+    w_klass.flag_heaptype = True
     
     space.appexec([w_klass, space.wrap(funcname)], """
         (klass, funcname):
             func = getattr(klass, funcname)
             setattr(klass, funcname, staticmethod(func.im_func))
     """)
-    w_klass.__flags__ = old_flags
+    w_klass.flag_heaptype = old_flag
 
 def post_install(module):
     makeStaticMethod(module, 'coroutine', 'getcurrent')

diff --git a/pypy/module/_stackless/interp_greenlet.py b/pypy/module/_stackless/interp_greenlet.py
--- a/pypy/module/_stackless/interp_greenlet.py
+++ b/pypy/module/_stackless/interp_greenlet.py
@@ -199,6 +199,7 @@
     return space.getattr(w_module, space.wrap(name))
 
 def post_install(module):
+    "NOT_RPYTHON"
     makeStaticMethod(module, 'greenlet', 'getcurrent')
     space = module.space
     state = AppGreenlet._get_state(space)
@@ -206,10 +207,10 @@
     w_greenlet = get(space, 'greenlet')
     # HACK HACK HACK
     # make the typeobject mutable for a while
-    from pypy.objspace.std.typeobject import _HEAPTYPE, W_TypeObject
+    from pypy.objspace.std.typeobject import W_TypeObject
     assert isinstance(w_greenlet, W_TypeObject)
-    old_flags = w_greenlet.__flags__
-    w_greenlet.__flags__ |= _HEAPTYPE
+    old_flag = w_greenlet.flag_heaptype
+    w_greenlet.flag_heaptype = True
     space.appexec([w_greenlet,
                    state.w_GreenletExit,
                    state.w_GreenletError], """
@@ -217,7 +218,7 @@
         greenlet.GreenletExit = exit
         greenlet.error = error
     """)
-    w_greenlet.__flags__ = old_flags
+    w_greenlet.flag_heaptype = old_flag
 
 AppGreenlet.typedef = TypeDef("greenlet",
     __new__ = interp2app(AppGreenlet.descr_method__new__.im_func),

diff --git a/pypy/objspace/std/typetype.py b/pypy/objspace/std/typetype.py
--- a/pypy/objspace/std/typetype.py
+++ b/pypy/objspace/std/typetype.py
@@ -190,8 +190,16 @@
         return space.get(w_result, space.w_None, w_type)
 
 def descr__flags(space, w_type):
+    from copy_reg import _HEAPTYPE
+    _CPYTYPE = 1 # used for non-heap types defined in C
+    _ABSTRACT = 1 << 20
+    #
     w_type = _check(space, w_type)
-    return space.wrap(w_type.__flags__)
+    flags = 0
+    if w_type.flag_heaptype: flags |= _HEAPTYPE
+    if w_type.flag_cpytype:  flags |= _CPYTYPE
+    if w_type.flag_abstract: flags |= _ABSTRACT
+    return space.wrap(flags)
 
 def descr_get__module(space, w_type):
     w_type = _check(space, w_type)


More information about the Pypy-commit mailing list