[pypy-commit] pypy malloc-value: malloc flavor = "value" now works.

alex_gaynor noreply at buildbot.pypy.org
Fri Oct 14 22:29:20 CEST 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: malloc-value
Changeset: r48067:4dd646606d28
Date: 2011-10-14 16:02 -0400
http://bitbucket.org/pypy/pypy/changeset/4dd646606d28/

Log:	malloc flavor = "value" now works.

diff --git a/pypy/rpython/lltypesystem/lltype.py b/pypy/rpython/lltypesystem/lltype.py
--- a/pypy/rpython/lltypesystem/lltype.py
+++ b/pypy/rpython/lltypesystem/lltype.py
@@ -48,7 +48,7 @@
         self.TYPE = TYPE
     def __repr__(self):
         return '<Uninitialized %r>'%(self.TYPE,)
-        
+
 
 def saferecursive(func, defl, TLS=TLS):
     def safe(*args):
@@ -302,7 +302,7 @@
     def _names_without_voids(self):
         names_without_voids = [name for name in self._names if self._flds[name] is not Void]
         return names_without_voids
-    
+
     def _str_fields_without_voids(self):
         return ', '.join(['%s: %s' % (name, self._flds[name])
                           for name in self._names_without_voids(False)])
@@ -410,7 +410,7 @@
     _gckind = 'raw'
     __name__ = 'array'
     _anonym_struct = False
-    
+
     def __init__(self, *fields, **kwds):
         if len(fields) == 1 and isinstance(fields[0], LowLevelType):
             self.OF = fields[0]
@@ -537,9 +537,9 @@
         return "Func ( %s ) -> %s" % (args, self.RESULT)
     __str__ = saferecursive(__str__, '...')
 
-    def _short_name(self):        
+    def _short_name(self):
         args = ', '.join([ARG._short_name() for ARG in self.ARGS])
-        return "Func(%s)->%s" % (args, self.RESULT._short_name())        
+        return "Func(%s)->%s" % (args, self.RESULT._short_name())
     _short_name = saferecursive(_short_name, '...')
 
     def _container_example(self):
@@ -553,7 +553,7 @@
 
 class OpaqueType(ContainerType):
     _gckind = 'raw'
-    
+
     def __init__(self, tag, hints={}):
         """ if hints['render_structure'] is set, the type is internal and not considered
             to come from somewhere else (it should be rendered as a structure) """
@@ -663,7 +663,7 @@
 
     def normalized(self):
         return build_number(None, normalizedinttype(self._type))
-        
+
 
 _numbertypes = {int: Number("Signed", int, intmask)}
 _numbertypes[r_int] = _numbertypes[int]
@@ -723,10 +723,10 @@
 
     def __str__(self):
         return '* %s' % (self.TO, )
-    
+
     def _short_name(self):
         return 'Ptr %s' % (self.TO._short_name(), )
-    
+
     def _is_atomic(self):
         return self.TO._gckind == 'raw'
 
@@ -753,7 +753,7 @@
                          adtmeths=TO._adtmeths)
         else:
             R = GcStruct("Interior", ('ptr', self), ('index', Signed),
-                         hints={'interior_ptr_type':True})            
+                         hints={'interior_ptr_type':True})
         return R
 
 class InteriorPtr(LowLevelType):
@@ -898,7 +898,7 @@
             return dwn
         OUTSIDE = getattr(OUTSIDE, first)
     return -1
- 
+
 def castable(PTRTYPE, CURTYPE):
     if CURTYPE.TO._gckind != PTRTYPE.TO._gckind:
         raise TypeError("cast_pointer() cannot change the gc status: %s to %s"
@@ -1107,7 +1107,7 @@
 
     # _setobj, _getobj and _obj0 are really _internal_ implementations details of _ptr,
     # use _obj if necessary instead !
-    def _setobj(self, pointing_to, solid=False):        
+    def _setobj(self, pointing_to, solid=False):
         if pointing_to is None:
             obj0 = None
         elif (solid or self._T._gckind != 'raw' or
@@ -1118,7 +1118,7 @@
             obj0 = weakref.ref(pointing_to)
         self._set_solid(solid)
         self._set_obj0(obj0)
-        
+
     def _getobj(self, check=True):
         obj = self._obj0
         if obj is not None:
@@ -1294,7 +1294,7 @@
             return result
 
 class _ptr(_abstract_ptr):
-    __slots__ = ('_TYPE', 
+    __slots__ = ('_TYPE',
                  '_weak', '_solid',
                  '_obj0', '__weakref__')
 
@@ -1447,9 +1447,9 @@
             assert T._gckind == 'raw'
             val = _interior_ptr(T, self._parent, self._offsets + [offset])
         return val
-    
-    
-        
+
+
+
 assert not '__dict__' in dir(_interior_ptr)
 
 class _container(object):
@@ -1520,7 +1520,7 @@
             and parentindex in (self._parent_type._names[0], 0)
             and self._TYPE._gckind == typeOf(parent)._gckind):
             # keep strong reference to parent, we share the same allocation
-            self._keepparent = parent 
+            self._keepparent = parent
 
     def _parentstructure(self, check=True):
         if self._wrparent is not None:
@@ -1566,7 +1566,7 @@
             __slots__ = flds
         cache[tag] = _struct1
         return _struct1
- 
+
 #for pickling support:
 def _get_empty_instance_of_struct_variety(flds):
     cls = _struct_variety(flds)
@@ -1629,7 +1629,7 @@
         return r
 
     # for FixedSizeArray kind of structs:
-    
+
     def getlength(self):
         assert isinstance(self._TYPE, FixedSizeArray)
         return self._TYPE.length
@@ -1928,7 +1928,7 @@
         # if we are an opaque containing a normal Struct/GcStruct,
         # unwrap it
         if hasattr(self, 'container'):
-            # an integer, cast to a ptr, cast to an opaque    
+            # an integer, cast to a ptr, cast to an opaque
             if type(self.container) is int:
                 return self.container
             if getattr(self.container, '_carry_around_for_tests', False):
@@ -1955,7 +1955,7 @@
 
 def malloc(T, n=None, flavor='gc', immortal=False, zero=False,
            track_allocation=True, add_memory_pressure=False):
-    assert flavor in ('gc', 'raw')
+    assert flavor in ('gc', 'raw', 'value')
     if zero or immortal:
         initialization = 'example'
     elif flavor == 'raw':
@@ -2045,7 +2045,7 @@
 
 def pyobjectptr(obj):
     o = _pyobject(obj)
-    return _ptr(Ptr(PyObject), o) 
+    return _ptr(Ptr(PyObject), o)
 
 def cast_ptr_to_int(ptr):
     return ptr._cast_to_int()
@@ -2069,7 +2069,7 @@
     if not isinstance(GCSTRUCT, RttiStruct):
         raise TypeError, "expected a RttiStruct: %s" % GCSTRUCT
     if GCSTRUCT._runtime_type_info is None:
-        raise ValueError, ("no attached runtime type info for GcStruct %s" % 
+        raise ValueError, ("no attached runtime type info for GcStruct %s" %
                            GCSTRUCT._name)
     return _ptr(Ptr(RuntimeTypeInfo), GCSTRUCT._runtime_type_info)
 
diff --git a/pypy/rpython/lltypesystem/test/test_lltype.py b/pypy/rpython/lltypesystem/test/test_lltype.py
--- a/pypy/rpython/lltypesystem/test/test_lltype.py
+++ b/pypy/rpython/lltypesystem/test/test_lltype.py
@@ -235,7 +235,7 @@
     del p1
     import gc
     gc.collect()
-    py.test.raises(RuntimeError, "p1_5.v")        
+    py.test.raises(RuntimeError, "p1_5.v")
 
 def test_examples():
     A1 = GcArray(('v', Signed))
@@ -388,11 +388,11 @@
         s.x = 1
     def type_info_S(p):
         return getRuntimeTypeInfo(S)
-    qp = functionptr(FuncType([Ptr(S)], Ptr(RuntimeTypeInfo)), 
-                     "type_info_S", 
+    qp = functionptr(FuncType([Ptr(S)], Ptr(RuntimeTypeInfo)),
+                     "type_info_S",
                      _callable=type_info_S)
-    dp = functionptr(FuncType([Ptr(S)], Void), 
-                     "destructor_funcptr", 
+    dp = functionptr(FuncType([Ptr(S)], Void),
+                     "destructor_funcptr",
                      _callable=f)
     pinf0 = attachRuntimeTypeInfo(S, qp, destrptr=dp)
     assert pinf0._obj.about == S
@@ -422,8 +422,8 @@
             return getRuntimeTypeInfo(S)
         else:
             return getRuntimeTypeInfo(S1)
-    fp = functionptr(FuncType([Ptr(S)], Ptr(RuntimeTypeInfo)), 
-                     "dynamic_type_info_S", 
+    fp = functionptr(FuncType([Ptr(S)], Ptr(RuntimeTypeInfo)),
+                     "dynamic_type_info_S",
                      _callable=dynamic_type_info_S)
     attachRuntimeTypeInfo(S, fp)
     assert s.x == 0
@@ -434,7 +434,7 @@
     py.test.raises(RuntimeError, "runtime_type_info(s1.sub)")
     s1.sub.x = 1
     assert runtime_type_info(s1.sub) == getRuntimeTypeInfo(S1)
-    
+
 def test_flavor_malloc():
     def isweak(p, T):
         return p._weak and typeOf(p).TO == T
@@ -450,7 +450,7 @@
     p = malloc(T, flavor="gc")
     assert typeOf(p).TO == T
     assert not isweak(p, T)
-    
+
 def test_opaque():
     O = OpaqueType('O')
     p1 = opaqueptr(O, 'p1', hello="world")
@@ -520,8 +520,8 @@
 def test_adtmeths():
     def h_newstruct():
         return malloc(S)
-    
-    S = GcStruct('s', ('x', Signed), 
+
+    S = GcStruct('s', ('x', Signed),
                  adtmeths={"h_newstruct": h_newstruct})
 
     s = S.h_newstruct()
@@ -553,15 +553,15 @@
     def h_newstruct(S):
         return malloc(S)
     h_newstruct = typeMethod(h_newstruct)
-    
-    S = GcStruct('s', ('x', Signed), 
+
+    S = GcStruct('s', ('x', Signed),
                  adtmeths={"h_newstruct": h_newstruct})
 
     s = S.h_newstruct()
 
     assert typeOf(s) == Ptr(S)
 
-    Sprime = GcStruct('s', ('x', Signed), 
+    Sprime = GcStruct('s', ('x', Signed),
                       adtmeths={"h_newstruct": h_newstruct})
 
     assert S == Sprime
@@ -592,7 +592,7 @@
     PA = Ptr(A)
     a = malloc(A, 2)
     assert cast_pointer(PA, a) == a
-        
+
 def test_array_with_no_length():
     A = GcArray(Signed, hints={'nolength': True})
     a = malloc(A, 10)
@@ -604,7 +604,7 @@
     s = malloc(GcS)
     s.x = 1
     assert list(dissect_ll_instance(s)) == [(Ptr(GcS), s), (GcS, s._obj), (Signed, 1)]
-    
+
     A = GcArray(('x', Signed))
     a = malloc(A, 10)
     for i in range(10):
@@ -812,6 +812,14 @@
     assert F.RESULT == Signed
     assert F.ARGS == (Signed,)
 
+def test_malloc_value():
+    T = Struct("complex", ("real", Float), ("imag", Float))
+    c = malloc(T, flavor="value")
+    c.real = 4.0
+    c.imag = 5.0
+    assert c.real == 4.0
+    assert c.imag == 5.0
+
 
 class TestTrackAllocation:
     def test_automatic_tracking(self):


More information about the pypy-commit mailing list