Author: Philip Jenvey <pjen...@underboss.org>
Branch: py3k
Changeset: r59426:c0aed73821fb
Date: 2012-12-14 16:12 -0800
http://bitbucket.org/pypy/pypy/changeset/c0aed73821fb/

Log:    merge default

diff --git a/pypy/module/_cffi_backend/ctypeenum.py 
b/pypy/module/_cffi_backend/ctypeenum.py
--- a/pypy/module/_cffi_backend/ctypeenum.py
+++ b/pypy/module/_cffi_backend/ctypeenum.py
@@ -37,6 +37,13 @@
                 space.setitem(w_dct, space.wrap(enumvalue),
                                      space.wrap(enumerator))
             return w_dct
+        if attrchar == 'R':     # relements
+            space = self.space
+            w_dct = space.newdict()
+            for enumerator, enumvalue in self.enumerators2values.iteritems():
+                space.setitem(w_dct, space.wrap(enumerator),
+                                     space.wrap(enumvalue))
+            return w_dct
         return W_CTypePrimitiveSigned._fget(self, attrchar)
 
     def string(self, cdataobj, maxlen):
diff --git a/pypy/module/_cffi_backend/ctypeobj.py 
b/pypy/module/_cffi_backend/ctypeobj.py
--- a/pypy/module/_cffi_backend/ctypeobj.py
+++ b/pypy/module/_cffi_backend/ctypeobj.py
@@ -188,7 +188,7 @@
         if attrchar == 'c':     # cname
             return space.wrap(self.name)
         raise operationerrfmt(space.w_AttributeError,
-                              "cdata '%s' has no such attribute",
+                              "ctype '%s' has no such attribute",
                               self.name)
 
     def fget_kind(self, space):     return self._fget('k')
@@ -201,6 +201,7 @@
     def fget_ellipsis(self, space): return self._fget('E')
     def fget_abi(self, space):      return self._fget('A')
     def fget_elements(self, space): return self._fget('e')
+    def fget_relements(self, space):return self._fget('R')
 
 
 W_CType.typedef = TypeDef(
@@ -218,6 +219,8 @@
     ellipsis = GetSetProperty(W_CType.fget_ellipsis, doc="function has '...'"),
     abi = GetSetProperty(W_CType.fget_abi, doc="function ABI"),
     elements = GetSetProperty(W_CType.fget_elements, doc="enum elements"),
+    relements = GetSetProperty(W_CType.fget_relements,
+                               doc="enum elements, reversed"),
     __dir__ = interp2app(W_CType.dir),
     )
 W_CType.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py 
b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -1272,6 +1272,10 @@
     # 'elements' is not the real dict, but merely a copy
     BEnum.elements[2] = '??'
     assert BEnum.elements == {-20: 'ab', 0: 'def', 1: 'c'}
+    #
+    BEnum = new_enum_type("bar", ('ab', 'cd'), (5, 5))
+    assert BEnum.elements == {5: 'ab'}
+    assert BEnum.relements == {'ab': 5, 'cd': 5}
 
 def test_cast_to_enum():
     BEnum = new_enum_type("foo", ('def', 'c', 'ab'), (0, 1, -20))
diff --git a/pypy/module/math/test/test_direct.py 
b/pypy/module/math/test/test_direct.py
--- a/pypy/module/math/test/test_direct.py
+++ b/pypy/module/math/test/test_direct.py
@@ -72,9 +72,10 @@
         ('exp', (9999.9,), OverflowError),
         ('pow', (10.0, 40000.0), OverflowError),
         ('ldexp', (10.0, 40000), OverflowError),
-        ('log', (0.0,), ValueError),
+        ('log', (0.0,), ValueError), #cpython does it this way
+        ('log1p', (-1.0,), OverflowError),
         ('log', (-1.,), ValueError),
-        ('log10', (0.0,), ValueError),
+        ('log10', (0.0,), ValueError), #cpython does it this way
         ]
 
     INFCASES = [
diff --git a/pypy/module/math/test/test_math.py 
b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -17,7 +17,8 @@
             if type(expected) is type and issubclass(expected, Exception):
                 expected = getattr(space, "w_%s" % expected.__name__)
             elif callable(expected):
-                expected = cls.make_callable_wrapper(expected)
+                if not cls.runappdirect:
+                    expected = cls.make_callable_wrapper(expected)
             else:
                 expected = space.wrap(expected)
             cases.append(space.newtuple([space.wrap(a), space.wrap(b), 
expected]))
diff --git a/pypy/module/micronumpy/test/test_complex.py 
b/pypy/module/micronumpy/test/test_complex.py
--- a/pypy/module/micronumpy/test/test_complex.py
+++ b/pypy/module/micronumpy/test/test_complex.py
@@ -94,30 +94,42 @@
         cls.w_testcases128 = cls.space.wrap(list(parse_testfile(fname128)))
         cls.w_testcases64 = cls.space.wrap(list(parse_testfile(fname64)))
 
-        def cls_c_pow(space, args_w):
-            try:
-                retVal = c_pow(*map(space.unwrap, args_w))
-                return space.wrap(retVal)
-            except ValueError, e:
-                if option.runappdirect:
-                    raise
-                raise OperationError(cls.space.w_ValueError,
-                        cls.space.wrap(e.message))
-        cls.w_c_pow = cls.space.wrap(interp2app(cls_c_pow))
         cls.w_runAppDirect = cls.space.wrap(option.runappdirect)
         cls.w_isWindows = cls.space.wrap(os.name == 'nt')
 
-        def cls_rAlmostEqual(space, __args__):
-            args, kwargs = __args__.unpack()
-            args = map(space.unwrap, args)
-            kwargs = dict([
-                (k, space.unwrap(v))
-                for k, v in kwargs.iteritems()
-            ])
-            if '__pypy__' not in sys.builtin_module_names:
-                kwargs['isnumpy'] = True
-            return space.wrap(rAlmostEqual(*args, **kwargs))
-        cls.w_rAlmostEqual = cls.space.wrap(interp2app(cls_rAlmostEqual))
+        if cls.runappdirect:
+            def cls_rAlmostEqual(space, *args, **kwargs):
+                return rAlmostEqual(*args, **kwargs)
+            cls.w_rAlmostEqual = cls.space.wrap(cls_rAlmostEqual)
+            def cls_c_pow(space, *args):
+                return c_pow(*args)
+            cls.w_c_pow = cls.space.wrap(cls_c_pow)
+        else:
+            def cls_rAlmostEqual(space, __args__):
+                args, kwargs = __args__.unpack()
+                args = map(space.unwrap, args)
+                kwargs = dict([
+                    (k, space.unwrap(v))
+                    for k, v in kwargs.iteritems()
+                ])
+                if '__pypy__' not in sys.builtin_module_names:
+                    kwargs['isnumpy'] = True
+                return space.wrap(rAlmostEqual(*args, **kwargs))
+            cls.w_rAlmostEqual = cls.space.wrap(interp2app(cls_rAlmostEqual))
+            def cls_c_pow(space, args_w):
+                try:
+                    retVal = c_pow(*map(space.unwrap, args_w))
+                    return space.wrap(retVal)
+                except ZeroDivisionError, e:
+                    raise OperationError(cls.space.w_ZeroDivisionError,
+                            cls.space.wrap(e.message))
+                except OverflowError, e:
+                    raise OperationError(cls.space.w_OverflowError,
+                            cls.space.wrap(e.message))
+                except ValueError, e:
+                    raise OperationError(cls.space.w_ValueError,
+                            cls.space.wrap(e.message))
+            cls.w_c_pow = cls.space.wrap(interp2app(cls_c_pow))
 
     def test_fabs(self):
         from _numpypy import fabs, complex128
diff --git a/pypy/module/select/test/test_select.py 
b/pypy/module/select/test/test_select.py
--- a/pypy/module/select/test/test_select.py
+++ b/pypy/module/select/test/test_select.py
@@ -272,6 +272,7 @@
         w_import = space.getattr(space.builtin, space.wrap("__import__"))
         w_socketmod = space.call_function(w_import, space.wrap("socket"))
         cls.w_sock = cls.space.call_method(w_socketmod, "socket")
+        cls.w_sock_err = space.getattr(w_socketmod, space.wrap("error"))
 
         try_ports = [1023] + range(20000, 30000, 437)
         for port in try_ports:
@@ -279,11 +280,8 @@
             cls.w_sockaddress = space.wrap(('127.0.0.1', port))
             try:
                 space.call_method(cls.w_sock, "bind", cls.w_sockaddress)
-                print 'works'
                 break
-            except OperationError, e:   # should get a "Permission denied"
-                if not e.match(space, space.getattr(w_socketmod, 
space.wrap("error"))):
-                    raise
+            except cls.w_sock_err, e:   # should get a "Permission denied"
                 print e
             else:
                 raise e
diff --git a/pypy/rlib/rfloat.py b/pypy/rlib/rfloat.py
--- a/pypy/rlib/rfloat.py
+++ b/pypy/rlib/rfloat.py
@@ -170,13 +170,17 @@
     result = formatd(value, tp, precision, flags)
     return result, special
 
-def round_double(value, ndigits):
+def round_double(value, ndigits, half_even=False):
+    """Round a float half away from zero.
+
+    Specify half_even=True to round half even instead.
+    """
     if USE_SHORT_FLOAT_REPR:
-        return round_double_short_repr(value, ndigits)
+        return round_double_short_repr(value, ndigits, half_even)
     else:
-        return round_double_fallback_repr(value, ndigits)
+        return round_double_fallback_repr(value, ndigits, half_even)
 
-def round_double_short_repr(value, ndigits):
+def round_double_short_repr(value, ndigits, half_even):
     # The basic idea is very simple: convert and round the double to
     # a decimal string using _Py_dg_dtoa, then convert that decimal
     # string back to a double with _Py_dg_strtod.  There's one minor
@@ -209,7 +213,7 @@
 
     # determine whether this is a halfway case.
     halfway_case = 0
-    if expo == -ndigits - 1:
+    if not half_even and expo == -ndigits - 1:
         if ndigits >= 0:
             halfway_case = 1
         elif ndigits >= -22:
@@ -224,7 +228,7 @@
     # round to a decimal string; use an extra place for halfway case
     strvalue = formatd(value, 'f', ndigits + halfway_case)
 
-    if halfway_case:
+    if not half_even and halfway_case:
         buf = [c for c in strvalue]
         if ndigits >= 0:
             endpos = len(buf) - 1
@@ -263,7 +267,7 @@
 
 # fallback version, to be used when correctly rounded
 # binary<->decimal conversions aren't available
-def round_double_fallback_repr(value, ndigits):
+def round_double_fallback_repr(value, ndigits, half_even):
     if ndigits >= 0:
         if ndigits > 22:
             # pow1 and pow2 are each safe from overflow, but
@@ -284,12 +288,17 @@
         pow2 = 1.0 # unused; for translation
         y = value / pow1
 
-    if y >= 0.0:
-        z = math.floor(y + 0.5)
+    if half_even:
+        z = round_away(y)
+        if math.fabs(y - z) == 0.5:
+            z = 2.0 * round_away(y / 2.0)
     else:
-        z = math.ceil(y - 0.5)
-    if math.fabs(y-z) == 1.0:   # obscure case, see the test
-        z = y
+        if y >= 0.0:
+            z = math.floor(y + 0.5)
+        else:
+            z = math.ceil(y - 0.5)
+        if math.fabs(y - z) == 1.0:   # obscure case, see the test
+            z = y
 
     if ndigits >= 0:
         z = (z / pow2) / pow1
diff --git a/pypy/rpython/lltypesystem/module/ll_math.py 
b/pypy/rpython/lltypesystem/module/ll_math.py
--- a/pypy/rpython/lltypesystem/module/ll_math.py
+++ b/pypy/rpython/lltypesystem/module/ll_math.py
@@ -368,6 +368,8 @@
     if x == 0.0:
         return x      # returns 0.0 or -0.0
     if x <= -1.0:
+        if x == -1:
+            raise OverflowError("math range  error")
         raise ValueError("math domain error")
     return math_log1p(x)
 
diff --git a/pypy/rpython/test/test_rfloat.py b/pypy/rpython/test/test_rfloat.py
--- a/pypy/rpython/test/test_rfloat.py
+++ b/pypy/rpython/test/test_rfloat.py
@@ -215,6 +215,174 @@
             # https://bugzilla.novell.com/show_bug.cgi?id=692493
             assert not self.interpret(fn, [1e200, 1e200]) # nan
 
+    def test_break_up_float(self):
+        from pypy.rlib.rfloat import break_up_float
+        assert break_up_float('1') == ('', '1', '', '')
+        assert break_up_float('+1') == ('+', '1', '', '')
+        assert break_up_float('-1') == ('-', '1', '', '')
+
+        assert break_up_float('.5') == ('', '', '5', '')
+
+        assert break_up_float('1.2e3') == ('', '1', '2', '3')
+        assert break_up_float('1.2e+3') == ('', '1', '2', '+3')
+        assert break_up_float('1.2e-3') == ('', '1', '2', '-3')
+
+        # some that will get thrown out on return:
+        assert break_up_float('.') == ('', '', '', '')
+        assert break_up_float('+') == ('+', '', '', '')
+        assert break_up_float('-') == ('-', '', '', '')
+        assert break_up_float('e1') == ('', '', '', '1')
+
+        raises(ValueError, break_up_float, 'e')
+
+    def test_formatd(self):
+        from pypy.rlib.rfloat import formatd
+        def f(x):
+            return formatd(x, 'f', 2, 0)
+        res = self.ll_to_string(self.interpret(f, [10/3.0]))
+        assert res == '3.33'
+
+    def test_formatd_repr(self):
+        from pypy.rlib.rfloat import formatd
+        def f(x):
+            return formatd(x, 'r', 0, 0)
+        res = self.ll_to_string(self.interpret(f, [1.1]))
+        assert res == '1.1'
+
+    def test_formatd_huge(self):
+        from pypy.rlib.rfloat import formatd
+        def f(x):
+            return formatd(x, 'f', 1234, 0)
+        res = self.ll_to_string(self.interpret(f, [1.0]))
+        assert res == '1.' + 1234 * '0'
+
+    def test_formatd_F(self):
+        from pypy.translator.c.test.test_genc import compile
+        from pypy.rlib.rfloat import formatd
+
+        def func(x):
+            # Test the %F format, which is not supported by
+            # the Microsoft's msvcrt library.
+            return formatd(x, 'F', 4)
+
+        f = compile(func, [float])
+        assert f(10/3.0) == '3.3333'
+
+    def test_parts_to_float(self):
+        from pypy.rlib.rfloat import parts_to_float, break_up_float
+        def f(x):
+            if x == 0:
+                s = '1.0'
+            else:
+                s = '1e-100'
+            sign, beforept, afterpt, expt = break_up_float(s)
+            return parts_to_float(sign, beforept, afterpt, expt)
+        res = self.interpret(f, [0])
+        assert res == 1.0
+
+        res = self.interpret(f, [1])
+        assert res == 1e-100
+
+    def test_string_to_float(self):
+        from pypy.rlib.rfloat import rstring_to_float
+        def func(x):
+            if x == 0:
+                s = '1e23'
+            else:
+                s = '-1e23'
+            return rstring_to_float(s)
+
+        assert self.interpret(func, [0]) == 1e23
+        assert self.interpret(func, [1]) == -1e23
+
+    def test_copysign(self):
+        from pypy.rlib.rfloat import copysign
+        assert copysign(1, 1) == 1
+        assert copysign(-1, 1) == 1
+        assert copysign(-1, -1) == -1
+        assert copysign(1, -1) == -1
+        assert copysign(1, -0.) == -1
+
+    def test_round_away(self):
+        from pypy.rlib.rfloat import round_away
+        assert round_away(.1) == 0.
+        assert round_away(.5) == 1.
+        assert round_away(.7) == 1.
+        assert round_away(1.) == 1.
+        assert round_away(-.5) == -1.
+        assert round_away(-.1) == 0.
+        assert round_away(-.7) == -1.
+        assert round_away(0.) == 0.
+
+    def test_round_double(self):
+        from pypy.rlib.rfloat import round_double
+        def almost_equal(x, y):
+            assert round(abs(x-y), 7) == 0
+
+        almost_equal(round_double(0.125, 2), 0.13)
+        almost_equal(round_double(0.375, 2), 0.38)
+        almost_equal(round_double(0.625, 2), 0.63)
+        almost_equal(round_double(0.875, 2), 0.88)
+        almost_equal(round_double(-0.125, 2), -0.13)
+        almost_equal(round_double(-0.375, 2), -0.38)
+        almost_equal(round_double(-0.625, 2), -0.63)
+        almost_equal(round_double(-0.875, 2), -0.88)
+
+        almost_equal(round_double(0.25, 1), 0.3)
+        almost_equal(round_double(0.75, 1), 0.8)
+        almost_equal(round_double(-0.25, 1), -0.3)
+        almost_equal(round_double(-0.75, 1), -0.8)
+
+        round_double(-6.5, 0) == -7.0
+        round_double(-5.5, 0) == -6.0
+        round_double(-1.5, 0) == -2.0
+        round_double(-0.5, 0) == -1.0
+        round_double(0.5, 0) == 1.0
+        round_double(1.5, 0) == 2.0
+        round_double(2.5, 0) == 3.0
+        round_double(3.5, 0) == 4.0
+        round_double(4.5, 0) == 5.0
+        round_double(5.5, 0) == 6.0
+        round_double(6.5, 0) == 7.0
+
+        round_double(-25.0, -1) == -30.0
+        round_double(-15.0, -1) == -20.0
+        round_double(-5.0, -1) == -10.0
+        round_double(5.0, -1) == 10.0
+        round_double(15.0, -1) == 20.0
+        round_double(25.0, -1) == 30.0
+        round_double(35.0, -1) == 40.0
+        round_double(45.0, -1) == 50.0
+        round_double(55.0, -1) == 60.0
+        round_double(65.0, -1) == 70.0
+        round_double(75.0, -1) == 80.0
+        round_double(85.0, -1) == 90.0
+        round_double(95.0, -1) == 100.0
+        round_double(12325.0, -1) == 12330.0
+
+        round_double(350.0, -2) == 400.0
+        round_double(450.0, -2) == 500.0
+
+        almost_equal(round_double(0.5e21, -21), 1e21)
+        almost_equal(round_double(1.5e21, -21), 2e21)
+        almost_equal(round_double(2.5e21, -21), 3e21)
+        almost_equal(round_double(5.5e21, -21), 6e21)
+        almost_equal(round_double(8.5e21, -21), 9e21)
+
+        almost_equal(round_double(-1.5e22, -22), -2e22)
+        almost_equal(round_double(-0.5e22, -22), -1e22)
+        almost_equal(round_double(0.5e22, -22), 1e22)
+        almost_equal(round_double(1.5e22, -22), 2e22)
+
+    def test_round_half_even(self):
+        from pypy.rlib import rfloat
+        for func in (rfloat.round_double_short_repr,
+                     rfloat.round_double_fallback_repr):
+            # 2.x behavior
+            assert func(2.5, 0, False) == 3.0
+            # 3.x behavior
+            assert func(2.5, 0, True) == 2.0
+
 
 class TestLLtype(BaseTestRfloat, LLRtypeMixin):
 
@@ -226,4 +394,15 @@
 
 
 class TestOOtype(BaseTestRfloat, OORtypeMixin):
-    pass
+
+    def test_formatd(self):
+        skip('formatd is broken on ootype')
+
+    def test_formatd_repr(self):
+        skip('formatd is broken on ootype')
+
+    def test_formatd_huge(self):
+        skip('formatd is broken on ootype')
+
+    def test_string_to_float(self):
+        skip('string_to_float is broken on ootype')
diff --git a/pypy/translator/platform/linux.py 
b/pypy/translator/platform/linux.py
--- a/pypy/translator/platform/linux.py
+++ b/pypy/translator/platform/linux.py
@@ -13,7 +13,8 @@
                  + os.environ.get('LDFLAGS', '').split())
     extra_libs = ('-lrt',)
     cflags = tuple(
-             ['-O3', '-pthread', '-fomit-frame-pointer',
+             ['-Os',   # more compact and actually a bit faster
+              '-pthread', '-fomit-frame-pointer',
               '-Wall', '-Wno-unused']
              + os.environ.get('CFLAGS', '').split())
     standalone_only = ()
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to