Author: Alex Gaynor <[email protected]>
Branch:
Changeset: r61268:71233ccceb29
Date: 2013-02-15 08:18 -0800
http://bitbucket.org/pypy/pypy/changeset/71233ccceb29/
Log: merged upstream
diff --git a/pypy/interpreter/test2/test_app_main.py
b/pypy/interpreter/test2/test_app_main.py
--- a/pypy/interpreter/test2/test_app_main.py
+++ b/pypy/interpreter/test2/test_app_main.py
@@ -208,11 +208,6 @@
These tests require pexpect (UNIX-only).
http://pexpect.sourceforge.net/
"""
- def setup_class(cls):
- # some tests need to be able to import test2, change the cwd
- goal_dir =
os.path.abspath(os.path.join(os.path.realpath(os.path.dirname(__file__)), '..'))
- os.chdir(goal_dir)
-
def _spawn(self, *args, **kwds):
try:
import pexpect
@@ -456,13 +451,14 @@
child.expect('789') # expect to see it before the timeout hits
child.sendline('X')
- def test_options_i_m(self):
+ def test_options_i_m(self, monkeypatch):
if sys.platform == "win32":
skip("close_fds is not supported on Windows platforms")
if not hasattr(runpy, '_run_module_as_main'):
skip("requires CPython >= 2.6")
p = os.path.join(os.path.realpath(os.path.dirname(__file__)),
'mymodule.py')
p = os.path.abspath(p)
+ monkeypatch.chdir(os.path.dirname(app_main))
child = self.spawn(['-i',
'-m', 'test2.mymodule',
'extra'])
@@ -562,12 +558,13 @@
child.sendline('Not at all. They could be carried.')
child.expect('A five ounce bird could not carry a one pound coconut.')
- def test_no_space_before_argument(self):
+ def test_no_space_before_argument(self, monkeypatch):
if not hasattr(runpy, '_run_module_as_main'):
skip("requires CPython >= 2.6")
child = self.spawn(['-cprint "hel" + "lo"'])
child.expect('hello')
+ monkeypatch.chdir(os.path.dirname(app_main))
child = self.spawn(['-mtest2.mymodule'])
child.expect('mymodule running')
@@ -667,11 +664,12 @@
'-c "import sys; print sys.warnoptions"')
assert "['ignore', 'default', 'once', 'error']" in data
- def test_option_m(self):
+ def test_option_m(self, monkeypatch):
if not hasattr(runpy, '_run_module_as_main'):
skip("requires CPython >= 2.6")
p = os.path.join(os.path.realpath(os.path.dirname(__file__)),
'mymodule.py')
p = os.path.abspath(p)
+ monkeypatch.chdir(os.path.dirname(app_main))
data = self.run('-m test2.mymodule extra')
assert 'mymodule running' in data
assert 'Name: __main__' in data
diff --git a/pypy/module/micronumpy/test/test_module.py
b/pypy/module/micronumpy/test/test_module.py
--- a/pypy/module/micronumpy/test/test_module.py
+++ b/pypy/module/micronumpy/test/test_module.py
@@ -13,11 +13,13 @@
assert sum(array(range(10))) == 45
def test_min(self):
- from _numpypy import array, min
+ from _numpypy import array, min, zeros
assert min(range(10)) == 0
assert min(array(range(10))) == 0
+ assert list(min(zeros((0, 2)), axis=1)) == []
def test_max(self):
- from _numpypy import array, max
+ from _numpypy import array, max, zeros
assert max(range(10)) == 9
assert max(array(range(10))) == 9
+ assert list(max(zeros((0, 2)), axis=1)) == []
diff --git a/pypy/module/micronumpy/test/test_numarray.py
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1662,13 +1662,13 @@
b = array([0, 1, 2], dtype=complex).astype(bool)
assert (b == [False, True, True]).all()
assert b.dtype == 'bool'
-
+
a = arange(6, dtype='f4').reshape(2,3)
b = a.astype('i4')
a = array('x').astype('S3').dtype
assert a.itemsize == 3
-
+
def test_base(self):
from _numpypy import array
assert array(1).base is None
@@ -1679,6 +1679,11 @@
def test_byteswap(self):
from _numpypy import array
+
+ s1 = array(1.).byteswap().tostring()
+ s2 = array([1.]).byteswap().tostring()
+ assert s1 == s2
+
a = array([1, 256 + 2, 3], dtype='i2')
assert (a.byteswap() == [0x0100, 0x0201, 0x0300]).all()
assert (a == [1, 256 + 2, 3]).all()
@@ -1686,39 +1691,40 @@
assert (a == [0x0100, 0x0201, 0x0300]).all()
a = array([1, -1, 1e300], dtype=float)
- s1 = map(ord,a.tostring())
+ s1 = map(ord, a.tostring())
s2 = map(ord, a.byteswap().tostring())
- assert s1[7::-1] == s2[:8]
- assert s1[15:7:-1] == s2[8:16]
- assert s1[:15:-1] == s2[16:]
+ assert a.dtype.itemsize == 8
+ for i in range(a.size):
+ i1 = i * a.dtype.itemsize
+ i2 = (i+1) * a.dtype.itemsize
+ assert list(reversed(s1[i1:i2])) == s2[i1:i2]
a = array([1+1e30j, -1, 1e10], dtype=complex)
- s1 = map(ord,a.tostring())
+ s1 = map(ord, a.tostring())
s2 = map(ord, a.byteswap().tostring())
- assert s1[7::-1] == s2[:8]
- assert s1[15:7:-1] == s2[8:16]
- assert s1[23:15:-1] == s2[16:24]
- assert s1[31:23:-1] == s2[24:32]
- assert s1[39:31:-1] == s2[32:40]
- assert s1[:39:-1] == s2[40:]
+ assert a.dtype.itemsize == 16
+ for i in range(a.size*2):
+ i1 = i * a.dtype.itemsize/2
+ i2 = (i+1) * a.dtype.itemsize/2
+ assert list(reversed(s1[i1:i2])) == s2[i1:i2]
a = array([3.14, -1.5, 10000], dtype='float16')
- s1 = map(ord,a.tostring())
+ s1 = map(ord, a.tostring())
s2 = map(ord, a.byteswap().tostring())
- s3 = [s1[1], s1[0],s1[3], s1[2], s1[5], s1[4]]
- assert s3 == s2
+ assert a.dtype.itemsize == 2
+ for i in range(a.size):
+ i1 = i * a.dtype.itemsize
+ i2 = (i+1) * a.dtype.itemsize
+ assert list(reversed(s1[i1:i2])) == s2[i1:i2]
a = array([1, -1, 10000], dtype='longfloat')
- s1 = map(ord,a.tostring())
- s2 = map(ord, a.byteswap().tostring())
- n = a.dtype.itemsize
- assert s1[n-1] == s2[0]
-
- a = array(0., dtype='longfloat')
s1 = map(ord, a.tostring())
s2 = map(ord, a.byteswap().tostring())
- n = a.dtype.itemsize
- assert s1[n-1] == s2[0]
+ assert a.dtype.itemsize >= 8
+ for i in range(a.size):
+ i1 = i * a.dtype.itemsize
+ i2 = (i+1) * a.dtype.itemsize
+ assert list(reversed(s1[i1:i2])) == s2[i1:i2]
def test_clip(self):
from _numpypy import array
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -14,8 +14,8 @@
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.rstruct.runpack import runpack
from rpython.rlib.rstruct.nativefmttable import native_is_bigendian
-from rpython.rlib.rstruct.ieee import (float_pack, float_unpack, pack_float80,
- unpack_float, unpack_float128)
+from rpython.rlib.rstruct.ieee import (float_pack, float_unpack, unpack_float,
+ pack_float80, unpack_float80)
from rpython.tool.sourcetools import func_with_new_name
from rpython.rlib import jit
from rpython.rlib.rstring import StringBuilder
@@ -958,7 +958,6 @@
swapped_value = byteswap(rffi.cast(self.T, value))
raw_storage_setitem(storage, i + offset, swapped_value)
-
class Float32(BaseType, Float):
_attrs_ = ()
@@ -1505,7 +1504,6 @@
BoxType = interp_boxes.W_Complex64Box
ComponentBoxType = interp_boxes.W_Float32Box
-
NonNativeComplex64 = Complex64
class Complex128(ComplexFloating, BaseType):
@@ -1515,7 +1513,6 @@
BoxType = interp_boxes.W_Complex128Box
ComponentBoxType = interp_boxes.W_Float64Box
-
NonNativeComplex128 = Complex128
if interp_boxes.long_double_size == 12:
@@ -1528,17 +1525,16 @@
def runpack_str(self, s):
assert len(s) == 12
- fval = unpack_float128(s, native_is_bigendian)
+ fval = unpack_float80(s, native_is_bigendian)
return self.box(fval)
def byteswap(self, w_v):
value = self.unbox(w_v)
result = StringBuilder(12)
- pack_float80(result, value, 12, not native_is_bigendian)
- return self.box(unpack_float128(result.build(),
native_is_bigendian))
+ pack_float80(result, value, not native_is_bigendian)
+ return self.box(unpack_float80(result.build(),
native_is_bigendian))
- class NonNativeFloat96(Float96):
- pass
+ NonNativeFloat96 = Float96
class Complex192(ComplexFloating, BaseType):
_attrs_ = ()
@@ -1549,7 +1545,6 @@
NonNativeComplex192 = Complex192
-
elif interp_boxes.long_double_size == 16:
class Float128(BaseType, Float):
_attrs_ = ()
@@ -1560,14 +1555,14 @@
def runpack_str(self, s):
assert len(s) == 16
- fval = unpack_float128(s, native_is_bigendian)
+ fval = unpack_float80(s, native_is_bigendian)
return self.box(fval)
def byteswap(self, w_v):
value = self.unbox(w_v)
result = StringBuilder(16)
- pack_float80(result, value, 16, not native_is_bigendian)
- return self.box(unpack_float128(result.build(),
native_is_bigendian))
+ pack_float80(result, value, not native_is_bigendian)
+ return self.box(unpack_float80(result.build(),
native_is_bigendian))
NonNativeFloat128 = Float128
@@ -1578,7 +1573,6 @@
BoxType = interp_boxes.W_Complex256Box
ComponentBoxType = interp_boxes.W_Float128Box
-
NonNativeComplex256 = Complex256
class BaseStringType(object):
diff --git a/pypy/module/sys/initpath.py b/pypy/module/sys/initpath.py
--- a/pypy/module/sys/initpath.py
+++ b/pypy/module/sys/initpath.py
@@ -67,6 +67,8 @@
stdlib.
If it cannot be found, return (None, None).
"""
+ if executable == '':
+ return None, None
search = executable
while True:
dirname = resolvedirof(search)
diff --git a/pypy/module/sys/test/test_initpath.py
b/pypy/module/sys/test/test_initpath.py
--- a/pypy/module/sys/test/test_initpath.py
+++ b/pypy/module/sys/test/test_initpath.py
@@ -10,12 +10,15 @@
b = prefix.join('lib-python', dirname).ensure(dir=1)
return a, b
-def test_find_stdlib(tmpdir):
+def test_find_stdlib(tmpdir, monkeypatch):
bin_dir = tmpdir.join('bin').ensure(dir=True)
pypy = bin_dir.join('pypy').ensure(file=True)
build_hierarchy(tmpdir)
path, prefix = find_stdlib(None, str(pypy))
assert prefix == tmpdir
+ # shouldn't find stdlib if executable == '' even if parent dir has a stdlib
+ monkeypatch.chdir(tmpdir.join('bin'))
+ assert find_stdlib(None, '') == (None, None)
@py.test.mark.skipif('not hasattr(os, "symlink")')
def test_find_stdlib_follow_symlink(tmpdir):
@@ -84,6 +87,7 @@
assert find_executable('pypy') == a.join('pypy.exe')
def test_resolvedirof(tmpdir):
+ assert resolvedirof('') == os.path.abspath(os.path.join(os.getcwd(), '..'))
foo = tmpdir.join('foo').ensure(dir=True)
bar = tmpdir.join('bar').ensure(dir=True)
myfile = foo.join('myfile').ensure(file=True)
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
@@ -127,7 +127,7 @@
assert reshape(a, (1, -1)).shape == (1, 105)
assert reshape(a, (1, 1, -1)).shape == (1, 1, 105)
assert reshape(a, (-1, 1, 1)).shape == (105, 1, 1)
-
+
def test_transpose(self):
from numpypy import arange, array, transpose, ones
x = arange(4).reshape((2,2))
@@ -136,7 +136,7 @@
raises(NotImplementedError, "transpose(x, axes=(1, 0, 2))")
# x = ones((1, 2, 3))
# assert transpose(x, (1, 0, 2)).shape == (2, 1, 3)
-
+
def test_fromnumeric(self):
from numpypy import array, swapaxes
x = array([[1,2,3]])
diff --git a/pypy/module/test_lib_pypy/numpypy/test_numpy.py
b/pypy/module/test_lib_pypy/numpypy/test_numpy.py
--- a/pypy/module/test_lib_pypy/numpypy/test_numpy.py
+++ b/pypy/module/test_lib_pypy/numpypy/test_numpy.py
@@ -8,3 +8,11 @@
pass
import numpypy
import numpy # works after 'numpypy' has been imported
+
+ def test_min_max_after_import(self):
+ from numpypy import *
+ assert min(1, 100) == 1
+ assert min(100, 1) == 1
+
+ assert max(1, 100) == 100
+ assert max(100, 1) == 100
diff --git a/pypy/module/test_lib_pypy/test_datetime.py
b/pypy/module/test_lib_pypy/test_datetime.py
--- a/pypy/module/test_lib_pypy/test_datetime.py
+++ b/pypy/module/test_lib_pypy/test_datetime.py
@@ -66,9 +66,12 @@
"""
import os
import time
+ if os.name == 'nt':
+ skip("setting os.environ['TZ'] ineffective on windows")
try:
prev_tz = os.environ.get("TZ")
os.environ["TZ"] = "GMT"
+ time.tzset()
for unused in xrange(100):
now = time.time()
delta = (datetime.datetime.utcfromtimestamp(now) -
@@ -79,6 +82,7 @@
del os.environ["TZ"]
else:
os.environ["TZ"] = prev_tz
+ time.tzset()
def test_utcfromtimestamp_microsecond():
dt = datetime.datetime.utcfromtimestamp(0)
diff --git a/rpython/rlib/rarithmetic.py b/rpython/rlib/rarithmetic.py
--- a/rpython/rlib/rarithmetic.py
+++ b/rpython/rlib/rarithmetic.py
@@ -630,21 +630,16 @@
uint2singlefloat, singlefloat2uint
T = lltype.typeOf(arg)
- is_float = False
- is_single_float = False
if T == lltype.SingleFloat:
- T = rffi.UINT
- is_single_float = True
arg = singlefloat2uint(arg)
elif T == lltype.Float:
- is_float = True
- T = rffi.LONGLONG
arg = float2longlong(arg)
elif T == lltype.LongFloat:
assert False
else:
# we cannot do arithmetics on small ints
arg = widen(arg)
+
if rffi.sizeof(T) == 1:
res = arg
elif rffi.sizeof(T) == 2:
@@ -667,9 +662,9 @@
(f >> 24) | (g >> 40) | (h >> 56))
else:
assert False # unreachable code
- if is_single_float:
+
+ if T == lltype.SingleFloat:
return uint2singlefloat(rffi.cast(rffi.UINT, res))
- if is_float:
- res = rffi.cast(rffi.LONGLONG, res)
- return longlong2float(res)
+ if T == lltype.Float:
+ return longlong2float(rffi.cast(rffi.LONGLONG, res))
return rffi.cast(T, res)
diff --git a/rpython/rlib/rstruct/ieee.py b/rpython/rlib/rstruct/ieee.py
--- a/rpython/rlib/rstruct/ieee.py
+++ b/rpython/rlib/rstruct/ieee.py
@@ -235,12 +235,12 @@
result.append("".join(l))
@jit.unroll_safe
-def pack_float80(result, x, size, be):
+def pack_float80(result, x, be):
l = []
unsigned = float_pack80(x)
for i in range(8):
l.append(chr((unsigned[0] >> (i * 8)) & 0xFF))
- for i in range(size - 8):
+ for i in range(2):
l.append(chr((unsigned[1] >> (i * 8)) & 0xFF))
if be:
l.reverse()
@@ -253,12 +253,14 @@
unsigned |= r_ulonglong(c) << (i * 8)
return float_unpack(unsigned, len(s))
-def unpack_float128(s, be):
+def unpack_float80(s, be):
+ if len(s) != 10:
+ raise ValueError
QQ = [r_ulonglong(0), r_ulonglong(0)]
for i in range(8):
- c = ord(s[len(s) - 1 - i if be else i])
+ c = ord(s[9 - i if be else i])
QQ[0] |= r_ulonglong(c) << (i * 8)
- for i in range(8, len(s)):
- c = ord(s[len(s) - 1 - i if be else i])
+ for i in range(8, 10):
+ c = ord(s[9 - i if be else i])
QQ[1] |= r_ulonglong(c) << ((i - 8) * 8)
return float_unpack80(QQ)
diff --git a/rpython/rlib/rstruct/runpack.py b/rpython/rlib/rstruct/runpack.py
--- a/rpython/rlib/rstruct/runpack.py
+++ b/rpython/rlib/rstruct/runpack.py
@@ -46,7 +46,7 @@
def __init__(self, fmt):
self.formats = []
self.fmt = fmt
-
+
def operate(self, fmtdesc, repetitions):
if fmtdesc.needcount:
self.formats.append((fmtdesc, repetitions, None))
@@ -110,5 +110,3 @@
unpacker = create_unpacker(fmt)
return unpacker.unpack(input)
runpack._annspecialcase_ = 'specialize:arg(0)'
-
-
diff --git a/rpython/rlib/rstruct/test/test_ieee.py
b/rpython/rlib/rstruct/test/test_ieee.py
--- a/rpython/rlib/rstruct/test/test_ieee.py
+++ b/rpython/rlib/rstruct/test/test_ieee.py
@@ -1,9 +1,12 @@
-import py, sys
+import py
+import sys
import random
import struct
-from rpython.rlib.rfloat import isnan
-from rpython.rlib.rstruct.ieee import float_pack, float_unpack, float_pack80,
float_unpack80
+from rpython.rlib.rstruct import ieee
+from rpython.rlib.rfloat import isnan, NAN, INFINITY
+from rpython.translator.c.test.test_genc import compile
+
class TestFloatPacking:
def setup_class(cls):
@@ -12,17 +15,29 @@
def check_float(self, x):
# check roundtrip
- Q = float_pack(x, 8)
- y = float_unpack(Q, 8)
- assert repr(x) == repr(y)
+ Q = ieee.float_pack(x, 8)
+ y = ieee.float_unpack(Q, 8)
+ assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)
- Q = float_pack80(x)
- y = float_unpack80(Q)
- assert repr(x) == repr(y),'%r != %r, Q=%r'%(x, y, Q)
+ Q = ieee.float_pack80(x)
+ y = ieee.float_unpack80(Q)
+ assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)
+
+ Q = []
+ ieee.pack_float(Q, x, 8, False)
+ Q = Q[0]
+ y = ieee.unpack_float(Q, False)
+ assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)
+
+ Q = []
+ ieee.pack_float80(Q, x, False)
+ Q = Q[0]
+ y = ieee.unpack_float80(Q, False)
+ assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)
# check that packing agrees with the struct module
struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0]
- float_pack8 = float_pack(x, 8)
+ float_pack8 = ieee.float_pack(x, 8)
assert struct_pack8 == float_pack8
# check that packing agrees with the struct module
@@ -31,7 +46,7 @@
except OverflowError:
struct_pack4 = "overflow"
try:
- float_pack4 = float_pack(x, 4)
+ float_pack4 = ieee.float_pack(x, 4)
except OverflowError:
float_pack4 = "overflow"
assert struct_pack4 == float_pack4
@@ -40,16 +55,16 @@
return
# if we didn't overflow, try round-tripping the binary32 value
- roundtrip = float_pack(float_unpack(float_pack4, 4), 4)
+ roundtrip = ieee.float_pack(ieee.float_unpack(float_pack4, 4), 4)
assert float_pack4 == roundtrip
try:
- float_pack2 = float_pack(x, 2)
+ float_pack2 = ieee.float_pack(x, 2)
except OverflowError:
return
- roundtrip = float_pack(float_unpack(float_pack2, 2), 2)
- assert (float_pack2,x) == (roundtrip,x)
+ roundtrip = ieee.float_pack(ieee.float_unpack(float_pack2, 2), 2)
+ assert (float_pack2, x) == (roundtrip, x)
def test_infinities(self):
self.check_float(float('inf'))
@@ -61,23 +76,23 @@
def test_check_size(self):
# these were refactored into separate pack80/unpack80 functions
- py.test.raises(ValueError, float_pack, 1.0, 12)
- py.test.raises(ValueError, float_pack, 1.0, 16)
- py.test.raises(ValueError, float_unpack, 1, 12)
- py.test.raises(ValueError, float_unpack, 1, 16)
+ py.test.raises(ValueError, ieee.float_pack, 1.0, 12)
+ py.test.raises(ValueError, ieee.float_pack, 1.0, 16)
+ py.test.raises(ValueError, ieee.float_unpack, 1, 12)
+ py.test.raises(ValueError, ieee.float_unpack, 1, 16)
def test_nans(self):
- Q = float_pack80(float('nan'))
- y = float_unpack80(Q)
+ Q = ieee.float_pack80(float('nan'))
+ y = ieee.float_unpack80(Q)
assert repr(y) == 'nan'
- Q = float_pack(float('nan'), 8)
- y = float_unpack(Q, 8)
+ Q = ieee.float_pack(float('nan'), 8)
+ y = ieee.float_unpack(Q, 8)
assert repr(y) == 'nan'
- L = float_pack(float('nan'), 4)
- z = float_unpack(L, 4)
+ L = ieee.float_pack(float('nan'), 4)
+ z = ieee.float_unpack(L, 4)
assert repr(z) == 'nan'
- L = float_pack(float('nan'), 2)
- z = float_unpack(L, 2)
+ L = ieee.float_pack(float('nan'), 2)
+ z = ieee.float_unpack(L, 2)
assert repr(z) == 'nan'
def test_simple(self):
@@ -138,22 +153,22 @@
def test_halffloat_exact(self):
#testcases generated from numpy.float16(x).view('uint16')
- cases = [[0, 0], [10, 18688], [-10, 51456], [10e3, 28898],
+ cases = [[0, 0], [10, 18688], [-10, 51456], [10e3, 28898],
[float('inf'), 31744], [-float('inf'), 64512]]
- for c,h in cases:
- hbit = float_pack(c, 2)
+ for c, h in cases:
+ hbit = ieee.float_pack(c, 2)
assert hbit == h
- assert c == float_unpack(h, 2)
+ assert c == ieee.float_unpack(h, 2)
def test_halffloat_inexact(self):
#testcases generated from numpy.float16(x).view('uint16')
cases = [[10.001, 18688, 10.], [-10.001, 51456, -10],
[0.027588, 10000, 0.027587890625],
[22001, 30047, 22000]]
- for c,h,f in cases:
- hbit = float_pack(c, 2)
+ for c, h, f in cases:
+ hbit = ieee.float_pack(c, 2)
assert hbit == h
- assert f == float_unpack(h, 2)
+ assert f == ieee.float_unpack(h, 2)
def test_halffloat_overunderflow(self):
import math
@@ -161,8 +176,43 @@
[1e-08, 0], [-1e-8, -0.]]
for f1, f2 in cases:
try:
- f_out = float_unpack(float_pack(f1, 2), 2)
+ f_out = ieee.float_unpack(ieee.float_pack(f1, 2), 2)
except OverflowError:
f_out = math.copysign(float('inf'), f1)
assert f_out == f2
assert math.copysign(1., f_out) == math.copysign(1., f2)
+
+
+class TestCompiled:
+ def test_pack_float(self):
+ def pack(x, size):
+ result = []
+ ieee.pack_float(result, x, size, False)
+ l = []
+ for x in result:
+ for c in x:
+ l.append(str(ord(c)))
+ return ','.join(l)
+ c_pack = compile(pack, [float, int])
+
+ def unpack(s):
+ l = s.split(',')
+ s = ''.join([chr(int(x)) for x in l])
+ return ieee.unpack_float(s, False)
+ c_unpack = compile(unpack, [str])
+
+ def check_roundtrip(x, size):
+ s = c_pack(x, size)
+ assert s == pack(x, size)
+ if not isnan(x):
+ assert unpack(s) == x
+ assert c_unpack(s) == x
+ else:
+ assert isnan(unpack(s))
+ assert isnan(c_unpack(s))
+
+ for size in [2, 4, 8]:
+ check_roundtrip(123.4375, size)
+ check_roundtrip(-123.4375, size)
+ check_roundtrip(INFINITY, size)
+ check_roundtrip(NAN, size)
diff --git a/rpython/rlib/rstruct/test/test_runpack.py
b/rpython/rlib/rstruct/test/test_runpack.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/rstruct/test/test_runpack.py
@@ -0,0 +1,33 @@
+from rpython.rtyper.test.tool import BaseRtypingTest, LLRtypeMixin,
OORtypeMixin
+from rpython.rlib.rstruct.runpack import runpack
+from rpython.rlib.rarithmetic import LONG_BIT
+import struct
+
+class BaseTestRStruct(BaseRtypingTest):
+ def test_unpack(self):
+ pad = '\x00' * (LONG_BIT//8-1) # 3 or 7 null bytes
+ def fn():
+ return runpack('sll', 'a'+pad+'\x03'+pad+'\x04'+pad)[1]
+ assert fn() == 3
+ assert self.interpret(fn, []) == 3
+
+ def test_unpack_2(self):
+ data = struct.pack('iiii', 0, 1, 2, 4)
+ def fn():
+ a, b, c, d = runpack('iiii', data)
+ return a * 1000 + b * 100 + c * 10 + d
+ assert fn() == 124
+ assert self.interpret(fn, []) == 124
+
+ def test_unpack_single(self):
+ data = struct.pack('i', 123)
+ def fn():
+ return runpack('i', data)
+ assert fn() == 123
+ assert self.interpret(fn, []) == 123
+
+class TestLLType(BaseTestRStruct, LLRtypeMixin):
+ pass
+
+class TestOOType(BaseTestRStruct, OORtypeMixin):
+ pass
diff --git a/rpython/rlib/test/test_rarithmetic.py
b/rpython/rlib/test/test_rarithmetic.py
--- a/rpython/rlib/test/test_rarithmetic.py
+++ b/rpython/rlib/test/test_rarithmetic.py
@@ -400,8 +400,8 @@
assert rffi.cast(lltype.Signed, byteswap(rffi.cast(rffi.USHORT, 0x0102)))
== 0x0201
assert rffi.cast(lltype.Signed, byteswap(rffi.cast(rffi.INT, 0x01020304)))
== 0x04030201
- assert byteswap(rffi.cast(rffi.ULONGLONG, 0x0102030405060708L)) ==
0x0807060504030201L
- assert byteswap(rffi.cast(rffi.LONGLONG, 0x0102030405060708L)) ==
0x0807060504030201L
+ assert byteswap(r_ulonglong(0x0102030405060708L)) ==
r_ulonglong(0x0807060504030201L)
+ assert byteswap(r_longlong(0x0102030405060708L)) ==
r_longlong(0x0807060504030201L)
assert ((byteswap(2.3) - 1.903598566252326e+185) / 1e185) < 0.000001
assert (rffi.cast(lltype.Float, byteswap(rffi.cast(lltype.SingleFloat,
2.3))) - 4.173496037651603e-08) < 1e-16
diff --git a/rpython/rlib/test/test_rstruct.py
b/rpython/rlib/test/test_rstruct.py
deleted file mode 100644
--- a/rpython/rlib/test/test_rstruct.py
+++ /dev/null
@@ -1,72 +0,0 @@
-
-from rpython.rtyper.test.tool import BaseRtypingTest, LLRtypeMixin,
OORtypeMixin
-from rpython.rlib.rstruct.runpack import runpack
-from rpython.rlib.rstruct import ieee
-from rpython.rlib.rarithmetic import LONG_BIT
-from rpython.rlib.rfloat import INFINITY, NAN, isnan
-from rpython.translator.c.test.test_genc import compile
-import struct
-
-class BaseTestRStruct(BaseRtypingTest):
- def test_unpack(self):
- pad = '\x00' * (LONG_BIT//8-1) # 3 or 7 null bytes
- def fn():
- return runpack('sll', 'a'+pad+'\x03'+pad+'\x04'+pad)[1]
- assert fn() == 3
- assert self.interpret(fn, []) == 3
-
- def test_unpack_2(self):
- data = struct.pack('iiii', 0, 1, 2, 4)
-
- def fn():
- a, b, c, d = runpack('iiii', data)
- return a * 1000 + b * 100 + c * 10 + d
-
- assert fn() == 124
- assert self.interpret(fn, []) == 124
-
- def test_unpack_single(self):
- data = struct.pack('i', 123)
- def fn():
- return runpack('i', data)
- assert fn() == 123
- assert self.interpret(fn, []) == 123
-
-class TestLLType(BaseTestRStruct, LLRtypeMixin):
- pass
-
-class TestOOType(BaseTestRStruct, OORtypeMixin):
- pass
-
-class TestCompiled:
- def test_pack_float(self):
- def pack(x):
- result = []
- ieee.pack_float(result, x, 8, False)
- l = []
- for x in result:
- for c in x:
- l.append(str(ord(c)))
- return ','.join(l)
- c_pack = compile(pack, [float])
- def unpack(s):
- l = s.split(',')
- s = ''.join([chr(int(x)) for x in l])
- return ieee.unpack_float(s, False)
- c_unpack = compile(unpack, [str])
-
- def check_roundtrip(x):
- s = c_pack(x)
- assert s == pack(x)
- if not isnan(x):
- assert unpack(s) == x
- assert c_unpack(s) == x
- else:
- assert isnan(unpack(s))
- assert isnan(c_unpack(s))
-
- check_roundtrip(123.456)
- check_roundtrip(-123.456)
- check_roundtrip(INFINITY)
- check_roundtrip(NAN)
-
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit