[pypy-commit] pypy arm-backend-2: merge default
bivab
noreply at buildbot.pypy.org
Fri Mar 23 16:47:05 CET 2012
Author: David Schneider <david.schneider at picle.org>
Branch: arm-backend-2
Changeset: r53944:7f1c4b6b1ad1
Date: 2012-03-23 16:46 +0100
http://bitbucket.org/pypy/pypy/changeset/7f1c4b6b1ad1/
Log: merge default
diff --git a/lib-python/modified-2.7/site.py b/lib-python/modified-2.7/site.py
--- a/lib-python/modified-2.7/site.py
+++ b/lib-python/modified-2.7/site.py
@@ -550,9 +550,18 @@
"'import usercustomize' failed; use -v for traceback"
+def import_builtin_stuff():
+ """PyPy specific: pre-import a few built-in modules, because
+ some programs actually rely on them to be in sys.modules :-("""
+ import exceptions
+ if 'zipimport' in sys.builtin_module_names:
+ import zipimport
+
+
def main():
global ENABLE_USER_SITE
+ import_builtin_stuff()
abs__file__()
known_paths = removeduppaths()
if (os.name == "posix" and sys.path and
diff --git a/lib_pypy/_locale.py b/lib_pypy/_locale.py
deleted file mode 100644
--- a/lib_pypy/_locale.py
+++ /dev/null
@@ -1,337 +0,0 @@
-# ctypes implementation of _locale module by Victor Stinner, 2008-03-27
-
-# ------------------------------------------------------------
-# Note that we also have our own interp-level implementation
-# ------------------------------------------------------------
-
-"""
-Support for POSIX locales.
-"""
-
-from ctypes import (Structure, POINTER, create_string_buffer,
- c_ubyte, c_int, c_char_p, c_wchar_p, c_size_t)
-from ctypes_support import standard_c_lib as libc
-from ctypes_support import get_errno
-
-# load the platform-specific cache made by running locale.ctc.py
-from ctypes_config_cache._locale_cache import *
-
-try: from __pypy__ import builtinify
-except ImportError: builtinify = lambda f: f
-
-
-# Ubuntu Gusty i386 structure
-class lconv(Structure):
- _fields_ = (
- # Numeric (non-monetary) information.
- ("decimal_point", c_char_p), # Decimal point character.
- ("thousands_sep", c_char_p), # Thousands separator.
-
- # Each element is the number of digits in each group;
- # elements with higher indices are farther left.
- # An element with value CHAR_MAX means that no further grouping is done.
- # An element with value 0 means that the previous element is used
- # for all groups farther left. */
- ("grouping", c_char_p),
-
- # Monetary information.
-
- # First three chars are a currency symbol from ISO 4217.
- # Fourth char is the separator. Fifth char is '\0'.
- ("int_curr_symbol", c_char_p),
- ("currency_symbol", c_char_p), # Local currency symbol.
- ("mon_decimal_point", c_char_p), # Decimal point character.
- ("mon_thousands_sep", c_char_p), # Thousands separator.
- ("mon_grouping", c_char_p), # Like `grouping' element (above).
- ("positive_sign", c_char_p), # Sign for positive values.
- ("negative_sign", c_char_p), # Sign for negative values.
- ("int_frac_digits", c_ubyte), # Int'l fractional digits.
- ("frac_digits", c_ubyte), # Local fractional digits.
- # 1 if currency_symbol precedes a positive value, 0 if succeeds.
- ("p_cs_precedes", c_ubyte),
- # 1 iff a space separates currency_symbol from a positive value.
- ("p_sep_by_space", c_ubyte),
- # 1 if currency_symbol precedes a negative value, 0 if succeeds.
- ("n_cs_precedes", c_ubyte),
- # 1 iff a space separates currency_symbol from a negative value.
- ("n_sep_by_space", c_ubyte),
-
- # Positive and negative sign positions:
- # 0 Parentheses surround the quantity and currency_symbol.
- # 1 The sign string precedes the quantity and currency_symbol.
- # 2 The sign string follows the quantity and currency_symbol.
- # 3 The sign string immediately precedes the currency_symbol.
- # 4 The sign string immediately follows the currency_symbol.
- ("p_sign_posn", c_ubyte),
- ("n_sign_posn", c_ubyte),
- # 1 if int_curr_symbol precedes a positive value, 0 if succeeds.
- ("int_p_cs_precedes", c_ubyte),
- # 1 iff a space separates int_curr_symbol from a positive value.
- ("int_p_sep_by_space", c_ubyte),
- # 1 if int_curr_symbol precedes a negative value, 0 if succeeds.
- ("int_n_cs_precedes", c_ubyte),
- # 1 iff a space separates int_curr_symbol from a negative value.
- ("int_n_sep_by_space", c_ubyte),
- # Positive and negative sign positions:
- # 0 Parentheses surround the quantity and int_curr_symbol.
- # 1 The sign string precedes the quantity and int_curr_symbol.
- # 2 The sign string follows the quantity and int_curr_symbol.
- # 3 The sign string immediately precedes the int_curr_symbol.
- # 4 The sign string immediately follows the int_curr_symbol.
- ("int_p_sign_posn", c_ubyte),
- ("int_n_sign_posn", c_ubyte),
- )
-
-_setlocale = libc.setlocale
-_setlocale.argtypes = (c_int, c_char_p)
-_setlocale.restype = c_char_p
-
-_localeconv = libc.localeconv
-_localeconv.argtypes = None
-_localeconv.restype = POINTER(lconv)
-
-_strcoll = libc.strcoll
-_strcoll.argtypes = (c_char_p, c_char_p)
-_strcoll.restype = c_int
-
-_wcscoll = libc.wcscoll
-_wcscoll.argtypes = (c_wchar_p, c_wchar_p)
-_wcscoll.restype = c_int
-
-_strxfrm = libc.strxfrm
-_strxfrm.argtypes = (c_char_p, c_char_p, c_size_t)
-_strxfrm.restype = c_size_t
-
-HAS_LIBINTL = hasattr(libc, 'gettext')
-if HAS_LIBINTL:
- _gettext = libc.gettext
- _gettext.argtypes = (c_char_p,)
- _gettext.restype = c_char_p
-
- _dgettext = libc.dgettext
- _dgettext.argtypes = (c_char_p, c_char_p)
- _dgettext.restype = c_char_p
-
- _dcgettext = libc.dcgettext
- _dcgettext.argtypes = (c_char_p, c_char_p, c_int)
- _dcgettext.restype = c_char_p
-
- _textdomain = libc.textdomain
- _textdomain.argtypes = (c_char_p,)
- _textdomain.restype = c_char_p
-
- _bindtextdomain = libc.bindtextdomain
- _bindtextdomain.argtypes = (c_char_p, c_char_p)
- _bindtextdomain.restype = c_char_p
-
- HAS_BIND_TEXTDOMAIN_CODESET = hasattr(libc, 'bindtextdomain_codeset')
- if HAS_BIND_TEXTDOMAIN_CODESET:
- _bind_textdomain_codeset = libc.bindtextdomain_codeset
- _bind_textdomain_codeset.argtypes = (c_char_p, c_char_p)
- _bind_textdomain_codeset.restype = c_char_p
-
-class Error(Exception):
- pass
-
-def fixup_ulcase():
- import string
- #import strop
-
- # create uppercase map string
- ul = []
- for c in xrange(256):
- c = chr(c)
- if c.isupper():
- ul.append(c)
- ul = ''.join(ul)
- string.uppercase = ul
- #strop.uppercase = ul
-
- # create lowercase string
- ul = []
- for c in xrange(256):
- c = chr(c)
- if c.islower():
- ul.append(c)
- ul = ''.join(ul)
- string.lowercase = ul
- #strop.lowercase = ul
-
- # create letters string
- ul = []
- for c in xrange(256):
- c = chr(c)
- if c.isalpha():
- ul.append(c)
- ul = ''.join(ul)
- string.letters = ul
-
- at builtinify
-def setlocale(category, locale=None):
- "(integer,string=None) -> string. Activates/queries locale processing."
- if locale:
- # set locale
- result = _setlocale(category, locale)
- if not result:
- raise Error("unsupported locale setting")
-
- # record changes to LC_CTYPE
- if category in (LC_CTYPE, LC_ALL):
- fixup_ulcase()
- else:
- # get locale
- result = _setlocale(category, None)
- if not result:
- raise Error("locale query failed")
- return result
-
-def _copy_grouping(text):
- groups = [ ord(group) for group in text ]
- if groups:
- groups.append(0)
- return groups
-
- at builtinify
-def localeconv():
- "() -> dict. Returns numeric and monetary locale-specific parameters."
-
- # if LC_NUMERIC is different in the C library, use saved value
- lp = _localeconv()
- l = lp.contents
-
- # hopefully, the localeconv result survives the C library calls
- # involved herein
-
- # Numeric information
- result = {
- "decimal_point": l.decimal_point,
- "thousands_sep": l.thousands_sep,
- "grouping": _copy_grouping(l.grouping),
- "int_curr_symbol": l.int_curr_symbol,
- "currency_symbol": l.currency_symbol,
- "mon_decimal_point": l.mon_decimal_point,
- "mon_thousands_sep": l.mon_thousands_sep,
- "mon_grouping": _copy_grouping(l.mon_grouping),
- "positive_sign": l.positive_sign,
- "negative_sign": l.negative_sign,
- "int_frac_digits": l.int_frac_digits,
- "frac_digits": l.frac_digits,
- "p_cs_precedes": l.p_cs_precedes,
- "p_sep_by_space": l.p_sep_by_space,
- "n_cs_precedes": l.n_cs_precedes,
- "n_sep_by_space": l.n_sep_by_space,
- "p_sign_posn": l.p_sign_posn,
- "n_sign_posn": l.n_sign_posn,
- }
- return result
-
- at builtinify
-def strcoll(s1, s2):
- "string,string -> int. Compares two strings according to the locale."
-
- # If both arguments are byte strings, use strcoll.
- if isinstance(s1, str) and isinstance(s2, str):
- return _strcoll(s1, s2)
-
- # If neither argument is unicode, it's an error.
- if not isinstance(s1, unicode) and not isinstance(s2, unicode):
- raise ValueError("strcoll arguments must be strings")
-
- # Convert the non-unicode argument to unicode.
- s1 = unicode(s1)
- s2 = unicode(s2)
-
- # Collate the strings.
- return _wcscoll(s1, s2)
-
- at builtinify
-def strxfrm(s):
- "string -> string. Returns a string that behaves for cmp locale-aware."
-
- # assume no change in size, first
- n1 = len(s) + 1
- buf = create_string_buffer(n1)
- n2 = _strxfrm(buf, s, n1) + 1
- if n2 > n1:
- # more space needed
- buf = create_string_buffer(n2)
- _strxfrm(buf, s, n2)
- return buf.value
-
- at builtinify
-def getdefaultlocale():
- # TODO: Port code from CPython for Windows and Mac OS
- raise NotImplementedError()
-
-if HAS_LANGINFO:
- _nl_langinfo = libc.nl_langinfo
- _nl_langinfo.argtypes = (nl_item,)
- _nl_langinfo.restype = c_char_p
-
- def nl_langinfo(key):
- """nl_langinfo(key) -> string
- Return the value for the locale information associated with key."""
- # Check whether this is a supported constant. GNU libc sometimes
- # returns numeric values in the char* return value, which would
- # crash PyString_FromString.
- result = _nl_langinfo(key)
- if result is not None:
- return result
- raise ValueError("unsupported langinfo constant")
-
-if HAS_LIBINTL:
- @builtinify
- def gettext(msg):
- """gettext(msg) -> string
- Return translation of msg."""
- return _gettext(msg)
-
- @builtinify
- def dgettext(domain, msg):
- """dgettext(domain, msg) -> string
- Return translation of msg in domain."""
- return _dgettext(domain, msg)
-
- @builtinify
- def dcgettext(domain, msg, category):
- """dcgettext(domain, msg, category) -> string
- Return translation of msg in domain and category."""
- return _dcgettext(domain, msg, category)
-
- @builtinify
- def textdomain(domain):
- """textdomain(domain) -> string
- Set the C library's textdomain to domain, returning the new domain."""
- return _textdomain(domain)
-
- @builtinify
- def bindtextdomain(domain, dir):
- """bindtextdomain(domain, dir) -> string
- Bind the C library's domain to dir."""
- dirname = _bindtextdomain(domain, dir)
- if not dirname:
- errno = get_errno()
- raise OSError(errno)
- return dirname
-
- if HAS_BIND_TEXTDOMAIN_CODESET:
- @builtinify
- def bind_textdomain_codeset(domain, codeset):
- """bind_textdomain_codeset(domain, codeset) -> string
- Bind the C library's domain to codeset."""
- codeset = _bind_textdomain_codeset(domain, codeset)
- if codeset:
- return codeset
- return None
-
-__all__ = (
- 'Error',
- 'setlocale', 'localeconv', 'strxfrm', 'strcoll',
-) + ALL_CONSTANTS
-if HAS_LIBINTL:
- __all__ += ('gettext', 'dgettext', 'dcgettext', 'textdomain',
- 'bindtextdomain')
- if HAS_BIND_TEXTDOMAIN_CODESET:
- __all__ += ('bind_textdomain_codeset',)
-if HAS_LANGINFO:
- __all__ += ('nl_langinfo',)
diff --git a/lib_pypy/array.py b/lib_pypy/array.py
deleted file mode 100644
--- a/lib_pypy/array.py
+++ /dev/null
@@ -1,531 +0,0 @@
-"""This module defines an object type which can efficiently represent
-an array of basic values: characters, integers, floating point
-numbers. Arrays are sequence types and behave very much like lists,
-except that the type of objects stored in them is constrained. The
-type is specified at object creation time by using a type code, which
-is a single character. The following type codes are defined:
-
- Type code C Type Minimum size in bytes
- 'c' character 1
- 'b' signed integer 1
- 'B' unsigned integer 1
- 'u' Unicode character 2
- 'h' signed integer 2
- 'H' unsigned integer 2
- 'i' signed integer 2
- 'I' unsigned integer 2
- 'l' signed integer 4
- 'L' unsigned integer 4
- 'f' floating point 4
- 'd' floating point 8
-
-The constructor is:
-
-array(typecode [, initializer]) -- create a new array
-"""
-
-from struct import calcsize, pack, pack_into, unpack_from
-import operator
-
-# the buffer-like object to use internally: trying from
-# various places in order...
-try:
- import _rawffi # a reasonable implementation based
- _RAWARRAY = _rawffi.Array('c') # on raw_malloc, and providing a
- def bytebuffer(size): # real address
- return _RAWARRAY(size, autofree=True)
- def getbufaddress(buf):
- return buf.buffer
-except ImportError:
- try:
- from __pypy__ import bytebuffer # a reasonable implementation
- def getbufaddress(buf): # compatible with oo backends,
- return 0 # but no address
- except ImportError:
- # not running on PyPy. Fall back to ctypes...
- import ctypes
- bytebuffer = ctypes.create_string_buffer
- def getbufaddress(buf):
- voidp = ctypes.cast(ctypes.pointer(buf), ctypes.c_void_p)
- return voidp.value
-
-# ____________________________________________________________
-
-TYPECODES = "cbBuhHiIlLfd"
-
-class array(object):
- """array(typecode [, initializer]) -> array
-
- Return a new array whose items are restricted by typecode, and
- initialized from the optional initializer value, which must be a list,
- string. or iterable over elements of the appropriate type.
-
- Arrays represent basic values and behave very much like lists, except
- the type of objects stored in them is constrained.
-
- Methods:
-
- append() -- append a new item to the end of the array
- buffer_info() -- return information giving the current memory info
- byteswap() -- byteswap all the items of the array
- count() -- return number of occurences of an object
- extend() -- extend array by appending multiple elements from an iterable
- fromfile() -- read items from a file object
- fromlist() -- append items from the list
- fromstring() -- append items from the string
- index() -- return index of first occurence of an object
- insert() -- insert a new item into the array at a provided position
- pop() -- remove and return item (default last)
- read() -- DEPRECATED, use fromfile()
- remove() -- remove first occurence of an object
- reverse() -- reverse the order of the items in the array
- tofile() -- write all items to a file object
- tolist() -- return the array converted to an ordinary list
- tostring() -- return the array converted to a string
- write() -- DEPRECATED, use tofile()
-
- Attributes:
-
- typecode -- the typecode character used to create the array
- itemsize -- the length in bytes of one array item
- """
- __slots__ = ["typecode", "itemsize", "_data", "_descriptor", "__weakref__"]
-
- def __new__(cls, typecode, initializer=[], **extrakwds):
- self = object.__new__(cls)
- if cls is array and extrakwds:
- raise TypeError("array() does not take keyword arguments")
- if not isinstance(typecode, str) or len(typecode) != 1:
- raise TypeError(
- "array() argument 1 must be char, not %s" % type(typecode))
- if typecode not in TYPECODES:
- raise ValueError(
- "bad typecode (must be one of %s)" % ', '.join(TYPECODES))
- self._data = bytebuffer(0)
- self.typecode = typecode
- self.itemsize = calcsize(typecode)
- if isinstance(initializer, list):
- self.fromlist(initializer)
- elif isinstance(initializer, str):
- self.fromstring(initializer)
- elif isinstance(initializer, unicode) and self.typecode == "u":
- self.fromunicode(initializer)
- else:
- self.extend(initializer)
- return self
-
- def _clear(self):
- self._data = bytebuffer(0)
-
- ##### array-specific operations
-
- def fromfile(self, f, n):
- """Read n objects from the file object f and append them to the end of
- the array. Also called as read."""
- if not isinstance(f, file):
- raise TypeError("arg1 must be open file")
- size = self.itemsize * n
- item = f.read(size)
- if len(item) < size:
- raise EOFError("not enough items in file")
- self.fromstring(item)
-
- def fromlist(self, l):
- """Append items to array from list."""
- if not isinstance(l, list):
- raise TypeError("arg must be list")
- self._fromiterable(l)
-
- def fromstring(self, s):
- """Appends items from the string, interpreting it as an array of machine
- values, as if it had been read from a file using the fromfile()
- method."""
- if isinstance(s, unicode):
- s = str(s)
- self._frombuffer(s)
-
- def _frombuffer(self, s):
- length = len(s)
- if length % self.itemsize != 0:
- raise ValueError("string length not a multiple of item size")
- boundary = len(self._data)
- newdata = bytebuffer(boundary + length)
- newdata[:boundary] = self._data
- newdata[boundary:] = s
- self._data = newdata
-
- def fromunicode(self, ustr):
- """Extends this array with data from the unicode string ustr. The array
- must be a type 'u' array; otherwise a ValueError is raised. Use
- array.fromstring(ustr.encode(...)) to append Unicode data to an array of
- some other type."""
- if not self.typecode == "u":
- raise ValueError(
- "fromunicode() may only be called on type 'u' arrays")
- # XXX the following probable bug is not emulated:
- # CPython accepts a non-unicode string or a buffer, and then
- # behaves just like fromstring(), except that it strangely truncates
- # string arguments at multiples of the unicode byte size.
- # Let's only accept unicode arguments for now.
- if not isinstance(ustr, unicode):
- raise TypeError("fromunicode() argument should probably be "
- "a unicode string")
- # _frombuffer() does the currect thing using
- # the buffer behavior of unicode objects
- self._frombuffer(buffer(ustr))
-
- def tofile(self, f):
- """Write all items (as machine values) to the file object f. Also
- called as write."""
- if not isinstance(f, file):
- raise TypeError("arg must be open file")
- f.write(self.tostring())
-
- def tolist(self):
- """Convert array to an ordinary list with the same items."""
- count = len(self._data) // self.itemsize
- return list(unpack_from('%d%s' % (count, self.typecode), self._data))
-
- def tostring(self):
- return self._data[:]
-
- def __buffer__(self):
- return buffer(self._data)
-
- def tounicode(self):
- """Convert the array to a unicode string. The array must be a type 'u'
- array; otherwise a ValueError is raised. Use array.tostring().decode()
- to obtain a unicode string from an array of some other type."""
- if self.typecode != "u":
- raise ValueError("tounicode() may only be called on type 'u' arrays")
- # XXX performance is not too good
- return u"".join(self.tolist())
-
- def byteswap(self):
- """Byteswap all items of the array. If the items in the array are not
- 1, 2, 4, or 8 bytes in size, RuntimeError is raised."""
- if self.itemsize not in [1, 2, 4, 8]:
- raise RuntimeError("byteswap not supported for this array")
- # XXX slowish
- itemsize = self.itemsize
- bytes = self._data
- for start in range(0, len(bytes), itemsize):
- stop = start + itemsize
- bytes[start:stop] = bytes[start:stop][::-1]
-
- def buffer_info(self):
- """Return a tuple (address, length) giving the current memory address
- and the length in items of the buffer used to hold array's contents. The
- length should be multiplied by the itemsize attribute to calculate the
- buffer length in bytes. On PyPy the address might be meaningless
- (returned as 0), depending on the available modules."""
- return (getbufaddress(self._data), len(self))
-
- read = fromfile
-
- write = tofile
-
- ##### general object protocol
-
- def __repr__(self):
- if len(self._data) == 0:
- return "array('%s')" % self.typecode
- elif self.typecode == "c":
- return "array('%s', %s)" % (self.typecode, repr(self.tostring()))
- elif self.typecode == "u":
- return "array('%s', %s)" % (self.typecode, repr(self.tounicode()))
- else:
- return "array('%s', %s)" % (self.typecode, repr(self.tolist()))
-
- def __copy__(self):
- a = array(self.typecode)
- a._data = bytebuffer(len(self._data))
- a._data[:] = self._data
- return a
-
- def __eq__(self, other):
- if not isinstance(other, array):
- return NotImplemented
- if self.typecode == 'c':
- return buffer(self._data) == buffer(other._data)
- else:
- return self.tolist() == other.tolist()
-
- def __ne__(self, other):
- if not isinstance(other, array):
- return NotImplemented
- if self.typecode == 'c':
- return buffer(self._data) != buffer(other._data)
- else:
- return self.tolist() != other.tolist()
-
- def __lt__(self, other):
- if not isinstance(other, array):
- return NotImplemented
- if self.typecode == 'c':
- return buffer(self._data) < buffer(other._data)
- else:
- return self.tolist() < other.tolist()
-
- def __gt__(self, other):
- if not isinstance(other, array):
- return NotImplemented
- if self.typecode == 'c':
- return buffer(self._data) > buffer(other._data)
- else:
- return self.tolist() > other.tolist()
-
- def __le__(self, other):
- if not isinstance(other, array):
- return NotImplemented
- if self.typecode == 'c':
- return buffer(self._data) <= buffer(other._data)
- else:
- return self.tolist() <= other.tolist()
-
- def __ge__(self, other):
- if not isinstance(other, array):
- return NotImplemented
- if self.typecode == 'c':
- return buffer(self._data) >= buffer(other._data)
- else:
- return self.tolist() >= other.tolist()
-
- def __reduce__(self):
- dict = getattr(self, '__dict__', None)
- data = self.tostring()
- if data:
- initargs = (self.typecode, data)
- else:
- initargs = (self.typecode,)
- return (type(self), initargs, dict)
-
- ##### list methods
-
- def append(self, x):
- """Append new value x to the end of the array."""
- self._frombuffer(pack(self.typecode, x))
-
- def count(self, x):
- """Return number of occurences of x in the array."""
- return operator.countOf(self, x)
-
- def extend(self, iterable):
- """Append items to the end of the array."""
- if isinstance(iterable, array) \
- and not self.typecode == iterable.typecode:
- raise TypeError("can only extend with array of same kind")
- self._fromiterable(iterable)
-
- def index(self, x):
- """Return index of first occurence of x in the array."""
- return operator.indexOf(self, x)
-
- def insert(self, i, x):
- """Insert a new item x into the array before position i."""
- seqlength = len(self)
- if i < 0:
- i += seqlength
- if i < 0:
- i = 0
- elif i > seqlength:
- i = seqlength
- boundary = i * self.itemsize
- data = pack(self.typecode, x)
- newdata = bytebuffer(len(self._data) + len(data))
- newdata[:boundary] = self._data[:boundary]
- newdata[boundary:boundary+self.itemsize] = data
- newdata[boundary+self.itemsize:] = self._data[boundary:]
- self._data = newdata
-
- def pop(self, i=-1):
- """Return the i-th element and delete it from the array. i defaults to
- -1."""
- seqlength = len(self)
- if i < 0:
- i += seqlength
- if not (0 <= i < seqlength):
- raise IndexError(i)
- boundary = i * self.itemsize
- result = unpack_from(self.typecode, self._data, boundary)[0]
- newdata = bytebuffer(len(self._data) - self.itemsize)
- newdata[:boundary] = self._data[:boundary]
- newdata[boundary:] = self._data[boundary+self.itemsize:]
- self._data = newdata
- return result
-
- def remove(self, x):
- """Remove the first occurence of x in the array."""
- self.pop(self.index(x))
-
- def reverse(self):
- """Reverse the order of the items in the array."""
- lst = self.tolist()
- lst.reverse()
- self._clear()
- self.fromlist(lst)
-
- ##### list protocol
-
- def __len__(self):
- return len(self._data) // self.itemsize
-
- def __add__(self, other):
- if not isinstance(other, array):
- raise TypeError("can only append array to array")
- if self.typecode != other.typecode:
- raise TypeError("bad argument type for built-in operation")
- return array(self.typecode, buffer(self._data) + buffer(other._data))
-
- def __mul__(self, repeat):
- return array(self.typecode, buffer(self._data) * repeat)
-
- __rmul__ = __mul__
-
- def __getitem__(self, i):
- seqlength = len(self)
- if isinstance(i, slice):
- start, stop, step = i.indices(seqlength)
- if step != 1:
- sublist = self.tolist()[i] # fall-back
- return array(self.typecode, sublist)
- if start < 0:
- start = 0
- if stop < start:
- stop = start
- assert stop <= seqlength
- return array(self.typecode, self._data[start * self.itemsize :
- stop * self.itemsize])
- else:
- if i < 0:
- i += seqlength
- if self.typecode == 'c': # speed trick
- return self._data[i]
- if not (0 <= i < seqlength):
- raise IndexError(i)
- boundary = i * self.itemsize
- return unpack_from(self.typecode, self._data, boundary)[0]
-
- def __getslice__(self, i, j):
- return self.__getitem__(slice(i, j))
-
- def __setitem__(self, i, x):
- if isinstance(i, slice):
- if (not isinstance(x, array)
- or self.typecode != x.typecode):
- raise TypeError("can only assign array of same kind"
- " to array slice")
- seqlength = len(self)
- start, stop, step = i.indices(seqlength)
- if step != 1:
- sublist = self.tolist() # fall-back
- sublist[i] = x.tolist()
- self._clear()
- self.fromlist(sublist)
- return
- if start < 0:
- start = 0
- if stop < start:
- stop = start
- assert stop <= seqlength
- boundary1 = start * self.itemsize
- boundary2 = stop * self.itemsize
- boundary2new = boundary1 + len(x._data)
- if boundary2 == boundary2new:
- self._data[boundary1:boundary2] = x._data
- else:
- newdata = bytebuffer(len(self._data) + boundary2new-boundary2)
- newdata[:boundary1] = self._data[:boundary1]
- newdata[boundary1:boundary2new] = x._data
- newdata[boundary2new:] = self._data[boundary2:]
- self._data = newdata
- else:
- seqlength = len(self)
- if i < 0:
- i += seqlength
- if self.typecode == 'c': # speed trick
- self._data[i] = x
- return
- if not (0 <= i < seqlength):
- raise IndexError(i)
- boundary = i * self.itemsize
- pack_into(self.typecode, self._data, boundary, x)
-
- def __setslice__(self, i, j, x):
- self.__setitem__(slice(i, j), x)
-
- def __delitem__(self, i):
- if isinstance(i, slice):
- seqlength = len(self)
- start, stop, step = i.indices(seqlength)
- if start < 0:
- start = 0
- if stop < start:
- stop = start
- assert stop <= seqlength
- if step != 1:
- sublist = self.tolist() # fall-back
- del sublist[i]
- self._clear()
- self.fromlist(sublist)
- return
- dellength = stop - start
- boundary1 = start * self.itemsize
- boundary2 = stop * self.itemsize
- newdata = bytebuffer(len(self._data) - (boundary2-boundary1))
- newdata[:boundary1] = self._data[:boundary1]
- newdata[boundary1:] = self._data[boundary2:]
- self._data = newdata
- else:
- seqlength = len(self)
- if i < 0:
- i += seqlength
- if not (0 <= i < seqlength):
- raise IndexError(i)
- boundary = i * self.itemsize
- newdata = bytebuffer(len(self._data) - self.itemsize)
- newdata[:boundary] = self._data[:boundary]
- newdata[boundary:] = self._data[boundary+self.itemsize:]
- self._data = newdata
-
- def __delslice__(self, i, j):
- self.__delitem__(slice(i, j))
-
- def __contains__(self, item):
- for x in self:
- if x == item:
- return True
- return False
-
- def __iadd__(self, other):
- if not isinstance(other, array):
- raise TypeError("can only extend array with array")
- self.extend(other)
- return self
-
- def __imul__(self, repeat):
- newdata = buffer(self._data) * repeat
- self._data = bytebuffer(len(newdata))
- self._data[:] = newdata
- return self
-
- def __iter__(self):
- p = 0
- typecode = self.typecode
- itemsize = self.itemsize
- while p < len(self._data):
- yield unpack_from(typecode, self._data, p)[0]
- p += itemsize
-
- ##### internal methods
-
- def _fromiterable(self, iterable):
- iterable = tuple(iterable)
- n = len(iterable)
- boundary = len(self._data)
- newdata = bytebuffer(boundary + n * self.itemsize)
- newdata[:boundary] = self._data
- pack_into('%d%s' % (n, self.typecode), newdata, boundary, *iterable)
- self._data = newdata
-
-ArrayType = array
diff --git a/lib_pypy/binascii.py b/lib_pypy/binascii.py
deleted file mode 100644
--- a/lib_pypy/binascii.py
+++ /dev/null
@@ -1,720 +0,0 @@
-"""A pure Python implementation of binascii.
-
-Rather slow and buggy in corner cases.
-PyPy provides an RPython version too.
-"""
-
-class Error(Exception):
- pass
-
-class Done(Exception):
- pass
-
-class Incomplete(Exception):
- pass
-
-def a2b_uu(s):
- if not s:
- return ''
-
- length = (ord(s[0]) - 0x20) % 64
-
- def quadruplets_gen(s):
- while s:
- try:
- yield ord(s[0]), ord(s[1]), ord(s[2]), ord(s[3])
- except IndexError:
- s += ' '
- yield ord(s[0]), ord(s[1]), ord(s[2]), ord(s[3])
- return
- s = s[4:]
-
- try:
- result = [''.join(
- [chr((A - 0x20) << 2 | (((B - 0x20) >> 4) & 0x3)),
- chr(((B - 0x20) & 0xf) << 4 | (((C - 0x20) >> 2) & 0xf)),
- chr(((C - 0x20) & 0x3) << 6 | ((D - 0x20) & 0x3f))
- ]) for A, B, C, D in quadruplets_gen(s[1:].rstrip())]
- except ValueError:
- raise Error('Illegal char')
- result = ''.join(result)
- trailingdata = result[length:]
- if trailingdata.strip('\x00'):
- raise Error('Trailing garbage')
- result = result[:length]
- if len(result) < length:
- result += ((length - len(result)) * '\x00')
- return result
-
-
-def b2a_uu(s):
- length = len(s)
- if length > 45:
- raise Error('At most 45 bytes at once')
-
- def triples_gen(s):
- while s:
- try:
- yield ord(s[0]), ord(s[1]), ord(s[2])
- except IndexError:
- s += '\0\0'
- yield ord(s[0]), ord(s[1]), ord(s[2])
- return
- s = s[3:]
-
- result = [''.join(
- [chr(0x20 + (( A >> 2 ) & 0x3F)),
- chr(0x20 + (((A << 4) | ((B >> 4) & 0xF)) & 0x3F)),
- chr(0x20 + (((B << 2) | ((C >> 6) & 0x3)) & 0x3F)),
- chr(0x20 + (( C ) & 0x3F))])
- for A, B, C in triples_gen(s)]
- return chr(ord(' ') + (length & 077)) + ''.join(result) + '\n'
-
-
-table_a2b_base64 = {
- 'A': 0,
- 'B': 1,
- 'C': 2,
- 'D': 3,
- 'E': 4,
- 'F': 5,
- 'G': 6,
- 'H': 7,
- 'I': 8,
- 'J': 9,
- 'K': 10,
- 'L': 11,
- 'M': 12,
- 'N': 13,
- 'O': 14,
- 'P': 15,
- 'Q': 16,
- 'R': 17,
- 'S': 18,
- 'T': 19,
- 'U': 20,
- 'V': 21,
- 'W': 22,
- 'X': 23,
- 'Y': 24,
- 'Z': 25,
- 'a': 26,
- 'b': 27,
- 'c': 28,
- 'd': 29,
- 'e': 30,
- 'f': 31,
- 'g': 32,
- 'h': 33,
- 'i': 34,
- 'j': 35,
- 'k': 36,
- 'l': 37,
- 'm': 38,
- 'n': 39,
- 'o': 40,
- 'p': 41,
- 'q': 42,
- 'r': 43,
- 's': 44,
- 't': 45,
- 'u': 46,
- 'v': 47,
- 'w': 48,
- 'x': 49,
- 'y': 50,
- 'z': 51,
- '0': 52,
- '1': 53,
- '2': 54,
- '3': 55,
- '4': 56,
- '5': 57,
- '6': 58,
- '7': 59,
- '8': 60,
- '9': 61,
- '+': 62,
- '/': 63,
- '=': 0,
-}
-
-
-def a2b_base64(s):
- if not isinstance(s, (str, unicode)):
- raise TypeError("expected string or unicode, got %r" % (s,))
- s = s.rstrip()
- # clean out all invalid characters, this also strips the final '=' padding
- # check for correct padding
-
- def next_valid_char(s, pos):
- for i in range(pos + 1, len(s)):
- c = s[i]
- if c < '\x7f':
- try:
- table_a2b_base64[c]
- return c
- except KeyError:
- pass
- return None
-
- quad_pos = 0
- leftbits = 0
- leftchar = 0
- res = []
- for i, c in enumerate(s):
- if c > '\x7f' or c == '\n' or c == '\r' or c == ' ':
- continue
- if c == '=':
- if quad_pos < 2 or (quad_pos == 2 and next_valid_char(s, i) != '='):
- continue
- else:
- leftbits = 0
- break
- try:
- next_c = table_a2b_base64[c]
- except KeyError:
- continue
- quad_pos = (quad_pos + 1) & 0x03
- leftchar = (leftchar << 6) | next_c
- leftbits += 6
- if leftbits >= 8:
- leftbits -= 8
- res.append((leftchar >> leftbits & 0xff))
- leftchar &= ((1 << leftbits) - 1)
- if leftbits != 0:
- raise Error('Incorrect padding')
-
- return ''.join([chr(i) for i in res])
-
-table_b2a_base64 = \
-"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-
-def b2a_base64(s):
- length = len(s)
- final_length = length % 3
-
- def triples_gen(s):
- while s:
- try:
- yield ord(s[0]), ord(s[1]), ord(s[2])
- except IndexError:
- s += '\0\0'
- yield ord(s[0]), ord(s[1]), ord(s[2])
- return
- s = s[3:]
-
-
- a = triples_gen(s[ :length - final_length])
-
- result = [''.join(
- [table_b2a_base64[( A >> 2 ) & 0x3F],
- table_b2a_base64[((A << 4) | ((B >> 4) & 0xF)) & 0x3F],
- table_b2a_base64[((B << 2) | ((C >> 6) & 0x3)) & 0x3F],
- table_b2a_base64[( C ) & 0x3F]])
- for A, B, C in a]
-
- final = s[length - final_length:]
- if final_length == 0:
- snippet = ''
- elif final_length == 1:
- a = ord(final[0])
- snippet = table_b2a_base64[(a >> 2 ) & 0x3F] + \
- table_b2a_base64[(a << 4 ) & 0x3F] + '=='
- else:
- a = ord(final[0])
- b = ord(final[1])
- snippet = table_b2a_base64[(a >> 2) & 0x3F] + \
- table_b2a_base64[((a << 4) | (b >> 4) & 0xF) & 0x3F] + \
- table_b2a_base64[(b << 2) & 0x3F] + '='
- return ''.join(result) + snippet + '\n'
-
-def a2b_qp(s, header=False):
- inp = 0
- odata = []
- while inp < len(s):
- if s[inp] == '=':
- inp += 1
- if inp >= len(s):
- break
- # Soft line breaks
- if (s[inp] == '\n') or (s[inp] == '\r'):
- if s[inp] != '\n':
- while inp < len(s) and s[inp] != '\n':
- inp += 1
- if inp < len(s):
- inp += 1
- elif s[inp] == '=':
- # broken case from broken python qp
- odata.append('=')
- inp += 1
- elif s[inp] in hex_numbers and s[inp + 1] in hex_numbers:
- ch = chr(int(s[inp:inp+2], 16))
- inp += 2
- odata.append(ch)
- else:
- odata.append('=')
- elif header and s[inp] == '_':
- odata.append(' ')
- inp += 1
- else:
- odata.append(s[inp])
- inp += 1
- return ''.join(odata)
-
-def b2a_qp(data, quotetabs=False, istext=True, header=False):
- """quotetabs=True means that tab and space characters are always
- quoted.
- istext=False means that \r and \n are treated as regular characters
- header=True encodes space characters with '_' and requires
- real '_' characters to be quoted.
- """
- MAXLINESIZE = 76
-
- # See if this string is using CRLF line ends
- lf = data.find('\n')
- crlf = lf > 0 and data[lf-1] == '\r'
-
- inp = 0
- linelen = 0
- odata = []
- while inp < len(data):
- c = data[inp]
- if (c > '~' or
- c == '=' or
- (header and c == '_') or
- (c == '.' and linelen == 0 and (inp+1 == len(data) or
- data[inp+1] == '\n' or
- data[inp+1] == '\r')) or
- (not istext and (c == '\r' or c == '\n')) or
- ((c == '\t' or c == ' ') and (inp + 1 == len(data))) or
- (c <= ' ' and c != '\r' and c != '\n' and
- (quotetabs or (not quotetabs and (c != '\t' and c != ' '))))):
- linelen += 3
- if linelen >= MAXLINESIZE:
- odata.append('=')
- if crlf: odata.append('\r')
- odata.append('\n')
- linelen = 3
- odata.append('=' + two_hex_digits(ord(c)))
- inp += 1
- else:
- if (istext and
- (c == '\n' or (inp+1 < len(data) and c == '\r' and
- data[inp+1] == '\n'))):
- linelen = 0
- # Protect against whitespace on end of line
- if (len(odata) > 0 and
- (odata[-1] == ' ' or odata[-1] == '\t')):
- ch = ord(odata[-1])
- odata[-1] = '='
- odata.append(two_hex_digits(ch))
-
- if crlf: odata.append('\r')
- odata.append('\n')
- if c == '\r':
- inp += 2
- else:
- inp += 1
- else:
- if (inp + 1 < len(data) and
- data[inp+1] != '\n' and
- (linelen + 1) >= MAXLINESIZE):
- odata.append('=')
- if crlf: odata.append('\r')
- odata.append('\n')
- linelen = 0
-
- linelen += 1
- if header and c == ' ':
- c = '_'
- odata.append(c)
- inp += 1
- return ''.join(odata)
-
-hex_numbers = '0123456789ABCDEF'
-def hex(n):
- if n == 0:
- return '0'
-
- if n < 0:
- n = -n
- sign = '-'
- else:
- sign = ''
- arr = []
-
- def hex_gen(n):
- """ Yield a nibble at a time. """
- while n:
- yield n % 0x10
- n = n / 0x10
-
- for nibble in hex_gen(n):
- arr = [hex_numbers[nibble]] + arr
- return sign + ''.join(arr)
-
-def two_hex_digits(n):
- return hex_numbers[n / 0x10] + hex_numbers[n % 0x10]
-
-
-def strhex_to_int(s):
- i = 0
- for c in s:
- i = i * 0x10 + hex_numbers.index(c)
- return i
-
-hqx_encoding = '!"#$%&\'()*+,-012345689 at ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr'
-
-DONE = 0x7f
-SKIP = 0x7e
-FAIL = 0x7d
-
-table_a2b_hqx = [
- #^@ ^A ^B ^C ^D ^E ^F ^G
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- #\b \t \n ^K ^L \r ^N ^O
- FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
- #^P ^Q ^R ^S ^T ^U ^V ^W
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- #^X ^Y ^Z ^[ ^\ ^] ^^ ^_
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- # ! " # $ % & '
- FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- #( ) * + , - . /
- 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
- #0 1 2 3 4 5 6 7
- 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
- #8 9 : ; < = > ?
- 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
- #@ A B C D E F G
- 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
- #H I J K L M N O
- 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
- #P Q R S T U V W
- 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
- #X Y Z [ \ ] ^ _
- 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
- #` a b c d e f g
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
- #h i j k l m n o
- 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
- #p q r s t u v w
- 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
- #x y z { | } ~ ^?
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
-]
-
-def a2b_hqx(s):
- result = []
-
- def quadruples_gen(s):
- t = []
- for c in s:
- res = table_a2b_hqx[ord(c)]
- if res == SKIP:
- continue
- elif res == FAIL:
- raise Error('Illegal character')
- elif res == DONE:
- yield t
- raise Done
- else:
- t.append(res)
- if len(t) == 4:
- yield t
- t = []
- yield t
-
- done = 0
- try:
- for snippet in quadruples_gen(s):
- length = len(snippet)
- if length == 4:
- result.append(chr(((snippet[0] & 0x3f) << 2) | (snippet[1] >> 4)))
- result.append(chr(((snippet[1] & 0x0f) << 4) | (snippet[2] >> 2)))
- result.append(chr(((snippet[2] & 0x03) << 6) | (snippet[3])))
- elif length == 3:
- result.append(chr(((snippet[0] & 0x3f) << 2) | (snippet[1] >> 4)))
- result.append(chr(((snippet[1] & 0x0f) << 4) | (snippet[2] >> 2)))
- elif length == 2:
- result.append(chr(((snippet[0] & 0x3f) << 2) | (snippet[1] >> 4)))
- except Done:
- done = 1
- except Error:
- raise
- return (''.join(result), done)
-
-def b2a_hqx(s):
- result =[]
-
- def triples_gen(s):
- while s:
- try:
- yield ord(s[0]), ord(s[1]), ord(s[2])
- except IndexError:
- yield tuple([ord(c) for c in s])
- s = s[3:]
-
- for snippet in triples_gen(s):
- length = len(snippet)
- if length == 3:
- result.append(
- hqx_encoding[(snippet[0] & 0xfc) >> 2])
- result.append(hqx_encoding[
- ((snippet[0] & 0x03) << 4) | ((snippet[1] & 0xf0) >> 4)])
- result.append(hqx_encoding[
- (snippet[1] & 0x0f) << 2 | ((snippet[2] & 0xc0) >> 6)])
- result.append(hqx_encoding[snippet[2] & 0x3f])
- elif length == 2:
- result.append(
- hqx_encoding[(snippet[0] & 0xfc) >> 2])
- result.append(hqx_encoding[
- ((snippet[0] & 0x03) << 4) | ((snippet[1] & 0xf0) >> 4)])
- result.append(hqx_encoding[
- (snippet[1] & 0x0f) << 2])
- elif length == 1:
- result.append(
- hqx_encoding[(snippet[0] & 0xfc) >> 2])
- result.append(hqx_encoding[
- ((snippet[0] & 0x03) << 4)])
- return ''.join(result)
-
-crctab_hqx = [
- 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
- 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
- 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
- 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
- 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
- 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
- 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
- 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
- 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
- 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
- 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
- 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
- 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
- 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
- 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
- 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
- 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
- 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
- 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
- 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
- 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
- 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
- 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
- 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
- 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
- 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
- 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
- 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
- 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
- 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
- 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
- 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
-]
-
-def crc_hqx(s, crc):
- for c in s:
- crc = ((crc << 8) & 0xff00) ^ crctab_hqx[((crc >> 8) & 0xff) ^ ord(c)]
-
- return crc
-
-def rlecode_hqx(s):
- """
- Run length encoding for binhex4.
- The CPython implementation does not do run length encoding
- of \x90 characters. This implementation does.
- """
- if not s:
- return ''
- result = []
- prev = s[0]
- count = 1
- # Add a dummy character to get the loop to go one extra round.
- # The dummy must be different from the last character of s.
- # In the same step we remove the first character, which has
- # already been stored in prev.
- if s[-1] == '!':
- s = s[1:] + '?'
- else:
- s = s[1:] + '!'
-
- for c in s:
- if c == prev and count < 255:
- count += 1
- else:
- if count == 1:
- if prev != '\x90':
- result.append(prev)
- else:
- result.extend(['\x90', '\x00'])
- elif count < 4:
- if prev != '\x90':
- result.extend([prev] * count)
- else:
- result.extend(['\x90', '\x00'] * count)
- else:
- if prev != '\x90':
- result.extend([prev, '\x90', chr(count)])
- else:
- result.extend(['\x90', '\x00', '\x90', chr(count)])
- count = 1
- prev = c
-
- return ''.join(result)
-
-def rledecode_hqx(s):
- s = s.split('\x90')
- result = [s[0]]
- prev = s[0]
- for snippet in s[1:]:
- count = ord(snippet[0])
- if count > 0:
- result.append(prev[-1] * (count-1))
- prev = snippet
- else:
- result. append('\x90')
- prev = '\x90'
- result.append(snippet[1:])
-
- return ''.join(result)
-
-crc_32_tab = [
- 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
- 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
- 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
- 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
- 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
- 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
- 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
- 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
- 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
- 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
- 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
- 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
- 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
- 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
- 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
- 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
- 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
- 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
- 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
- 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
- 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
- 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
- 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
- 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
- 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
- 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
- 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
- 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
- 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
- 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
- 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
- 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
- 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
- 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
- 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
- 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
- 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
- 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
- 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
- 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
- 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
- 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
- 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
- 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
- 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
- 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
- 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
- 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
- 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
- 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
- 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
- 0x2d02ef8dL
-]
-
-def crc32(s, crc=0):
- result = 0
- crc = ~long(crc) & 0xffffffffL
- for c in s:
- crc = crc_32_tab[(crc ^ long(ord(c))) & 0xffL] ^ (crc >> 8)
- #/* Note: (crc >> 8) MUST zero fill on left
-
- result = crc ^ 0xffffffffL
-
- if result > 2**31:
- result = ((result + 2**31) % 2**32) - 2**31
-
- return result
-
-def b2a_hex(s):
- result = []
- for char in s:
- c = (ord(char) >> 4) & 0xf
- if c > 9:
- c = c + ord('a') - 10
- else:
- c = c + ord('0')
- result.append(chr(c))
- c = ord(char) & 0xf
- if c > 9:
- c = c + ord('a') - 10
- else:
- c = c + ord('0')
- result.append(chr(c))
- return ''.join(result)
-
-hexlify = b2a_hex
-
-table_hex = [
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
- -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
-]
-
-
-def a2b_hex(t):
- result = []
-
- def pairs_gen(s):
- while s:
- try:
- yield table_hex[ord(s[0])], table_hex[ord(s[1])]
- except IndexError:
- if len(s):
- raise TypeError('Odd-length string')
- return
- s = s[2:]
-
- for a, b in pairs_gen(t):
- if a < 0 or b < 0:
- raise TypeError('Non-hexadecimal digit found')
- result.append(chr((a << 4) + b))
- return ''.join(result)
-
-
-unhexlify = a2b_hex
diff --git a/lib_pypy/numpypy/core/numeric.py b/lib_pypy/numpypy/core/numeric.py
--- a/lib_pypy/numpypy/core/numeric.py
+++ b/lib_pypy/numpypy/core/numeric.py
@@ -306,6 +306,125 @@
else:
return multiarray.set_string_function(f, repr)
+def array_equal(a1, a2):
+ """
+ True if two arrays have the same shape and elements, False otherwise.
+
+ Parameters
+ ----------
+ a1, a2 : array_like
+ Input arrays.
+
+ Returns
+ -------
+ b : bool
+ Returns True if the arrays are equal.
+
+ See Also
+ --------
+ allclose: Returns True if two arrays are element-wise equal within a
+ tolerance.
+ array_equiv: Returns True if input arrays are shape consistent and all
+ elements equal.
+
+ Examples
+ --------
+ >>> np.array_equal([1, 2], [1, 2])
+ True
+ >>> np.array_equal(np.array([1, 2]), np.array([1, 2]))
+ True
+ >>> np.array_equal([1, 2], [1, 2, 3])
+ False
+ >>> np.array_equal([1, 2], [1, 4])
+ False
+
+ """
+ try:
+ a1, a2 = asarray(a1), asarray(a2)
+ except:
+ return False
+ if a1.shape != a2.shape:
+ return False
+ return bool((a1 == a2).all())
+
+def asarray(a, dtype=None, order=None, maskna=None, ownmaskna=False):
+ """
+ Convert the input to an array.
+
+ Parameters
+ ----------
+ a : array_like
+ Input data, in any form that can be converted to an array. This
+ includes lists, lists of tuples, tuples, tuples of tuples, tuples
+ of lists and ndarrays.
+ dtype : data-type, optional
+ By default, the data-type is inferred from the input data.
+ order : {'C', 'F'}, optional
+ Whether to use row-major ('C') or column-major ('F' for FORTRAN)
+ memory representation. Defaults to 'C'.
+ maskna : bool or None, optional
+ If this is set to True, it forces the array to have an NA mask.
+ If this is set to False, it forces the array to not have an NA
+ mask.
+ ownmaskna : bool, optional
+ If this is set to True, forces the array to have a mask which
+ it owns.
+
+ Returns
+ -------
+ out : ndarray
+ Array interpretation of `a`. No copy is performed if the input
+ is already an ndarray. If `a` is a subclass of ndarray, a base
+ class ndarray is returned.
+
+ See Also
+ --------
+ asanyarray : Similar function which passes through subclasses.
+ ascontiguousarray : Convert input to a contiguous array.
+ asfarray : Convert input to a floating point ndarray.
+ asfortranarray : Convert input to an ndarray with column-major
+ memory order.
+ asarray_chkfinite : Similar function which checks input for NaNs and Infs.
+ fromiter : Create an array from an iterator.
+ fromfunction : Construct an array by executing a function on grid
+ positions.
+
+ Examples
+ --------
+ Convert a list into an array:
+
+ >>> a = [1, 2]
+ >>> np.asarray(a)
+ array([1, 2])
+
+ Existing arrays are not copied:
+
+ >>> a = np.array([1, 2])
+ >>> np.asarray(a) is a
+ True
+
+ If `dtype` is set, array is copied only if dtype does not match:
+
+ >>> a = np.array([1, 2], dtype=np.float32)
+ >>> np.asarray(a, dtype=np.float32) is a
+ True
+ >>> np.asarray(a, dtype=np.float64) is a
+ False
+
+ Contrary to `asanyarray`, ndarray subclasses are not passed through:
+
+ >>> issubclass(np.matrix, np.ndarray)
+ True
+ >>> a = np.matrix([[1, 2]])
+ >>> np.asarray(a) is a
+ False
+ >>> np.asanyarray(a) is a
+ True
+
+ """
+ return array(a, dtype, copy=False, order=order,
+ maskna=maskna, ownmaskna=ownmaskna)
+
set_string_function(array_str, 0)
set_string_function(array_repr, 1)
diff --git a/lib_pypy/pypy_test/test_binascii.py b/lib_pypy/pypy_test/test_binascii.py
deleted file mode 100644
--- a/lib_pypy/pypy_test/test_binascii.py
+++ /dev/null
@@ -1,168 +0,0 @@
-from __future__ import absolute_import
-import py
-from lib_pypy import binascii
-
-# Create binary test data
-data = "The quick brown fox jumps over the lazy dog.\r\n"
-# Be slow so we don't depend on other modules
-data += "".join(map(chr, xrange(256)))
-data += "\r\nHello world.\n"
-
-def test_exceptions():
- # Check module exceptions
- assert issubclass(binascii.Error, Exception)
- assert issubclass(binascii.Incomplete, Exception)
-
-def test_functions():
- # Check presence of all functions
- funcs = []
- for suffix in "base64", "hqx", "uu", "hex":
- prefixes = ["a2b_", "b2a_"]
- if suffix == "hqx":
- prefixes.extend(["crc_", "rlecode_", "rledecode_"])
- for prefix in prefixes:
- name = prefix + suffix
- assert callable(getattr(binascii, name))
- py.test.raises(TypeError, getattr(binascii, name))
- for name in ("hexlify", "unhexlify"):
- assert callable(getattr(binascii, name))
- py.test.raises(TypeError, getattr(binascii, name))
-
-def test_base64valid():
- # Test base64 with valid data
- MAX_BASE64 = 57
- lines = []
- for i in range(0, len(data), MAX_BASE64):
- b = data[i:i+MAX_BASE64]
- a = binascii.b2a_base64(b)
- lines.append(a)
- res = ""
- for line in lines:
- b = binascii.a2b_base64(line)
- res = res + b
- assert res == data
-
-def test_base64invalid():
- # Test base64 with random invalid characters sprinkled throughout
- # (This requires a new version of binascii.)
- MAX_BASE64 = 57
- lines = []
- for i in range(0, len(data), MAX_BASE64):
- b = data[i:i+MAX_BASE64]
- a = binascii.b2a_base64(b)
- lines.append(a)
-
- fillers = ""
- valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
- for i in xrange(256):
- c = chr(i)
- if c not in valid:
- fillers += c
- def addnoise(line):
- noise = fillers
- ratio = len(line) // len(noise)
- res = ""
- while line and noise:
- if len(line) // len(noise) > ratio:
- c, line = line[0], line[1:]
- else:
- c, noise = noise[0], noise[1:]
- res += c
- return res + noise + line
- res = ""
- for line in map(addnoise, lines):
- b = binascii.a2b_base64(line)
- res += b
- assert res == data
-
- # Test base64 with just invalid characters, which should return
- # empty strings. TBD: shouldn't it raise an exception instead ?
- assert binascii.a2b_base64(fillers) == ''
-
-def test_uu():
- MAX_UU = 45
- lines = []
- for i in range(0, len(data), MAX_UU):
- b = data[i:i+MAX_UU]
- a = binascii.b2a_uu(b)
- lines.append(a)
- res = ""
- for line in lines:
- b = binascii.a2b_uu(line)
- res += b
- assert res == data
-
- assert binascii.a2b_uu("\x7f") == "\x00"*31
- assert binascii.a2b_uu("\x80") == "\x00"*32
- assert binascii.a2b_uu("\xff") == "\x00"*31
- py.test.raises(binascii.Error, binascii.a2b_uu, "\xff\x00")
- py.test.raises(binascii.Error, binascii.a2b_uu, "!!!!")
-
- py.test.raises(binascii.Error, binascii.b2a_uu, 46*"!")
-
-def test_crc32():
- crc = binascii.crc32("Test the CRC-32 of")
- crc = binascii.crc32(" this string.", crc)
- assert crc == 1571220330
-
- crc = binascii.crc32('frotz\n', 0)
- assert crc == -372923920
-
- py.test.raises(TypeError, binascii.crc32)
-
-def test_hex():
- # test hexlification
- s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
- t = binascii.b2a_hex(s)
- u = binascii.a2b_hex(t)
- assert s == u
- py.test.raises(TypeError, binascii.a2b_hex, t[:-1])
- py.test.raises(TypeError, binascii.a2b_hex, t[:-1] + 'q')
-
- # Verify the treatment of Unicode strings
- assert binascii.hexlify(unicode('a', 'ascii')) == '61'
-
-def test_qp():
- # A test for SF bug 534347 (segfaults without the proper fix)
- try:
- binascii.a2b_qp("", **{1:1})
- except TypeError:
- pass
- else:
- fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
- assert binascii.a2b_qp("= ") == "= "
- assert binascii.a2b_qp("==") == "="
- assert binascii.a2b_qp("=AX") == "=AX"
- py.test.raises(TypeError, binascii.b2a_qp, foo="bar")
- assert binascii.a2b_qp("=00\r\n=00") == "\x00\r\n\x00"
- assert binascii.b2a_qp("\xff\r\n\xff\n\xff") == "=FF\r\n=FF\r\n=FF"
- target = "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
- assert binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff") == target
-
-def test_empty_string():
- # A test for SF bug #1022953. Make sure SystemError is not raised.
- for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp',
- 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx',
- 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu',
- 'rledecode_hqx']:
- f = getattr(binascii, n)
- f('')
- binascii.crc_hqx('', 0)
-
-def test_qp_bug_case():
- assert binascii.b2a_qp('y'*77, False, False) == 'y'*75 + '=\nyy'
- assert binascii.b2a_qp(' '*77, False, False) == ' '*75 + '=\n =20'
- assert binascii.b2a_qp('y'*76, False, False) == 'y'*76
- assert binascii.b2a_qp(' '*76, False, False) == ' '*75 + '=\n=20'
-
-def test_wrong_padding():
- s = 'CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3IQ'
- py.test.raises(binascii.Error, binascii.a2b_base64, s)
-
-def test_crap_after_padding():
- s = 'xxx=axxxx'
- assert binascii.a2b_base64(s) == '\xc7\x1c'
-
-def test_wrong_args():
- # this should grow as a way longer list
- py.test.raises(TypeError, binascii.a2b_base64, 42)
diff --git a/lib_pypy/pypy_test/test_locale.py b/lib_pypy/pypy_test/test_locale.py
deleted file mode 100644
--- a/lib_pypy/pypy_test/test_locale.py
+++ /dev/null
@@ -1,79 +0,0 @@
-from __future__ import absolute_import
-import py
-import sys
-
-from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('locale.ctc.py')
-
-from lib_pypy import _locale
-
-
-def setup_module(mod):
- if sys.platform == 'darwin':
- py.test.skip("Locale support on MacOSX is minimal and cannot be tested")
-
-class TestLocale:
- def setup_class(cls):
- cls.oldlocale = _locale.setlocale(_locale.LC_NUMERIC)
- if sys.platform.startswith("win"):
- cls.tloc = "en"
- elif sys.platform.startswith("freebsd"):
- cls.tloc = "en_US.US-ASCII"
- else:
- cls.tloc = "en_US.UTF8"
- try:
- _locale.setlocale(_locale.LC_NUMERIC, cls.tloc)
- except _locale.Error:
- py.test.skip("test locale %s not supported" % cls.tloc)
-
- def teardown_class(cls):
- _locale.setlocale(_locale.LC_NUMERIC, cls.oldlocale)
-
- def test_format(self):
- py.test.skip("XXX fix or kill me")
-
- def testformat(formatstr, value, grouping = 0, output=None):
- if output:
- print "%s %% %s =? %s ..." %\
- (repr(formatstr), repr(value), repr(output)),
- else:
- print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
- result = locale.format(formatstr, value, grouping = grouping)
- assert result == output
-
- testformat("%f", 1024, grouping=1, output='1,024.000000')
- testformat("%f", 102, grouping=1, output='102.000000')
- testformat("%f", -42, grouping=1, output='-42.000000')
- testformat("%+f", -42, grouping=1, output='-42.000000')
- testformat("%20.f", -42, grouping=1, output=' -42')
- testformat("%+10.f", -4200, grouping=1, output=' -4,200')
- testformat("%-10.f", 4200, grouping=1, output='4,200 ')
-
- def test_getpreferredencoding(self):
- py.test.skip("XXX fix or kill me")
- # Invoke getpreferredencoding to make sure it does not cause exceptions
- _locale.getpreferredencoding()
-
- # Test BSD Rune locale's bug for isctype functions.
- def test_bsd_bug(self):
- def teststrop(s, method, output):
- print "%s.%s() =? %s ..." % (repr(s), method, repr(output)),
- result = getattr(s, method)()
- assert result == output
-
- oldlocale = _locale.setlocale(_locale.LC_CTYPE)
- _locale.setlocale(_locale.LC_CTYPE, self.tloc)
- try:
- teststrop('\x20', 'isspace', True)
- teststrop('\xa0', 'isspace', False)
- teststrop('\xa1', 'isspace', False)
- teststrop('\xc0', 'isalpha', False)
- teststrop('\xc0', 'isalnum', False)
- teststrop('\xc0', 'isupper', False)
- teststrop('\xc0', 'islower', False)
- teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc'])
- teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0')
- teststrop('\xcc\x85', 'lower', '\xcc\x85')
- teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0')
- finally:
- _locale.setlocale(_locale.LC_CTYPE, oldlocale)
diff --git a/lib_pypy/pypy_test/test_site_extra.py b/lib_pypy/pypy_test/test_site_extra.py
new file mode 100644
--- /dev/null
+++ b/lib_pypy/pypy_test/test_site_extra.py
@@ -0,0 +1,13 @@
+import sys, os
+
+
+def test_preimported_modules():
+ lst = ['__builtin__', '_codecs', '_warnings', 'codecs', 'encodings',
+ 'exceptions', 'signal', 'sys', 'zipimport']
+ g = os.popen("'%s' -c 'import sys; print sorted(sys.modules)'" %
+ (sys.executable,))
+ real_data = g.read()
+ g.close()
+ for name in lst:
+ quoted_name = repr(name)
+ assert quoted_name in real_data
diff --git a/lib_pypy/pypy_test/test_struct_extra.py b/lib_pypy/pypy_test/test_struct_extra.py
deleted file mode 100644
--- a/lib_pypy/pypy_test/test_struct_extra.py
+++ /dev/null
@@ -1,25 +0,0 @@
-from __future__ import absolute_import
-from lib_pypy import struct
-
-def test_simple():
- morezeros = '\x00' * (struct.calcsize('l')-4)
- assert struct.pack('<l', 16) == '\x10\x00\x00\x00' + morezeros
- assert struct.pack('4s', 'WAVE') == 'WAVE'
- assert struct.pack('<4sl', 'WAVE', 16) == 'WAVE\x10\x00\x00\x00' + morezeros
- s = 'ABCD01234567\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00'
- assert struct.unpack('<4s4H2lH', s) == ('ABCD', 0x3130, 0x3332, 0x3534,
- 0x3736, 1, 2, 3)
-
-def test_infinity():
- INFINITY = 1e200 * 1e200
- assert str(struct.unpack("!d", struct.pack("!d", INFINITY))[0]) \
- == str(INFINITY)
- assert str(struct.unpack("!d", struct.pack("!d", -INFINITY))[0]) \
- == str(-INFINITY)
-
-def test_nan():
- INFINITY = 1e200 * 1e200
- NAN = INFINITY / INFINITY
- assert str(struct.unpack("!d", '\xff\xf8\x00\x00\x00\x00\x00\x00')[0]) \
- == str(NAN)
- assert str(struct.unpack("!d", struct.pack("!d", NAN))[0]) == str(NAN)
diff --git a/lib_pypy/struct.py b/lib_pypy/struct.py
deleted file mode 100644
--- a/lib_pypy/struct.py
+++ /dev/null
@@ -1,417 +0,0 @@
-#
-# This module is a pure Python version of pypy.module.struct.
-# It is only imported if the vastly faster pypy.module.struct is not
-# compiled in. For now we keep this version for reference and
-# because pypy.module.struct is not ootype-backend-friendly yet.
-#
-
-"""Functions to convert between Python values and C structs.
-Python strings are used to hold the data representing the C struct
-and also as format strings to describe the layout of data in the C struct.
-
-The optional first format char indicates byte order, size and alignment:
- @: native order, size & alignment (default)
- =: native order, std. size & alignment
- <: little-endian, std. size & alignment
- >: big-endian, std. size & alignment
- !: same as >
-
-The remaining chars indicate types of args and must match exactly;
-these can be preceded by a decimal repeat count:
- x: pad byte (no data);
- c:char;
- b:signed byte;
- B:unsigned byte;
- h:short;
- H:unsigned short;
- i:int;
- I:unsigned int;
- l:long;
- L:unsigned long;
- f:float;
- d:double.
-Special cases (preceding decimal count indicates length):
- s:string (array of char); p: pascal string (with count byte).
-Special case (only available in native format):
- P:an integer type that is wide enough to hold a pointer.
-Special case (not in native mode unless 'long long' in platform C):
- q:long long;
- Q:unsigned long long
-Whitespace between formats is ignored.
-
-The variable struct.error is an exception raised on errors."""
-
-import math, sys
-
-# TODO: XXX Find a way to get information on native sizes and alignments
-class StructError(Exception):
- pass
-error = StructError
-def unpack_int(data,index,size,le):
- bytes = [ord(b) for b in data[index:index+size]]
- if le == 'little':
- bytes.reverse()
- number = 0L
- for b in bytes:
- number = number << 8 | b
- return int(number)
-
-def unpack_signed_int(data,index,size,le):
- number = unpack_int(data,index,size,le)
- max = 2**(size*8)
- if number > 2**(size*8 - 1) - 1:
- number = int(-1*(max - number))
- return number
-
-INFINITY = 1e200 * 1e200
-NAN = INFINITY / INFINITY
-
-def unpack_char(data,index,size,le):
- return data[index:index+size]
-
-def pack_int(number,size,le):
- x=number
- res=[]
- for i in range(size):
- res.append(chr(x&0xff))
- x >>= 8
- if le == 'big':
- res.reverse()
- return ''.join(res)
-
-def pack_signed_int(number,size,le):
- if not isinstance(number, (int,long)):
- raise StructError,"argument for i,I,l,L,q,Q,h,H must be integer"
- if number > 2**(8*size-1)-1 or number < -1*2**(8*size-1):
- raise OverflowError,"Number:%i too large to convert" % number
- return pack_int(number,size,le)
-
-def pack_unsigned_int(number,size,le):
- if not isinstance(number, (int,long)):
- raise StructError,"argument for i,I,l,L,q,Q,h,H must be integer"
- if number < 0:
- raise TypeError,"can't convert negative long to unsigned"
- if number > 2**(8*size)-1:
- raise OverflowError,"Number:%i too large to convert" % number
- return pack_int(number,size,le)
-
-def pack_char(char,size,le):
- return str(char)
-
-def isinf(x):
- return x != 0.0 and x / 2 == x
-def isnan(v):
- return v != v*1.0 or (v == 1.0 and v == 2.0)
-
-def pack_float(x, size, le):
- unsigned = float_pack(x, size)
- result = []
- for i in range(8):
- result.append(chr((unsigned >> (i * 8)) & 0xFF))
- if le == "big":
- result.reverse()
- return ''.join(result)
-
-def unpack_float(data, index, size, le):
- binary = [data[i] for i in range(index, index + 8)]
- if le == "big":
- binary.reverse()
- unsigned = 0
- for i in range(8):
- unsigned |= ord(binary[i]) << (i * 8)
- return float_unpack(unsigned, size, le)
-
-def round_to_nearest(x):
- """Python 3 style round: round a float x to the nearest int, but
- unlike the builtin Python 2.x round function:
-
- - return an int, not a float
- - do round-half-to-even, not round-half-away-from-zero.
-
- We assume that x is finite and nonnegative; except wrong results
- if you use this for negative x.
-
- """
- int_part = int(x)
- frac_part = x - int_part
- if frac_part > 0.5 or frac_part == 0.5 and int_part & 1 == 1:
- int_part += 1
- return int_part
-
-def float_unpack(Q, size, le):
- """Convert a 32-bit or 64-bit integer created
- by float_pack into a Python float."""
-
- if size == 8:
- MIN_EXP = -1021 # = sys.float_info.min_exp
- MAX_EXP = 1024 # = sys.float_info.max_exp
- MANT_DIG = 53 # = sys.float_info.mant_dig
- BITS = 64
- elif size == 4:
- MIN_EXP = -125 # C's FLT_MIN_EXP
- MAX_EXP = 128 # FLT_MAX_EXP
- MANT_DIG = 24 # FLT_MANT_DIG
- BITS = 32
- else:
- raise ValueError("invalid size value")
-
- if Q >> BITS:
- raise ValueError("input out of range")
-
- # extract pieces
- sign = Q >> BITS - 1
- exp = (Q & ((1 << BITS - 1) - (1 << MANT_DIG - 1))) >> MANT_DIG - 1
- mant = Q & ((1 << MANT_DIG - 1) - 1)
-
- if exp == MAX_EXP - MIN_EXP + 2:
- # nan or infinity
- result = float('nan') if mant else float('inf')
- elif exp == 0:
- # subnormal or zero
- result = math.ldexp(float(mant), MIN_EXP - MANT_DIG)
- else:
- # normal
- mant += 1 << MANT_DIG - 1
- result = math.ldexp(float(mant), exp + MIN_EXP - MANT_DIG - 1)
- return -result if sign else result
-
-
-def float_pack(x, size):
- """Convert a Python float x into a 64-bit unsigned integer
- with the same byte representation."""
-
- if size == 8:
- MIN_EXP = -1021 # = sys.float_info.min_exp
- MAX_EXP = 1024 # = sys.float_info.max_exp
- MANT_DIG = 53 # = sys.float_info.mant_dig
- BITS = 64
- elif size == 4:
- MIN_EXP = -125 # C's FLT_MIN_EXP
- MAX_EXP = 128 # FLT_MAX_EXP
- MANT_DIG = 24 # FLT_MANT_DIG
- BITS = 32
- else:
- raise ValueError("invalid size value")
-
- sign = math.copysign(1.0, x) < 0.0
- if math.isinf(x):
- mant = 0
- exp = MAX_EXP - MIN_EXP + 2
- elif math.isnan(x):
- mant = 1 << (MANT_DIG-2) # other values possible
- exp = MAX_EXP - MIN_EXP + 2
- elif x == 0.0:
- mant = 0
- exp = 0
- else:
- m, e = math.frexp(abs(x)) # abs(x) == m * 2**e
- exp = e - (MIN_EXP - 1)
- if exp > 0:
- # Normal case.
- mant = round_to_nearest(m * (1 << MANT_DIG))
- mant -= 1 << MANT_DIG - 1
- else:
- # Subnormal case.
- if exp + MANT_DIG - 1 >= 0:
- mant = round_to_nearest(m * (1 << exp + MANT_DIG - 1))
- else:
- mant = 0
- exp = 0
-
- # Special case: rounding produced a MANT_DIG-bit mantissa.
- assert 0 <= mant <= 1 << MANT_DIG - 1
- if mant == 1 << MANT_DIG - 1:
- mant = 0
- exp += 1
-
- # Raise on overflow (in some circumstances, may want to return
- # infinity instead).
- if exp >= MAX_EXP - MIN_EXP + 2:
- raise OverflowError("float too large to pack in this format")
-
- # check constraints
- assert 0 <= mant < 1 << MANT_DIG - 1
- assert 0 <= exp <= MAX_EXP - MIN_EXP + 2
- assert 0 <= sign <= 1
- return ((sign << BITS - 1) | (exp << MANT_DIG - 1)) | mant
-
-
-big_endian_format = {
- 'x':{ 'size' : 1, 'alignment' : 0, 'pack' : None, 'unpack' : None},
- 'b':{ 'size' : 1, 'alignment' : 0, 'pack' : pack_signed_int, 'unpack' : unpack_signed_int},
- 'B':{ 'size' : 1, 'alignment' : 0, 'pack' : pack_unsigned_int, 'unpack' : unpack_int},
- 'c':{ 'size' : 1, 'alignment' : 0, 'pack' : pack_char, 'unpack' : unpack_char},
- 's':{ 'size' : 1, 'alignment' : 0, 'pack' : None, 'unpack' : None},
- 'p':{ 'size' : 1, 'alignment' : 0, 'pack' : None, 'unpack' : None},
- 'h':{ 'size' : 2, 'alignment' : 0, 'pack' : pack_signed_int, 'unpack' : unpack_signed_int},
- 'H':{ 'size' : 2, 'alignment' : 0, 'pack' : pack_unsigned_int, 'unpack' : unpack_int},
- 'i':{ 'size' : 4, 'alignment' : 0, 'pack' : pack_signed_int, 'unpack' : unpack_signed_int},
- 'I':{ 'size' : 4, 'alignment' : 0, 'pack' : pack_unsigned_int, 'unpack' : unpack_int},
- 'l':{ 'size' : 4, 'alignment' : 0, 'pack' : pack_signed_int, 'unpack' : unpack_signed_int},
- 'L':{ 'size' : 4, 'alignment' : 0, 'pack' : pack_unsigned_int, 'unpack' : unpack_int},
- 'q':{ 'size' : 8, 'alignment' : 0, 'pack' : pack_signed_int, 'unpack' : unpack_signed_int},
- 'Q':{ 'size' : 8, 'alignment' : 0, 'pack' : pack_unsigned_int, 'unpack' : unpack_int},
- 'f':{ 'size' : 4, 'alignment' : 0, 'pack' : pack_float, 'unpack' : unpack_float},
- 'd':{ 'size' : 8, 'alignment' : 0, 'pack' : pack_float, 'unpack' : unpack_float},
- }
-default = big_endian_format
-formatmode={ '<' : (default, 'little'),
- '>' : (default, 'big'),
- '!' : (default, 'big'),
- '=' : (default, sys.byteorder),
- '@' : (default, sys.byteorder)
- }
-
-def getmode(fmt):
- try:
- formatdef,endianness = formatmode[fmt[0]]
- index = 1
- except KeyError:
- formatdef,endianness = formatmode['@']
- index = 0
- return formatdef,endianness,index
-def getNum(fmt,i):
- num=None
- cur = fmt[i]
- while ('0'<= cur ) and ( cur <= '9'):
- if num == None:
- num = int(cur)
- else:
- num = 10*num + int(cur)
- i += 1
- cur = fmt[i]
- return num,i
-
-def calcsize(fmt):
- """calcsize(fmt) -> int
- Return size of C struct described by format string fmt.
- See struct.__doc__ for more on format strings."""
-
- formatdef,endianness,i = getmode(fmt)
- num = 0
- result = 0
- while i<len(fmt):
- num,i = getNum(fmt,i)
- cur = fmt[i]
- try:
- format = formatdef[cur]
- except KeyError:
- raise StructError,"%s is not a valid format"%cur
- if num != None :
- result += num*format['size']
- else:
- result += format['size']
- num = 0
- i += 1
- return result
-
-def pack(fmt,*args):
- """pack(fmt, v1, v2, ...) -> string
- Return string containing values v1, v2, ... packed according to fmt.
- See struct.__doc__ for more on format strings."""
- formatdef,endianness,i = getmode(fmt)
- args = list(args)
- n_args = len(args)
- result = []
- while i<len(fmt):
- num,i = getNum(fmt,i)
- cur = fmt[i]
- try:
- format = formatdef[cur]
- except KeyError:
- raise StructError,"%s is not a valid format"%cur
- if num == None :
- num_s = 0
- num = 1
- else:
- num_s = num
-
- if cur == 'x':
- result += ['\0'*num]
- elif cur == 's':
- if isinstance(args[0], str):
- padding = num - len(args[0])
- result += [args[0][:num] + '\0'*padding]
- args.pop(0)
- else:
- raise StructError,"arg for string format not a string"
- elif cur == 'p':
- if isinstance(args[0], str):
- padding = num - len(args[0]) - 1
-
- if padding > 0:
- result += [chr(len(args[0])) + args[0][:num-1] + '\0'*padding]
- else:
- if num<255:
- result += [chr(num-1) + args[0][:num-1]]
- else:
- result += [chr(255) + args[0][:num-1]]
- args.pop(0)
- else:
- raise StructError,"arg for string format not a string"
-
- else:
- if len(args) < num:
- raise StructError,"insufficient arguments to pack"
- for var in args[:num]:
- result += [format['pack'](var,format['size'],endianness)]
- args=args[num:]
- num = None
- i += 1
- if len(args) != 0:
- raise StructError,"too many arguments for pack format"
- return ''.join(result)
-
-def unpack(fmt,data):
- """unpack(fmt, string) -> (v1, v2, ...)
- Unpack the string, containing packed C structure data, according
- to fmt. Requires len(string)==calcsize(fmt).
- See struct.__doc__ for more on format strings."""
- formatdef,endianness,i = getmode(fmt)
- j = 0
- num = 0
- result = []
- length= calcsize(fmt)
- if length != len (data):
- raise StructError,"unpack str size does not match format"
- while i<len(fmt):
- num,i=getNum(fmt,i)
- cur = fmt[i]
- i += 1
- try:
- format = formatdef[cur]
- except KeyError:
- raise StructError,"%s is not a valid format"%cur
-
- if not num :
- num = 1
-
- if cur == 'x':
- j += num
- elif cur == 's':
- result.append(data[j:j+num])
- j += num
- elif cur == 'p':
- n=ord(data[j])
- if n >= num:
- n = num-1
- result.append(data[j+1:j+n+1])
- j += num
- else:
- for n in range(num):
- result += [format['unpack'](data,j,format['size'],endianness)]
- j += format['size']
-
- return tuple(result)
-
-def pack_into(fmt, buf, offset, *args):
- data = pack(fmt, *args)
- buffer(buf)[offset:offset+len(data)] = data
-
-def unpack_from(fmt, buf, offset=0):
- size = calcsize(fmt)
- data = buffer(buf)[offset:offset+size]
- if len(data) != size:
- raise error("unpack_from requires a buffer of at least %d bytes"
- % (size,))
- return unpack(fmt, data)
diff --git a/pypy/__init__.py b/pypy/__init__.py
--- a/pypy/__init__.py
+++ b/pypy/__init__.py
@@ -1,1 +1,16 @@
# Empty
+
+# XXX Should be empty again, soon.
+# XXX hack for win64:
+# This patch must stay here until the END OF STAGE 1
+# When all tests work, this branch will be merged
+# and the branch stage 2 is started, where we remove this patch.
+import sys
+if hasattr(sys, "maxsize"):
+ if sys.maxint != sys.maxsize:
+ sys.maxint = sys.maxsize
+ import warnings
+ warnings.warn("""\n
+---> This win64 port is now in stage 1: sys.maxint was modified.
+---> When pypy/__init__.py becomes empty again, we have reached stage 2.
+""")
diff --git a/pypy/annotation/classdef.py b/pypy/annotation/classdef.py
--- a/pypy/annotation/classdef.py
+++ b/pypy/annotation/classdef.py
@@ -148,7 +148,6 @@
"the attribute here; the list of read locations is:\n" +
'\n'.join([str(loc[0]) for loc in self.read_locations]))
-
class ClassDef(object):
"Wraps a user class."
diff --git a/pypy/doc/project-ideas.rst b/pypy/doc/project-ideas.rst
--- a/pypy/doc/project-ideas.rst
+++ b/pypy/doc/project-ideas.rst
@@ -103,21 +103,13 @@
* A concurrent garbage collector (a lot of work)
-Remove the GIL
---------------
+STM, a.k.a. "remove the GIL"
+----------------------------
-This is a major task that requires lots of thinking. However, few subprojects
-can be potentially specified, unless a better plan can be thought out:
+Removing the GIL --- or more precisely, a GIL-less thread-less solution ---
+is `now work in progress.`__ Contributions welcome.
-* A thread-aware garbage collector
-
-* Better RPython primitives for dealing with concurrency
-
-* JIT passes to remove locks on objects
-
-* (maybe) implement locking in Python interpreter
-
-* alternatively, look at Software Transactional Memory
+.. __: http://pypy.org/tmdonate.html
Introduce new benchmarks
------------------------
diff --git a/pypy/doc/sandbox.rst b/pypy/doc/sandbox.rst
--- a/pypy/doc/sandbox.rst
+++ b/pypy/doc/sandbox.rst
@@ -82,7 +82,10 @@
In pypy/translator/goal::
- ./translate.py --sandbox targetpypystandalone.py
+ ./translate.py -O2 --sandbox targetpypystandalone.py
+
+If you don't have a regular PyPy installed, you should, because it's
+faster to translate, but you can also run ``python translate.py`` instead.
To run it, use the tools in the pypy/translator/sandbox directory::
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1336,7 +1336,7 @@
if not self.is_true(self.isinstance(w_obj, self.w_str)):
raise OperationError(self.w_TypeError,
self.wrap('argument must be a string'))
- return self.str_w(w_obj)
+ return self.str_w(w_obj)
def unicode_w(self, w_obj):
return w_obj.unicode_w(self)
diff --git a/pypy/interpreter/test/test_objspace.py b/pypy/interpreter/test/test_objspace.py
--- a/pypy/interpreter/test/test_objspace.py
+++ b/pypy/interpreter/test/test_objspace.py
@@ -312,8 +312,8 @@
mods = space.get_builtinmodule_to_install()
assert '__pypy__' in mods # real builtin
- assert 'array' not in mods # in lib_pypy
- assert 'faked+array' not in mods # in lib_pypy
+ assert '_functools' not in mods # in lib_pypy
+ assert 'faked+_functools' not in mods # in lib_pypy
assert 'this_doesnt_exist' not in mods # not in lib_pypy
assert 'faked+this_doesnt_exist' in mods # not in lib_pypy, but in
# ALL_BUILTIN_MODULES
diff --git a/pypy/interpreter/test/test_zzpickle_and_slow.py b/pypy/interpreter/test/test_zzpickle_and_slow.py
--- a/pypy/interpreter/test/test_zzpickle_and_slow.py
+++ b/pypy/interpreter/test/test_zzpickle_and_slow.py
@@ -75,6 +75,7 @@
class AppTestInterpObjectPickling:
pytestmark = py.test.mark.skipif("config.option.runappdirect")
def setup_class(cls):
+ cls.space = gettestobjspace(usemodules=['struct'])
_attach_helpers(cls.space)
def teardown_class(cls):
diff --git a/pypy/jit/backend/test/runner_test.py b/pypy/jit/backend/test/runner_test.py
--- a/pypy/jit/backend/test/runner_test.py
+++ b/pypy/jit/backend/test/runner_test.py
@@ -5,7 +5,7 @@
BoxInt, Box, BoxPtr,
JitCellToken, TargetToken,
ConstInt, ConstPtr,
- BoxObj, Const,
+ BoxObj,
ConstObj, BoxFloat, ConstFloat)
from pypy.jit.metainterp.resoperation import ResOperation, rop
from pypy.jit.metainterp.typesystem import deref
@@ -16,9 +16,11 @@
from pypy.rpython.annlowlevel import llhelper
from pypy.rpython.llinterp import LLException
from pypy.jit.codewriter import heaptracker, longlong
+from pypy.rlib import longlong2float
from pypy.rlib.rarithmetic import intmask, is_valid_int
from pypy.jit.backend.detect_cpu import autodetect_main_model_and_size
+
def boxfloat(x):
return BoxFloat(longlong.getfloatstorage(x))
@@ -1655,13 +1657,28 @@
def test_read_timestamp(self):
if not self.cpu.supports_longlong:
py.test.skip("longlong test")
+ # so we stretch the time a little bit.
+ # On my virtual Parallels machine in a 2GHz Core i7 Mac Mini,
+ # the test starts working at delay == 21670 and stops at 20600000.
+ # We take the geometric mean value.
+ from math import log, exp
+ delay_min = 21670
+ delay_max = 20600000
+ delay = int(exp((log(delay_min)+log(delay_max))/2))
+ def wait_a_bit():
+ for i in xrange(delay): pass
+ else:
+ def wait_a_bit():
+ pass
if longlong.is_64_bit:
got1 = self.execute_operation(rop.READ_TIMESTAMP, [], 'int')
+ wait_a_bit()
got2 = self.execute_operation(rop.READ_TIMESTAMP, [], 'int')
res1 = got1.getint()
res2 = got2.getint()
else:
got1 = self.execute_operation(rop.READ_TIMESTAMP, [], 'float')
+ wait_a_bit()
got2 = self.execute_operation(rop.READ_TIMESTAMP, [], 'float')
res1 = got1.getlonglong()
res2 = got2.getlonglong()
@@ -1758,6 +1775,12 @@
[BoxPtr(x)], 'int').value
assert res == -19
+ def test_convert_float_bytes(self):
+ t = 'int' if longlong.is_64_bit else 'float'
+ res = self.execute_operation(rop.CONVERT_FLOAT_BYTES_TO_LONGLONG,
+ [boxfloat(2.5)], t).value
+ assert res == longlong2float.float2longlong(2.5)
+
def test_ooops_non_gc(self):
x = lltype.malloc(lltype.Struct('x'), flavor='raw')
v = heaptracker.adr2int(llmemory.cast_ptr_to_adr(x))
@@ -3326,6 +3349,7 @@
lines = [line for line in mc if line.count('\t') >= 2]
checkops(lines, self.bridge_loop_instructions)
+
def test_compile_bridge_with_target(self):
# This test creates a loopy piece of code in a bridge, and builds another
# unrelated loop that ends in a jump directly to this loopy bit of code.
diff --git a/pypy/jit/backend/test/test_random.py b/pypy/jit/backend/test/test_random.py
--- a/pypy/jit/backend/test/test_random.py
+++ b/pypy/jit/backend/test/test_random.py
@@ -449,6 +449,7 @@
OPERATIONS.append(CastFloatToIntOperation(rop.CAST_FLOAT_TO_INT))
OPERATIONS.append(CastIntToFloatOperation(rop.CAST_INT_TO_FLOAT))
+OPERATIONS.append(CastFloatToIntOperation(rop.CONVERT_FLOAT_BYTES_TO_LONGLONG))
OperationBuilder.OPERATIONS = OPERATIONS
@@ -502,11 +503,11 @@
else:
assert 0, "unknown backend %r" % pytest.config.option.backend
-# ____________________________________________________________
+# ____________________________________________________________
class RandomLoop(object):
dont_generate_more = False
-
+
def __init__(self, cpu, builder_factory, r, startvars=None):
self.cpu = cpu
if startvars is None:
diff --git a/pypy/jit/backend/x86/assembler.py b/pypy/jit/backend/x86/assembler.py
--- a/pypy/jit/backend/x86/assembler.py
+++ b/pypy/jit/backend/x86/assembler.py
@@ -606,7 +606,7 @@
else:
assert token
struct.number = compute_unique_id(token)
- self.loop_run_counters.append(struct)
+ self.loop_run_counters.append(struct)
return struct
def _find_failure_recovery_bytecode(self, faildescr):
@@ -665,7 +665,7 @@
ResOperation(rop.SETFIELD_RAW, [c_adr, box2],
None, descr=self.debug_counter_descr)]
operations.extend(ops)
-
+
@specialize.argtype(1)
def _inject_debugging_code(self, looptoken, operations, tp, number):
if self._debug:
@@ -836,8 +836,8 @@
self.mc.MOVSD_sx(0, loc.value)
elif WORD == 4 and isinstance(loc, StackLoc) and loc.get_width() == 8:
# XXX evil trick
- self.mc.PUSH_b(get_ebp_ofs(loc.position))
- self.mc.PUSH_b(get_ebp_ofs(loc.position + 1))
+ self.mc.PUSH_b(loc.value + 4)
+ self.mc.PUSH_b(loc.value)
else:
self.mc.PUSH(loc)
@@ -847,8 +847,8 @@
self.mc.ADD_ri(esp.value, 8) # = size of doubles
elif WORD == 4 and isinstance(loc, StackLoc) and loc.get_width() == 8:
# XXX evil trick
- self.mc.POP_b(get_ebp_ofs(loc.position + 1))
- self.mc.POP_b(get_ebp_ofs(loc.position))
+ self.mc.POP_b(loc.value)
+ self.mc.POP_b(loc.value + 4)
else:
self.mc.POP(loc)
@@ -1242,6 +1242,15 @@
self.mc.MOVD_xr(resloc.value, loc0.value)
self.mc.CVTSS2SD_xx(resloc.value, resloc.value)
+ def genop_convert_float_bytes_to_longlong(self, op, arglocs, resloc):
+ loc0, = arglocs
+ if longlong.is_64_bit:
+ assert isinstance(resloc, RegLoc)
+ assert isinstance(loc0, RegLoc)
+ self.mc.MOVD(resloc, loc0)
+ else:
+ self.mov(loc0, resloc)
+
def genop_guard_int_is_true(self, op, guard_op, guard_token, arglocs, resloc):
guard_opnum = guard_op.getopnum()
self.mc.CMP(arglocs[0], imm0)
@@ -1954,8 +1963,6 @@
mc.PUSH_r(ebx.value)
elif IS_X86_64:
mc.MOV_rr(edi.value, ebx.value)
- # XXX: Correct to only align the stack on 64-bit?
- mc.AND_ri(esp.value, -16)
else:
raise AssertionError("Shouldn't happen")
@@ -2117,9 +2124,12 @@
# First, we need to save away the registers listed in
# 'save_registers' that are not callee-save. XXX We assume that
# the XMM registers won't be modified. We store them in
- # [ESP+4], [ESP+8], etc., leaving enough room in [ESP] for the
- # single argument to closestack_addr below.
- p = WORD
+ # [ESP+4], [ESP+8], etc.; on x86-32 we leave enough room in [ESP]
+ # for the single argument to closestack_addr below.
+ if IS_X86_32:
+ p = WORD
+ elif IS_X86_64:
+ p = 0
for reg in self._regalloc.rm.save_around_call_regs:
if reg in save_registers:
self.mc.MOV_sr(p, reg.value)
@@ -2174,7 +2184,10 @@
#
self._emit_call(-1, imm(self.releasegil_addr), args)
# Finally, restore the registers saved above.
- p = WORD
+ if IS_X86_32:
+ p = WORD
+ elif IS_X86_64:
+ p = 0
for reg in self._regalloc.rm.save_around_call_regs:
if reg in save_registers:
self.mc.MOV_rs(reg.value, p)
diff --git a/pypy/jit/backend/x86/regalloc.py b/pypy/jit/backend/x86/regalloc.py
--- a/pypy/jit/backend/x86/regalloc.py
+++ b/pypy/jit/backend/x86/regalloc.py
@@ -708,6 +708,18 @@
consider_cast_singlefloat_to_float = consider_cast_int_to_float
+ def consider_convert_float_bytes_to_longlong(self, op):
+ if longlong.is_64_bit:
+ loc0 = self.xrm.make_sure_var_in_reg(op.getarg(0))
+ loc1 = self.rm.force_allocate_reg(op.result)
+ self.Perform(op, [loc0], loc1)
+ self.xrm.possibly_free_var(op.getarg(0))
+ else:
+ loc0 = self.xrm.loc(op.getarg(0))
+ loc1 = self.xrm.force_allocate_reg(op.result)
+ self.Perform(op, [loc0], loc1)
+ self.xrm.possibly_free_var(op.getarg(0))
+
def _consider_llong_binop_xx(self, op):
# must force both arguments into xmm registers, because we don't
# know if they will be suitably aligned. Exception: if the second
diff --git a/pypy/jit/backend/x86/rx86.py b/pypy/jit/backend/x86/rx86.py
--- a/pypy/jit/backend/x86/rx86.py
+++ b/pypy/jit/backend/x86/rx86.py
@@ -601,9 +601,12 @@
CVTSS2SD_xb = xmminsn('\xF3', rex_nw, '\x0F\x5A',
register(1, 8), stack_bp(2))
- MOVD_rx = xmminsn('\x66', rex_nw, '\x0F\x7E', register(2, 8), register(1), '\xC0')
- MOVD_xr = xmminsn('\x66', rex_nw, '\x0F\x6E', register(1, 8), register(2), '\xC0')
- MOVD_xb = xmminsn('\x66', rex_nw, '\x0F\x6E', register(1, 8), stack_bp(2))
+ # These work on machine sized registers, so MOVD is actually MOVQ
+ # when running on 64 bits. Note a bug in the Intel documentation:
+ # http://lists.gnu.org/archive/html/bug-binutils/2007-07/msg00095.html
+ MOVD_rx = xmminsn('\x66', rex_w, '\x0F\x7E', register(2, 8), register(1), '\xC0')
+ MOVD_xr = xmminsn('\x66', rex_w, '\x0F\x6E', register(1, 8), register(2), '\xC0')
+ MOVD_xb = xmminsn('\x66', rex_w, '\x0F\x6E', register(1, 8), stack_bp(2))
PSRAD_xi = xmminsn('\x66', rex_nw, '\x0F\x72', register(1), '\xE0', immediate(2, 'b'))
diff --git a/pypy/jit/backend/x86/test/test_rx86_32_auto_encoding.py b/pypy/jit/backend/x86/test/test_rx86_32_auto_encoding.py
--- a/pypy/jit/backend/x86/test/test_rx86_32_auto_encoding.py
+++ b/pypy/jit/backend/x86/test/test_rx86_32_auto_encoding.py
@@ -182,6 +182,12 @@
filename = str(testdir.join(FILENAME % methname))
g = open(inputname, 'w')
g.write('\x09.string "%s"\n' % BEGIN_TAG)
+ #
+ if instrname == 'MOVD' and self.WORD == 8:
+ instrname = 'MOVQ'
+ if argmodes == 'xb':
+ py.test.skip('"as" uses an undocumented alternate encoding??')
+ #
for args in args_lists:
suffix = ""
## all = instr.as_all_suffixes
@@ -229,9 +235,6 @@
# movq $xxx, %rax => movl $xxx, %eax
suffix = 'l'
ops[1] = reduce_to_32bit(ops[1])
- if instrname.lower() == 'movd':
- ops[0] = reduce_to_32bit(ops[0])
- ops[1] = reduce_to_32bit(ops[1])
#
op = '\t%s%s %s%s' % (instrname.lower(), suffix,
', '.join(ops), following)
diff --git a/pypy/jit/codewriter/jtransform.py b/pypy/jit/codewriter/jtransform.py
--- a/pypy/jit/codewriter/jtransform.py
+++ b/pypy/jit/codewriter/jtransform.py
@@ -291,6 +291,11 @@
op1 = SpaceOperation('-live-', [], None)
return [op, op1]
+ def _noop_rewrite(self, op):
+ return op
+
+ rewrite_op_convert_float_bytes_to_longlong = _noop_rewrite
+
# ----------
# Various kinds of calls
diff --git a/pypy/jit/codewriter/test/test_flatten.py b/pypy/jit/codewriter/test/test_flatten.py
--- a/pypy/jit/codewriter/test/test_flatten.py
+++ b/pypy/jit/codewriter/test/test_flatten.py
@@ -968,6 +968,21 @@
int_return %i2
""", transform=True)
+ def test_convert_float_bytes_to_int(self):
+ from pypy.rlib.longlong2float import float2longlong
+ def f(x):
+ return float2longlong(x)
+ if longlong.is_64_bit:
+ result_var = "%i0"
+ return_op = "int_return"
+ else:
+ result_var = "%f1"
+ return_op = "float_return"
+ self.encoding_test(f, [25.0], """
+ convert_float_bytes_to_longlong %%f0 -> %(result_var)s
+ %(return_op)s %(result_var)s
+ """ % {"result_var": result_var, "return_op": return_op})
+
def check_force_cast(FROM, TO, operations, value):
"""Check that the test is correctly written..."""
diff --git a/pypy/jit/metainterp/blackhole.py b/pypy/jit/metainterp/blackhole.py
--- a/pypy/jit/metainterp/blackhole.py
+++ b/pypy/jit/metainterp/blackhole.py
@@ -1,15 +1,16 @@
+from pypy.jit.codewriter import heaptracker, longlong
+from pypy.jit.codewriter.jitcode import JitCode, SwitchDictDescr
+from pypy.jit.metainterp.compile import ResumeAtPositionDescr
+from pypy.jit.metainterp.jitexc import JitException, get_llexception, reraise
+from pypy.rlib import longlong2float
+from pypy.rlib.debug import debug_start, debug_stop, ll_assert, make_sure_not_resized
+from pypy.rlib.objectmodel import we_are_translated
+from pypy.rlib.rarithmetic import intmask, LONG_BIT, r_uint, ovfcheck
+from pypy.rlib.rtimer import read_timestamp
from pypy.rlib.unroll import unrolling_iterable
-from pypy.rlib.rtimer import read_timestamp
-from pypy.rlib.rarithmetic import intmask, LONG_BIT, r_uint, ovfcheck
-from pypy.rlib.objectmodel import we_are_translated
-from pypy.rlib.debug import debug_start, debug_stop, ll_assert
-from pypy.rlib.debug import make_sure_not_resized
from pypy.rpython.lltypesystem import lltype, llmemory, rclass
from pypy.rpython.lltypesystem.lloperation import llop
-from pypy.jit.codewriter.jitcode import JitCode, SwitchDictDescr
-from pypy.jit.codewriter import heaptracker, longlong
-from pypy.jit.metainterp.jitexc import JitException, get_llexception, reraise
-from pypy.jit.metainterp.compile import ResumeAtPositionDescr
+
def arguments(*argtypes, **kwds):
resulttype = kwds.pop('returns', None)
@@ -20,6 +21,9 @@
return function
return decorate
+LONGLONG_TYPECODE = 'i' if longlong.is_64_bit else 'f'
+
+
class LeaveFrame(JitException):
pass
@@ -663,6 +667,11 @@
a = float(a)
return longlong.getfloatstorage(a)
+ @arguments("f", returns=LONGLONG_TYPECODE)
+ def bhimpl_convert_float_bytes_to_longlong(a):
+ a = longlong.getrealfloat(a)
+ return longlong2float.float2longlong(a)
+
# ----------
# control flow operations
@@ -1309,7 +1318,7 @@
def bhimpl_copyunicodecontent(cpu, src, dst, srcstart, dststart, length):
cpu.bh_copyunicodecontent(src, dst, srcstart, dststart, length)
- @arguments(returns=(longlong.is_64_bit and "i" or "f"))
+ @arguments(returns=LONGLONG_TYPECODE)
def bhimpl_ll_read_timestamp():
return read_timestamp()
diff --git a/pypy/jit/metainterp/pyjitpl.py b/pypy/jit/metainterp/pyjitpl.py
--- a/pypy/jit/metainterp/pyjitpl.py
+++ b/pypy/jit/metainterp/pyjitpl.py
@@ -223,6 +223,7 @@
'cast_float_to_singlefloat', 'cast_singlefloat_to_float',
'float_neg', 'float_abs',
'cast_ptr_to_int', 'cast_int_to_ptr',
+ 'convert_float_bytes_to_longlong',
]:
exec py.code.Source('''
@arguments("box")
diff --git a/pypy/jit/metainterp/resoperation.py b/pypy/jit/metainterp/resoperation.py
--- a/pypy/jit/metainterp/resoperation.py
+++ b/pypy/jit/metainterp/resoperation.py
@@ -419,6 +419,7 @@
'CAST_INT_TO_FLOAT/1', # need some messy code in the backend
'CAST_FLOAT_TO_SINGLEFLOAT/1',
'CAST_SINGLEFLOAT_TO_FLOAT/1',
+ 'CONVERT_FLOAT_BYTES_TO_LONGLONG/1',
#
'INT_LT/2b',
'INT_LE/2b',
diff --git a/pypy/jit/metainterp/test/test_ajit.py b/pypy/jit/metainterp/test/test_ajit.py
--- a/pypy/jit/metainterp/test/test_ajit.py
+++ b/pypy/jit/metainterp/test/test_ajit.py
@@ -3,6 +3,7 @@
import py
from pypy import conftest
+from pypy.jit.codewriter import longlong
from pypy.jit.codewriter.policy import JitPolicy, StopAtXPolicy
from pypy.jit.metainterp import pyjitpl, history
from pypy.jit.metainterp.optimizeopt import ALL_OPTS_DICT
@@ -14,6 +15,7 @@
loop_invariant, elidable, promote, jit_debug, assert_green,
AssertGreenFailed, unroll_safe, current_trace_length, look_inside_iff,
isconstant, isvirtual, promote_string, set_param, record_known_class)
+from pypy.rlib.longlong2float import float2longlong
from pypy.rlib.rarithmetic import ovfcheck, is_valid_int
from pypy.rpython.lltypesystem import lltype, llmemory, rffi
from pypy.rpython.ootypesystem import ootype
@@ -292,7 +294,7 @@
assert res == f(6, sys.maxint, 32, 48)
res = self.meta_interp(f, [sys.maxint, 6, 32, 48])
assert res == f(sys.maxint, 6, 32, 48)
-
+
def test_loop_invariant_intbox(self):
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x'])
@@ -953,7 +955,7 @@
self.meta_interp(f, [20], repeat=7)
# the loop and the entry path as a single trace
self.check_jitcell_token_count(1)
-
+
# we get:
# ENTER - compile the new loop and the entry bridge
# ENTER - compile the leaving path
@@ -1470,7 +1472,7 @@
assert res == f(299)
self.check_resops(guard_class=0, guard_nonnull=4,
guard_nonnull_class=4, guard_isnull=2)
-
+
def test_merge_guardnonnull_guardvalue(self):
from pypy.rlib.objectmodel import instantiate
@@ -1499,7 +1501,7 @@
assert res == f(299)
self.check_resops(guard_value=4, guard_class=0, guard_nonnull=4,
guard_nonnull_class=0, guard_isnull=2)
-
+
def test_merge_guardnonnull_guardvalue_2(self):
from pypy.rlib.objectmodel import instantiate
@@ -1528,7 +1530,7 @@
assert res == f(299)
self.check_resops(guard_value=4, guard_class=0, guard_nonnull=4,
guard_nonnull_class=0, guard_isnull=2)
-
+
def test_merge_guardnonnull_guardclass_guardvalue(self):
from pypy.rlib.objectmodel import instantiate
@@ -2636,7 +2638,7 @@
return sa
assert self.meta_interp(f, [20]) == f(20)
self.check_resops(int_lt=6, int_le=2, int_ge=4, int_gt=3)
-
+
def test_intbounds_not_generalized2(self):
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'node'])
@@ -2677,7 +2679,7 @@
assert self.meta_interp(f, [20, 3]) == f(20, 3)
self.check_jitcell_token_count(1)
self.check_target_token_count(5)
-
+
def test_max_retrace_guards(self):
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a'])
@@ -2815,7 +2817,7 @@
for cell in get_stats().get_all_jitcell_tokens():
# Initialal trace with two labels and 5 retraces
assert len(cell.target_tokens) <= 7
-
+
def test_nested_retrace(self):
myjitdriver = JitDriver(greens = ['pc'], reds = ['n', 'a', 'i', 'j', 'sa'])
@@ -3793,6 +3795,16 @@
res = self.interp_operations(g, [1])
assert res == 3
+ def test_float2longlong(self):
+ def f(n):
+ return float2longlong(n)
+
+ for x in [2.5, float("nan"), -2.5, float("inf")]:
+ # There are tests elsewhere to verify the correctness of this.
+ expected = float2longlong(x)
+ res = self.interp_operations(f, [x])
+ assert longlong.getfloatstorage(res) == expected
+
class TestLLtype(BaseLLtypeTests, LLJitMixin):
def test_tagged(self):
diff --git a/pypy/module/__builtin__/interp_memoryview.py b/pypy/module/__builtin__/interp_memoryview.py
--- a/pypy/module/__builtin__/interp_memoryview.py
+++ b/pypy/module/__builtin__/interp_memoryview.py
@@ -69,6 +69,10 @@
return W_MemoryView(buf)
def descr_buffer(self, space):
+ """Note that memoryview() objects in PyPy support buffer(), whereas
+ not in CPython; but CPython supports passing memoryview() to most
+ built-in functions that accept buffers, with the notable exception
+ of the buffer() built-in."""
return space.wrap(self.buf)
def descr_tobytes(self, space):
diff --git a/pypy/module/_ast/test/test_ast.py b/pypy/module/_ast/test/test_ast.py
--- a/pypy/module/_ast/test/test_ast.py
+++ b/pypy/module/_ast/test/test_ast.py
@@ -1,9 +1,10 @@
import py
-
+from pypy.conftest import gettestobjspace
class AppTestAST:
def setup_class(cls):
+ cls.space = gettestobjspace(usemodules=['struct'])
cls.w_ast = cls.space.appexec([], """():
import _ast
return _ast""")
diff --git a/pypy/module/_codecs/test/test_codecs.py b/pypy/module/_codecs/test/test_codecs.py
--- a/pypy/module/_codecs/test/test_codecs.py
+++ b/pypy/module/_codecs/test/test_codecs.py
@@ -4,7 +4,7 @@
class AppTestCodecs:
def setup_class(cls):
- space = gettestobjspace(usemodules=('unicodedata',))
+ space = gettestobjspace(usemodules=('unicodedata', 'struct'))
cls.space = space
def test_register_noncallable(self):
diff --git a/pypy/module/_continuation/test/test_zpickle.py b/pypy/module/_continuation/test/test_zpickle.py
--- a/pypy/module/_continuation/test/test_zpickle.py
+++ b/pypy/module/_continuation/test/test_zpickle.py
@@ -106,8 +106,9 @@
version = 0
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=('_continuation',),
+ cls.space = gettestobjspace(usemodules=('_continuation', 'struct'),
CALL_METHOD=True)
+ cls.space.config.translation.continuation = True
cls.space.appexec([], """():
global continulet, A, __name__
diff --git a/pypy/module/_hashlib/test/test_hashlib.py b/pypy/module/_hashlib/test/test_hashlib.py
--- a/pypy/module/_hashlib/test/test_hashlib.py
+++ b/pypy/module/_hashlib/test/test_hashlib.py
@@ -3,7 +3,7 @@
class AppTestHashlib:
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=['_hashlib'])
+ cls.space = gettestobjspace(usemodules=['_hashlib', 'array', 'struct'])
def test_simple(self):
import _hashlib
diff --git a/pypy/module/_io/test/test_io.py b/pypy/module/_io/test/test_io.py
--- a/pypy/module/_io/test/test_io.py
+++ b/pypy/module/_io/test/test_io.py
@@ -158,7 +158,7 @@
class AppTestOpen:
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=['_io', '_locale'])
+ cls.space = gettestobjspace(usemodules=['_io', '_locale', 'array', 'struct'])
tmpfile = udir.join('tmpfile').ensure()
cls.w_tmpfile = cls.space.wrap(str(tmpfile))
diff --git a/pypy/module/_multiprocessing/test/test_connection.py b/pypy/module/_multiprocessing/test/test_connection.py
--- a/pypy/module/_multiprocessing/test/test_connection.py
+++ b/pypy/module/_multiprocessing/test/test_connection.py
@@ -92,7 +92,8 @@
class AppTestSocketConnection(BaseConnectionTest):
def setup_class(cls):
- space = gettestobjspace(usemodules=('_multiprocessing', 'thread', 'signal'))
+ space = gettestobjspace(usemodules=('_multiprocessing', 'thread', 'signal',
+ 'struct', 'array'))
cls.space = space
cls.w_connections = space.newlist([])
diff --git a/pypy/module/_socket/test/test_sock_app.py b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -6,7 +6,7 @@
from pypy.rpython.lltypesystem import lltype, rffi
def setup_module(mod):
- mod.space = gettestobjspace(usemodules=['_socket', 'array'])
+ mod.space = gettestobjspace(usemodules=['_socket', 'array', 'struct'])
global socket
import socket
mod.w_socket = space.appexec([], "(): import _socket as m; return m")
@@ -372,10 +372,9 @@
def test_socket_connect(self):
import _socket, os
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM, 0)
- # XXX temporarily we use python.org to test, will have more robust tests
- # in the absence of a network connection later when more parts of the
- # socket API are implemented. Currently skip the test if there is no
- # connection.
+ # it would be nice to have a test which works even if there is no
+ # network connection. However, this one is "good enough" for now. Skip
+ # it if there is no connection.
try:
s.connect(("www.python.org", 80))
except _socket.gaierror, ex:
diff --git a/pypy/module/_ssl/test/test_ssl.py b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -1,7 +1,6 @@
from pypy.conftest import gettestobjspace
import os
import py
-from pypy.rlib.rarithmetic import is_valid_int
class AppTestSSL:
@@ -31,7 +30,6 @@
assert isinstance(_ssl.SSL_ERROR_EOF, int)
assert isinstance(_ssl.SSL_ERROR_INVALID_ERROR_CODE, int)
- assert is_valid_int(_ssl.OPENSSL_VERSION_NUMBER)
assert isinstance(_ssl.OPENSSL_VERSION_INFO, tuple)
assert len(_ssl.OPENSSL_VERSION_INFO) == 5
assert isinstance(_ssl.OPENSSL_VERSION, str)
@@ -92,7 +90,7 @@
class AppTestConnectedSSL:
def setup_class(cls):
- space = gettestobjspace(usemodules=('_ssl', '_socket'))
+ space = gettestobjspace(usemodules=('_ssl', '_socket', 'struct'))
cls.space = space
def setup_method(self, method):
@@ -181,7 +179,7 @@
# to exercise the poll() calls
def setup_class(cls):
- space = gettestobjspace(usemodules=('_ssl', '_socket'))
+ space = gettestobjspace(usemodules=('_ssl', '_socket', 'struct'))
cls.space = space
cls.space.appexec([], """():
import socket; socket.setdefaulttimeout(1)
diff --git a/pypy/module/cpyext/test/conftest.py b/pypy/module/cpyext/test/conftest.py
--- a/pypy/module/cpyext/test/conftest.py
+++ b/pypy/module/cpyext/test/conftest.py
@@ -10,7 +10,7 @@
return False
def pytest_funcarg__space(request):
- return gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi'])
+ return gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi', 'array'])
def pytest_funcarg__api(request):
return request.cls.api
diff --git a/pypy/module/cpyext/test/test_api.py b/pypy/module/cpyext/test/test_api.py
--- a/pypy/module/cpyext/test/test_api.py
+++ b/pypy/module/cpyext/test/test_api.py
@@ -19,7 +19,8 @@
class BaseApiTest(LeakCheckingTest):
def setup_class(cls):
- cls.space = space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi'])
+ cls.space = space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi',
+ 'array'])
# warm up reference counts:
# - the posix module allocates a HCRYPTPROV on Windows
diff --git a/pypy/module/cpyext/test/test_arraymodule.py b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -1,3 +1,4 @@
+from pypy.conftest import gettestobjspace
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
import py
diff --git a/pypy/module/cpyext/test/test_cpyext.py b/pypy/module/cpyext/test/test_cpyext.py
--- a/pypy/module/cpyext/test/test_cpyext.py
+++ b/pypy/module/cpyext/test/test_cpyext.py
@@ -35,7 +35,7 @@
class AppTestApi:
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi'])
+ cls.space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi', 'array'])
from pypy.rlib.libffi import get_libc_name
cls.w_libc = cls.space.wrap(get_libc_name())
@@ -165,8 +165,9 @@
return leaking
class AppTestCpythonExtensionBase(LeakCheckingTest):
+
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi'])
+ cls.space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi', 'array'])
cls.space.getbuiltinmodule("cpyext")
from pypy.module.imp.importing import importhook
importhook(cls.space, "os") # warm up reference counts
diff --git a/pypy/module/cpyext/test/test_import.py b/pypy/module/cpyext/test/test_import.py
--- a/pypy/module/cpyext/test/test_import.py
+++ b/pypy/module/cpyext/test/test_import.py
@@ -19,7 +19,7 @@
space.wrap('__name__'))) == 'foobar'
def test_getmoduledict(self, space, api):
- testmod = "binascii"
+ testmod = "_functools"
w_pre_dict = api.PyImport_GetModuleDict()
assert not space.is_true(space.contains(w_pre_dict, space.wrap(testmod)))
diff --git a/pypy/module/fcntl/test/test_fcntl.py b/pypy/module/fcntl/test/test_fcntl.py
--- a/pypy/module/fcntl/test/test_fcntl.py
+++ b/pypy/module/fcntl/test/test_fcntl.py
@@ -13,7 +13,7 @@
class AppTestFcntl:
def setup_class(cls):
- space = gettestobjspace(usemodules=('fcntl', 'array'))
+ space = gettestobjspace(usemodules=('fcntl', 'array', 'struct'))
cls.space = space
tmpprefix = str(udir.ensure('test_fcntl', dir=1).join('tmp_'))
cls.w_tmp = space.wrap(tmpprefix)
diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -987,6 +987,10 @@
os.environ['LANG'] = oldlang
class AppTestImportHooks(object):
+
+ def setup_class(cls):
+ cls.space = gettestobjspace(usemodules=('struct',))
+
def test_meta_path(self):
tried_imports = []
class Importer(object):
diff --git a/pypy/module/itertools/test/test_itertools.py b/pypy/module/itertools/test/test_itertools.py
--- a/pypy/module/itertools/test/test_itertools.py
+++ b/pypy/module/itertools/test/test_itertools.py
@@ -891,7 +891,7 @@
class AppTestItertools27:
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=['itertools'])
+ cls.space = gettestobjspace(usemodules=['itertools', 'struct'])
if cls.space.is_true(cls.space.appexec([], """():
import sys; return sys.version_info < (2, 7)
""")):
diff --git a/pypy/module/marshal/test/make_test_marshal.py b/pypy/module/marshal/test/make_test_marshal.py
deleted file mode 100644
--- a/pypy/module/marshal/test/make_test_marshal.py
+++ /dev/null
@@ -1,78 +0,0 @@
-
-TESTCASES = """\
- None
- False
- True
- StopIteration
- Ellipsis
- 42
- -17
- sys.maxint
- -1.25
- -1.25 #2
- 2+5j
- 2+5j #2
- 42L
- -1234567890123456789012345678901234567890L
- hello # not interned
- "hello"
- ()
- (1, 2)
- []
- [3, 4]
- {}
- {5: 6, 7: 8}
- func.func_code
- scopefunc.func_code
- u'hello'
- set()
- set([1, 2])
- frozenset()
- frozenset([3, 4])
-""".strip().split('\n')
-
-def readable(s):
- for c, repl in (
- ("'", '_quote_'), ('"', '_Quote_'), (':', '_colon_'), ('.', '_dot_'),
- ('[', '_list_'), (']', '_tsil_'), ('{', '_dict_'), ('}', '_tcid_'),
- ('-', '_minus_'), ('+', '_plus_'),
- (',', '_comma_'), ('(', '_brace_'), (')', '_ecarb_') ):
- s = s.replace(c, repl)
- lis = list(s)
- for i, c in enumerate(lis):
- if c.isalnum() or c == '_':
- continue
- lis[i] = '_'
- return ''.join(lis)
-
-print """class AppTestMarshal:
-"""
-for line in TESTCASES:
- line = line.strip()
- name = readable(line)
- version = ''
- extra = ''
- if line.endswith('#2'):
- version = ', 2'
- extra = '; assert len(s) in (9, 17)'
- src = '''\
- def test_%(name)s(self):
- import sys
- hello = "he"
- hello += "llo"
- def func(x):
- return lambda y: x+y
- scopefunc = func(42)
- import marshal, StringIO
- case = %(line)s
- print "case: %%-30s func=%(name)s" %% (case, )
- s = marshal.dumps(case%(version)s)%(extra)s
- x = marshal.loads(s)
- assert x == case
- f = StringIO.StringIO()
- marshal.dump(case, f)
- f.seek(0)
- x = marshal.load(f)
- assert x == case
-''' % {'name': name, 'line': line, 'version' : version, 'extra': extra}
- print src
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
@@ -6,7 +6,7 @@
class AppTestMath:
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=['math'])
+ cls.space = gettestobjspace(usemodules=['math', 'struct'])
cls.w_cases = cls.space.wrap(test_direct.MathTests.TESTCASES)
cls.w_consistent_host = cls.space.wrap(test_direct.consistent_host)
diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -37,26 +37,44 @@
'True_': 'types.Bool.True',
'False_': 'types.Bool.False',
+ 'typeinfo': 'interp_dtype.get_dtype_cache(space).w_typeinfo',
+
'generic': 'interp_boxes.W_GenericBox',
'number': 'interp_boxes.W_NumberBox',
'integer': 'interp_boxes.W_IntegerBox',
'signedinteger': 'interp_boxes.W_SignedIntegerBox',
'unsignedinteger': 'interp_boxes.W_UnsignedIntegerBox',
'bool_': 'interp_boxes.W_BoolBox',
+ 'bool8': 'interp_boxes.W_BoolBox',
'int8': 'interp_boxes.W_Int8Box',
+ 'byte': 'interp_boxes.W_Int8Box',
'uint8': 'interp_boxes.W_UInt8Box',
+ 'ubyte': 'interp_boxes.W_UInt8Box',
'int16': 'interp_boxes.W_Int16Box',
+ 'short': 'interp_boxes.W_Int16Box',
'uint16': 'interp_boxes.W_UInt16Box',
+ 'ushort': 'interp_boxes.W_UInt16Box',
'int32': 'interp_boxes.W_Int32Box',
+ 'intc': 'interp_boxes.W_Int32Box',
'uint32': 'interp_boxes.W_UInt32Box',
+ 'uintc': 'interp_boxes.W_UInt32Box',
'int64': 'interp_boxes.W_Int64Box',
'uint64': 'interp_boxes.W_UInt64Box',
+ 'longlong': 'interp_boxes.W_LongLongBox',
+ 'ulonglong': 'interp_boxes.W_ULongLongBox',
'int_': 'interp_boxes.W_LongBox',
'inexact': 'interp_boxes.W_InexactBox',
'floating': 'interp_boxes.W_FloatingBox',
'float_': 'interp_boxes.W_Float64Box',
'float32': 'interp_boxes.W_Float32Box',
'float64': 'interp_boxes.W_Float64Box',
+ 'intp': 'types.IntP.BoxType',
+ 'uintp': 'types.UIntP.BoxType',
+ 'flexible': 'interp_boxes.W_FlexibleBox',
+ 'character': 'interp_boxes.W_CharacterBox',
+ 'str_': 'interp_boxes.W_StringBox',
+ 'unicode_': 'interp_boxes.W_UnicodeBox',
+ 'void': 'interp_boxes.W_VoidBox',
}
# ufuncs
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -33,7 +33,7 @@
pass
SINGLE_ARG_FUNCTIONS = ["sum", "prod", "max", "min", "all", "any",
- "unegative", "flat"]
+ "unegative", "flat", "tostring"]
TWO_ARG_FUNCTIONS = ["dot", 'take']
class FakeSpace(object):
@@ -51,6 +51,8 @@
w_long = "long"
w_tuple = 'tuple'
w_slice = "slice"
+ w_str = "str"
+ w_unicode = "unicode"
def __init__(self):
"""NOT_RPYTHON"""
@@ -91,8 +93,12 @@
return BoolObject(obj)
elif isinstance(obj, int):
return IntObject(obj)
+ elif isinstance(obj, long):
+ return LongObject(obj)
elif isinstance(obj, W_Root):
return obj
+ elif isinstance(obj, str):
+ return StringObject(obj)
raise NotImplementedError
def newlist(self, items):
@@ -120,6 +126,11 @@
return int(w_obj.floatval)
raise NotImplementedError
+ def str_w(self, w_obj):
+ if isinstance(w_obj, StringObject):
+ return w_obj.v
+ raise NotImplementedError
+
def int(self, w_obj):
if isinstance(w_obj, IntObject):
return w_obj
@@ -151,7 +162,13 @@
return instantiate(klass)
def newtuple(self, list_w):
- raise ValueError
+ return ListObject(list_w)
+
+ def newdict(self):
+ return {}
+
+ def setitem(self, dict, item, value):
+ dict[item] = value
def len_w(self, w_obj):
if isinstance(w_obj, ListObject):
@@ -178,6 +195,11 @@
def __init__(self, intval):
self.intval = intval
+class LongObject(W_Root):
+ tp = FakeSpace.w_long
+ def __init__(self, intval):
+ self.intval = intval
+
class ListObject(W_Root):
tp = FakeSpace.w_list
def __init__(self, items):
@@ -190,6 +212,11 @@
self.stop = stop
self.step = step
+class StringObject(W_Root):
+ tp = FakeSpace.w_str
+ def __init__(self, v):
+ self.v = v
+
class InterpreterState(object):
def __init__(self, code):
self.code = code
@@ -407,6 +434,9 @@
w_res = neg.call(interp.space, [arr])
elif self.name == "flat":
w_res = arr.descr_get_flatiter(interp.space)
+ elif self.name == "tostring":
+ arr.descr_tostring(interp.space)
+ w_res = None
else:
assert False # unreachable code
elif self.name in TWO_ARG_FUNCTIONS:
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -1,24 +1,25 @@
from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.error import operationerrfmt
+from pypy.interpreter.error import operationerrfmt, OperationError
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef
from pypy.objspace.std.floattype import float_typedef
+from pypy.objspace.std.stringtype import str_typedef
+from pypy.objspace.std.unicodetype import unicode_typedef, unicode_from_object
from pypy.objspace.std.inttype import int_typedef
from pypy.rlib.rarithmetic import LONG_BIT
from pypy.tool.sourcetools import func_with_new_name
-
MIXIN_32 = (int_typedef,) if LONG_BIT == 32 else ()
MIXIN_64 = (int_typedef,) if LONG_BIT == 64 else ()
def new_dtype_getter(name):
- def get_dtype(space):
+ def _get_dtype(space):
from pypy.module.micronumpy.interp_dtype import get_dtype_cache
return getattr(get_dtype_cache(space), "w_%sdtype" % name)
def new(space, w_subtype, w_value):
- dtype = get_dtype(space)
+ dtype = _get_dtype(space)
return dtype.itemtype.coerce_subtype(space, w_subtype, w_value)
- return func_with_new_name(new, name + "_box_new"), staticmethod(get_dtype)
+ return func_with_new_name(new, name + "_box_new"), staticmethod(_get_dtype)
class PrimitiveBox(object):
_mixin_ = True
@@ -37,6 +38,9 @@
w_subtype.getname(space, '?')
)
+ def get_dtype(self, space):
+ return self._get_dtype(space)
+
def descr_str(self, space):
return space.wrap(self.get_dtype(space).itemtype.str_format(self))
@@ -44,12 +48,12 @@
return space.format(self.item(space), w_spec)
def descr_int(self, space):
- box = self.convert_to(W_LongBox.get_dtype(space))
+ box = self.convert_to(W_LongBox._get_dtype(space))
assert isinstance(box, W_LongBox)
return space.wrap(box.value)
def descr_float(self, space):
- box = self.convert_to(W_Float64Box.get_dtype(space))
+ box = self.convert_to(W_Float64Box._get_dtype(space))
assert isinstance(box, W_Float64Box)
return space.wrap(box.value)
@@ -130,7 +134,7 @@
class W_BoolBox(W_GenericBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("bool")
+ descr__new__, _get_dtype = new_dtype_getter("bool")
class W_NumberBox(W_GenericBox):
_attrs_ = ()
@@ -146,34 +150,40 @@
pass
class W_Int8Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int8")
+ descr__new__, _get_dtype = new_dtype_getter("int8")
class W_UInt8Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint8")
+ descr__new__, _get_dtype = new_dtype_getter("uint8")
class W_Int16Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int16")
+ descr__new__, _get_dtype = new_dtype_getter("int16")
class W_UInt16Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint16")
+ descr__new__, _get_dtype = new_dtype_getter("uint16")
class W_Int32Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int32")
+ descr__new__, _get_dtype = new_dtype_getter("int32")
class W_UInt32Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint32")
+ descr__new__, _get_dtype = new_dtype_getter("uint32")
class W_LongBox(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("long")
+ descr__new__, _get_dtype = new_dtype_getter("long")
class W_ULongBox(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("ulong")
+ descr__new__, _get_dtype = new_dtype_getter("ulong")
class W_Int64Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int64")
+ descr__new__, _get_dtype = new_dtype_getter("int64")
+
+class W_LongLongBox(W_SignedIntegerBox, PrimitiveBox):
+ descr__new__, _get_dtype = new_dtype_getter('longlong')
class W_UInt64Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint64")
+ descr__new__, _get_dtype = new_dtype_getter("uint64")
+
+class W_ULongLongBox(W_SignedIntegerBox, PrimitiveBox):
+ descr__new__, _get_dtype = new_dtype_getter('ulonglong')
class W_InexactBox(W_NumberBox):
_attrs_ = ()
@@ -182,16 +192,71 @@
_attrs_ = ()
class W_Float32Box(W_FloatingBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("float32")
+ descr__new__, _get_dtype = new_dtype_getter("float32")
class W_Float64Box(W_FloatingBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("float64")
+ descr__new__, _get_dtype = new_dtype_getter("float64")
+class W_FlexibleBox(W_GenericBox):
+ def __init__(self, arr, ofs, dtype):
+ self.arr = arr # we have to keep array alive
+ self.ofs = ofs
+ self.dtype = dtype
+
+ def get_dtype(self, space):
+ return self.arr.dtype
+
@unwrap_spec(self=W_GenericBox)
def descr_index(space, self):
return space.index(self.item(space))
+class W_VoidBox(W_FlexibleBox):
+ @unwrap_spec(item=str)
+ def descr_getitem(self, space, item):
+ try:
+ ofs, dtype = self.dtype.fields[item]
+ except KeyError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("Field %s does not exist" % item))
+ return dtype.itemtype.read(self.arr, 1, self.ofs, ofs, dtype)
+
+ @unwrap_spec(item=str)
+ def descr_setitem(self, space, item, w_value):
+ try:
+ ofs, dtype = self.dtype.fields[item]
+ except KeyError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("Field %s does not exist" % item))
+ dtype.itemtype.store(self.arr, 1, self.ofs, ofs,
+ dtype.coerce(space, w_value))
+
+class W_CharacterBox(W_FlexibleBox):
+ pass
+
+class W_StringBox(W_CharacterBox):
+ def descr__new__string_box(space, w_subtype, w_arg):
+ from pypy.module.micronumpy.interp_numarray import W_NDimArray
+ from pypy.module.micronumpy.interp_dtype import new_string_dtype
+
+ arg = space.str_w(space.str(w_arg))
+ arr = W_NDimArray([1], new_string_dtype(space, len(arg)))
+ for i in range(len(arg)):
+ arr.storage[i] = arg[i]
+ return W_StringBox(arr, 0, arr.dtype)
+
+
+class W_UnicodeBox(W_CharacterBox):
+ def descr__new__unicode_box(space, w_subtype, w_arg):
+ from pypy.module.micronumpy.interp_numarray import W_NDimArray
+ from pypy.module.micronumpy.interp_dtype import new_unicode_dtype
+
+ arg = space.unicode_w(unicode_from_object(space, w_arg))
+ arr = W_NDimArray([1], new_unicode_dtype(space, len(arg)))
+ # XXX not this way, we need store
+ #for i in range(len(arg)):
+ # arr.storage[i] = arg[i]
+ return W_UnicodeBox(arr, 0, arr.dtype)
W_GenericBox.typedef = TypeDef("generic",
__module__ = "numpypy",
@@ -348,3 +413,28 @@
__new__ = interp2app(W_Float64Box.descr__new__.im_func),
)
+
+W_FlexibleBox.typedef = TypeDef("flexible", W_GenericBox.typedef,
+ __module__ = "numpypy",
+)
+
+W_VoidBox.typedef = TypeDef("void", W_FlexibleBox.typedef,
+ __module__ = "numpypy",
+ __getitem__ = interp2app(W_VoidBox.descr_getitem),
+ __setitem__ = interp2app(W_VoidBox.descr_setitem),
+)
+
+W_CharacterBox.typedef = TypeDef("character", W_FlexibleBox.typedef,
+ __module__ = "numpypy",
+)
+
+W_StringBox.typedef = TypeDef("string_", (str_typedef, W_CharacterBox.typedef),
+ __module__ = "numpypy",
+ __new__ = interp2app(W_StringBox.descr__new__string_box.im_func),
+)
+
+W_UnicodeBox.typedef = TypeDef("unicode_", (unicode_typedef, W_CharacterBox.typedef),
+ __module__ = "numpypy",
+ __new__ = interp2app(W_UnicodeBox.descr__new__unicode_box.im_func),
+)
+
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -1,26 +1,29 @@
+
+import sys
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
interp_attrproperty, interp_attrproperty_w)
-from pypy.module.micronumpy import types, signature, interp_boxes
+from pypy.module.micronumpy import types, interp_boxes
from pypy.rlib.objectmodel import specialize
-from pypy.rlib.rarithmetic import LONG_BIT
-from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rlib.rarithmetic import LONG_BIT, r_longlong, r_ulonglong
UNSIGNEDLTR = "u"
SIGNEDLTR = "i"
BOOLLTR = "b"
FLOATINGLTR = "f"
-
-
-VOID_STORAGE = lltype.Array(lltype.Char, hints={'nolength': True, 'render_as_void': True})
+VOIDLTR = 'V'
+STRINGLTR = 'S'
+UNICODELTR = 'U'
class W_Dtype(Wrappable):
_immutable_fields_ = ["itemtype", "num", "kind"]
- def __init__(self, itemtype, num, kind, name, char, w_box_type, alternate_constructors=[], aliases=[]):
+ def __init__(self, itemtype, num, kind, name, char, w_box_type,
+ alternate_constructors=[], aliases=[],
+ fields=None, fieldnames=None, native=True):
self.itemtype = itemtype
self.num = num
self.kind = kind
@@ -29,53 +32,28 @@
self.w_box_type = w_box_type
self.alternate_constructors = alternate_constructors
self.aliases = aliases
-
- def malloc(self, length):
- # XXX find out why test_zjit explodes with tracking of allocations
- return lltype.malloc(VOID_STORAGE, self.itemtype.get_element_size() * length,
- zero=True, flavor="raw",
- track_allocation=False, add_memory_pressure=True
- )
+ self.fields = fields
+ self.fieldnames = fieldnames
+ self.native = native
@specialize.argtype(1)
def box(self, value):
return self.itemtype.box(value)
def coerce(self, space, w_item):
- return self.itemtype.coerce(space, w_item)
+ return self.itemtype.coerce(space, self, w_item)
- def getitem(self, storage, i):
- return self.itemtype.read(storage, self.itemtype.get_element_size(), i, 0)
+ def getitem(self, arr, i):
+ return self.itemtype.read(arr, 1, i, 0)
- def getitem_bool(self, storage, i):
- isize = self.itemtype.get_element_size()
- return self.itemtype.read_bool(storage, isize, i, 0)
+ def getitem_bool(self, arr, i):
+ return self.itemtype.read_bool(arr, 1, i, 0)
- def setitem(self, storage, i, box):
- self.itemtype.store(storage, self.itemtype.get_element_size(), i, 0, box)
+ def setitem(self, arr, i, box):
+ self.itemtype.store(arr, 1, i, 0, box)
def fill(self, storage, box, start, stop):
- self.itemtype.fill(storage, self.itemtype.get_element_size(), box, start, stop, 0)
-
- def descr__new__(space, w_subtype, w_dtype):
- cache = get_dtype_cache(space)
-
- if space.is_w(w_dtype, space.w_None):
- return cache.w_float64dtype
- elif space.isinstance_w(w_dtype, w_subtype):
- return w_dtype
- elif space.isinstance_w(w_dtype, space.w_str):
- name = space.str_w(w_dtype)
- for dtype in cache.builtin_dtypes:
- if dtype.name == name or dtype.char == name or name in dtype.aliases:
- return dtype
- else:
- for dtype in cache.builtin_dtypes:
- if w_dtype in dtype.alternate_constructors:
- return dtype
- if w_dtype is dtype.w_box_type:
- return dtype
- raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+ self.itemtype.fill(storage, self.get_size(), box, start, stop, 0)
def descr_str(self, space):
return space.wrap(self.name)
@@ -86,6 +64,14 @@
def descr_get_itemsize(self, space):
return space.wrap(self.itemtype.get_element_size())
+ def descr_get_byteorder(self, space):
+ if self.native:
+ return space.wrap('=')
+ return space.wrap(nonnative_byteorder_prefix)
+
+ def descr_get_alignment(self, space):
+ return space.wrap(self.itemtype.alignment)
+
def descr_get_shape(self, space):
return space.newtuple([])
@@ -99,31 +85,193 @@
def descr_ne(self, space, w_other):
return space.wrap(not self.eq(space, w_other))
+ def descr_get_fields(self, space):
+ if self.fields is None:
+ return space.w_None
+ w_d = space.newdict()
+ for name, (offset, subdtype) in self.fields.iteritems():
+ space.setitem(w_d, space.wrap(name), space.newtuple([subdtype,
+ space.wrap(offset)]))
+ return w_d
+
+ def descr_get_names(self, space):
+ if self.fieldnames is None:
+ return space.w_None
+ return space.newtuple([space.wrap(name) for name in self.fieldnames])
+
+ @unwrap_spec(item=str)
+ def descr_getitem(self, space, item):
+ if self.fields is None:
+ raise OperationError(space.w_KeyError, space.wrap("There are no keys in dtypes %s" % self.name))
+ try:
+ return self.fields[item][1]
+ except KeyError:
+ raise OperationError(space.w_KeyError, space.wrap("Field named %s not found" % item))
+
def is_int_type(self):
return (self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR or
self.kind == BOOLLTR)
+ def is_signed(self):
+ return self.kind == SIGNEDLTR
+
def is_bool_type(self):
return self.kind == BOOLLTR
+ def is_record_type(self):
+ return self.fields is not None
+
+ def __repr__(self):
+ if self.fields is not None:
+ return '<DType %r>' % self.fields
+ return '<DType %r>' % self.itemtype
+
+ def get_size(self):
+ return self.itemtype.get_element_size()
+
+def dtype_from_list(space, w_lst):
+ lst_w = space.listview(w_lst)
+ fields = {}
+ offset = 0
+ ofs_and_items = []
+ fieldnames = []
+ for w_elem in lst_w:
+ w_fldname, w_flddesc = space.fixedview(w_elem, 2)
+ subdtype = descr__new__(space, space.gettypefor(W_Dtype), w_flddesc)
+ fldname = space.str_w(w_fldname)
+ if fldname in fields:
+ raise OperationError(space.w_ValueError, space.wrap("two fields with the same name"))
+ assert isinstance(subdtype, W_Dtype)
+ fields[fldname] = (offset, subdtype)
+ ofs_and_items.append((offset, subdtype.itemtype))
+ offset += subdtype.itemtype.get_element_size()
+ fieldnames.append(fldname)
+ itemtype = types.RecordType(ofs_and_items, offset)
+ return W_Dtype(itemtype, 20, VOIDLTR, "void" + str(8 * itemtype.get_element_size()),
+ "V", space.gettypefor(interp_boxes.W_VoidBox), fields=fields,
+ fieldnames=fieldnames)
+
+def dtype_from_dict(space, w_dict):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "dtype from dict"))
+
+def variable_dtype(space, name):
+ if name[0] in '<>=':
+ name = name[1:]
+ char = name[0]
+ if len(name) == 1:
+ size = 0
+ else:
+ try:
+ size = int(name[1:])
+ except ValueError:
+ raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+ if char == 'S':
+ itemtype = types.StringType(size)
+ basename = 'string'
+ num = 18
+ w_box_type = space.gettypefor(interp_boxes.W_StringBox)
+ elif char == 'V':
+ num = 20
+ basename = 'void'
+ w_box_type = space.gettypefor(interp_boxes.W_VoidBox)
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "pure void dtype"))
+ else:
+ assert char == 'U'
+ basename = 'unicode'
+ itemtype = types.UnicodeType(size)
+ num = 19
+ w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
+ return W_Dtype(itemtype, num, char,
+ basename + str(8 * itemtype.get_element_size()),
+ char, w_box_type)
+
+def dtype_from_spec(space, name):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "dtype from spec"))
+
+def descr__new__(space, w_subtype, w_dtype):
+ cache = get_dtype_cache(space)
+
+ if space.is_w(w_dtype, space.w_None):
+ return cache.w_float64dtype
+ elif space.isinstance_w(w_dtype, w_subtype):
+ return w_dtype
+ elif space.isinstance_w(w_dtype, space.w_str):
+ name = space.str_w(w_dtype)
+ if ',' in name:
+ return dtype_from_spec(space, name)
+ try:
+ return cache.dtypes_by_name[name]
+ except KeyError:
+ pass
+ if name[0] in 'VSU' or name[0] in '<>=' and name[1] in 'VSU':
+ return variable_dtype(space, name)
+ elif space.isinstance_w(w_dtype, space.w_list):
+ return dtype_from_list(space, w_dtype)
+ elif space.isinstance_w(w_dtype, space.w_dict):
+ return dtype_from_dict(space, w_dtype)
+ else:
+ for dtype in cache.builtin_dtypes:
+ if w_dtype in dtype.alternate_constructors:
+ return dtype
+ if w_dtype is dtype.w_box_type:
+ return dtype
+ raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+
W_Dtype.typedef = TypeDef("dtype",
__module__ = "numpypy",
- __new__ = interp2app(W_Dtype.descr__new__.im_func),
+ __new__ = interp2app(descr__new__),
__str__= interp2app(W_Dtype.descr_str),
__repr__ = interp2app(W_Dtype.descr_repr),
__eq__ = interp2app(W_Dtype.descr_eq),
__ne__ = interp2app(W_Dtype.descr_ne),
+ __getitem__ = interp2app(W_Dtype.descr_getitem),
num = interp_attrproperty("num", cls=W_Dtype),
kind = interp_attrproperty("kind", cls=W_Dtype),
+ char = interp_attrproperty("char", cls=W_Dtype),
type = interp_attrproperty_w("w_box_type", cls=W_Dtype),
+ byteorder = GetSetProperty(W_Dtype.descr_get_byteorder),
itemsize = GetSetProperty(W_Dtype.descr_get_itemsize),
+ alignment = GetSetProperty(W_Dtype.descr_get_alignment),
shape = GetSetProperty(W_Dtype.descr_get_shape),
name = interp_attrproperty('name', cls=W_Dtype),
+ fields = GetSetProperty(W_Dtype.descr_get_fields),
+ names = GetSetProperty(W_Dtype.descr_get_names),
)
W_Dtype.typedef.acceptable_as_base_class = False
+if sys.byteorder == 'little':
+ byteorder_prefix = '<'
+ nonnative_byteorder_prefix = '>'
+else:
+ byteorder_prefix = '>'
+ nonnative_byteorder_prefix = '<'
+
+def new_string_dtype(space, size):
+ return W_Dtype(
+ types.StringType(size),
+ num=18,
+ kind=STRINGLTR,
+ name='string',
+ char='S' + str(size),
+ w_box_type = space.gettypefor(interp_boxes.W_StringBox),
+ )
+
+def new_unicode_dtype(space, size):
+ return W_Dtype(
+ types.UnicodeType(size),
+ num=19,
+ kind=UNICODELTR,
+ name='unicode',
+ char='U' + str(size),
+ w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
+ )
+
+
class DtypeCache(object):
def __init__(self, space):
self.w_booldtype = W_Dtype(
@@ -239,18 +387,134 @@
alternate_constructors=[space.w_float],
aliases=["float"],
)
-
+ self.w_stringdtype = W_Dtype(
+ types.StringType(1),
+ num=18,
+ kind=STRINGLTR,
+ name='string',
+ char='S',
+ w_box_type = space.gettypefor(interp_boxes.W_StringBox),
+ alternate_constructors=[space.w_str],
+ )
+ self.w_unicodedtype = W_Dtype(
+ types.UnicodeType(1),
+ num=19,
+ kind=UNICODELTR,
+ name='unicode',
+ char='U',
+ w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
+ alternate_constructors=[space.w_unicode],
+ )
+ self.w_voiddtype = W_Dtype(
+ types.VoidType(0),
+ num=20,
+ kind=VOIDLTR,
+ name='void',
+ char='V',
+ w_box_type = space.gettypefor(interp_boxes.W_VoidBox),
+ #alternate_constructors=[space.w_buffer],
+ # XXX no buffer in space
+ )
self.builtin_dtypes = [
self.w_booldtype, self.w_int8dtype, self.w_uint8dtype,
self.w_int16dtype, self.w_uint16dtype, self.w_int32dtype,
self.w_uint32dtype, self.w_longdtype, self.w_ulongdtype,
- self.w_int64dtype, self.w_uint64dtype, self.w_float32dtype,
- self.w_float64dtype
+ self.w_int64dtype, self.w_uint64dtype,
+ self.w_float32dtype,
+ self.w_float64dtype, self.w_stringdtype, self.w_unicodedtype,
+ self.w_voiddtype,
]
- self.dtypes_by_num_bytes = sorted(
+ self.float_dtypes_by_num_bytes = sorted(
(dtype.itemtype.get_element_size(), dtype)
- for dtype in self.builtin_dtypes
+ for dtype in [self.w_float32dtype, self.w_float64dtype]
)
+ self.dtypes_by_name = {}
+ # we reverse, so the stuff with lower numbers override stuff with
+ # higher numbers
+ for dtype in reversed(self.builtin_dtypes):
+ self.dtypes_by_name[dtype.name] = dtype
+ can_name = dtype.kind + str(dtype.itemtype.get_element_size())
+ self.dtypes_by_name[can_name] = dtype
+ self.dtypes_by_name[byteorder_prefix + can_name] = dtype
+ self.dtypes_by_name['=' + can_name] = dtype
+ new_name = nonnative_byteorder_prefix + can_name
+ itemtypename = dtype.itemtype.__class__.__name__
+ itemtype = getattr(types, 'NonNative' + itemtypename)()
+ self.dtypes_by_name[new_name] = W_Dtype(
+ itemtype,
+ dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
+ native=False)
+ for alias in dtype.aliases:
+ self.dtypes_by_name[alias] = dtype
+ self.dtypes_by_name[dtype.char] = dtype
+
+ typeinfo_full = {
+ 'LONGLONG': self.w_int64dtype,
+ 'SHORT': self.w_int16dtype,
+ 'VOID': self.w_voiddtype,
+ #'LONGDOUBLE':,
+ 'UBYTE': self.w_uint8dtype,
+ 'UINTP': self.w_ulongdtype,
+ 'ULONG': self.w_ulongdtype,
+ 'LONG': self.w_longdtype,
+ 'UNICODE': self.w_unicodedtype,
+ #'OBJECT',
+ 'ULONGLONG': self.w_uint64dtype,
+ 'STRING': self.w_stringdtype,
+ #'CDOUBLE',
+ #'DATETIME',
+ 'UINT': self.w_uint32dtype,
+ 'INTP': self.w_longdtype,
+ #'HALF',
+ 'BYTE': self.w_int8dtype,
+ #'CFLOAT': ,
+ #'TIMEDELTA',
+ 'INT': self.w_int32dtype,
+ 'DOUBLE': self.w_float64dtype,
+ 'USHORT': self.w_uint16dtype,
+ 'FLOAT': self.w_float32dtype,
+ 'BOOL': self.w_booldtype,
+ #, 'CLONGDOUBLE']
+ }
+ typeinfo_partial = {
+ 'Generic': interp_boxes.W_GenericBox,
+ 'Character': interp_boxes.W_CharacterBox,
+ 'Flexible': interp_boxes.W_FlexibleBox,
+ 'Inexact': interp_boxes.W_InexactBox,
+ 'Integer': interp_boxes.W_IntegerBox,
+ 'SignedInteger': interp_boxes.W_SignedIntegerBox,
+ 'UnsignedInteger': interp_boxes.W_UnsignedIntegerBox,
+ #'ComplexFloating',
+ 'Number': interp_boxes.W_NumberBox,
+ 'Floating': interp_boxes.W_FloatingBox
+ }
+ w_typeinfo = space.newdict()
+ for k, v in typeinfo_partial.iteritems():
+ space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
+ for k, dtype in typeinfo_full.iteritems():
+ itemsize = dtype.itemtype.get_element_size()
+ items_w = [space.wrap(dtype.char),
+ space.wrap(dtype.num),
+ space.wrap(itemsize * 8), # in case of changing
+ # number of bits per byte in the future
+ space.wrap(itemsize or 1)]
+ if dtype.is_int_type():
+ if dtype.kind == BOOLLTR:
+ w_maxobj = space.wrap(1)
+ w_minobj = space.wrap(0)
+ elif dtype.is_signed():
+ w_maxobj = space.wrap(r_longlong((1 << (itemsize*8 - 1))
+ - 1))
+ w_minobj = space.wrap(r_longlong(-1) << (itemsize*8 - 1))
+ else:
+ w_maxobj = space.wrap(r_ulonglong(1 << (itemsize*8)) - 1)
+ w_minobj = space.wrap(0)
+ items_w = items_w + [w_maxobj, w_minobj]
+ items_w = items_w + [dtype.w_box_type]
+
+ w_tuple = space.newtuple(items_w)
+ space.setitem(w_typeinfo, space.wrap(k), w_tuple)
+ self.w_typeinfo = w_typeinfo
def get_dtype_cache(space):
return space.fromcache(DtypeCache)
diff --git a/pypy/module/micronumpy/interp_iter.py b/pypy/module/micronumpy/interp_iter.py
--- a/pypy/module/micronumpy/interp_iter.py
+++ b/pypy/module/micronumpy/interp_iter.py
@@ -2,7 +2,7 @@
from pypy.rlib import jit
from pypy.rlib.objectmodel import instantiate
from pypy.module.micronumpy.strides import calculate_broadcast_strides,\
- calculate_slice_strides, calculate_dot_strides
+ calculate_slice_strides, calculate_dot_strides, enumerate_chunks
""" This is a mini-tutorial on iterators, strides, and
memory layout. It assumes you are familiar with the terms, see
@@ -42,25 +42,67 @@
we can go faster.
All the calculations happen in next()
-next_step_x() tries to do the iteration for a number of steps at once,
+next_skip_x() tries to do the iteration for a number of steps at once,
but then we cannot gaurentee that we only overflow one single shape
dimension, perhaps we could overflow times in one big step.
"""
# structures to describe slicing
-class Chunk(object):
+class BaseChunk(object):
+ pass
+
+class RecordChunk(BaseChunk):
+ def __init__(self, name):
+ self.name = name
+
+ def apply(self, arr):
+ from pypy.module.micronumpy.interp_numarray import W_NDimSlice
+
+ arr = arr.get_concrete()
+ ofs, subdtype = arr.dtype.fields[self.name]
+ # strides backstrides are identical, ofs only changes start
+ return W_NDimSlice(arr.start + ofs, arr.strides[:], arr.backstrides[:],
+ arr.shape[:], arr, subdtype)
+
+class Chunks(BaseChunk):
+ def __init__(self, l):
+ self.l = l
+
+ @jit.unroll_safe
+ def extend_shape(self, old_shape):
+ shape = []
+ i = -1
+ for i, c in enumerate_chunks(self.l):
+ if c.step != 0:
+ shape.append(c.lgt)
+ s = i + 1
+ assert s >= 0
+ return shape[:] + old_shape[s:]
+
+ def apply(self, arr):
+ from pypy.module.micronumpy.interp_numarray import W_NDimSlice,\
+ VirtualSlice, ConcreteArray
+
+ shape = self.extend_shape(arr.shape)
+ if not isinstance(arr, ConcreteArray):
+ return VirtualSlice(arr, self, shape)
+ r = calculate_slice_strides(arr.shape, arr.start, arr.strides,
+ arr.backstrides, self.l)
+ _, start, strides, backstrides = r
+ return W_NDimSlice(start, strides[:], backstrides[:],
+ shape[:], arr)
+
+
+class Chunk(BaseChunk):
axis_step = 1
+
def __init__(self, start, stop, step, lgt):
self.start = start
self.stop = stop
self.step = step
self.lgt = lgt
- def extend_shape(self, shape):
- if self.step != 0:
- shape.append(self.lgt)
-
def __repr__(self):
return 'Chunk(%d, %d, %d, %d)' % (self.start, self.stop, self.step,
self.lgt)
@@ -106,17 +148,19 @@
raise NotImplementedError
class ArrayIterator(BaseIterator):
- def __init__(self, size):
+ def __init__(self, size, element_size):
self.offset = 0
self.size = size
+ self.element_size = element_size
def next(self, shapelen):
return self.next_skip_x(1)
- def next_skip_x(self, ofs):
+ def next_skip_x(self, x):
arr = instantiate(ArrayIterator)
arr.size = self.size
- arr.offset = self.offset + ofs
+ arr.offset = self.offset + x * self.element_size
+ arr.element_size = self.element_size
return arr
def next_no_increase(self, shapelen):
@@ -163,7 +207,7 @@
elif isinstance(t, ViewTransform):
r = calculate_slice_strides(self.res_shape, self.offset,
self.strides,
- self.backstrides, t.chunks)
+ self.backstrides, t.chunks.l)
return ViewIterator(r[1], r[2], r[3], r[0])
@jit.unroll_safe
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -7,10 +7,10 @@
from pypy.module.micronumpy.appbridge import get_appbridge_cache
from pypy.module.micronumpy.dot import multidim_dot, match_dot_shapes
from pypy.module.micronumpy.interp_iter import (ArrayIterator,
- SkipLastAxisIterator, Chunk, NewAxisChunk, ViewIterator)
-from pypy.module.micronumpy.strides import (calculate_slice_strides,
- shape_agreement, find_shape_and_elems, get_shape_from_iterable,
- calc_new_strides, to_coords, enumerate_chunks)
+ SkipLastAxisIterator, Chunk, ViewIterator, Chunks, RecordChunk,
+ NewAxisChunk)
+from pypy.module.micronumpy.strides import (shape_agreement,
+ find_shape_and_elems, get_shape_from_iterable, calc_new_strides, to_coords)
from pypy.rlib import jit
from pypy.rlib.rstring import StringBuilder
from pypy.rpython.lltypesystem import lltype, rffi
@@ -47,7 +47,7 @@
)
flat_set_driver = jit.JitDriver(
greens=['shapelen', 'base'],
- reds=['step', 'ai', 'lngth', 'arr', 'basei'],
+ reds=['step', 'lngth', 'ri', 'arr', 'basei'],
name='numpy_flatset',
)
@@ -79,8 +79,8 @@
dtype = space.interp_w(interp_dtype.W_Dtype,
space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
)
- size, shape = _find_size_and_shape(space, w_size)
- return space.wrap(W_NDimArray(size, shape[:], dtype=dtype))
+ shape = _find_shape(space, w_size)
+ return space.wrap(W_NDimArray(shape[:], dtype=dtype))
def _unaryop_impl(ufunc_name):
def impl(self, space):
@@ -225,8 +225,7 @@
return scalar_w(space, dtype, space.wrap(0))
# Do the dims match?
out_shape, other_critical_dim = match_dot_shapes(space, self, other)
- out_size = support.product(out_shape)
- result = W_NDimArray(out_size, out_shape, dtype)
+ result = W_NDimArray(out_shape, dtype)
# This is the place to add fpypy and blas
return multidim_dot(space, self.get_concrete(),
other.get_concrete(), result, dtype,
@@ -245,7 +244,7 @@
return space.wrap(self.find_dtype().itemtype.get_element_size())
def descr_get_nbytes(self, space):
- return space.wrap(self.size * self.find_dtype().itemtype.get_element_size())
+ return space.wrap(self.size)
@jit.unroll_safe
def descr_get_shape(self, space):
@@ -253,13 +252,16 @@
def descr_set_shape(self, space, w_iterable):
new_shape = get_shape_from_iterable(space,
- self.size, w_iterable)
+ support.product(self.shape), w_iterable)
if isinstance(self, Scalar):
return
self.get_concrete().setshape(space, new_shape)
def descr_get_size(self, space):
- return space.wrap(self.size)
+ return space.wrap(self.get_size())
+
+ def get_size(self):
+ return self.size // self.find_dtype().get_size()
def descr_copy(self, space):
return self.copy(space)
@@ -279,7 +281,7 @@
def empty_copy(self, space, dtype):
shape = self.shape
- return W_NDimArray(support.product(shape), shape[:], dtype, 'C')
+ return W_NDimArray(shape[:], dtype, 'C')
def descr_len(self, space):
if len(self.shape):
@@ -320,13 +322,15 @@
""" The result of getitem/setitem is a single item if w_idx
is a list of scalars that match the size of shape
"""
+ if space.isinstance_w(w_idx, space.w_str):
+ return False
shape_len = len(self.shape)
if space.isinstance_w(w_idx, space.w_tuple):
for w_item in space.fixedview(w_idx):
if (space.isinstance_w(w_item, space.w_slice) or
- space.isinstance_w(w_item, space.w_NoneType)):
+ space.is_w(w_item, space.w_None)):
return False
- elif space.isinstance_w(w_idx, space.w_NoneType):
+ elif space.is_w(w_idx, space.w_None):
return False
if shape_len == 0:
raise OperationError(space.w_IndexError, space.wrap(
@@ -347,44 +351,51 @@
@jit.unroll_safe
def _prepare_slice_args(self, space, w_idx):
+ if space.isinstance_w(w_idx, space.w_str):
+ idx = space.str_w(w_idx)
+ dtype = self.find_dtype()
+ if not dtype.is_record_type() or idx not in dtype.fields:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "field named %s not defined" % idx))
+ return RecordChunk(idx)
if (space.isinstance_w(w_idx, space.w_int) or
space.isinstance_w(w_idx, space.w_slice)):
- return [Chunk(*space.decode_index4(w_idx, self.shape[0]))]
- elif space.isinstance_w(w_idx, space.w_NoneType):
- return [NewAxisChunk()]
+ return Chunks([Chunk(*space.decode_index4(w_idx, self.shape[0]))])
+ elif space.is_w(w_idx, space.w_None):
+ return Chunks([NewAxisChunk()])
result = []
i = 0
for w_item in space.fixedview(w_idx):
- if space.isinstance_w(w_item, space.w_NoneType):
+ if space.is_w(w_item, space.w_None):
result.append(NewAxisChunk())
else:
result.append(Chunk(*space.decode_index4(w_item,
self.shape[i])))
i += 1
- return result
+ return Chunks(result)
- def count_all_true(self, arr):
- sig = arr.find_sig()
- frame = sig.create_frame(arr)
- shapelen = len(arr.shape)
+ def count_all_true(self):
+ sig = self.find_sig()
+ frame = sig.create_frame(self)
+ shapelen = len(self.shape)
s = 0
iter = None
while not frame.done():
- count_driver.jit_merge_point(arr=arr, frame=frame, iter=iter, s=s,
+ count_driver.jit_merge_point(arr=self, frame=frame, iter=iter, s=s,
shapelen=shapelen)
iter = frame.get_final_iter()
- s += arr.dtype.getitem_bool(arr.storage, iter.offset)
+ s += self.dtype.getitem_bool(self, iter.offset)
frame.next(shapelen)
return s
def getitem_filter(self, space, arr):
concr = arr.get_concrete()
- if concr.size > self.size:
+ if concr.get_size() > self.get_size():
raise OperationError(space.w_IndexError,
space.wrap("index out of range for array"))
- size = self.count_all_true(concr)
- res = W_NDimArray(size, [size], self.find_dtype())
- ri = ArrayIterator(size)
+ size = concr.count_all_true()
+ res = W_NDimArray([size], self.find_dtype())
+ ri = res.create_iter()
shapelen = len(self.shape)
argi = concr.create_iter()
sig = self.find_sig()
@@ -394,7 +405,7 @@
filter_driver.jit_merge_point(concr=concr, argi=argi, ri=ri,
frame=frame, v=v, res=res, sig=sig,
shapelen=shapelen, self=self)
- if concr.dtype.getitem_bool(concr.storage, argi.offset):
+ if concr.dtype.getitem_bool(concr, argi.offset):
v = sig.eval(frame, self)
res.setitem(ri.offset, v)
ri = ri.next(1)
@@ -404,23 +415,6 @@
frame.next(shapelen)
return res
- def setitem_filter(self, space, idx, val):
- size = self.count_all_true(idx)
- arr = SliceArray([size], self.dtype, self, val)
- sig = arr.find_sig()
- shapelen = len(self.shape)
- frame = sig.create_frame(arr)
- idxi = idx.create_iter()
- while not frame.done():
- filter_set_driver.jit_merge_point(idx=idx, idxi=idxi, sig=sig,
- frame=frame, arr=arr,
- shapelen=shapelen)
- if idx.dtype.getitem_bool(idx.storage, idxi.offset):
- sig.eval(frame, arr)
- frame.next_from_second(1)
- frame.next_first(shapelen)
- idxi = idxi.next(shapelen)
-
def descr_getitem(self, space, w_idx):
if (isinstance(w_idx, BaseArray) and w_idx.shape == self.shape and
w_idx.find_dtype().is_bool_type()):
@@ -430,7 +424,24 @@
item = concrete._index_of_single_item(space, w_idx)
return concrete.getitem(item)
chunks = self._prepare_slice_args(space, w_idx)
- return self.create_slice(chunks)
+ return chunks.apply(self)
+
+ def setitem_filter(self, space, idx, val):
+ size = idx.count_all_true()
+ arr = SliceArray([size], self.dtype, self, val)
+ sig = arr.find_sig()
+ shapelen = len(self.shape)
+ frame = sig.create_frame(arr)
+ idxi = idx.create_iter()
+ while not frame.done():
+ filter_set_driver.jit_merge_point(idx=idx, idxi=idxi, sig=sig,
+ frame=frame, arr=arr,
+ shapelen=shapelen)
+ if idx.dtype.getitem_bool(idx, idxi.offset):
+ sig.eval(frame, arr)
+ frame.next_from_second(1)
+ frame.next_first(shapelen)
+ idxi = idxi.next(shapelen)
def descr_setitem(self, space, w_idx, w_value):
self.invalidated()
@@ -448,26 +459,9 @@
if not isinstance(w_value, BaseArray):
w_value = convert_to_array(space, w_value)
chunks = self._prepare_slice_args(space, w_idx)
- view = self.create_slice(chunks).get_concrete()
+ view = chunks.apply(self).get_concrete()
view.setslice(space, w_value)
- @jit.unroll_safe
- def create_slice(self, chunks):
- shape = []
- i = -1
- for i, chunk in enumerate_chunks(chunks):
- chunk.extend_shape(shape)
- s = i + 1
- assert s >= 0
- shape += self.shape[s:]
- if not isinstance(self, ConcreteArray):
- return VirtualSlice(self, chunks, shape)
- r = calculate_slice_strides(self.shape, self.start, self.strides,
- self.backstrides, chunks)
- _, start, strides, backstrides = r
- return W_NDimSlice(start, strides[:], backstrides[:],
- shape[:], self)
-
def descr_reshape(self, space, args_w):
"""reshape(...)
a.reshape(shape)
@@ -484,7 +478,8 @@
w_shape = args_w[0]
else:
w_shape = space.newtuple(args_w)
- new_shape = get_shape_from_iterable(space, self.size, w_shape)
+ new_shape = get_shape_from_iterable(space, support.product(self.shape),
+ w_shape)
return self.reshape(space, new_shape)
def reshape(self, space, new_shape):
@@ -522,7 +517,7 @@
def descr_mean(self, space, w_axis=None):
if space.is_w(w_axis, space.w_None):
w_axis = space.wrap(-1)
- w_denom = space.wrap(self.size)
+ w_denom = space.wrap(support.product(self.shape))
else:
dim = space.int_w(w_axis)
w_denom = space.wrap(self.shape[dim])
@@ -541,7 +536,7 @@
concr.fill(space, w_value)
def descr_nonzero(self, space):
- if self.size > 1:
+ if self.get_size() > 1:
raise OperationError(space.w_ValueError, space.wrap(
"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"))
concr = self.get_concrete_or_scalar()
@@ -620,8 +615,7 @@
space.wrap("axis unsupported for take"))
index_i = index.create_iter()
res_shape = index.shape
- size = support.product(res_shape)
- res = W_NDimArray(size, res_shape[:], concr.dtype, concr.order)
+ res = W_NDimArray(res_shape[:], concr.dtype, concr.order)
res_i = res.create_iter()
shapelen = len(index.shape)
sig = concr.find_sig()
@@ -660,6 +654,11 @@
raise OperationError(space.w_NotImplementedError, space.wrap(
"non-int arg not supported"))
+ def descr_tostring(self, space):
+ ra = ToStringArray(self)
+ loop.compute(ra)
+ return space.wrap(ra.s.build())
+
def compute_first_step(self, sig, frame):
pass
@@ -681,8 +680,7 @@
"""
Intermediate class representing a literal.
"""
- size = 1
- _attrs_ = ["dtype", "value", "shape"]
+ _attrs_ = ["dtype", "value", "shape", "size"]
def __init__(self, dtype, value):
self.shape = []
@@ -690,6 +688,7 @@
self.dtype = dtype
assert isinstance(value, interp_boxes.W_GenericBox)
self.value = value
+ self.size = dtype.get_size()
def find_dtype(self):
return self.dtype
@@ -707,8 +706,7 @@
return self
def reshape(self, space, new_shape):
- size = support.product(new_shape)
- res = W_NDimArray(size, new_shape, self.dtype, 'C')
+ res = W_NDimArray(new_shape, self.dtype, 'C')
res.setitem(0, self.value)
return res
@@ -721,6 +719,7 @@
self.forced_result = None
self.res_dtype = res_dtype
self.name = name
+ self.size = support.product(self.shape) * res_dtype.get_size()
def _del_sources(self):
# Function for deleting references to source arrays,
@@ -728,7 +727,7 @@
raise NotImplementedError
def compute(self):
- ra = ResultArray(self, self.size, self.shape, self.res_dtype)
+ ra = ResultArray(self, self.shape, self.res_dtype)
loop.compute(ra)
return ra.left
@@ -756,7 +755,6 @@
def __init__(self, child, chunks, shape):
self.child = child
self.chunks = chunks
- self.size = support.product(shape)
VirtualArray.__init__(self, 'slice', shape, child.find_dtype())
def create_sig(self):
@@ -768,7 +766,7 @@
def force_if_needed(self):
if self.forced_result is None:
concr = self.child.get_concrete()
- self.forced_result = concr.create_slice(self.chunks)
+ self.forced_result = self.chunks.apply(concr)
def _del_sources(self):
self.child = None
@@ -801,7 +799,6 @@
self.left = left
self.right = right
self.calc_dtype = calc_dtype
- self.size = support.product(self.shape)
def _del_sources(self):
self.left = None
@@ -829,15 +826,30 @@
self.left.create_sig(), self.right.create_sig())
class ResultArray(Call2):
- def __init__(self, child, size, shape, dtype, res=None, order='C'):
+ def __init__(self, child, shape, dtype, res=None, order='C'):
if res is None:
- res = W_NDimArray(size, shape, dtype, order)
+ res = W_NDimArray(shape, dtype, order)
Call2.__init__(self, None, 'assign', shape, dtype, dtype, res, child)
def create_sig(self):
return signature.ResultSignature(self.res_dtype, self.left.create_sig(),
self.right.create_sig())
+class ToStringArray(Call1):
+ def __init__(self, child):
+ dtype = child.find_dtype()
+ self.item_size = dtype.itemtype.get_element_size()
+ self.s = StringBuilder(child.size * self.item_size)
+ Call1.__init__(self, None, 'tostring', child.shape, dtype, dtype,
+ child)
+ self.res = W_NDimArray([1], dtype, 'C')
+ self.res_casted = rffi.cast(rffi.CArrayPtr(lltype.Char),
+ self.res.storage)
+
+ def create_sig(self):
+ return signature.ToStringSignature(self.calc_dtype,
+ self.values.create_sig())
+
def done_if_true(dtype, val):
return dtype.itemtype.bool(val)
@@ -909,13 +921,13 @@
"""
_immutable_fields_ = ['storage']
- def __init__(self, size, shape, dtype, order='C', parent=None):
- self.size = size
+ def __init__(self, shape, dtype, order='C', parent=None):
self.parent = parent
+ self.size = support.product(shape) * dtype.get_size()
if parent is not None:
self.storage = parent.storage
else:
- self.storage = dtype.malloc(size)
+ self.storage = dtype.itemtype.malloc(self.size)
self.order = order
self.dtype = dtype
if self.strides is None:
@@ -934,13 +946,14 @@
return self.dtype
def getitem(self, item):
- return self.dtype.getitem(self.storage, item)
+ return self.dtype.getitem(self, item)
def setitem(self, item, value):
self.invalidated()
- self.dtype.setitem(self.storage, item, value)
+ self.dtype.setitem(self, item, value)
def calc_strides(self, shape):
+ dtype = self.find_dtype()
strides = []
backstrides = []
s = 1
@@ -948,8 +961,8 @@
if self.order == 'C':
shape_rev.reverse()
for sh in shape_rev:
- strides.append(s)
- backstrides.append(s * (sh - 1))
+ strides.append(s * dtype.get_size())
+ backstrides.append(s * (sh - 1) * dtype.get_size())
s *= sh
if self.order == 'C':
strides.reverse()
@@ -997,9 +1010,9 @@
shapelen = len(self.shape)
if shapelen == 1:
rffi.c_memcpy(
- rffi.ptradd(self.storage, self.start * itemsize),
- rffi.ptradd(w_value.storage, w_value.start * itemsize),
- self.size * itemsize
+ rffi.ptradd(self.storage, self.start),
+ rffi.ptradd(w_value.storage, w_value.start),
+ self.size
)
else:
dest = SkipLastAxisIterator(self)
@@ -1014,7 +1027,7 @@
dest.next()
def copy(self, space):
- array = W_NDimArray(self.size, self.shape[:], self.dtype, self.order)
+ array = W_NDimArray(self.shape[:], self.dtype, self.order)
array.setslice(space, self)
return array
@@ -1028,14 +1041,15 @@
class W_NDimSlice(ViewArray):
- def __init__(self, start, strides, backstrides, shape, parent):
+ def __init__(self, start, strides, backstrides, shape, parent, dtype=None):
assert isinstance(parent, ConcreteArray)
if isinstance(parent, W_NDimSlice):
parent = parent.parent
self.strides = strides
self.backstrides = backstrides
- ViewArray.__init__(self, support.product(shape), shape, parent.dtype,
- parent.order, parent)
+ if dtype is None:
+ dtype = parent.dtype
+ ViewArray.__init__(self, shape, dtype, parent.order, parent)
self.start = start
def create_iter(self, transforms=None):
@@ -1050,12 +1064,13 @@
# but then calc_strides would have to accept a stepping factor
strides = []
backstrides = []
- s = self.strides[0]
+ dtype = self.find_dtype()
+ s = self.strides[0] // dtype.get_size()
if self.order == 'C':
new_shape.reverse()
for sh in new_shape:
- strides.append(s)
- backstrides.append(s * (sh - 1))
+ strides.append(s * dtype.get_size())
+ backstrides.append(s * (sh - 1) * dtype.get_size())
s *= max(1, sh)
if self.order == 'C':
strides.reverse()
@@ -1083,14 +1098,16 @@
"""
def setitem(self, item, value):
self.invalidated()
- self.dtype.setitem(self.storage, item, value)
+ self.dtype.setitem(self, item, value)
def setshape(self, space, new_shape):
self.shape = new_shape
self.calc_strides(new_shape)
def create_iter(self, transforms=None):
- return ArrayIterator(self.size).apply_transformations(self, transforms)
+ esize = self.find_dtype().get_size()
+ return ArrayIterator(self.size, esize).apply_transformations(self,
+ transforms)
def create_sig(self):
return signature.ArraySignature(self.dtype)
@@ -1098,22 +1115,18 @@
def __del__(self):
lltype.free(self.storage, flavor='raw', track_allocation=False)
-def _find_size_and_shape(space, w_size):
+def _find_shape(space, w_size):
if space.isinstance_w(w_size, space.w_int):
- size = space.int_w(w_size)
- shape = [size]
- else:
- size = 1
- shape = []
- for w_item in space.fixedview(w_size):
- item = space.int_w(w_item)
- size *= item
- shape.append(item)
- return size, shape
+ return [space.int_w(w_size)]
+ shape = []
+ for w_item in space.fixedview(w_size):
+ shape.append(space.int_w(w_item))
+ return shape
@unwrap_spec(subok=bool, copy=bool, ownmaskna=bool)
def array(space, w_item_or_iterable, w_dtype=None, w_order=None,
- subok=True, copy=True, w_maskna=None, ownmaskna=False):
+ subok=True, copy=True, w_maskna=None, ownmaskna=False,
+ w_ndmin=None):
# find scalar
if w_maskna is None:
w_maskna = space.w_None
@@ -1143,28 +1156,33 @@
if copy:
return w_item_or_iterable.copy(space)
return w_item_or_iterable
- shape, elems_w = find_shape_and_elems(space, w_item_or_iterable)
+ if w_dtype is None or space.is_w(w_dtype, space.w_None):
+ dtype = None
+ else:
+ dtype = space.interp_w(interp_dtype.W_Dtype,
+ space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
+ shape, elems_w = find_shape_and_elems(space, w_item_or_iterable, dtype)
# they come back in C order
- size = len(elems_w)
- if w_dtype is None or space.is_w(w_dtype, space.w_None):
- w_dtype = None
+ if dtype is None:
for w_elem in elems_w:
- w_dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
- w_dtype)
- if w_dtype is interp_dtype.get_dtype_cache(space).w_float64dtype:
+ dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
+ dtype)
+ if dtype is interp_dtype.get_dtype_cache(space).w_float64dtype:
break
- if w_dtype is None:
- w_dtype = space.w_None
- dtype = space.interp_w(interp_dtype.W_Dtype,
- space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
- )
- arr = W_NDimArray(size, shape[:], dtype=dtype, order=order)
+ if dtype is None:
+ dtype = interp_dtype.get_dtype_cache(space).w_float64dtype
shapelen = len(shape)
- arr_iter = ArrayIterator(arr.size)
+ if w_ndmin is not None and not space.is_w(w_ndmin, space.w_None):
+ ndmin = space.int_w(w_ndmin)
+ if ndmin > shapelen:
+ shape = [1] * (ndmin - shapelen) + shape
+ shapelen = ndmin
+ arr = W_NDimArray(shape[:], dtype=dtype, order=order)
+ arr_iter = arr.create_iter()
# XXX we might want to have a jitdriver here
for i in range(len(elems_w)):
w_elem = elems_w[i]
- dtype.setitem(arr.storage, arr_iter.offset,
+ dtype.setitem(arr, arr_iter.offset,
dtype.coerce(space, w_elem))
arr_iter = arr_iter.next(shapelen)
return arr
@@ -1173,22 +1191,22 @@
dtype = space.interp_w(interp_dtype.W_Dtype,
space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
)
- size, shape = _find_size_and_shape(space, w_size)
+ shape = _find_shape(space, w_size)
if not shape:
return scalar_w(space, dtype, space.wrap(0))
- return space.wrap(W_NDimArray(size, shape[:], dtype=dtype))
+ return space.wrap(W_NDimArray(shape[:], dtype=dtype))
def ones(space, w_size, w_dtype=None):
dtype = space.interp_w(interp_dtype.W_Dtype,
space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
)
- size, shape = _find_size_and_shape(space, w_size)
+ shape = _find_shape(space, w_size)
if not shape:
return scalar_w(space, dtype, space.wrap(1))
- arr = W_NDimArray(size, shape[:], dtype=dtype)
+ arr = W_NDimArray(shape[:], dtype=dtype)
one = dtype.box(1)
- arr.dtype.fill(arr.storage, one, 0, size)
+ arr.dtype.fill(arr.storage, one, 0, arr.size)
return space.wrap(arr)
@unwrap_spec(arr=BaseArray, skipna=bool, keepdims=bool)
@@ -1236,13 +1254,13 @@
"array dimensions must agree except for axis being concatenated"))
elif i == axis:
shape[i] += axis_size
- res = W_NDimArray(support.product(shape), shape, dtype, 'C')
+ res = W_NDimArray(shape, dtype, 'C')
chunks = [Chunk(0, i, 1, i) for i in shape]
axis_start = 0
for arr in args_w:
chunks[axis] = Chunk(axis_start, axis_start + arr.shape[axis], 1,
arr.shape[axis])
- res.create_slice(chunks).setslice(space, arr)
+ Chunks(chunks).apply(res).setslice(space, arr)
axis_start += arr.shape[axis]
return res
@@ -1330,6 +1348,7 @@
std = interp2app(BaseArray.descr_std),
fill = interp2app(BaseArray.descr_fill),
+ tostring = interp2app(BaseArray.descr_tostring),
copy = interp2app(BaseArray.descr_copy),
flatten = interp2app(BaseArray.descr_flatten),
@@ -1352,7 +1371,7 @@
self.iter = sig.create_frame(arr).get_final_iter()
self.base = arr
self.index = 0
- ViewArray.__init__(self, arr.size, [arr.size], arr.dtype, arr.order,
+ ViewArray.__init__(self, [arr.get_size()], arr.dtype, arr.order,
arr)
def descr_next(self, space):
@@ -1367,7 +1386,7 @@
return self
def descr_len(self, space):
- return space.wrap(self.size)
+ return space.wrap(self.get_size())
def descr_index(self, space):
return space.wrap(self.index)
@@ -1385,28 +1404,26 @@
raise OperationError(space.w_IndexError,
space.wrap('unsupported iterator index'))
base = self.base
- start, stop, step, lngth = space.decode_index4(w_idx, base.size)
+ start, stop, step, lngth = space.decode_index4(w_idx, base.get_size())
# setslice would have been better, but flat[u:v] for arbitrary
# shapes of array a cannot be represented as a[x1:x2, y1:y2]
basei = ViewIterator(base.start, base.strides,
- base.backstrides,base.shape)
+ base.backstrides, base.shape)
shapelen = len(base.shape)
basei = basei.next_skip_x(shapelen, start)
if lngth <2:
return base.getitem(basei.offset)
- ri = ArrayIterator(lngth)
- res = W_NDimArray(lngth, [lngth], base.dtype,
- base.order)
+ res = W_NDimArray([lngth], base.dtype, base.order)
+ ri = res.create_iter()
while not ri.done():
flat_get_driver.jit_merge_point(shapelen=shapelen,
base=base,
basei=basei,
step=step,
res=res,
- ri=ri,
- )
+ ri=ri)
w_val = base.getitem(basei.offset)
- res.setitem(ri.offset,w_val)
+ res.setitem(ri.offset, w_val)
basei = basei.next_skip_x(shapelen, step)
ri = ri.next(shapelen)
return res
@@ -1417,27 +1434,28 @@
raise OperationError(space.w_IndexError,
space.wrap('unsupported iterator index'))
base = self.base
- start, stop, step, lngth = space.decode_index4(w_idx, base.size)
+ start, stop, step, lngth = space.decode_index4(w_idx, base.get_size())
arr = convert_to_array(space, w_value)
- ai = 0
+ ri = arr.create_iter()
basei = ViewIterator(base.start, base.strides,
- base.backstrides,base.shape)
+ base.backstrides, base.shape)
shapelen = len(base.shape)
basei = basei.next_skip_x(shapelen, start)
while lngth > 0:
flat_set_driver.jit_merge_point(shapelen=shapelen,
- basei=basei,
- base=base,
- step=step,
- arr=arr,
- ai=ai,
- lngth=lngth,
- )
- v = arr.getitem(ai).convert_to(base.dtype)
+ basei=basei,
+ base=base,
+ step=step,
+ arr=arr,
+ lngth=lngth,
+ ri=ri)
+ v = arr.getitem(ri.offset).convert_to(base.dtype)
base.setitem(basei.offset, v)
# need to repeat input values until all assignments are done
- ai = (ai + 1) % arr.size
basei = basei.next_skip_x(shapelen, step)
+ ri = ri.next(shapelen)
+ # WTF is numpy thinking?
+ ri.offset %= arr.size
lngth -= 1
def create_sig(self):
@@ -1445,9 +1463,9 @@
def create_iter(self, transforms=None):
return ViewIterator(self.base.start, self.base.strides,
- self.base.backstrides,
- self.base.shape).apply_transformations(self.base,
- transforms)
+ self.base.backstrides,
+ self.base.shape).apply_transformations(self.base,
+ transforms)
def descr_base(self, space):
return space.wrap(self.base)
diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -51,9 +51,11 @@
raise OperationError(space.w_ValueError, space.wrap(
"string is smaller than requested size"))
- a = W_NDimArray(num_items, [num_items], dtype=dtype)
- for i, val in enumerate(items):
- a.dtype.setitem(a.storage, i, val)
+ a = W_NDimArray([num_items], dtype=dtype)
+ ai = a.create_iter()
+ for val in items:
+ a.dtype.setitem(a, ai.offset, val)
+ ai = ai.next(1)
return space.wrap(a)
@@ -61,6 +63,7 @@
from pypy.module.micronumpy.interp_numarray import W_NDimArray
itemsize = dtype.itemtype.get_element_size()
+ assert itemsize >= 0
if count == -1:
count = length / itemsize
if length % itemsize != 0:
@@ -71,20 +74,23 @@
raise OperationError(space.w_ValueError, space.wrap(
"string is smaller than requested size"))
- a = W_NDimArray(count, [count], dtype=dtype)
- fromstring_loop(a, count, dtype, itemsize, s)
+ a = W_NDimArray([count], dtype=dtype)
+ fromstring_loop(a, dtype, itemsize, s)
return space.wrap(a)
-fromstring_driver = jit.JitDriver(greens=[], reds=['count', 'i', 'itemsize',
- 'dtype', 's', 'a'])
+fromstring_driver = jit.JitDriver(greens=[], reds=['i', 'itemsize',
+ 'dtype', 'ai', 's', 'a'])
-def fromstring_loop(a, count, dtype, itemsize, s):
+def fromstring_loop(a, dtype, itemsize, s):
i = 0
- while i < count:
- fromstring_driver.jit_merge_point(a=a, count=count, dtype=dtype,
- itemsize=itemsize, s=s, i=i)
+ ai = a.create_iter()
+ while not ai.done():
+ fromstring_driver.jit_merge_point(a=a, dtype=dtype,
+ itemsize=itemsize, s=s, i=i,
+ ai=ai)
val = dtype.itemtype.runpack_str(s[i*itemsize:i*itemsize + itemsize])
- a.dtype.setitem(a.storage, i, val)
+ a.dtype.setitem(a, ai.offset, val)
+ ai = ai.next(1)
i += 1
@unwrap_spec(s=str, count=int, sep=str)
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -156,7 +156,7 @@
shape = obj.shape[:dim] + [1] + obj.shape[dim + 1:]
else:
shape = obj.shape[:dim] + obj.shape[dim + 1:]
- result = W_NDimArray(support.product(shape), shape, dtype)
+ result = W_NDimArray(shape, dtype)
arr = AxisReduce(self.func, self.name, self.identity, obj.shape, dtype,
result, obj, dim)
loop.compute(arr)
@@ -314,7 +314,7 @@
return dt
if dt.num >= 5:
return interp_dtype.get_dtype_cache(space).w_float64dtype
- for bytes, dtype in interp_dtype.get_dtype_cache(space).dtypes_by_num_bytes:
+ for bytes, dtype in interp_dtype.get_dtype_cache(space).float_dtypes_by_num_bytes:
if (dtype.kind == interp_dtype.FLOATINGLTR and
dtype.itemtype.get_element_size() > dt.itemtype.get_element_size()):
return dtype
diff --git a/pypy/module/micronumpy/signature.py b/pypy/module/micronumpy/signature.py
--- a/pypy/module/micronumpy/signature.py
+++ b/pypy/module/micronumpy/signature.py
@@ -4,6 +4,7 @@
ViewTransform, BroadcastTransform
from pypy.tool.pairtype import extendabletype
from pypy.module.micronumpy.loop import ComputationDone
+from pypy.rlib import jit
""" Signature specifies both the numpy expression that has been constructed
and the assembler to be compiled. This is a very important observation -
@@ -142,11 +143,10 @@
from pypy.module.micronumpy.interp_numarray import ConcreteArray
concr = arr.get_concrete()
assert isinstance(concr, ConcreteArray)
- storage = concr.storage
if self.iter_no >= len(iterlist):
iterlist.append(concr.create_iter(transforms))
if self.array_no >= len(arraylist):
- arraylist.append(storage)
+ arraylist.append(concr)
def eval(self, frame, arr):
iter = frame.iterators[self.iter_no]
@@ -318,6 +318,20 @@
offset = frame.get_final_iter().offset
arr.left.setitem(offset, self.right.eval(frame, arr.right))
+class ToStringSignature(Call1):
+ def __init__(self, dtype, child):
+ Call1.__init__(self, None, 'tostring', dtype, child)
+
+ @jit.unroll_safe
+ def eval(self, frame, arr):
+ from pypy.module.micronumpy.interp_numarray import ToStringArray
+
+ assert isinstance(arr, ToStringArray)
+ arr.res.setitem(0, self.child.eval(frame, arr.values).convert_to(
+ self.dtype))
+ for i in range(arr.item_size):
+ arr.s.append(arr.res_casted[i])
+
class BroadcastLeft(Call2):
def _invent_numbering(self, cache, allnumbers):
self.left._invent_numbering(new_cache(), allnumbers)
diff --git a/pypy/module/micronumpy/strides.py b/pypy/module/micronumpy/strides.py
--- a/pypy/module/micronumpy/strides.py
+++ b/pypy/module/micronumpy/strides.py
@@ -46,22 +46,31 @@
rbackstrides = [0] * (len(res_shape) - len(orig_shape)) + rbackstrides
return rstrides, rbackstrides
-def find_shape_and_elems(space, w_iterable):
+def is_single_elem(space, w_elem, is_rec_type):
+ if (is_rec_type and space.isinstance_w(w_elem, space.w_tuple)):
+ return True
+ if space.issequence_w(w_elem):
+ return False
+ return True
+
+def find_shape_and_elems(space, w_iterable, dtype):
shape = [space.len_w(w_iterable)]
batch = space.listview(w_iterable)
+ is_rec_type = dtype is not None and dtype.is_record_type()
while True:
new_batch = []
if not batch:
return shape, []
- if not space.issequence_w(batch[0]):
- for elem in batch:
- if space.issequence_w(elem):
+ if is_single_elem(space, batch[0], is_rec_type):
+ for w_elem in batch:
+ if not is_single_elem(space, w_elem, is_rec_type):
raise OperationError(space.w_ValueError, space.wrap(
"setting an array element with a sequence"))
return shape, batch
size = space.len_w(batch[0])
for w_elem in batch:
- if not space.issequence_w(w_elem) or space.len_w(w_elem) != size:
+ if (is_single_elem(space, w_elem, is_rec_type) or
+ space.len_w(w_elem) != size):
raise OperationError(space.w_ValueError, space.wrap(
"setting an array element with a sequence"))
new_batch += space.listview(w_elem)
diff --git a/pypy/module/micronumpy/test/test_base.py b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -4,6 +4,8 @@
from pypy.module.micronumpy.interp_ufuncs import (find_binop_result_dtype,
find_unaryop_result_dtype)
from pypy.module.micronumpy.interp_boxes import W_Float64Box
+from pypy.module.micronumpy.interp_dtype import nonnative_byteorder_prefix,\
+ byteorder_prefix
from pypy.conftest import option
import sys
@@ -15,14 +17,16 @@
sys.modules['numpypy'] = numpy
sys.modules['_numpypy'] = numpy
cls.space = gettestobjspace(usemodules=['micronumpy'])
+ cls.w_non_native_prefix = cls.space.wrap(nonnative_byteorder_prefix)
+ cls.w_native_prefix = cls.space.wrap(byteorder_prefix)
class TestSignature(object):
def test_binop_signature(self, space):
float64_dtype = get_dtype_cache(space).w_float64dtype
bool_dtype = get_dtype_cache(space).w_booldtype
- ar = W_NDimArray(10, [10], dtype=float64_dtype)
- ar2 = W_NDimArray(10, [10], dtype=float64_dtype)
+ ar = W_NDimArray([10], dtype=float64_dtype)
+ ar2 = W_NDimArray([10], dtype=float64_dtype)
v1 = ar.descr_add(space, ar)
v2 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(2.0)))
sig1 = v1.find_sig()
@@ -40,7 +44,7 @@
v4 = ar.descr_add(space, ar)
assert v1.find_sig() is v4.find_sig()
- bool_ar = W_NDimArray(10, [10], dtype=bool_dtype)
+ bool_ar = W_NDimArray([10], dtype=bool_dtype)
v5 = ar.descr_add(space, bool_ar)
assert v5.find_sig() is not v1.find_sig()
assert v5.find_sig() is not v2.find_sig()
@@ -57,7 +61,7 @@
def test_slice_signature(self, space):
float64_dtype = get_dtype_cache(space).w_float64dtype
- ar = W_NDimArray(10, [10], dtype=float64_dtype)
+ ar = W_NDimArray([10], dtype=float64_dtype)
v1 = ar.descr_getitem(space, space.wrap(slice(1, 3, 1)))
v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1)))
assert v1.find_sig() is v2.find_sig()
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -1,5 +1,7 @@
+import py
+from pypy.conftest import option
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
-
+from pypy.interpreter.gateway import interp2app
class AppTestDtypes(BaseNumpyAppTest):
def test_dtype(self):
@@ -12,7 +14,10 @@
assert dtype(d) is d
assert dtype(None) is dtype(float)
assert dtype('int8').name == 'int8'
+ assert dtype(int).fields is None
+ assert dtype(int).names is None
raises(TypeError, dtype, 1042)
+ raises(KeyError, 'dtype(int)["asdasd"]')
def test_dtype_eq(self):
from _numpypy import dtype
@@ -53,13 +58,13 @@
assert a[i] is True_
def test_copy_array_with_dtype(self):
- from _numpypy import array, False_, True_, int64
+ from _numpypy import array, False_, longlong
a = array([0, 1, 2, 3], dtype=long)
# int on 64-bit, long in 32-bit
- assert isinstance(a[0], int64)
+ assert isinstance(a[0], longlong)
b = a.copy()
- assert isinstance(b[0], int64)
+ assert isinstance(b[0], longlong)
a = array([0, 1, 2, 3], dtype=bool)
assert a[0] is False_
@@ -81,17 +86,17 @@
assert a[i] is True_
def test_zeros_long(self):
- from _numpypy import zeros, int64
+ from _numpypy import zeros, longlong
a = zeros(10, dtype=long)
for i in range(10):
- assert isinstance(a[i], int64)
+ assert isinstance(a[i], longlong)
assert a[1] == 0
def test_ones_long(self):
- from _numpypy import ones, int64
+ from _numpypy import ones, longlong
a = ones(10, dtype=long)
for i in range(10):
- assert isinstance(a[i], int64)
+ assert isinstance(a[i], longlong)
assert a[1] == 1
def test_overflow(self):
@@ -181,17 +186,18 @@
assert dtype("float") is dtype(float)
-class AppTestTypes(BaseNumpyAppTest):
+class AppTestTypes(BaseNumpyAppTest):
def test_abstract_types(self):
import _numpypy as numpy
raises(TypeError, numpy.generic, 0)
raises(TypeError, numpy.number, 0)
raises(TypeError, numpy.integer, 0)
exc = raises(TypeError, numpy.signedinteger, 0)
- assert str(exc.value) == "cannot create 'signedinteger' instances"
+ assert 'cannot create' in str(exc.value)
+ assert 'signedinteger' in str(exc.value)
exc = raises(TypeError, numpy.unsignedinteger, 0)
- assert str(exc.value) == "cannot create 'unsignedinteger' instances"
-
+ assert 'cannot create' in str(exc.value)
+ assert 'unsignedinteger' in str(exc.value)
raises(TypeError, numpy.floating, 0)
raises(TypeError, numpy.inexact, 0)
@@ -296,6 +302,7 @@
else:
raises(OverflowError, numpy.int32, 2147483648)
raises(OverflowError, numpy.int32, '2147483648')
+ assert numpy.dtype('int32') is numpy.dtype(numpy.int32)
def test_uint32(self):
import sys
@@ -327,15 +334,11 @@
assert numpy.dtype(numpy.int64).type is numpy.int64
assert numpy.int64(3) == 3
- if sys.maxint >= 2 ** 63 - 1:
- assert numpy.int64(9223372036854775807) == 9223372036854775807
- assert numpy.int64('9223372036854775807') == 9223372036854775807
- else:
- raises(OverflowError, numpy.int64, 9223372036854775807)
- raises(OverflowError, numpy.int64, '9223372036854775807')
+ assert numpy.int64(9223372036854775807) == 9223372036854775807
+ assert numpy.int64(9223372036854775807) == 9223372036854775807
raises(OverflowError, numpy.int64, 9223372036854775808)
- raises(OverflowError, numpy.int64, '9223372036854775808')
+ raises(OverflowError, numpy.int64, 9223372036854775808L)
def test_uint64(self):
import sys
@@ -404,10 +407,29 @@
assert issubclass(int64, int)
assert int_ is int64
+ def test_various_types(self):
+ import _numpypy as numpy
+ import sys
+
+ assert numpy.int16 is numpy.short
+ assert numpy.int8 is numpy.byte
+ assert numpy.bool_ is numpy.bool8
+ if sys.maxint == (1 << 63) - 1:
+ assert numpy.intp is numpy.int64
+ else:
+ assert numpy.intp is numpy.int32
+
+ def test_mro(self):
+ import _numpypy as numpy
+
+ assert numpy.int16.__mro__ == (numpy.int16, numpy.signedinteger,
+ numpy.integer, numpy.number,
+ numpy.generic, object)
+ assert numpy.bool_.__mro__ == (numpy.bool_, numpy.generic, object)
+
def test_operators(self):
from operator import truediv
from _numpypy import float64, int_, True_, False_
-
assert 5 / int_(2) == int_(2)
assert truediv(int_(3), int_(2)) == float64(1.5)
assert truediv(3, int_(2)) == float64(1.5)
@@ -427,9 +449,115 @@
assert int_(3) ^ int_(5) == int_(6)
assert True_ ^ False_ is True_
assert 5 ^ int_(3) == int_(6)
-
assert +int_(3) == int_(3)
assert ~int_(3) == int_(-4)
-
raises(TypeError, lambda: float64(3) & 1)
+ def test_alternate_constructs(self):
+ from _numpypy import dtype
+ nnp = self.non_native_prefix
+ byteorder = self.native_prefix
+ assert dtype('i8') == dtype(byteorder + 'i8') == dtype('=i8') # XXX should be equal == dtype(long)
+ assert dtype(nnp + 'i8') != dtype('i8')
+ assert dtype(nnp + 'i8').byteorder == nnp
+ assert dtype('=i8').byteorder == '='
+ assert dtype(byteorder + 'i8').byteorder == '='
+
+ def test_alignment(self):
+ from _numpypy import dtype
+ assert dtype('i4').alignment == 4
+
+ def test_typeinfo(self):
+ from _numpypy import typeinfo, void, number, int64, bool_
+ assert typeinfo['Number'] == number
+ assert typeinfo['LONGLONG'] == ('q', 9, 64, 8, 9223372036854775807L, -9223372036854775808L, int64)
+ assert typeinfo['VOID'] == ('V', 20, 0, 1, void)
+ assert typeinfo['BOOL'] == ('?', 0, 8, 1, 1, 0, bool_)
+
+class AppTestStrUnicodeDtypes(BaseNumpyAppTest):
+ def test_str_unicode(self):
+ from _numpypy import str_, unicode_, character, flexible, generic
+
+ assert str_.mro() == [str_, str, basestring, character, flexible, generic, object]
+ assert unicode_.mro() == [unicode_, unicode, basestring, character, flexible, generic, object]
+
+ def test_str_dtype(self):
+ from _numpypy import dtype, str_
+
+ raises(TypeError, "dtype('Sx')")
+ d = dtype('S8')
+ assert d.itemsize == 8
+ assert dtype(str) == dtype('S')
+ assert d.kind == 'S'
+ assert d.type is str_
+ assert d.name == "string64"
+ assert d.num == 18
+
+ def test_unicode_dtype(self):
+ from _numpypy import dtype, unicode_
+
+ raises(TypeError, "dtype('Ux')")
+ d = dtype('U8')
+ assert d.itemsize == 8 * 4
+ assert dtype(unicode) == dtype('U')
+ assert d.kind == 'U'
+ assert d.type is unicode_
+ assert d.name == "unicode256"
+ assert d.num == 19
+
+ def test_string_boxes(self):
+ from _numpypy import str_
+ assert isinstance(str_(3), str_)
+
+ def test_unicode_boxes(self):
+ from _numpypy import unicode_
+ assert isinstance(unicode_(3), unicode)
+
+class AppTestRecordDtypes(BaseNumpyAppTest):
+ def test_create(self):
+ from _numpypy import dtype, void
+
+ raises(ValueError, "dtype([('x', int), ('x', float)])")
+ d = dtype([("x", "int32"), ("y", "int32"), ("z", "int32"), ("value", float)])
+ assert d.fields['x'] == (dtype('int32'), 0)
+ assert d.fields['value'] == (dtype(float), 12)
+ assert d['x'] == dtype('int32')
+ assert d.name == "void160"
+ assert d.num == 20
+ assert d.itemsize == 20
+ assert d.kind == 'V'
+ assert d.type is void
+ assert d.char == 'V'
+ assert d.names == ("x", "y", "z", "value")
+ raises(KeyError, 'd["xyz"]')
+ raises(KeyError, 'd.fields["xyz"]')
+
+ def test_create_from_dict(self):
+ skip("not yet")
+ from _numpypy import dtype
+ d = dtype({'names': ['a', 'b', 'c'],
+ })
+
+class AppTestNotDirect(BaseNumpyAppTest):
+ def setup_class(cls):
+ BaseNumpyAppTest.setup_class.im_func(cls)
+ def check_non_native(w_obj, w_obj2):
+ assert w_obj.storage[0] == w_obj2.storage[1]
+ assert w_obj.storage[1] == w_obj2.storage[0]
+ if w_obj.storage[0] == '\x00':
+ assert w_obj2.storage[1] == '\x00'
+ assert w_obj2.storage[0] == '\x01'
+ else:
+ assert w_obj2.storage[1] == '\x01'
+ assert w_obj2.storage[0] == '\x00'
+ cls.w_check_non_native = cls.space.wrap(interp2app(check_non_native))
+ if option.runappdirect:
+ py.test.skip("not a direct test")
+
+ def test_non_native(self):
+ from _numpypy import array
+ a = array([1, 2, 3], dtype=self.non_native_prefix + 'i2')
+ assert a[0] == 1
+ assert (a + a)[1] == 4
+ self.check_non_native(a, array([1, 2, 3], 'i2'))
+
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
@@ -5,15 +5,23 @@
from pypy.interpreter.error import OperationError
from pypy.module.micronumpy import signature
from pypy.module.micronumpy.appbridge import get_appbridge_cache
-from pypy.module.micronumpy.interp_iter import Chunk
+from pypy.module.micronumpy.interp_iter import Chunk, Chunks
from pypy.module.micronumpy.interp_numarray import W_NDimArray, shape_agreement
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
class MockDtype(object):
- def malloc(self, size):
- return None
+ class itemtype(object):
+ @staticmethod
+ def malloc(size):
+ return None
+ def get_size(self):
+ return 1
+
+
+def create_slice(a, chunks):
+ return Chunks(chunks).apply(a)
class TestNumArrayDirect(object):
def newslice(self, *args):
@@ -29,116 +37,116 @@
return self.space.newtuple(args_w)
def test_strides_f(self):
- a = W_NDimArray(100, [10, 5, 3], MockDtype(), 'F')
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
assert a.strides == [1, 10, 50]
assert a.backstrides == [9, 40, 100]
def test_strides_c(self):
- a = W_NDimArray(100, [10, 5, 3], MockDtype(), 'C')
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'C')
assert a.strides == [15, 3, 1]
assert a.backstrides == [135, 12, 2]
def test_create_slice_f(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice([Chunk(3, 0, 0, 1)])
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+ s = create_slice(a, [Chunk(3, 0, 0, 1)])
assert s.start == 3
assert s.strides == [10, 50]
assert s.backstrides == [40, 100]
- s = a.create_slice([Chunk(1, 9, 2, 4)])
+ s = create_slice(a, [Chunk(1, 9, 2, 4)])
assert s.start == 1
assert s.strides == [2, 10, 50]
assert s.backstrides == [6, 40, 100]
- s = a.create_slice([Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1), Chunk(1, 0, 0, 1)])
+ s = create_slice(a, [Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1), Chunk(1, 0, 0, 1)])
assert s.shape == [2, 1]
assert s.strides == [3, 10]
assert s.backstrides == [3, 0]
- s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
+ s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
assert s.start == 20
assert s.shape == [10, 3]
def test_create_slice_c(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C')
- s = a.create_slice([Chunk(3, 0, 0, 1)])
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'C')
+ s = create_slice(a, [Chunk(3, 0, 0, 1)])
assert s.start == 45
assert s.strides == [3, 1]
assert s.backstrides == [12, 2]
- s = a.create_slice([Chunk(1, 9, 2, 4)])
+ s = create_slice(a, [Chunk(1, 9, 2, 4)])
assert s.start == 15
assert s.strides == [30, 3, 1]
assert s.backstrides == [90, 12, 2]
- s = a.create_slice([Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1),
+ s = create_slice(a, [Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1),
Chunk(1, 0, 0, 1)])
assert s.start == 19
assert s.shape == [2, 1]
assert s.strides == [45, 3]
assert s.backstrides == [45, 0]
- s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
+ s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
assert s.start == 6
assert s.shape == [10, 3]
def test_slice_of_slice_f(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice([Chunk(5, 0, 0, 1)])
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+ s = create_slice(a, [Chunk(5, 0, 0, 1)])
assert s.start == 5
- s2 = s.create_slice([Chunk(3, 0, 0, 1)])
+ s2 = create_slice(s, [Chunk(3, 0, 0, 1)])
assert s2.shape == [3]
assert s2.strides == [50]
assert s2.parent is a
assert s2.backstrides == [100]
assert s2.start == 35
- s = a.create_slice([Chunk(1, 5, 3, 2)])
- s2 = s.create_slice([Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)])
+ s = create_slice(a, [Chunk(1, 5, 3, 2)])
+ s2 = create_slice(s, [Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)])
assert s2.shape == [2, 3]
assert s2.strides == [3, 50]
assert s2.backstrides == [3, 100]
assert s2.start == 1 * 15 + 2 * 3
def test_slice_of_slice_c(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), order='C')
- s = a.create_slice([Chunk(5, 0, 0, 1)])
+ a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
+ s = create_slice(a, [Chunk(5, 0, 0, 1)])
assert s.start == 15 * 5
- s2 = s.create_slice([Chunk(3, 0, 0, 1)])
+ s2 = create_slice(s, [Chunk(3, 0, 0, 1)])
assert s2.shape == [3]
assert s2.strides == [1]
assert s2.parent is a
assert s2.backstrides == [2]
assert s2.start == 5 * 15 + 3 * 3
- s = a.create_slice([Chunk(1, 5, 3, 2)])
- s2 = s.create_slice([Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)])
+ s = create_slice(a, [Chunk(1, 5, 3, 2)])
+ s2 = create_slice(s, [Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)])
assert s2.shape == [2, 3]
assert s2.strides == [45, 1]
assert s2.backstrides == [45, 2]
assert s2.start == 1 * 15 + 2 * 3
def test_negative_step_f(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice([Chunk(9, -1, -2, 5)])
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+ s = create_slice(a, [Chunk(9, -1, -2, 5)])
assert s.start == 9
assert s.strides == [-2, 10, 50]
assert s.backstrides == [-8, 40, 100]
def test_negative_step_c(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), order='C')
- s = a.create_slice([Chunk(9, -1, -2, 5)])
+ a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
+ s = create_slice(a, [Chunk(9, -1, -2, 5)])
assert s.start == 135
assert s.strides == [-30, 3, 1]
assert s.backstrides == [-120, 12, 2]
def test_index_of_single_item_f(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
assert r == 1 + 2 * 10 + 2 * 50
- s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
+ s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
r = s._index_of_single_item(self.space, self.newtuple(1, 0))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0))
r = s._index_of_single_item(self.space, self.newtuple(1, 1))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 1))
def test_index_of_single_item_c(self):
- a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C')
+ a = W_NDimArray([10, 5, 3], MockDtype(), 'C')
r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
assert r == 1 * 3 * 5 + 2 * 3 + 2
- s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
+ s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
r = s._index_of_single_item(self.space, self.newtuple(1, 0))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0))
r = s._index_of_single_item(self.space, self.newtuple(1, 1))
@@ -203,6 +211,18 @@
assert a.shape == (3,)
assert a.dtype is dtype(int)
+ def test_ndmin(self):
+ from _numpypy import array
+
+ arr = array([[[1]]], ndmin=1)
+ assert arr.shape == (1, 1, 1)
+
+ def test_noop_ndmin(self):
+ from _numpypy import array
+
+ arr = array([1], ndmin=3)
+ assert arr.shape == (1, 1, 1)
+
def test_type(self):
from _numpypy import array
ar = array(range(5))
@@ -416,6 +436,7 @@
from numpypy.core.numeric import newaxis
a = array(range(5))
b = a[newaxis]
+ assert b.shape == (1, 5)
assert (b[0,1:] == a[1:]).all()
def test_slice_then_newaxis(self):
@@ -1140,7 +1161,7 @@
assert array([True, False]).dtype is dtype(bool)
assert array([True, 1]).dtype is dtype(int)
assert array([1, 2, 3]).dtype is dtype(int)
- assert array([1L, 2, 3]).dtype is dtype(long)
+ #assert array([1L, 2, 3]).dtype is dtype(long)
assert array([1.2, True]).dtype is dtype(float)
assert array([1.2, 5]).dtype is dtype(float)
assert array([]).dtype is dtype(float)
@@ -1647,6 +1668,7 @@
a = arange(12).reshape(3,4)
b = a.T.flat
b[6::2] = [-1, -2]
+ print a == [[0, 1, -1, 3], [4, 5, 6, -1], [8, 9, -2, 11]]
assert (a == [[0, 1, -1, 3], [4, 5, 6, -1], [8, 9, -2, 11]]).all()
b[0:2] = [[[100]]]
assert(a[0,0] == 100)
@@ -1921,6 +1943,12 @@
#5 bytes is larger than 3 bytes
raises(ValueError, fromstring, "\x01\x02\x03", count=5, dtype=uint8)
+ def test_tostring(self):
+ from _numpypy import array
+ assert array([1, 2, 3], 'i2').tostring() == '\x01\x00\x02\x00\x03\x00'
+ assert array([1, 2, 3], 'i2')[::2].tostring() == '\x01\x00\x03\x00'
+ assert array([1, 2, 3], '<i2')[::2].tostring() == '\x01\x00\x03\x00'
+ assert array([1, 2, 3], '>i2')[::2].tostring() == '\x00\x01\x00\x03'
class AppTestRanges(BaseNumpyAppTest):
def test_arange(self):
@@ -1966,3 +1994,57 @@
cache = get_appbridge_cache(cls.space)
cache.w_array_repr = cls.old_array_repr
cache.w_array_str = cls.old_array_str
+
+class AppTestRecordDtype(BaseNumpyAppTest):
+ def test_zeros(self):
+ from _numpypy import zeros
+ a = zeros(2, dtype=[('x', int), ('y', float)])
+ raises(IndexError, 'a[0]["xyz"]')
+ assert a[0]['x'] == 0
+ assert a[0]['y'] == 0
+ raises(ValueError, "a[0] = (1, 2, 3)")
+ a[0]['x'] = 13
+ assert a[0]['x'] == 13
+ a[1] = (1, 2)
+ assert a[1]['y'] == 2
+ b = zeros(2, dtype=[('x', int), ('y', float)])
+ b[1] = a[1]
+ assert a[1]['y'] == 2
+
+ def test_views(self):
+ from _numpypy import array
+ a = array([(1, 2), (3, 4)], dtype=[('x', int), ('y', float)])
+ raises(ValueError, 'array([1])["x"]')
+ raises(ValueError, 'a["z"]')
+ assert a['x'][1] == 3
+ assert a['y'][1] == 4
+ a['x'][0] = 15
+ assert a['x'][0] == 15
+ b = a['x'] + a['y']
+ assert (b == [15+2, 3+4]).all()
+ assert b.dtype == float
+
+ def test_assign_tuple(self):
+ from _numpypy import zeros
+ a = zeros((2, 3), dtype=[('x', int), ('y', float)])
+ a[1, 2] = (1, 2)
+ assert a['x'][1, 2] == 1
+ assert a['y'][1, 2] == 2
+
+ def test_creation_and_repr(self):
+ from _numpypy import array
+ a = array([(1, 2), (3, 4)], dtype=[('x', int), ('y', float)])
+ assert repr(a[0]) == '(1, 2.0)'
+
+ def test_nested_dtype(self):
+ from _numpypy import zeros
+ a = [('x', int), ('y', float)]
+ b = [('x', int), ('y', a)]
+ arr = zeros(3, dtype=b)
+ arr[1]['x'] = 15
+ assert arr[1]['x'] == 15
+ arr[1]['y']['y'] = 3.5
+ assert arr[1]['y']['y'] == 3.5
+ assert arr[1]['y']['x'] == 0.0
+ assert arr[1]['x'] == 15
+
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -197,7 +197,6 @@
def test_signbit(self):
from _numpypy import signbit, copysign
- import struct
assert (signbit([0, 0.0, 1, 1.0, float('inf'), float('nan')]) ==
[False, False, False, False, False, False]).all()
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
@@ -1,15 +1,20 @@
import functools
import math
+import struct
from pypy.interpreter.error import OperationError
from pypy.module.micronumpy import interp_boxes
from pypy.objspace.std.floatobject import float2string
from pypy.rlib import rfloat, libffi, clibffi
-from pypy.rlib.objectmodel import specialize
-from pypy.rlib.rarithmetic import LONG_BIT, widen
+from pypy.rlib.objectmodel import specialize, we_are_translated
+from pypy.rlib.rarithmetic import widen, byteswap
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.rlib.rstruct.runpack import runpack
+from pypy.tool.sourcetools import func_with_new_name
+from pypy.rlib import jit
+VOID_STORAGE = lltype.Array(lltype.Char, hints={'nolength': True,
+ 'render_as_void': True})
degToRad = math.pi / 180.0
log2 = math.log(2)
@@ -59,9 +64,20 @@
return dispatcher
class BaseType(object):
+ _attrs_ = ()
+
def _unimplemented_ufunc(self, *args):
raise NotImplementedError
+ def malloc(self, size):
+ # XXX find out why test_zjit explodes with tracking of allocations
+ return lltype.malloc(VOID_STORAGE, size,
+ zero=True, flavor="raw",
+ track_allocation=False, add_memory_pressure=True)
+
+ def __repr__(self):
+ return self.__class__.__name__
+
class Primitive(object):
_mixin_ = True
@@ -76,7 +92,7 @@
assert isinstance(box, self.BoxType)
return box.value
- def coerce(self, space, w_item):
+ def coerce(self, space, dtype, w_item):
if isinstance(w_item, self.BoxType):
return w_item
return self.coerce_subtype(space, space.gettypefor(self.BoxType), w_item)
@@ -97,32 +113,41 @@
def default_fromstring(self, space):
raise NotImplementedError
- def read(self, storage, width, i, offset):
- return self.box(libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
- width, storage, i, offset
- ))
+ def _read(self, storage, width, i, offset):
+ if we_are_translated():
+ return libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset)
+ else:
+ return libffi.array_getitem_T(self.T, width, storage, i, offset)
- def read_bool(self, storage, width, i, offset):
- return bool(self.for_computation(
- libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
- width, storage, i, offset)))
+ def read(self, arr, width, i, offset, dtype=None):
+ return self.box(self._read(arr.storage, width, i, offset))
- def store(self, storage, width, i, offset, box):
- value = self.unbox(box)
- libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
- width, storage, i, offset, value
- )
+ def read_bool(self, arr, width, i, offset):
+ return bool(self.for_computation(self._read(arr.storage, width, i, offset)))
+
+ def _write(self, storage, width, i, offset, value):
+ if we_are_translated():
+ libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset, value)
+ else:
+ libffi.array_setitem_T(self.T, width, storage, i, offset, value)
+
+
+ def store(self, arr, width, i, offset, box):
+ self._write(arr.storage, width, i, offset, self.unbox(box))
def fill(self, storage, width, box, start, stop, offset):
value = self.unbox(box)
- for i in xrange(start, stop):
- libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
- width, storage, i, offset, value
- )
+ for i in xrange(start, stop, width):
+ self._write(storage, 1, i, offset, value)
def runpack_str(self, s):
return self.box(runpack(self.format_code, s))
+ def pack_str(self, box):
+ return struct.pack(self.format_code, self.unbox(box))
+
@simple_binary_op
def add(self, v1, v2):
return v1 + v2
@@ -214,8 +239,31 @@
def min(self, v1, v2):
return min(v1, v2)
+class NonNativePrimitive(Primitive):
+ _mixin_ = True
+
+ def _read(self, storage, width, i, offset):
+ if we_are_translated():
+ res = libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset)
+ else:
+ res = libffi.array_getitem_T(self.T, width, storage, i, offset)
+ return byteswap(res)
+
+ def _write(self, storage, width, i, offset, value):
+ value = byteswap(value)
+ if we_are_translated():
+ libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset, value)
+ else:
+ libffi.array_setitem_T(self.T, width, storage, i, offset, value)
+
+ def pack_str(self, box):
+ return struct.pack(self.format_code, byteswap(self.unbox(box)))
class Bool(BaseType, Primitive):
+ _attrs_ = ()
+
T = lltype.Bool
BoxType = interp_boxes.W_BoolBox
format_code = "?"
@@ -242,8 +290,7 @@
return space.wrap(self.unbox(w_item))
def str_format(self, box):
- value = self.unbox(box)
- return "True" if value else "False"
+ return "True" if self.unbox(box) else "False"
def for_computation(self, v):
return int(v)
@@ -267,15 +314,18 @@
def invert(self, v):
return ~v
+NonNativeBool = Bool
+
class Integer(Primitive):
_mixin_ = True
+ def _base_coerce(self, space, w_item):
+ return self.box(space.int_w(space.call_function(space.w_int, w_item)))
def _coerce(self, space, w_item):
- return self.box(space.int_w(space.call_function(space.w_int, w_item)))
+ return self._base_coerce(space, w_item)
def str_format(self, box):
- value = self.unbox(box)
- return str(self.for_computation(value))
+ return str(self.for_computation(self.unbox(box)))
def for_computation(self, v):
return widen(v)
@@ -347,68 +397,170 @@
def invert(self, v):
return ~v
+class NonNativeInteger(NonNativePrimitive, Integer):
+ _mixin_ = True
+
class Int8(BaseType, Integer):
+ _attrs_ = ()
+
T = rffi.SIGNEDCHAR
BoxType = interp_boxes.W_Int8Box
format_code = "b"
+NonNativeInt8 = Int8
class UInt8(BaseType, Integer):
+ _attrs_ = ()
+
T = rffi.UCHAR
BoxType = interp_boxes.W_UInt8Box
format_code = "B"
+NonNativeUInt8 = UInt8
class Int16(BaseType, Integer):
+ _attrs_ = ()
+
+ T = rffi.SHORT
+ BoxType = interp_boxes.W_Int16Box
+ format_code = "h"
+
+class NonNativeInt16(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
T = rffi.SHORT
BoxType = interp_boxes.W_Int16Box
format_code = "h"
class UInt16(BaseType, Integer):
+ _attrs_ = ()
+
+ T = rffi.USHORT
+ BoxType = interp_boxes.W_UInt16Box
+ format_code = "H"
+
+class NonNativeUInt16(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
T = rffi.USHORT
BoxType = interp_boxes.W_UInt16Box
format_code = "H"
class Int32(BaseType, Integer):
+ _attrs_ = ()
+
+ T = rffi.INT
+ BoxType = interp_boxes.W_Int32Box
+ format_code = "i"
+
+class NonNativeInt32(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
T = rffi.INT
BoxType = interp_boxes.W_Int32Box
format_code = "i"
class UInt32(BaseType, Integer):
+ _attrs_ = ()
+
+ T = rffi.UINT
+ BoxType = interp_boxes.W_UInt32Box
+ format_code = "I"
+
+class NonNativeUInt32(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
T = rffi.UINT
BoxType = interp_boxes.W_UInt32Box
format_code = "I"
class Long(BaseType, Integer):
+ _attrs_ = ()
+
+ T = rffi.LONG
+ BoxType = interp_boxes.W_LongBox
+ format_code = "l"
+
+class NonNativeLong(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
T = rffi.LONG
BoxType = interp_boxes.W_LongBox
format_code = "l"
class ULong(BaseType, Integer):
+ _attrs_ = ()
+
T = rffi.ULONG
BoxType = interp_boxes.W_ULongBox
format_code = "L"
+class NonNativeULong(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
+ T = rffi.ULONG
+ BoxType = interp_boxes.W_ULongBox
+ format_code = "L"
+
+def _int64_coerce(self, space, w_item):
+ try:
+ return self._base_coerce(space, w_item)
+ except OperationError, e:
+ if not e.match(space, space.w_OverflowError):
+ raise
+ bigint = space.bigint_w(w_item)
+ try:
+ value = bigint.tolonglong()
+ except OverflowError:
+ raise OperationError(space.w_OverflowError, space.w_None)
+ return self.box(value)
+
class Int64(BaseType, Integer):
+ _attrs_ = ()
+
T = rffi.LONGLONG
BoxType = interp_boxes.W_Int64Box
format_code = "q"
+ _coerce = func_with_new_name(_int64_coerce, '_coerce')
+
+class NonNativeInt64(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
+ T = rffi.LONGLONG
+ BoxType = interp_boxes.W_Int64Box
+ format_code = "q"
+
+ _coerce = func_with_new_name(_int64_coerce, '_coerce')
+
+def _uint64_coerce(self, space, w_item):
+ try:
+ return self._base_coerce(space, w_item)
+ except OperationError, e:
+ if not e.match(space, space.w_OverflowError):
+ raise
+ bigint = space.bigint_w(w_item)
+ try:
+ value = bigint.toulonglong()
+ except OverflowError:
+ raise OperationError(space.w_OverflowError, space.w_None)
+ return self.box(value)
+
class UInt64(BaseType, Integer):
+ _attrs_ = ()
+
T = rffi.ULONGLONG
BoxType = interp_boxes.W_UInt64Box
format_code = "Q"
- def _coerce(self, space, w_item):
- try:
- return Integer._coerce(self, space, w_item)
- except OperationError, e:
- if not e.match(space, space.w_OverflowError):
- raise
- bigint = space.bigint_w(w_item)
- try:
- value = bigint.toulonglong()
- except OverflowError:
- raise OperationError(space.w_OverflowError, space.w_None)
- return self.box(value)
+ _coerce = func_with_new_name(_uint64_coerce, '_coerce')
+
+class NonNativeUInt64(BaseType, NonNativeInteger):
+ _attrs_ = ()
+
+ T = rffi.ULONGLONG
+ BoxType = interp_boxes.W_UInt64Box
+ format_code = "Q"
+
+ _coerce = func_with_new_name(_uint64_coerce, '_coerce')
class Float(Primitive):
_mixin_ = True
@@ -417,8 +569,8 @@
return self.box(space.float_w(space.call_function(space.w_float, w_item)))
def str_format(self, box):
- value = self.unbox(box)
- return float2string(self.for_computation(value), "g", rfloat.DTSF_STR_PRECISION)
+ return float2string(self.for_computation(self.unbox(box)), "g",
+ rfloat.DTSF_STR_PRECISION)
def for_computation(self, v):
return float(v)
@@ -702,13 +854,158 @@
return -rfloat.INFINITY
return rfloat.NAN
+class NonNativeFloat(NonNativePrimitive, Float):
+ _mixin_ = True
+
+ def _read(self, storage, width, i, offset):
+ if we_are_translated():
+ res = libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset)
+ else:
+ res = libffi.array_getitem_T(self.T, width, storage, i, offset)
+ #return byteswap(res)
+ return res
+
+ def _write(self, storage, width, i, offset, value):
+ #value = byteswap(value) XXX
+ if we_are_translated():
+ libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset, value)
+ else:
+ libffi.array_setitem_T(self.T, width, storage, i, offset, value)
+
+ def pack_str(self, box):
+ # XXX byteswap
+ return struct.pack(self.format_code, self.unbox(box))
+
class Float32(BaseType, Float):
+ _attrs_ = ()
+
T = rffi.FLOAT
BoxType = interp_boxes.W_Float32Box
format_code = "f"
+class NonNativeFloat32(BaseType, NonNativeFloat):
+ _attrs_ = ()
+
+ T = rffi.FLOAT
+ BoxType = interp_boxes.W_Float32Box
+ format_code = "f"
+
class Float64(BaseType, Float):
+ _attrs_ = ()
+
T = rffi.DOUBLE
BoxType = interp_boxes.W_Float64Box
format_code = "d"
+
+class NonNativeFloat64(BaseType, NonNativeFloat):
+ _attrs_ = ()
+
+ T = rffi.DOUBLE
+ BoxType = interp_boxes.W_Float64Box
+ format_code = "d"
+
+class BaseStringType(object):
+ _mixin_ = True
+
+ def __init__(self, size=0):
+ self.size = size
+
+ def get_element_size(self):
+ return self.size * rffi.sizeof(self.T)
+
+class StringType(BaseType, BaseStringType):
+ T = lltype.Char
+
+class VoidType(BaseType, BaseStringType):
+ T = lltype.Char
+
+NonNativeVoidType = VoidType
+NonNativeStringType = StringType
+
+class UnicodeType(BaseType, BaseStringType):
+ T = lltype.UniChar
+
+NonNativeUnicodeType = UnicodeType
+
+class RecordType(BaseType):
+
+ T = lltype.Char
+
+ def __init__(self, offsets_and_fields, size):
+ self.offsets_and_fields = offsets_and_fields
+ self.size = size
+
+ def get_element_size(self):
+ return self.size
+
+ def read(self, arr, width, i, offset, dtype=None):
+ if dtype is None:
+ dtype = arr.dtype
+ return interp_boxes.W_VoidBox(arr, i + offset, dtype)
+
+ @jit.unroll_safe
+ def coerce(self, space, dtype, w_item):
+ from pypy.module.micronumpy.interp_numarray import W_NDimArray
+
+ if isinstance(w_item, interp_boxes.W_VoidBox):
+ return w_item
+ # we treat every sequence as sequence, no special support
+ # for arrays
+ if not space.issequence_w(w_item):
+ raise OperationError(space.w_TypeError, space.wrap(
+ "expected sequence"))
+ if len(self.offsets_and_fields) != space.int_w(space.len(w_item)):
+ raise OperationError(space.w_ValueError, space.wrap(
+ "wrong length"))
+ items_w = space.fixedview(w_item)
+ # XXX optimize it out one day, but for now we just allocate an
+ # array
+ arr = W_NDimArray([1], dtype)
+ for i in range(len(items_w)):
+ subdtype = dtype.fields[dtype.fieldnames[i]][1]
+ ofs, itemtype = self.offsets_and_fields[i]
+ w_item = items_w[i]
+ w_box = itemtype.coerce(space, subdtype, w_item)
+ itemtype.store(arr, 1, 0, ofs, w_box)
+ return interp_boxes.W_VoidBox(arr, 0, arr.dtype)
+
+ @jit.unroll_safe
+ def store(self, arr, _, i, ofs, box):
+ assert isinstance(box, interp_boxes.W_VoidBox)
+ for k in range(self.get_element_size()):
+ arr.storage[k + i] = box.arr.storage[k + box.ofs]
+
+ @jit.unroll_safe
+ def str_format(self, box):
+ assert isinstance(box, interp_boxes.W_VoidBox)
+ pieces = ["("]
+ first = True
+ for ofs, tp in self.offsets_and_fields:
+ if first:
+ first = False
+ else:
+ pieces.append(", ")
+ pieces.append(tp.str_format(tp.read(box.arr, 1, box.ofs, ofs)))
+ pieces.append(")")
+ return "".join(pieces)
+
+for tp in [Int32, Int64]:
+ if tp.T == lltype.Signed:
+ IntP = tp
+ break
+for tp in [UInt32, UInt64]:
+ if tp.T == lltype.Unsigned:
+ UIntP = tp
+ break
+del tp
+
+def _setup():
+ # compute alignment
+ for tp in globals().values():
+ if isinstance(tp, type) and hasattr(tp, 'T'):
+ tp.alignment = clibffi.cast_type_to_ffitype(tp.T).c_alignment
+_setup()
+del _setup
diff --git a/pypy/module/posix/test/test_posix2.py b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -14,10 +14,10 @@
def setup_module(mod):
if os.name != 'nt':
- mod.space = gettestobjspace(usemodules=['posix', 'fcntl'])
+ mod.space = gettestobjspace(usemodules=['posix', 'fcntl', 'struct'])
else:
# On windows, os.popen uses the subprocess module
- mod.space = gettestobjspace(usemodules=['posix', '_rawffi', 'thread'])
+ mod.space = gettestobjspace(usemodules=['posix', '_rawffi', 'thread', 'struct'])
mod.path = udir.join('posixtestfile.txt')
mod.path.write("this is a test")
mod.path2 = udir.join('test_posix2-')
diff --git a/pypy/module/rctime/test/test_rctime.py b/pypy/module/rctime/test/test_rctime.py
--- a/pypy/module/rctime/test/test_rctime.py
+++ b/pypy/module/rctime/test/test_rctime.py
@@ -3,7 +3,7 @@
class AppTestRCTime:
def setup_class(cls):
- space = gettestobjspace(usemodules=('rctime',))
+ space = gettestobjspace(usemodules=('rctime', 'struct'))
cls.space = space
def test_attributes(self):
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
@@ -34,7 +34,7 @@
assert repr(a) == "array([], dtype=float64)"
a = zeros(1001)
assert repr(a) == "array([ 0., 0., 0., ..., 0., 0., 0.])"
- a = array(range(5), long)
+ a = array(range(5), int)
if a.dtype.itemsize == int_size:
assert repr(a) == "array([0, 1, 2, 3, 4])"
else:
@@ -142,3 +142,39 @@
assert str(b) == "[7 8 9]"
b = a[2:1, ]
assert str(b) == "[]"
+
+ def test_equal(self):
+ from _numpypy import array
+ from numpypy import array_equal
+
+ a = [1, 2, 3]
+ b = [1, 2, 3]
+
+ assert array_equal(a, b)
+ assert array_equal(a, array(b))
+ assert array_equal(array(a), b)
+ assert array_equal(array(a), array(b))
+
+ def test_not_equal(self):
+ from _numpypy import array
+ from numpypy import array_equal
+
+ a = [1, 2, 3]
+ b = [1, 2, 4]
+
+ assert not array_equal(a, b)
+ assert not array_equal(a, array(b))
+ assert not array_equal(array(a), b)
+ assert not array_equal(array(a), array(b))
+
+ def test_mismatched_shape(self):
+ from _numpypy import array
+ from numpypy import array_equal
+
+ a = [1, 2, 3]
+ b = [[1, 2, 3], [1, 2, 3]]
+
+ assert not array_equal(a, b)
+ assert not array_equal(a, array(b))
+ assert not array_equal(array(a), b)
+ assert not array_equal(array(a), array(b))
diff --git a/pypy/module/test_lib_pypy/test_binascii.py b/pypy/module/test_lib_pypy/test_binascii.py
deleted file mode 100644
--- a/pypy/module/test_lib_pypy/test_binascii.py
+++ /dev/null
@@ -1,8 +0,0 @@
-
-""" Some more binascii.py tests
-"""
-
-class AppTestBinAscii:
- def test_incorrect_padding(self):
- import binascii
- raises(binascii.Error, "'x'.decode('base64')")
diff --git a/pypy/module/zipimport/test/test_undocumented.py b/pypy/module/zipimport/test/test_undocumented.py
--- a/pypy/module/zipimport/test/test_undocumented.py
+++ b/pypy/module/zipimport/test/test_undocumented.py
@@ -19,7 +19,7 @@
class AppTestZipImport:
def setup_class(cls):
- space = gettestobjspace(usemodules=['zipimport', 'rctime'])
+ space = gettestobjspace(usemodules=['zipimport', 'rctime', 'struct'])
cls.space = space
cls.w_created_paths = space.wrap(created_paths)
diff --git a/pypy/module/zipimport/test/test_zipimport.py b/pypy/module/zipimport/test/test_zipimport.py
--- a/pypy/module/zipimport/test/test_zipimport.py
+++ b/pypy/module/zipimport/test/test_zipimport.py
@@ -47,9 +47,9 @@
""").compile()
if cls.compression == ZIP_DEFLATED:
- space = gettestobjspace(usemodules=['zipimport', 'zlib', 'rctime'])
+ space = gettestobjspace(usemodules=['zipimport', 'zlib', 'rctime', 'struct'])
else:
- space = gettestobjspace(usemodules=['zipimport', 'rctime'])
+ space = gettestobjspace(usemodules=['zipimport', 'rctime', 'struct'])
cls.space = space
tmpdir = udir.ensure('zipimport_%s' % cls.__name__, dir=1)
diff --git a/pypy/objspace/fake/objspace.py b/pypy/objspace/fake/objspace.py
--- a/pypy/objspace/fake/objspace.py
+++ b/pypy/objspace/fake/objspace.py
@@ -207,6 +207,11 @@
is_arguments(args)
return w_some_obj()
+ def get_and_call_function(space, w_descr, w_obj, *args_w):
+ args = argument.Arguments(space, list(args_w))
+ w_impl = space.get(w_descr, w_obj)
+ return space.call_args(w_impl, args)
+
def gettypefor(self, cls):
return self.gettypeobject(cls.typedef)
diff --git a/pypy/objspace/flow/model.py b/pypy/objspace/flow/model.py
--- a/pypy/objspace/flow/model.py
+++ b/pypy/objspace/flow/model.py
@@ -7,8 +7,7 @@
from pypy.tool.uid import uid, Hashable
from pypy.tool.descriptor import roproperty
from pypy.tool.sourcetools import PY_IDENTIFIER, nice_repr_for_func
-from pypy.tool.identity_dict import identity_dict
-from pypy.rlib.rarithmetic import is_valid_int
+from pypy.rlib.rarithmetic import is_valid_int, r_longlong, r_ulonglong, r_uint
"""
@@ -546,6 +545,8 @@
for n in cases[:len(cases)-has_default]:
if is_valid_int(n):
continue
+ if isinstance(n, (r_longlong, r_ulonglong, r_uint)):
+ continue
if isinstance(n, (str, unicode)) and len(n) == 1:
continue
assert n != 'default', (
diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -9,6 +9,7 @@
from pypy.rlib.rfloat import (
formatd, DTSF_STR_PRECISION, isinf, isnan, copysign)
from pypy.rlib import jit
+from pypy.rlib.rarithmetic import intmask
import math
@@ -173,7 +174,7 @@
def hash__Complex(space, w_value):
hashreal = _hash_float(space, w_value.realval)
hashimg = _hash_float(space, w_value.imagval)
- combined = hashreal + 1000003 * hashimg
+ combined = intmask(hashreal + 1000003 * hashimg)
return space.newint(combined)
def add__Complex_Complex(space, w_complex1, w_complex2):
diff --git a/pypy/objspace/std/iterobject.py b/pypy/objspace/std/iterobject.py
--- a/pypy/objspace/std/iterobject.py
+++ b/pypy/objspace/std/iterobject.py
@@ -22,7 +22,7 @@
index = self.index
w_length = space.len(self.w_seq)
w_len = space.sub(w_length, space.wrap(index))
- if space.is_true(space.lt(w_len,space.wrap(0))):
+ if space.is_true(space.lt(w_len, space.wrap(0))):
w_len = space.wrap(0)
return w_len
@@ -30,21 +30,21 @@
"""Sequence iterator implementation for general sequences."""
class W_FastListIterObject(W_AbstractSeqIterObject):
- """Sequence iterator specialized for lists, accessing
- directly their RPython-level list of wrapped objects.
+ """Sequence iterator specialized for lists, accessing directly their
+ RPython-level list of wrapped objects.
"""
class W_FastTupleIterObject(W_AbstractSeqIterObject):
- """Sequence iterator specialized for tuples, accessing
- directly their RPython-level list of wrapped objects.
- """
- def __init__(w_self, w_seq, wrappeditems):
+ """Sequence iterator specialized for tuples, accessing directly
+ their RPython-level list of wrapped objects.
+ """
+ def __init__(w_self, w_seq, wrappeditems):
W_AbstractSeqIterObject.__init__(w_self, w_seq)
w_self.tupleitems = wrappeditems
class W_ReverseSeqIterObject(W_Object):
from pypy.objspace.std.itertype import reverse_iter_typedef as typedef
-
+
def __init__(w_self, space, w_seq, index=-1):
w_self.w_seq = w_seq
w_self.w_len = space.len(w_seq)
@@ -61,15 +61,15 @@
def next__SeqIter(space, w_seqiter):
if w_seqiter.w_seq is None:
- raise OperationError(space.w_StopIteration, space.w_None)
+ raise OperationError(space.w_StopIteration, space.w_None)
try:
w_item = space.getitem(w_seqiter.w_seq, space.wrap(w_seqiter.index))
except OperationError, e:
w_seqiter.w_seq = None
if not e.match(space, space.w_IndexError):
raise
- raise OperationError(space.w_StopIteration, space.w_None)
- w_seqiter.index += 1
+ raise OperationError(space.w_StopIteration, space.w_None)
+ w_seqiter.index += 1
return w_item
# XXX __length_hint__()
@@ -89,7 +89,7 @@
except IndexError:
w_seqiter.tupleitems = None
w_seqiter.w_seq = None
- raise OperationError(space.w_StopIteration, space.w_None)
+ raise OperationError(space.w_StopIteration, space.w_None)
w_seqiter.index = index + 1
return w_item
@@ -112,7 +112,7 @@
w_item = w_seq.getitem(index)
except IndexError:
w_seqiter.w_seq = None
- raise OperationError(space.w_StopIteration, space.w_None)
+ raise OperationError(space.w_StopIteration, space.w_None)
w_seqiter.index = index + 1
return w_item
@@ -126,15 +126,15 @@
def next__ReverseSeqIter(space, w_seqiter):
if w_seqiter.w_seq is None or w_seqiter.index < 0:
- raise OperationError(space.w_StopIteration, space.w_None)
+ raise OperationError(space.w_StopIteration, space.w_None)
try:
w_item = space.getitem(w_seqiter.w_seq, space.wrap(w_seqiter.index))
- w_seqiter.index -= 1
+ w_seqiter.index -= 1
except OperationError, e:
w_seqiter.w_seq = None
if not e.match(space, space.w_IndexError):
raise
- raise OperationError(space.w_StopIteration, space.w_None)
+ raise OperationError(space.w_StopIteration, space.w_None)
return w_item
# XXX __length_hint__()
diff --git a/pypy/objspace/std/ropeobject.py b/pypy/objspace/std/ropeobject.py
--- a/pypy/objspace/std/ropeobject.py
+++ b/pypy/objspace/std/ropeobject.py
@@ -41,11 +41,6 @@
return w_self
return W_RopeObject(w_self._node)
- def unicode_w(w_self, space):
- # XXX should this use the default encoding?
- from pypy.objspace.std.unicodetype import plain_str2unicode
- return plain_str2unicode(space, w_self._node.flatten_string())
-
W_RopeObject.EMPTY = W_RopeObject(rope.LiteralStringNode.EMPTY)
W_RopeObject.PREBUILT = [W_RopeObject(rope.LiteralStringNode.PREBUILT[i])
for i in range(256)]
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -37,6 +37,20 @@
return None
return space.wrap(compute_unique_id(space.str_w(self)))
+ def unicode_w(w_self, space):
+ # Use the default encoding.
+ from pypy.objspace.std.unicodetype import unicode_from_string, \
+ decode_object
+ w_defaultencoding = space.call_function(space.sys.get(
+ 'getdefaultencoding'))
+ from pypy.objspace.std.unicodetype import _get_encoding_and_errors, \
+ unicode_from_string, decode_object
+ encoding, errors = _get_encoding_and_errors(space, w_defaultencoding,
+ space.w_None)
+ if encoding is None and errors is None:
+ return space.unicode_w(unicode_from_string(space, w_self))
+ return space.unicode_w(decode_object(space, w_self, encoding, errors))
+
class W_StringObject(W_AbstractStringObject):
from pypy.objspace.std.stringtype import str_typedef as typedef
@@ -55,20 +69,6 @@
def str_w(w_self, space):
return w_self._value
- def unicode_w(w_self, space):
- # Use the default encoding.
- from pypy.objspace.std.unicodetype import unicode_from_string, \
- decode_object
- w_defaultencoding = space.call_function(space.sys.get(
- 'getdefaultencoding'))
- from pypy.objspace.std.unicodetype import _get_encoding_and_errors, \
- unicode_from_string, decode_object
- encoding, errors = _get_encoding_and_errors(space, w_defaultencoding,
- space.w_None)
- if encoding is None and errors is None:
- return space.unicode_w(unicode_from_string(space, w_self))
- return space.unicode_w(decode_object(space, w_self, encoding, errors))
-
registerimplementation(W_StringObject)
W_StringObject.EMPTY = W_StringObject('')
diff --git a/pypy/objspace/std/test/test_obj.py b/pypy/objspace/std/test/test_obj.py
--- a/pypy/objspace/std/test/test_obj.py
+++ b/pypy/objspace/std/test/test_obj.py
@@ -265,4 +265,7 @@
space = objspace.StdObjSpace()
w_a = space.wrap("a")
space.type = None
+ # if it crashes, it means that space._type_isinstance didn't go through
+ # the fast path, and tries to call type() (which is set to None just
+ # above)
space.isinstance_w(w_a, space.w_str) # does not crash
diff --git a/pypy/rlib/bitmanipulation.py b/pypy/rlib/bitmanipulation.py
--- a/pypy/rlib/bitmanipulation.py
+++ b/pypy/rlib/bitmanipulation.py
@@ -1,5 +1,6 @@
from pypy.rlib import unroll
+
class BitSplitter(dict):
def __getitem__(self, lengths):
if isinstance(lengths, int):
diff --git a/pypy/rlib/clibffi.py b/pypy/rlib/clibffi.py
--- a/pypy/rlib/clibffi.py
+++ b/pypy/rlib/clibffi.py
@@ -233,6 +233,7 @@
(rffi.LONGLONG, _signed_type_for(rffi.LONGLONG)),
(lltype.UniChar, _unsigned_type_for(lltype.UniChar)),
(lltype.Bool, _unsigned_type_for(lltype.Bool)),
+ (lltype.Char, _signed_type_for(lltype.Char)),
]
__float_type_map = [
diff --git a/pypy/rlib/libffi.py b/pypy/rlib/libffi.py
--- a/pypy/rlib/libffi.py
+++ b/pypy/rlib/libffi.py
@@ -429,6 +429,11 @@
return rffi.cast(rffi.CArrayPtr(TYPE), addr)[0]
assert False
+def array_getitem_T(TYPE, width, addr, index, offset):
+ addr = rffi.ptradd(addr, index * width)
+ addr = rffi.ptradd(addr, offset)
+ return rffi.cast(rffi.CArrayPtr(TYPE), addr)[0]
+
@specialize.call_location()
@jit.oopspec("libffi_array_setitem(ffitype, width, addr, index, offset, value)")
def array_setitem(ffitype, width, addr, index, offset, value):
@@ -439,3 +444,8 @@
rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] = value
return
assert False
+
+def array_setitem_T(TYPE, width, addr, index, offset, value):
+ addr = rffi.ptradd(addr, index * width)
+ addr = rffi.ptradd(addr, offset)
+ rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] = value
diff --git a/pypy/rlib/longlong2float.py b/pypy/rlib/longlong2float.py
--- a/pypy/rlib/longlong2float.py
+++ b/pypy/rlib/longlong2float.py
@@ -5,7 +5,12 @@
long long to a float and back to a long long. There are corner cases
in which it does not work.
"""
+
+from pypy.annotation import model as annmodel
+from pypy.rlib.rarithmetic import r_int64
from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rpython.extregistry import ExtRegistryEntry
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
# -------- implement longlong2float and float2longlong --------
@@ -16,38 +21,33 @@
# these definitions are used only in tests, when not translated
def longlong2float_emulator(llval):
- d_array = lltype.malloc(DOUBLE_ARRAY_PTR.TO, 1, flavor='raw')
- ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array)
- ll_array[0] = llval
- floatval = d_array[0]
- lltype.free(d_array, flavor='raw')
- return floatval
+ with lltype.scoped_alloc(DOUBLE_ARRAY_PTR.TO, 1) as d_array:
+ ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array)
+ ll_array[0] = llval
+ floatval = d_array[0]
+ return floatval
-def float2longlong_emulator(floatval):
- d_array = lltype.malloc(DOUBLE_ARRAY_PTR.TO, 1, flavor='raw')
- ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array)
- d_array[0] = floatval
- llval = ll_array[0]
- lltype.free(d_array, flavor='raw')
- return llval
+def float2longlong(floatval):
+ with lltype.scoped_alloc(DOUBLE_ARRAY_PTR.TO, 1) as d_array:
+ ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array)
+ d_array[0] = floatval
+ llval = ll_array[0]
+ return llval
def uint2singlefloat_emulator(ival):
- f_array = lltype.malloc(FLOAT_ARRAY_PTR.TO, 1, flavor='raw')
- i_array = rffi.cast(UINT_ARRAY_PTR, f_array)
- i_array[0] = ival
- singlefloatval = f_array[0]
- lltype.free(f_array, flavor='raw')
- return singlefloatval
+ with lltype.scoped_alloc(FLOAT_ARRAY_PTR.TO, 1) as f_array:
+ i_array = rffi.cast(UINT_ARRAY_PTR, f_array)
+ i_array[0] = ival
+ singlefloatval = f_array[0]
+ return singlefloatval
def singlefloat2uint_emulator(singlefloatval):
- f_array = lltype.malloc(FLOAT_ARRAY_PTR.TO, 1, flavor='raw')
- i_array = rffi.cast(UINT_ARRAY_PTR, f_array)
- f_array[0] = singlefloatval
- ival = i_array[0]
- lltype.free(f_array, flavor='raw')
- return ival
+ with lltype.scoped_alloc(FLOAT_ARRAY_PTR.TO, 1) as f_array:
+ i_array = rffi.cast(UINT_ARRAY_PTR, f_array)
+ f_array[0] = singlefloatval
+ ival = i_array[0]
+ return ival
-from pypy.translator.tool.cbuild import ExternalCompilationInfo
eci = ExternalCompilationInfo(includes=['string.h', 'assert.h'],
post_include_bits=["""
static double pypy__longlong2float(long long x) {
@@ -56,12 +56,6 @@
memcpy(&dd, &x, 8);
return dd;
}
-static long long pypy__float2longlong(double x) {
- long long ll;
- assert(sizeof(double) == 8 && sizeof(long long) == 8);
- memcpy(&ll, &x, 8);
- return ll;
-}
static float pypy__uint2singlefloat(unsigned int x) {
float ff;
assert(sizeof(float) == 4 && sizeof(unsigned int) == 4);
@@ -82,12 +76,6 @@
_nowrapper=True, elidable_function=True, sandboxsafe=True,
oo_primitive="pypy__longlong2float")
-float2longlong = rffi.llexternal(
- "pypy__float2longlong", [rffi.DOUBLE], rffi.LONGLONG,
- _callable=float2longlong_emulator, compilation_info=eci,
- _nowrapper=True, elidable_function=True, sandboxsafe=True,
- oo_primitive="pypy__float2longlong")
-
uint2singlefloat = rffi.llexternal(
"pypy__uint2singlefloat", [rffi.UINT], rffi.FLOAT,
_callable=uint2singlefloat_emulator, compilation_info=eci,
@@ -99,3 +87,15 @@
_callable=singlefloat2uint_emulator, compilation_info=eci,
_nowrapper=True, elidable_function=True, sandboxsafe=True,
oo_primitive="pypy__singlefloat2uint")
+
+
+class Float2LongLongEntry(ExtRegistryEntry):
+ _about_ = float2longlong
+
+ def compute_result_annotation(self, s_float):
+ assert annmodel.SomeFloat().contains(s_float)
+ return annmodel.SomeInteger(knowntype=r_int64)
+
+ def specialize_call(self, hop):
+ [v_float] = hop.inputargs(lltype.Float)
+ return hop.genop("convert_float_bytes_to_longlong", [v_float], resulttype=hop.r_result)
diff --git a/pypy/rlib/rarithmetic.py b/pypy/rlib/rarithmetic.py
--- a/pypy/rlib/rarithmetic.py
+++ b/pypy/rlib/rarithmetic.py
@@ -569,3 +569,37 @@
if not objectmodel.we_are_translated():
assert n <= p
return llop.int_between(lltype.Bool, n, m, p)
+
+ at objectmodel.specialize.ll()
+def byteswap(arg):
+ """ Convert little->big endian and the opposite
+ """
+ from pypy.rpython.lltypesystem import lltype, rffi
+
+ T = lltype.typeOf(arg)
+ # XXX we cannot do arithmetics on small ints
+ if isinstance(arg, base_int):
+ arg = widen(arg)
+ if rffi.sizeof(T) == 1:
+ res = arg
+ elif rffi.sizeof(T) == 2:
+ a, b = arg & 0xFF, arg & 0xFF00
+ res = (a << 8) | (b >> 8)
+ elif rffi.sizeof(T) == 4:
+ FF = r_uint(0xFF)
+ arg = r_uint(arg)
+ a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16),
+ arg & (FF << 24))
+ res = (a << 24) | (b << 8) | (c >> 8) | (d >> 24)
+ elif rffi.sizeof(T) == 8:
+ FF = r_ulonglong(0xFF)
+ arg = r_ulonglong(arg)
+ a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16),
+ arg & (FF << 24))
+ e, f, g, h = (arg & (FF << 32), arg & (FF << 40), arg & (FF << 48),
+ arg & (FF << 56))
+ res = ((a << 56) | (b << 40) | (c << 24) | (d << 8) | (e >> 8) |
+ (f >> 24) | (g >> 40) | (h >> 56))
+ else:
+ assert False # unreachable code
+ return rffi.cast(T, res)
diff --git a/pypy/rlib/rfloat.py b/pypy/rlib/rfloat.py
--- a/pypy/rlib/rfloat.py
+++ b/pypy/rlib/rfloat.py
@@ -1,11 +1,13 @@
"""Float constants"""
import math
+
+from pypy.annotation.model import SomeString
+from pypy.rlib import objectmodel
+from pypy.rpython.extfunc import register_external
from pypy.rpython.tool import rffi_platform
from pypy.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.rlib import objectmodel
-from pypy.rpython.extfunc import register_external
-from pypy.annotation.model import SomeString
+
USE_SHORT_FLOAT_REPR = True # XXX make it a translation option?
@@ -74,7 +76,7 @@
while i < len(s) and s[i] in '0123456789':
after_point += s[i]
i += 1
-
+
if i == len(s):
return sign, before_point, after_point, exponent
@@ -91,7 +93,7 @@
if i == len(s):
raise ValueError
-
+
while i < len(s) and s[i] in '0123456789':
exponent += s[i]
i += 1
diff --git a/pypy/rlib/rmmap.py b/pypy/rlib/rmmap.py
--- a/pypy/rlib/rmmap.py
+++ b/pypy/rlib/rmmap.py
@@ -228,6 +228,7 @@
# XXX should be propagate the real type, allowing
# for 2*sys.maxint?
high = high_ref[0]
+ high = rffi.cast(lltype.Signed, high)
# low might just happen to have the value INVALID_FILE_SIZE
# so we need to check the last error also
INVALID_FILE_SIZE = -1
@@ -550,7 +551,7 @@
FILE_BEGIN = 0
high_ref = lltype.malloc(PLONG.TO, 1, flavor='raw')
try:
- high_ref[0] = newsize_high
+ high_ref[0] = rffi.cast(LONG, newsize_high)
SetFilePointer(self.file_handle, newsize_low, high_ref,
FILE_BEGIN)
finally:
@@ -712,7 +713,9 @@
free = c_munmap_safe
elif _MS_WINDOWS:
- def mmap(fileno, length, tagname="", access=_ACCESS_DEFAULT, offset=0):
+ def mmap(fileno, length, flags=0, tagname="", access=_ACCESS_DEFAULT, offset=0):
+ # XXX flags is or-ed into access by now.
+
# check size boundaries
_check_map_size(length)
map_size = length
@@ -794,6 +797,7 @@
offset_hi = 0
offset_lo = offset
+ flProtect |= flags
m.map_handle = CreateFileMapping(m.file_handle, NULL, flProtect,
size_hi, size_lo, m.tagname)
@@ -811,6 +815,11 @@
m.map_handle = INVALID_HANDLE
raise winerror
+ class Hint:
+ pos = -0x4fff0000 # for reproducible results
+ hint = Hint()
+ # XXX this has no effect on windows
+
def alloc(map_size):
"""Allocate memory. This is intended to be used by the JIT,
so the memory has the executable bit set.
diff --git a/pypy/rlib/rstruct/nativefmttable.py b/pypy/rlib/rstruct/nativefmttable.py
--- a/pypy/rlib/rstruct/nativefmttable.py
+++ b/pypy/rlib/rstruct/nativefmttable.py
@@ -3,14 +3,17 @@
The table 'native_fmttable' is also used by pypy.module.array.interp_array.
"""
import struct
-from pypy.rlib import jit
+
+from pypy.rlib import jit, longlong2float
+from pypy.rlib.objectmodel import specialize
+from pypy.rlib.rarithmetic import r_singlefloat, widen
from pypy.rlib.rstruct import standardfmttable as std
from pypy.rlib.rstruct.error import StructError
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.rpython.lltypesystem import lltype, rffi
from pypy.rpython.tool import rffi_platform
-from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.rlib.rarithmetic import r_singlefloat
from pypy.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.rlib.objectmodel import specialize
+
native_is_bigendian = struct.pack("=i", 1) == struct.pack(">i", 1)
@@ -23,18 +26,24 @@
# ____________________________________________________________
+
double_buf = lltype.malloc(rffi.DOUBLEP.TO, 1, flavor='raw', immortal=True)
float_buf = lltype.malloc(rffi.FLOATP.TO, 1, flavor='raw', immortal=True)
- at jit.dont_look_inside
-def double_to_ccharp(doubleval):
- double_buf[0] = doubleval
- return rffi.cast(rffi.CCHARP, double_buf)
+range_8_unroll = unrolling_iterable(list(reversed(range(8))))
+range_4_unroll = unrolling_iterable(list(reversed(range(4))))
def pack_double(fmtiter):
doubleval = fmtiter.accept_float_arg()
- p = double_to_ccharp(doubleval)
- fmtiter.result.append_charpsize(p, rffi.sizeof(rffi.DOUBLE))
+ value = longlong2float.float2longlong(doubleval)
+ if fmtiter.bigendian:
+ for i in range_8_unroll:
+ x = (value >> (8*i)) & 0xff
+ fmtiter.result.append(chr(x))
+ else:
+ for i in range_8_unroll:
+ fmtiter.result.append(chr(value & 0xff))
+ value >>= 8
@specialize.argtype(0)
def unpack_double(fmtiter):
@@ -45,16 +54,19 @@
doubleval = double_buf[0]
fmtiter.appendobj(doubleval)
- at jit.dont_look_inside
-def float_to_ccharp(floatval):
- float_buf[0] = floatval
- return rffi.cast(rffi.CCHARP, float_buf)
-
def pack_float(fmtiter):
doubleval = fmtiter.accept_float_arg()
floatval = r_singlefloat(doubleval)
- p = float_to_ccharp(floatval)
- fmtiter.result.append_charpsize(p, rffi.sizeof(rffi.FLOAT))
+ value = longlong2float.singlefloat2uint(floatval)
+ value = widen(value)
+ if fmtiter.bigendian:
+ for i in range_4_unroll:
+ x = (value >> (8*i)) & 0xff
+ fmtiter.result.append(chr(x))
+ else:
+ for i in range_4_unroll:
+ fmtiter.result.append(chr(value & 0xff))
+ value >>= 8
@specialize.argtype(0)
def unpack_float(fmtiter):
diff --git a/pypy/rlib/rstruct/runpack.py b/pypy/rlib/rstruct/runpack.py
--- a/pypy/rlib/rstruct/runpack.py
+++ b/pypy/rlib/rstruct/runpack.py
@@ -4,11 +4,10 @@
"""
import py
-from struct import pack, unpack
+from struct import unpack
from pypy.rlib.rstruct.formatiterator import FormatIterator
from pypy.rlib.rstruct.error import StructError
from pypy.rlib.rstruct.nativefmttable import native_is_bigendian
-from pypy.rpython.extregistry import ExtRegistryEntry
class MasterReader(object):
def __init__(self, s):
diff --git a/pypy/rlib/rstruct/standardfmttable.py b/pypy/rlib/rstruct/standardfmttable.py
--- a/pypy/rlib/rstruct/standardfmttable.py
+++ b/pypy/rlib/rstruct/standardfmttable.py
@@ -6,11 +6,12 @@
# values when packing.
import struct
+
+from pypy.rlib.objectmodel import specialize
+from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
+from pypy.rlib.rstruct import ieee
from pypy.rlib.rstruct.error import StructError, StructOverflowError
-from pypy.rlib.rstruct import ieee
from pypy.rlib.unroll import unrolling_iterable
-from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
-from pypy.rlib.objectmodel import specialize
# In the CPython struct module, pack() unconsistently accepts inputs
# that are out-of-range or floats instead of ints. Should we emulate
diff --git a/pypy/rlib/rzipfile.py b/pypy/rlib/rzipfile.py
--- a/pypy/rlib/rzipfile.py
+++ b/pypy/rlib/rzipfile.py
@@ -12,8 +12,7 @@
rzlib = None
# XXX hack to get crc32 to work
-from pypy.tool.lib_pypy import import_from_lib_pypy
-crc_32_tab = import_from_lib_pypy('binascii').crc_32_tab
+from pypy.module.binascii.interp_crc32 import crc_32_tab
rcrc_32_tab = [r_uint(i) for i in crc_32_tab]
diff --git a/pypy/rlib/test/test_rarithmetic.py b/pypy/rlib/test/test_rarithmetic.py
--- a/pypy/rlib/test/test_rarithmetic.py
+++ b/pypy/rlib/test/test_rarithmetic.py
@@ -383,3 +383,9 @@
assert not int_between(1, 2, 2)
assert not int_between(1, 1, 1)
+def test_byteswap():
+ from pypy.rpython.lltypesystem import rffi
+
+ assert byteswap(rffi.cast(rffi.USHORT, 0x0102)) == 0x0201
+ assert byteswap(rffi.cast(rffi.INT, 0x01020304)) == 0x04030201
+ assert byteswap(rffi.cast(rffi.ULONGLONG, 0x0102030405060708L)) == 0x0807060504030201L
diff --git a/pypy/rpython/llinterp.py b/pypy/rpython/llinterp.py
--- a/pypy/rpython/llinterp.py
+++ b/pypy/rpython/llinterp.py
@@ -770,6 +770,10 @@
checkadr(adr)
return llmemory.cast_adr_to_int(adr, mode)
+ def op_convert_float_bytes_to_longlong(self, f):
+ from pypy.rlib import longlong2float
+ return longlong2float.float2longlong(f)
+
def op_weakref_create(self, v_obj):
def objgetter(): # special support for gcwrapper.py
return self.getval(v_obj)
diff --git a/pypy/rpython/lltypesystem/lloperation.py b/pypy/rpython/lltypesystem/lloperation.py
--- a/pypy/rpython/lltypesystem/lloperation.py
+++ b/pypy/rpython/lltypesystem/lloperation.py
@@ -349,6 +349,7 @@
'cast_float_to_ulonglong':LLOp(canfold=True),
'truncate_longlong_to_int':LLOp(canfold=True),
'force_cast': LLOp(sideeffects=False), # only for rffi.cast()
+ 'convert_float_bytes_to_longlong': LLOp(canfold=True),
# __________ pointer operations __________
diff --git a/pypy/rpython/module/ll_time.py b/pypy/rpython/module/ll_time.py
--- a/pypy/rpython/module/ll_time.py
+++ b/pypy/rpython/module/ll_time.py
@@ -9,7 +9,7 @@
from pypy.rpython.lltypesystem import lltype
from pypy.rpython.extfunc import BaseLazyRegistering, registering, extdef
from pypy.rlib import rposix
-from pypy.rlib.rarithmetic import intmask
+from pypy.rlib.rarithmetic import intmask, maxint32
from pypy.translator.tool.cbuild import ExternalCompilationInfo
if sys.platform == 'win32':
@@ -177,7 +177,7 @@
@registering(time.sleep)
def register_time_sleep(self):
if sys.platform == 'win32':
- MAX = sys.maxint
+ MAX = maxint32
Sleep = self.llexternal('Sleep', [rffi.ULONG], lltype.Void)
def time_sleep_llimpl(secs):
millisecs = secs * 1000.0
diff --git a/pypy/rpython/ootypesystem/ootype.py b/pypy/rpython/ootypesystem/ootype.py
--- a/pypy/rpython/ootypesystem/ootype.py
+++ b/pypy/rpython/ootypesystem/ootype.py
@@ -1295,6 +1295,8 @@
for meth in self.overloadings:
ARGS = meth._TYPE.ARGS
if ARGS in signatures:
+ # XXX Conflict on 'Signed' vs 'SignedLongLong' on win64.
+ # XXX note that this partially works if this error is ignored.
raise TypeError, 'Bad overloading'
signatures.add(ARGS)
diff --git a/pypy/tool/test/test_lib_pypy.py b/pypy/tool/test/test_lib_pypy.py
--- a/pypy/tool/test/test_lib_pypy.py
+++ b/pypy/tool/test/test_lib_pypy.py
@@ -11,7 +11,7 @@
assert lib_pypy.LIB_PYTHON_MODIFIED.check(dir=1)
def test_import_from_lib_pypy():
- binascii = lib_pypy.import_from_lib_pypy('binascii')
- assert type(binascii) is type(lib_pypy)
- assert binascii.__name__ == 'lib_pypy.binascii'
- assert hasattr(binascii, 'crc_32_tab')
+ _functools = lib_pypy.import_from_lib_pypy('_functools')
+ assert type(_functools) is type(lib_pypy)
+ assert _functools.__name__ == 'lib_pypy._functools'
+ assert hasattr(_functools, 'partial')
diff --git a/pypy/translator/c/gcc/trackgcroot.py b/pypy/translator/c/gcc/trackgcroot.py
--- a/pypy/translator/c/gcc/trackgcroot.py
+++ b/pypy/translator/c/gcc/trackgcroot.py
@@ -484,7 +484,7 @@
'shl', 'shr', 'sal', 'sar', 'rol', 'ror', 'mul', 'imul', 'div', 'idiv',
'bswap', 'bt', 'rdtsc',
'punpck', 'pshufd', 'pcmp', 'pand', 'psllw', 'pslld', 'psllq',
- 'paddq', 'pinsr', 'pmul', 'psrl',
+ 'paddq', 'pinsr', 'pmul', 'psrl', 'vmul',
# sign-extending moves should not produce GC pointers
'cbtw', 'cwtl', 'cwtd', 'cltd', 'cltq', 'cqto',
# zero-extending moves should not produce GC pointers
diff --git a/pypy/translator/c/src/float.h b/pypy/translator/c/src/float.h
--- a/pypy/translator/c/src/float.h
+++ b/pypy/translator/c/src/float.h
@@ -27,7 +27,7 @@
#define OP_FLOAT_SUB(x,y,r) r = x - y
#define OP_FLOAT_MUL(x,y,r) r = x * y
#define OP_FLOAT_TRUEDIV(x,y,r) r = x / y
-#define OP_FLOAT_POW(x,y,r) r = pow(x, y)
+#define OP_FLOAT_POW(x,y,r) r = pow(x, y)
/*** conversions ***/
@@ -42,5 +42,6 @@
#ifdef HAVE_LONG_LONG
#define OP_CAST_FLOAT_TO_LONGLONG(x,r) r = (long long)(x)
#define OP_CAST_FLOAT_TO_ULONGLONG(x,r) r = (unsigned long long)(x)
+#define OP_CONVERT_FLOAT_BYTES_TO_LONGLONG(x,r) memcpy(&r, &x, sizeof(double))
#endif
diff --git a/pypy/translator/cli/sdk.py b/pypy/translator/cli/sdk.py
--- a/pypy/translator/cli/sdk.py
+++ b/pypy/translator/cli/sdk.py
@@ -103,6 +103,11 @@
mono_bin = find_mono_on_windows()
if mono_bin is not None:
SDK.ILASM = os.path.join(mono_bin, 'ilasm2.bat')
+ # XXX the failing tests are boring, and the SDK is usually installed
+ # on windows. I do not care right now, because the Linux buildbots
+ # don't test this at all...
+ if platform.architecture()[0] == '64bit':
+ py.test.skip('mono on 64bit is not well enough supported')
else:
SDK = MonoSDK
return SDK
diff --git a/pypy/translator/driver.py b/pypy/translator/driver.py
--- a/pypy/translator/driver.py
+++ b/pypy/translator/driver.py
@@ -585,22 +585,6 @@
#
task_compile_c = taskdef(task_compile_c, ['source_c'], "Compiling c source")
- def backend_run(self, backend):
- c_entryp = self.c_entryp
- standalone = self.standalone
- if standalone:
- os.system(c_entryp)
- else:
- runner = self.extra.get('run', lambda f: f())
- runner(c_entryp)
-
- def task_run_c(self):
- self.backend_run('c')
- #
- task_run_c = taskdef(task_run_c, ['compile_c'],
- "Running compiled c source",
- idemp=True)
-
def task_llinterpret_lltype(self):
from pypy.rpython.llinterp import LLInterpreter
py.log.setconsumer("llinterp operation", None)
@@ -710,11 +694,6 @@
shutil.copy(main_exe, '.')
self.log.info("Copied to %s" % os.path.join(os.getcwd(), dllname))
- def task_run_cli(self):
- pass
- task_run_cli = taskdef(task_run_cli, ['compile_cli'],
- 'XXX')
-
def task_source_jvm(self):
from pypy.translator.jvm.genjvm import GenJvm
from pypy.translator.jvm.node import EntryPoint
diff --git a/pypy/translator/goal/translate.py b/pypy/translator/goal/translate.py
--- a/pypy/translator/goal/translate.py
+++ b/pypy/translator/goal/translate.py
@@ -31,7 +31,6 @@
("backendopt", "do backend optimizations", "--backendopt", ""),
("source", "create source", "-s --source", ""),
("compile", "compile", "-c --compile", " (default goal)"),
- ("run", "run the resulting binary", "--run", ""),
("llinterpret", "interpret the rtyped flow graphs", "--llinterpret", ""),
]
def goal_options():
@@ -78,7 +77,7 @@
defaultfactory=list),
# xxx default goals ['annotate', 'rtype', 'backendopt', 'source', 'compile']
ArbitraryOption("skipped_goals", "XXX",
- defaultfactory=lambda: ['run']),
+ defaultfactory=list),
OptionDescription("goal_options",
"Goals that should be reached during translation",
goal_options()),
diff --git a/pypy/translator/jvm/opcodes.py b/pypy/translator/jvm/opcodes.py
--- a/pypy/translator/jvm/opcodes.py
+++ b/pypy/translator/jvm/opcodes.py
@@ -241,4 +241,6 @@
'cast_ulonglong_to_float': jvm.PYPYULONGTODOUBLE,
'cast_primitive': [PushAllArgs, CastPrimitive, StoreResult],
'force_cast': [PushAllArgs, CastPrimitive, StoreResult],
+
+ 'convert_float_bytes_to_longlong': jvm.PYPYDOUBLEBYTESTOLONG,
})
diff --git a/pypy/translator/jvm/typesystem.py b/pypy/translator/jvm/typesystem.py
--- a/pypy/translator/jvm/typesystem.py
+++ b/pypy/translator/jvm/typesystem.py
@@ -941,6 +941,7 @@
PYPYDOUBLETOULONG = Method.s(jPyPy, 'double_to_ulong', (jDouble,), jLong)
PYPYULONGTODOUBLE = Method.s(jPyPy, 'ulong_to_double', (jLong,), jDouble)
PYPYLONGBITWISENEGATE = Method.v(jPyPy, 'long_bitwise_negate', (jLong,), jLong)
+PYPYDOUBLEBYTESTOLONG = Method.v(jPyPy, 'pypy__float2longlong', (jDouble,), jLong)
PYPYSTRTOINT = Method.v(jPyPy, 'str_to_int', (jString,), jInt)
PYPYSTRTOUINT = Method.v(jPyPy, 'str_to_uint', (jString,), jInt)
PYPYSTRTOLONG = Method.v(jPyPy, 'str_to_long', (jString,), jLong)
diff --git a/pypy/translator/test/test_driver.py b/pypy/translator/test/test_driver.py
--- a/pypy/translator/test/test_driver.py
+++ b/pypy/translator/test/test_driver.py
@@ -6,7 +6,7 @@
def test_ctr():
td = TranslationDriver()
expected = ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source',
- 'compile', 'run', 'pyjitpl']
+ 'compile', 'pyjitpl']
assert set(td.exposed) == set(expected)
assert td.backend_select_goals(['compile_c']) == ['compile_c']
@@ -33,7 +33,6 @@
'rtype_ootype', 'rtype_lltype',
'source_cli', 'source_c',
'compile_cli', 'compile_c',
- 'run_c', 'run_cli',
'compile_jvm', 'source_jvm', 'run_jvm',
'pyjitpl_lltype',
'pyjitpl_ootype']
@@ -50,6 +49,6 @@
'backendopt_lltype']
expected = ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source_c',
- 'compile_c', 'run_c', 'pyjitpl']
+ 'compile_c', 'pyjitpl']
assert set(td.exposed) == set(expected)
diff --git a/pytest.py b/pytest.py
--- a/pytest.py
+++ b/pytest.py
@@ -4,6 +4,20 @@
"""
__all__ = ['main']
+# XXX hack for win64:
+# This patch must stay here until the END OF STAGE 1
+# When all tests work, this branch will be merged
+# and the branch stage 2 is started, where we remove this patch.
+import sys
+if hasattr(sys, "maxsize"):
+ if sys.maxint != sys.maxsize:
+ sys.maxint = sys.maxsize
+ import warnings
+ warnings.warn("""\n
+---> This win64 port is now in stage 1: sys.maxint was modified.
+---> When pypy/__init__.py becomes empty again, we have reached stage 2.
+""")
+
from _pytest.core import main, UsageError, _preloadplugins
from _pytest import core as cmdline
from _pytest import __version__
More information about the pypy-commit
mailing list