Maciej Fijalkowski avatar Maciej Fijalkowski committed fd19fa6 Merge

merge

Comments (0)

Files changed (9)

pypy/rpython/lltypesystem/test/test_rffi.py

             return len(res)
     
         xf = self.compile(f, [], backendopt=False)
-        assert xf(expected_extra_mallocs=-1) == 3
+        assert xf() == 3
     
     def test_stringstar(self):
         c_source = """
             try:
                 for i in range(len(d)):
                     raw_buf[i] = d[i]
-                return unicode_from_buffer(raw_buf, gc_buf, len(d), len(d)-1)
+                return (unicode_from_buffer(raw_buf, gc_buf, len(d), len(d)-1)
+                        .encode('ascii'))
             finally:
                 keep_unicodebuffer_alive_until_here(raw_buf, gc_buf)
         assert f() == d[:-1]

pypy/rpython/tool/rffi_platform.py

         allfields = tuple(['c_' + name for name, _ in fields])
         padfields = tuple(padfields)
         name = self.name
-        padding_drop = PaddingDrop(name, allfields, padfields,
-                                   config_result.CConfig._compilation_info_)
+        eci = config_result.CConfig._compilation_info_
+        padding_drop = PaddingDrop(name, allfields, padfields, eci)
         hints = {'align': info['align'],
                  'size': info['size'],
                  'fieldoffsets': tuple(fieldoffsets),
                  'padding': padfields,
-                 'get_padding_drop': padding_drop}
+                 'get_padding_drop': padding_drop,
+                 'eci': eci}
         if name.startswith('struct '):
             name = name[7:]
         else:

pypy/translator/c/genc.py

             eci = node.compilation_info()
             if eci:
                 all.append(eci)
+        for node in self.db.getstructdeflist():
+            try:
+                all.append(node.STRUCT._hints['eci'])
+            except (AttributeError, KeyError):
+                pass
         self.merge_eci(*all)
 
     def get_gcpolicyclass(self):

pypy/translator/c/src/main.h

         pypy_debug_catch_fatal_exception();
     }
 
+    pypy_malloc_counters_results();
+
     return exitcode;
 
  memory_out:

pypy/translator/c/src/mem.h

 		r = (restype) PyObject_Malloc(size);			\
 		if (r != NULL) {					\
 			memset((void*)r, 0, size);			\
+			COUNT_MALLOC;					\
 		}							\
 	}
 
 
 #define OP_RAW_MALLOC(size, r, restype)  {				\
 		r = (restype) PyObject_Malloc(size);			\
+		if (r != NULL) {					\
+			COUNT_MALLOC;					\
+		} 							\
 	}
 
 #endif
 
-#define OP_RAW_FREE(p, r) PyObject_Free(p);
+#define OP_RAW_FREE(p, r) PyObject_Free(p); COUNT_FREE;
 
 #define OP_RAW_MEMCLEAR(p, size, r) memset((void*)p, 0, size)
 
 
 #define OP_FREE(p)	OP_RAW_FREE(p, do_not_use)
 
+/*------------------------------------------------------------*/
+#ifndef COUNT_OP_MALLOCS
+/*------------------------------------------------------------*/
+
+#define COUNT_MALLOC	/* nothing */
+#define COUNT_FREE	/* nothing */
+
+#define pypy_malloc_counters_results()  /* nothing */
+
+/*------------------------------------------------------------*/
+#else /*COUNT_OP_MALLOCS*/
+/*------------------------------------------------------------*/
+
+static int count_mallocs=0, count_frees=0;
+
+#define COUNT_MALLOC	count_mallocs++
+#define COUNT_FREE	count_frees++
+
+#define pypy_malloc_counters_results()  \
+    printf("MALLOC COUNTERS: %d %d\n", count_mallocs, count_frees)
+
+/*------------------------------------------------------------*/
+#endif /*COUNT_OP_MALLOCS*/
+/*------------------------------------------------------------*/
+
 /* for Boehm GC */
 
 #ifdef USING_BOEHM_GC

pypy/translator/c/test/test_backendoptimized.py

                 a = A()
             return b.num_deleted
 
-        fn = self.getcompiled(f, [int])
+        fn = self.getcompiled(f, [int], gcpolicy='ref')
         res = f(5)
         assert res == 5
         res = fn(5)
                 return s.a_dels * 10 + s.b_dels
             else:
                 return -1
-        fn = self.getcompiled(f, [int])
+        fn = self.getcompiled(f, [int], gcpolicy='ref')
         res = f(1)
         assert res == 42
         res = fn(1)
         codegenerator = self.CodeGenerator()
         fn = codegenerator.getcompiled(f, [r_uint])
         for x in (0,1,2,3,9,27,48):
-            assert fn(x) == f(x)
+            assert fn(r_uint(x)) == f(r_uint(x))
 
     def test_longlong_switch(self):
         def f(x):
         codegenerator = self.CodeGenerator()
         fn = codegenerator.getcompiled(f, [r_longlong])
         for x in (0,1,2,3,9,27,48, -9):
-            assert fn(x) == f(x)
+            assert fn(r_longlong(x)) == f(r_longlong(x))
 
     def test_ulonglong_switch(self):
         def f(x):
         codegenerator = self.CodeGenerator()
         fn = codegenerator.getcompiled(f, [r_ulonglong])
         for x in (0,1,2,3,9,27,48, r_ulonglong(-9)):
-            assert fn(x) == f(x)
+            assert fn(r_ulonglong(x)) == f(r_ulonglong(x))
 
     def test_chr_switch(self):
         def f(y):

pypy/translator/c/test/test_boehm.py

             a3, b3, c3 = run_once()
             a4, b4, c4 = run_once()
             a5, b5, c5 = run_once()
-            return (s.a_dels, s.b_dels,
-                    a1, b1, c1,
-                    a2, b2, c2,
-                    a3, b3, c3,
-                    a4, b4, c4,
-                    a5, b5, c5)
+            return str((s.a_dels, s.b_dels,
+                        a1, b1, c1,
+                        a2, b2, c2,
+                        a3, b3, c3,
+                        a4, b4, c4,
+                        a5, b5, c5))
         fn = self.getcompiled(f, [int])
         # we can't demand that boehm has collected all of the objects,
         # even with the gc__collect call.
         res = fn(50)
-        res1, res2 = res[:2]
+        res1, res2 = eval(res)[:2]
         # if res1 or res2 is still 0, then we haven't tested anything so fail.
         # it might be the test's fault though.
         print res1, res2
         #
         def fn():
             d2 = D()
-            return (compute_hash(d2),
-                    current_object_addr_as_int(d2),
-                    compute_hash(c),
-                    compute_hash(d),
-                    compute_hash(("Hi", None, (7.5, 2, d))))
+            return str((compute_hash(d2),
+                        current_object_addr_as_int(d2),
+                        compute_hash(c),
+                        compute_hash(d),
+                        compute_hash(("Hi", None, (7.5, 2, d)))))
 
         f = self.getcompiled(fn)
         res = f()
+        res = eval(res)
 
         # xxx the next line is too precise, checking the exact implementation
         assert res[0] == ~res[1]

pypy/translator/c/test/test_genc.py

 
 from pypy.rlib.entrypoint import entrypoint
 from pypy.rlib.unroll import unrolling_iterable
-from pypy.rlib.rarithmetic import r_longlong, r_ulonglong, intmask
+from pypy.rlib.rarithmetic import r_longlong, r_ulonglong, r_uint, intmask
+from pypy.rlib.objectmodel import specialize
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.lltypesystem.lltype import *
 from pypy.rpython.lltypesystem.rstr import STR
 signed_ffffffff = r_longlong(0xffffffff)
 unsigned_ffffffff = r_ulonglong(0xffffffff)
 
-def llrepr(v):
+def llrepr_in(v):
     if isinstance(v, r_ulonglong):
         return "%d:%d" % (intmask(v >> 32), intmask(v & unsigned_ffffffff))
     elif isinstance(v, r_longlong):
         return repr(v)    # extra precision than str(v)
     return str(v)
 
+@specialize.argtype(0)
+def llrepr_out(v):
+    if isinstance(v, float):
+        from pypy.rlib.rfloat import formatd, DTSF_ADD_DOT_0
+        return formatd(v, 'r', 0, DTSF_ADD_DOT_0)
+    return v
+
 def parse_longlong(a):
     p0, p1 = a.split(":")
     return (r_longlong(int(p0)) << 32) + (r_longlong(int(p1)) &
     argtypes_unroll = unrolling_iterable(enumerate(argtypes))
 
     for argtype in argtypes:
-        if argtype not in [int, float, str, bool, r_ulonglong, r_longlong]:
+        if argtype not in [int, float, str, bool, r_ulonglong, r_longlong,
+                           r_uint]:
             raise Exception("Unsupported argtype, %r" % (argtype,))
 
     def entry_point(argv):
             a = argv[i + 1]
             if argtype is int:
                 args += (int(a),)
+            elif argtype is r_uint:
+                args += (r_uint(int(a)),)
             elif argtype is r_longlong:
                 args += (parse_longlong(a),)
             elif argtype is r_ulonglong:
             else:
                 args += (a,)
         res = fn(*args)
-        print "THE RESULT IS:", res, ";"
+        print "THE RESULT IS:", llrepr_out(res), ";"
         return 0
 
     t = Translation(entry_point, None, gc=gcpolicy, backend="c",
                     policy=annotatorpolicy, thread=thread)
     if not backendopt:
         t.disable(["backendopt_lltype"])
+    t.driver.config.translation.countmallocs = True
     t.annotate()
     t.compile_c()
     ll_res = graphof(t.context, fn).getreturnvar().concretetype
             t.view()
     except AttributeError:
         pass
-    def f(*args):
+
+    def f(*args, **kwds):
+        if 'expected_extra_mallocs' in kwds:
+            expected_extra_mallocs = kwds.pop('expected_extra_mallocs')
+        else:
+            expected_extra_mallocs = 0
+        assert not kwds
         assert len(args) == len(argtypes)
         for arg, argtype in zip(args, argtypes):
             assert isinstance(arg, argtype)
-        stdout = t.driver.cbuilder.cmdexec(" ".join([llrepr(arg) for arg in args]))
+        stdout = t.driver.cbuilder.cmdexec(" ".join([llrepr_in(arg) for arg in args]))
         print stdout
-        assert stdout.endswith(' ;\n')
+        stdout, lastline, empty = stdout.rsplit('\n', 2)
+        assert empty == ''
+        assert lastline.startswith('MALLOC COUNTERS: ')
+        mallocs, frees = map(int, lastline.split()[2:])
+        assert stdout.endswith(' ;')
         pos = stdout.rindex('THE RESULT IS: ')
-        res = stdout[pos + len('THE RESULT IS: '):-3]
+        res = stdout[pos + len('THE RESULT IS: '):-2]
+        #
+        if isinstance(expected_extra_mallocs, int):
+            assert mallocs - frees == expected_extra_mallocs
+        else:
+            assert mallocs - frees in expected_extra_mallocs
+        #
         if ll_res in [lltype.Signed, lltype.Unsigned, lltype.SignedLongLong,
                       lltype.UnsignedLongLong]:
             return int(res)

pypy/translator/c/test/test_lltyped.py

 import py
 from pypy.rpython.lltypesystem.lltype import *
-from pypy.translator.c.test import test_typed
+from pypy.translator.c.test.test_genc import compile
 from pypy.tool.sourcetools import func_with_new_name
 
 
-class TestLowLevelType(test_typed.CompilationTestCase):
+class TestLowLevelType(object):
+    def getcompiled(self, func, argtypes):
+        return compile(func, argtypes, backendopt=False)
 
     def test_simple(self):
         S = GcStruct("s", ('v', Signed))
         def llf():
             s = malloc(S)
             return s.v
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         assert fn() == 0
 
     def test_simple2(self):
             s.a.v = 6
             s.b.v = 12
             return s.a.v + s.b.v
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         assert fn() == 18
 
     def test_fixedsizearray(self):
             assert a42[0][0] == -5
             assert a42[5][6] == -6
             return len(a42)*100 + len(a42[4])
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         res = fn()
         assert fn() == 607
 
             tree.root[0].a = tree.root
             tree.root[1].a = tree.other
             assert tree.root[0].a[0].a[0].a[0].a[0].a[1].a == tree.other
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         fn()
 
     def test_prebuilt_array(self):
             for i in range(5):
                 s += chr(64+a[i])
             assert s == "HELLO"
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         fn()
 
     def test_call_with_fixedsizearray(self):
             s = malloc(S)
             s.a = a
             return g(s.a)
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         res = fn()
         assert res == 123
 
             s.y   +=    1
             return p1[0] + p2[0] + p3[0] + p4[0]
 
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         res = fn()
         assert res == 8765
 
                     x = getmax(cls)
                     res1 = OP(x, nn)
                     result = result + (res1,)
-            return result
-
-        def assert_eq(a, b):
-            # for better error messages when it fails
-            assert len(a) == len(b)
-            for i in range(len(a)):
-                assert a[i] == b[i]
+            return str(result)
 
         fn = self.getcompiled(f, [int])
         res = fn(1)
         print res
-        assert_eq(res, (
+        assert eval(res) == (
             # int
             -sys.maxint, undefined,               # add
             undefined, sys.maxint-1,              # sub
             0, 0,                                 # mod
             0, maxlonglong*2,                     # lshift
             0, maxlonglong,                       # rshift
-            ))
+            )
 
         res = fn(5)
         print res
-        assert_eq(res, (
+        assert eval(res) == (
             # int
             -sys.maxint+4, undefined,             # add
             undefined, sys.maxint-5,              # sub
             0, (maxlonglong*2+1)%5,               # mod
             0, maxlonglong*2-30,                  # lshift
             0, maxlonglong>>4,                    # rshift
-            ))
+            )
 
     def test_direct_ptradd_barebone(self):
         from pypy.rpython.lltypesystem import rffi
                 assert a2[i] == a[i + 2]
             free(a, flavor='raw')
 
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         fn()
 
     def test_r_singlefloat(self):
         a[2][5] = 888000
         def llf():
             return b[3][4] + a[2][5]
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         assert fn() == 888999
 
     def test_prebuilt_nolength_array(self):
             for i in range(5):
                 s += chr(64+a[i])
             assert s == "HELLO"
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         fn()
 
     def test_prebuilt_nolength_char_array(self):
                     s += a[i]
                 print s
                 assert s == "85?!" + lastchar
-            fn = self.getcompiled(llf)
+            fn = self.getcompiled(llf, [])
             fn()
 
     def test_prebuilt_raw_arrays(self):
                     return i    # returns the index of the failing function
                 i += 1
             return -42
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         res = fn()
         assert res == -42, "failing function: %r" % (records[res],)
 
                 s += a[i]
             return 'abcd' == s
 
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         assert fn()
 
     def test_ll2ctypes_array_from_c(self):
                 s += a[i]
             print s
             return s == 'abcd'
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         assert fn()
 
     def test_cast_to_void_array(self):
         def llf():
             TYPE = Ptr(rffi.CArray(Void))
             y = rffi.cast(TYPE, 0)
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         fn()
 
     def test_llgroup(self):
         from pypy.rpython.lltypesystem.test import test_llgroup
         f = test_llgroup.build_test()
-        fn = self.getcompiled(f)
+        fn = self.getcompiled(f, [])
         res = fn()
         assert res == 42
 
                                            ("l3", Signed)])
         S = rffi_platform.configure(CConfig)['STRUCT']
         assert 'get_padding_drop' in S._hints
+        assert 'eci' in S._hints
         s1 = malloc(S, immortal=True)
         s1.c_c1 = rffi.cast(S.c_c1, -12)
         s1.c_s1 = rffi.cast(S.c_s1, -7843)
                 s = s2
             return s.c_l3
         #
-        self.include_also_eci = eci
         fn = self.getcompiled(f, [int])
         res = fn(10)
         assert res == -98765432
             render_immortal(a2)
             a2[0] = 3
             return a1[0] + a2[0]
-        fn = self.getcompiled(llf)
+        fn = self.getcompiled(llf, [])
         assert fn() == 45
 
     def test_rstring_to_float(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.