[pypy-commit] pypy backend-vector-ops: merge numpy-record-dtypes.
fijal
noreply at buildbot.pypy.org
Mon Feb 20 23:30:22 CET 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: backend-vector-ops
Changeset: r52693:069abc3ddd46
Date: 2012-02-20 13:18 +0100
http://bitbucket.org/pypy/pypy/changeset/069abc3ddd46/
Log: merge numpy-record-dtypes.
diff --git a/ctypes_configure/cbuild.py b/ctypes_configure/cbuild.py
--- a/ctypes_configure/cbuild.py
+++ b/ctypes_configure/cbuild.py
@@ -206,8 +206,9 @@
cfiles += eci.separate_module_files
include_dirs = list(eci.include_dirs)
library_dirs = list(eci.library_dirs)
- if sys.platform == 'darwin': # support Fink & Darwinports
- for s in ('/sw/', '/opt/local/'):
+ if (sys.platform == 'darwin' or # support Fink & Darwinports
+ sys.platform.startswith('freebsd')):
+ for s in ('/sw/', '/opt/local/', '/usr/local/'):
if s + 'include' not in include_dirs and \
os.path.exists(s + 'include'):
include_dirs.append(s + 'include')
@@ -380,9 +381,9 @@
self.link_extra += ['-pthread']
if sys.platform == 'win32':
self.link_extra += ['/DEBUG'] # generate .pdb file
- if sys.platform == 'darwin':
- # support Fink & Darwinports
- for s in ('/sw/', '/opt/local/'):
+ if (sys.platform == 'darwin' or # support Fink & Darwinports
+ sys.platform.startswith('freebsd')):
+ for s in ('/sw/', '/opt/local/', '/usr/local/'):
if s + 'include' not in self.include_dirs and \
os.path.exists(s + 'include'):
self.include_dirs.append(s + 'include')
@@ -395,7 +396,6 @@
self.outputfilename = py.path.local(cfilenames[0]).new(ext=ext)
else:
self.outputfilename = py.path.local(outputfilename)
- self.eci = eci
def build(self, noerr=False):
basename = self.outputfilename.new(ext='')
@@ -436,7 +436,7 @@
old = cfile.dirpath().chdir()
try:
res = compiler.compile([cfile.basename],
- include_dirs=self.eci.include_dirs,
+ include_dirs=self.include_dirs,
extra_preargs=self.compile_extra)
assert len(res) == 1
cobjfile = py.path.local(res[0])
@@ -445,9 +445,9 @@
finally:
old.chdir()
compiler.link_executable(objects, str(self.outputfilename),
- libraries=self.eci.libraries,
+ libraries=self.libraries,
extra_preargs=self.link_extra,
- library_dirs=self.eci.library_dirs)
+ library_dirs=self.library_dirs)
def build_executable(*args, **kwds):
noerr = kwds.pop('noerr', False)
diff --git a/lib-python/modified-2.7/UserDict.py b/lib-python/modified-2.7/UserDict.py
--- a/lib-python/modified-2.7/UserDict.py
+++ b/lib-python/modified-2.7/UserDict.py
@@ -85,8 +85,12 @@
def __iter__(self):
return iter(self.data)
-import _abcoll
-_abcoll.MutableMapping.register(IterableUserDict)
+try:
+ import _abcoll
+except ImportError:
+ pass # e.g. no '_weakref' module on this pypy
+else:
+ _abcoll.MutableMapping.register(IterableUserDict)
class DictMixin:
diff --git a/lib_pypy/_subprocess.py b/lib_pypy/_subprocess.py
--- a/lib_pypy/_subprocess.py
+++ b/lib_pypy/_subprocess.py
@@ -87,7 +87,7 @@
# Now the _subprocess module implementation
-from ctypes import c_int as _c_int, byref as _byref
+from ctypes import c_int as _c_int, byref as _byref, WinError as _WinError
class _handle:
def __init__(self, handle):
@@ -116,7 +116,7 @@
res = _CreatePipe(_byref(read), _byref(write), None, size)
if not res:
- raise WindowsError("Error")
+ raise _WinError()
return _handle(read.value), _handle(write.value)
@@ -132,7 +132,7 @@
access, inherit, options)
if not res:
- raise WindowsError("Error")
+ raise _WinError()
return _handle(target.value)
DUPLICATE_SAME_ACCESS = 2
@@ -165,7 +165,7 @@
start_dir, _byref(si), _byref(pi))
if not res:
- raise WindowsError("Error")
+ raise _WinError()
return _handle(pi.hProcess), _handle(pi.hThread), pi.dwProcessID, pi.dwThreadID
STARTF_USESHOWWINDOW = 0x001
@@ -178,7 +178,7 @@
res = _WaitForSingleObject(int(handle), milliseconds)
if res < 0:
- raise WindowsError("Error")
+ raise _WinError()
return res
INFINITE = 0xffffffff
@@ -190,7 +190,7 @@
res = _GetExitCodeProcess(int(handle), _byref(code))
if not res:
- raise WindowsError("Error")
+ raise _WinError()
return code.value
@@ -198,7 +198,7 @@
res = _TerminateProcess(int(handle), exitcode)
if not res:
- raise WindowsError("Error")
+ raise _WinError()
def GetStdHandle(stdhandle):
res = _GetStdHandle(stdhandle)
diff --git a/lib_pypy/ctypes_config_cache/pyexpat.ctc.py b/lib_pypy/ctypes_config_cache/pyexpat.ctc.py
deleted file mode 100644
--- a/lib_pypy/ctypes_config_cache/pyexpat.ctc.py
+++ /dev/null
@@ -1,45 +0,0 @@
-"""
-'ctypes_configure' source for pyexpat.py.
-Run this to rebuild _pyexpat_cache.py.
-"""
-
-import ctypes
-from ctypes import c_char_p, c_int, c_void_p, c_char
-from ctypes_configure import configure
-import dumpcache
-
-
-class CConfigure:
- _compilation_info_ = configure.ExternalCompilationInfo(
- includes = ['expat.h'],
- libraries = ['expat'],
- pre_include_lines = [
- '#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)'],
- )
-
- XML_Char = configure.SimpleType('XML_Char', c_char)
- XML_COMBINED_VERSION = configure.ConstantInteger('XML_COMBINED_VERSION')
- for name in ['XML_PARAM_ENTITY_PARSING_NEVER',
- 'XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE',
- 'XML_PARAM_ENTITY_PARSING_ALWAYS']:
- locals()[name] = configure.ConstantInteger(name)
-
- XML_Encoding = configure.Struct('XML_Encoding',[
- ('data', c_void_p),
- ('convert', c_void_p),
- ('release', c_void_p),
- ('map', c_int * 256)])
- XML_Content = configure.Struct('XML_Content',[
- ('numchildren', c_int),
- ('children', c_void_p),
- ('name', c_char_p),
- ('type', c_int),
- ('quant', c_int),
- ])
- # this is insanely stupid
- XML_FALSE = configure.ConstantInteger('XML_FALSE')
- XML_TRUE = configure.ConstantInteger('XML_TRUE')
-
-config = configure.configure(CConfigure)
-
-dumpcache.dumpcache2('pyexpat', config)
diff --git a/lib_pypy/ctypes_config_cache/test/test_cache.py b/lib_pypy/ctypes_config_cache/test/test_cache.py
--- a/lib_pypy/ctypes_config_cache/test/test_cache.py
+++ b/lib_pypy/ctypes_config_cache/test/test_cache.py
@@ -39,10 +39,6 @@
d = run('resource.ctc.py', '_resource_cache.py')
assert 'RLIM_NLIMITS' in d
-def test_pyexpat():
- d = run('pyexpat.ctc.py', '_pyexpat_cache.py')
- assert 'XML_COMBINED_VERSION' in d
-
def test_locale():
d = run('locale.ctc.py', '_locale_cache.py')
assert 'LC_ALL' in d
diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -271,8 +271,9 @@
raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
def _check_date_fields(year, month, day):
- if not isinstance(year, (int, long)):
- raise TypeError('int expected')
+ for value in [year, day]:
+ if not isinstance(value, (int, long)):
+ raise TypeError('int expected')
if not MINYEAR <= year <= MAXYEAR:
raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
if not 1 <= month <= 12:
@@ -282,8 +283,9 @@
raise ValueError('day must be in 1..%d' % dim, day)
def _check_time_fields(hour, minute, second, microsecond):
- if not isinstance(hour, (int, long)):
- raise TypeError('int expected')
+ for value in [hour, minute, second, microsecond]:
+ if not isinstance(value, (int, long)):
+ raise TypeError('int expected')
if not 0 <= hour <= 23:
raise ValueError('hour must be in 0..23', hour)
if not 0 <= minute <= 59:
diff --git a/lib_pypy/pyexpat.py b/lib_pypy/pyexpat.py
deleted file mode 100644
--- a/lib_pypy/pyexpat.py
+++ /dev/null
@@ -1,448 +0,0 @@
-
-import ctypes
-import ctypes.util
-from ctypes import c_char_p, c_int, c_void_p, POINTER, c_char, c_wchar_p
-import sys
-
-# load the platform-specific cache made by running pyexpat.ctc.py
-from ctypes_config_cache._pyexpat_cache import *
-
-try: from __pypy__ import builtinify
-except ImportError: builtinify = lambda f: f
-
-
-lib = ctypes.CDLL(ctypes.util.find_library('expat'))
-
-
-XML_Content.children = POINTER(XML_Content)
-XML_Parser = ctypes.c_void_p # an opaque pointer
-assert XML_Char is ctypes.c_char # this assumption is everywhere in
-# cpython's expat, let's explode
-
-def declare_external(name, args, res):
- func = getattr(lib, name)
- func.args = args
- func.restype = res
- globals()[name] = func
-
-declare_external('XML_ParserCreate', [c_char_p], XML_Parser)
-declare_external('XML_ParserCreateNS', [c_char_p, c_char], XML_Parser)
-declare_external('XML_Parse', [XML_Parser, c_char_p, c_int, c_int], c_int)
-currents = ['CurrentLineNumber', 'CurrentColumnNumber',
- 'CurrentByteIndex']
-for name in currents:
- func = getattr(lib, 'XML_Get' + name)
- func.args = [XML_Parser]
- func.restype = c_int
-
-declare_external('XML_SetReturnNSTriplet', [XML_Parser, c_int], None)
-declare_external('XML_GetSpecifiedAttributeCount', [XML_Parser], c_int)
-declare_external('XML_SetParamEntityParsing', [XML_Parser, c_int], None)
-declare_external('XML_GetErrorCode', [XML_Parser], c_int)
-declare_external('XML_StopParser', [XML_Parser, c_int], None)
-declare_external('XML_ErrorString', [c_int], c_char_p)
-declare_external('XML_SetBase', [XML_Parser, c_char_p], None)
-if XML_COMBINED_VERSION >= 19505:
- declare_external('XML_UseForeignDTD', [XML_Parser, c_int], None)
-
-declare_external('XML_SetUnknownEncodingHandler', [XML_Parser, c_void_p,
- c_void_p], None)
-declare_external('XML_FreeContentModel', [XML_Parser, POINTER(XML_Content)],
- None)
-declare_external('XML_ExternalEntityParserCreate', [XML_Parser,c_char_p,
- c_char_p],
- XML_Parser)
-
-handler_names = [
- 'StartElement',
- 'EndElement',
- 'ProcessingInstruction',
- 'CharacterData',
- 'UnparsedEntityDecl',
- 'NotationDecl',
- 'StartNamespaceDecl',
- 'EndNamespaceDecl',
- 'Comment',
- 'StartCdataSection',
- 'EndCdataSection',
- 'Default',
- 'DefaultHandlerExpand',
- 'NotStandalone',
- 'ExternalEntityRef',
- 'StartDoctypeDecl',
- 'EndDoctypeDecl',
- 'EntityDecl',
- 'XmlDecl',
- 'ElementDecl',
- 'AttlistDecl',
- ]
-if XML_COMBINED_VERSION >= 19504:
- handler_names.append('SkippedEntity')
-setters = {}
-
-for name in handler_names:
- if name == 'DefaultHandlerExpand':
- newname = 'XML_SetDefaultHandlerExpand'
- else:
- name += 'Handler'
- newname = 'XML_Set' + name
- cfunc = getattr(lib, newname)
- cfunc.args = [XML_Parser, ctypes.c_void_p]
- cfunc.result = ctypes.c_int
- setters[name] = cfunc
-
-class ExpatError(Exception):
- def __str__(self):
- return self.s
-
-error = ExpatError
-
-class XMLParserType(object):
- specified_attributes = 0
- ordered_attributes = 0
- returns_unicode = 1
- encoding = 'utf-8'
- def __init__(self, encoding, namespace_separator, _hook_external_entity=False):
- self.returns_unicode = 1
- if encoding:
- self.encoding = encoding
- if not _hook_external_entity:
- if namespace_separator is None:
- self.itself = XML_ParserCreate(encoding)
- else:
- self.itself = XML_ParserCreateNS(encoding, ord(namespace_separator))
- if not self.itself:
- raise RuntimeError("Creating parser failed")
- self._set_unknown_encoding_handler()
- self.storage = {}
- self.buffer = None
- self.buffer_size = 8192
- self.character_data_handler = None
- self.intern = {}
- self.__exc_info = None
-
- def _flush_character_buffer(self):
- if not self.buffer:
- return
- res = self._call_character_handler(''.join(self.buffer))
- self.buffer = []
- return res
-
- def _call_character_handler(self, buf):
- if self.character_data_handler:
- self.character_data_handler(buf)
-
- def _set_unknown_encoding_handler(self):
- def UnknownEncoding(encodingData, name, info_p):
- info = info_p.contents
- s = ''.join([chr(i) for i in range(256)])
- u = s.decode(self.encoding, 'replace')
- for i in range(len(u)):
- if u[i] == u'\xfffd':
- info.map[i] = -1
- else:
- info.map[i] = ord(u[i])
- info.data = None
- info.convert = None
- info.release = None
- return 1
-
- CB = ctypes.CFUNCTYPE(c_int, c_void_p, c_char_p, POINTER(XML_Encoding))
- cb = CB(UnknownEncoding)
- self._unknown_encoding_handler = (cb, UnknownEncoding)
- XML_SetUnknownEncodingHandler(self.itself, cb, None)
-
- def _set_error(self, code):
- e = ExpatError()
- e.code = code
- lineno = lib.XML_GetCurrentLineNumber(self.itself)
- colno = lib.XML_GetCurrentColumnNumber(self.itself)
- e.offset = colno
- e.lineno = lineno
- err = XML_ErrorString(code)[:200]
- e.s = "%s: line: %d, column: %d" % (err, lineno, colno)
- e.message = e.s
- self._error = e
-
- def Parse(self, data, is_final=0):
- res = XML_Parse(self.itself, data, len(data), is_final)
- if res == 0:
- self._set_error(XML_GetErrorCode(self.itself))
- if self.__exc_info:
- exc_info = self.__exc_info
- self.__exc_info = None
- raise exc_info[0], exc_info[1], exc_info[2]
- else:
- raise self._error
- self._flush_character_buffer()
- return res
-
- def _sethandler(self, name, real_cb):
- setter = setters[name]
- try:
- cb = self.storage[(name, real_cb)]
- except KeyError:
- cb = getattr(self, 'get_cb_for_%s' % name)(real_cb)
- self.storage[(name, real_cb)] = cb
- except TypeError:
- # weellll...
- cb = getattr(self, 'get_cb_for_%s' % name)(real_cb)
- setter(self.itself, cb)
-
- def _wrap_cb(self, cb):
- def f(*args):
- try:
- return cb(*args)
- except:
- self.__exc_info = sys.exc_info()
- XML_StopParser(self.itself, XML_FALSE)
- return f
-
- def get_cb_for_StartElementHandler(self, real_cb):
- def StartElement(unused, name, attrs):
- # unpack name and attrs
- conv = self.conv
- self._flush_character_buffer()
- if self.specified_attributes:
- max = XML_GetSpecifiedAttributeCount(self.itself)
- else:
- max = 0
- while attrs[max]:
- max += 2 # copied
- if self.ordered_attributes:
- res = [attrs[i] for i in range(max)]
- else:
- res = {}
- for i in range(0, max, 2):
- res[conv(attrs[i])] = conv(attrs[i + 1])
- real_cb(conv(name), res)
- StartElement = self._wrap_cb(StartElement)
- CB = ctypes.CFUNCTYPE(None, c_void_p, c_char_p, POINTER(c_char_p))
- return CB(StartElement)
-
- def get_cb_for_ExternalEntityRefHandler(self, real_cb):
- def ExternalEntity(unused, context, base, sysId, pubId):
- self._flush_character_buffer()
- conv = self.conv
- res = real_cb(conv(context), conv(base), conv(sysId),
- conv(pubId))
- if res is None:
- return 0
- return res
- ExternalEntity = self._wrap_cb(ExternalEntity)
- CB = ctypes.CFUNCTYPE(c_int, c_void_p, *([c_char_p] * 4))
- return CB(ExternalEntity)
-
- def get_cb_for_CharacterDataHandler(self, real_cb):
- def CharacterData(unused, s, lgt):
- if self.buffer is None:
- self._call_character_handler(self.conv(s[:lgt]))
- else:
- if len(self.buffer) + lgt > self.buffer_size:
- self._flush_character_buffer()
- if self.character_data_handler is None:
- return
- if lgt >= self.buffer_size:
- self._call_character_handler(s[:lgt])
- self.buffer = []
- else:
- self.buffer.append(s[:lgt])
- CharacterData = self._wrap_cb(CharacterData)
- CB = ctypes.CFUNCTYPE(None, c_void_p, POINTER(c_char), c_int)
- return CB(CharacterData)
-
- def get_cb_for_NotStandaloneHandler(self, real_cb):
- def NotStandaloneHandler(unused):
- return real_cb()
- NotStandaloneHandler = self._wrap_cb(NotStandaloneHandler)
- CB = ctypes.CFUNCTYPE(c_int, c_void_p)
- return CB(NotStandaloneHandler)
-
- def get_cb_for_EntityDeclHandler(self, real_cb):
- def EntityDecl(unused, ename, is_param, value, value_len, base,
- system_id, pub_id, not_name):
- self._flush_character_buffer()
- if not value:
- value = None
- else:
- value = value[:value_len]
- args = [ename, is_param, value, base, system_id,
- pub_id, not_name]
- args = [self.conv(arg) for arg in args]
- real_cb(*args)
- EntityDecl = self._wrap_cb(EntityDecl)
- CB = ctypes.CFUNCTYPE(None, c_void_p, c_char_p, c_int, c_char_p,
- c_int, c_char_p, c_char_p, c_char_p, c_char_p)
- return CB(EntityDecl)
-
- def _conv_content_model(self, model):
- children = tuple([self._conv_content_model(model.children[i])
- for i in range(model.numchildren)])
- return (model.type, model.quant, self.conv(model.name),
- children)
-
- def get_cb_for_ElementDeclHandler(self, real_cb):
- def ElementDecl(unused, name, model):
- self._flush_character_buffer()
- modelobj = self._conv_content_model(model[0])
- real_cb(name, modelobj)
- XML_FreeContentModel(self.itself, model)
-
- ElementDecl = self._wrap_cb(ElementDecl)
- CB = ctypes.CFUNCTYPE(None, c_void_p, c_char_p, POINTER(XML_Content))
- return CB(ElementDecl)
-
- def _new_callback_for_string_len(name, sign):
- def get_callback_for_(self, real_cb):
- def func(unused, s, len):
- self._flush_character_buffer()
- arg = self.conv(s[:len])
- real_cb(arg)
- func.func_name = name
- func = self._wrap_cb(func)
- CB = ctypes.CFUNCTYPE(*sign)
- return CB(func)
- get_callback_for_.func_name = 'get_cb_for_' + name
- return get_callback_for_
-
- for name in ['DefaultHandlerExpand',
- 'DefaultHandler']:
- sign = [None, c_void_p, POINTER(c_char), c_int]
- name = 'get_cb_for_' + name
- locals()[name] = _new_callback_for_string_len(name, sign)
-
- def _new_callback_for_starargs(name, sign):
- def get_callback_for_(self, real_cb):
- def func(unused, *args):
- self._flush_character_buffer()
- args = [self.conv(arg) for arg in args]
- real_cb(*args)
- func.func_name = name
- func = self._wrap_cb(func)
- CB = ctypes.CFUNCTYPE(*sign)
- return CB(func)
- get_callback_for_.func_name = 'get_cb_for_' + name
- return get_callback_for_
-
- for name, num_or_sign in [
- ('EndElementHandler', 1),
- ('ProcessingInstructionHandler', 2),
- ('UnparsedEntityDeclHandler', 5),
- ('NotationDeclHandler', 4),
- ('StartNamespaceDeclHandler', 2),
- ('EndNamespaceDeclHandler', 1),
- ('CommentHandler', 1),
- ('StartCdataSectionHandler', 0),
- ('EndCdataSectionHandler', 0),
- ('StartDoctypeDeclHandler', [None, c_void_p] + [c_char_p] * 3 + [c_int]),
- ('XmlDeclHandler', [None, c_void_p, c_char_p, c_char_p, c_int]),
- ('AttlistDeclHandler', [None, c_void_p] + [c_char_p] * 4 + [c_int]),
- ('EndDoctypeDeclHandler', 0),
- ('SkippedEntityHandler', [None, c_void_p, c_char_p, c_int]),
- ]:
- if isinstance(num_or_sign, int):
- sign = [None, c_void_p] + [c_char_p] * num_or_sign
- else:
- sign = num_or_sign
- name = 'get_cb_for_' + name
- locals()[name] = _new_callback_for_starargs(name, sign)
-
- def conv_unicode(self, s):
- if s is None or isinstance(s, int):
- return s
- return s.decode(self.encoding, "strict")
-
- def __setattr__(self, name, value):
- # forest of ifs...
- if name in ['ordered_attributes',
- 'returns_unicode', 'specified_attributes']:
- if value:
- if name == 'returns_unicode':
- self.conv = self.conv_unicode
- self.__dict__[name] = 1
- else:
- if name == 'returns_unicode':
- self.conv = lambda s: s
- self.__dict__[name] = 0
- elif name == 'buffer_text':
- if value:
- self.buffer = []
- else:
- self._flush_character_buffer()
- self.buffer = None
- elif name == 'buffer_size':
- if not isinstance(value, int):
- raise TypeError("Expected int")
- if value <= 0:
- raise ValueError("Expected positive int")
- self.__dict__[name] = value
- elif name == 'namespace_prefixes':
- XML_SetReturnNSTriplet(self.itself, int(bool(value)))
- elif name in setters:
- if name == 'CharacterDataHandler':
- # XXX we need to flush buffer here
- self._flush_character_buffer()
- self.character_data_handler = value
- #print name
- #print value
- #print
- self._sethandler(name, value)
- else:
- self.__dict__[name] = value
-
- def SetParamEntityParsing(self, arg):
- XML_SetParamEntityParsing(self.itself, arg)
-
- if XML_COMBINED_VERSION >= 19505:
- def UseForeignDTD(self, arg=True):
- if arg:
- flag = XML_TRUE
- else:
- flag = XML_FALSE
- XML_UseForeignDTD(self.itself, flag)
-
- def __getattr__(self, name):
- if name == 'buffer_text':
- return self.buffer is not None
- elif name in currents:
- return getattr(lib, 'XML_Get' + name)(self.itself)
- elif name == 'ErrorColumnNumber':
- return lib.XML_GetCurrentColumnNumber(self.itself)
- elif name == 'ErrorLineNumber':
- return lib.XML_GetCurrentLineNumber(self.itself)
- return self.__dict__[name]
-
- def ParseFile(self, file):
- return self.Parse(file.read(), False)
-
- def SetBase(self, base):
- XML_SetBase(self.itself, base)
-
- def ExternalEntityParserCreate(self, context, encoding=None):
- """ExternalEntityParserCreate(context[, encoding])
- Create a parser for parsing an external entity based on the
- information passed to the ExternalEntityRefHandler."""
- new_parser = XMLParserType(encoding, None, True)
- new_parser.itself = XML_ExternalEntityParserCreate(self.itself,
- context, encoding)
- new_parser._set_unknown_encoding_handler()
- return new_parser
-
- at builtinify
-def ErrorString(errno):
- return XML_ErrorString(errno)[:200]
-
- at builtinify
-def ParserCreate(encoding=None, namespace_separator=None, intern=None):
- if (not isinstance(encoding, str) and
- not encoding is None):
- raise TypeError("ParserCreate() argument 1 must be string or None, not %s" % encoding.__class__.__name__)
- if (not isinstance(namespace_separator, str) and
- not namespace_separator is None):
- raise TypeError("ParserCreate() argument 2 must be string or None, not %s" % namespace_separator.__class__.__name__)
- if namespace_separator is not None:
- if len(namespace_separator) > 1:
- raise ValueError('namespace_separator must be at most one character, omitted, or None')
- if len(namespace_separator) == 0:
- namespace_separator = None
- return XMLParserType(encoding, namespace_separator)
diff --git a/lib_pypy/pypy_test/test_pyexpat.py b/lib_pypy/pypy_test/test_pyexpat.py
deleted file mode 100644
--- a/lib_pypy/pypy_test/test_pyexpat.py
+++ /dev/null
@@ -1,665 +0,0 @@
-# XXX TypeErrors on calling handlers, or on bad return values from a
-# handler, are obscure and unhelpful.
-
-from __future__ import absolute_import
-import StringIO, sys
-import unittest, py
-
-from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('pyexpat.ctc.py')
-
-from lib_pypy import pyexpat
-#from xml.parsers import expat
-expat = pyexpat
-
-from test.test_support import sortdict, run_unittest
-
-
-class TestSetAttribute:
- def setup_method(self, meth):
- self.parser = expat.ParserCreate(namespace_separator='!')
- self.set_get_pairs = [
- [0, 0],
- [1, 1],
- [2, 1],
- [0, 0],
- ]
-
- def test_returns_unicode(self):
- for x, y in self.set_get_pairs:
- self.parser.returns_unicode = x
- assert self.parser.returns_unicode == y
-
- def test_ordered_attributes(self):
- for x, y in self.set_get_pairs:
- self.parser.ordered_attributes = x
- assert self.parser.ordered_attributes == y
-
- def test_specified_attributes(self):
- for x, y in self.set_get_pairs:
- self.parser.specified_attributes = x
- assert self.parser.specified_attributes == y
-
-
-data = '''\
-<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
-<?xml-stylesheet href="stylesheet.css"?>
-<!-- comment data -->
-<!DOCTYPE quotations SYSTEM "quotations.dtd" [
-<!ELEMENT root ANY>
-<!ATTLIST root attr1 CDATA #REQUIRED attr2 CDATA #IMPLIED>
-<!NOTATION notation SYSTEM "notation.jpeg">
-<!ENTITY acirc "â">
-<!ENTITY external_entity SYSTEM "entity.file">
-<!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation>
-%unparsed_entity;
-]>
-
-<root attr1="value1" attr2="value2ὀ">
-<myns:subelement xmlns:myns="http://www.python.org/namespace">
- Contents of subelements
-</myns:subelement>
-<sub2><![CDATA[contents of CDATA section]]></sub2>
-&external_entity;
-&skipped_entity;
-</root>
-'''
-
-
-# Produce UTF-8 output
-class TestParse:
- class Outputter:
- def __init__(self):
- self.out = []
-
- def StartElementHandler(self, name, attrs):
- self.out.append('Start element: ' + repr(name) + ' ' +
- sortdict(attrs))
-
- def EndElementHandler(self, name):
- self.out.append('End element: ' + repr(name))
-
- def CharacterDataHandler(self, data):
- data = data.strip()
- if data:
- self.out.append('Character data: ' + repr(data))
-
- def ProcessingInstructionHandler(self, target, data):
- self.out.append('PI: ' + repr(target) + ' ' + repr(data))
-
- def StartNamespaceDeclHandler(self, prefix, uri):
- self.out.append('NS decl: ' + repr(prefix) + ' ' + repr(uri))
-
- def EndNamespaceDeclHandler(self, prefix):
- self.out.append('End of NS decl: ' + repr(prefix))
-
- def StartCdataSectionHandler(self):
- self.out.append('Start of CDATA section')
-
- def EndCdataSectionHandler(self):
- self.out.append('End of CDATA section')
-
- def CommentHandler(self, text):
- self.out.append('Comment: ' + repr(text))
-
- def NotationDeclHandler(self, *args):
- name, base, sysid, pubid = args
- self.out.append('Notation declared: %s' %(args,))
-
- def UnparsedEntityDeclHandler(self, *args):
- entityName, base, systemId, publicId, notationName = args
- self.out.append('Unparsed entity decl: %s' %(args,))
-
- def NotStandaloneHandler(self):
- self.out.append('Not standalone')
- return 1
-
- def ExternalEntityRefHandler(self, *args):
- context, base, sysId, pubId = args
- self.out.append('External entity ref: %s' %(args[1:],))
- return 1
-
- def StartDoctypeDeclHandler(self, *args):
- self.out.append(('Start doctype', args))
- return 1
-
- def EndDoctypeDeclHandler(self):
- self.out.append("End doctype")
- return 1
-
- def EntityDeclHandler(self, *args):
- self.out.append(('Entity declaration', args))
- return 1
-
- def XmlDeclHandler(self, *args):
- self.out.append(('XML declaration', args))
- return 1
-
- def ElementDeclHandler(self, *args):
- self.out.append(('Element declaration', args))
- return 1
-
- def AttlistDeclHandler(self, *args):
- self.out.append(('Attribute list declaration', args))
- return 1
-
- def SkippedEntityHandler(self, *args):
- self.out.append(("Skipped entity", args))
- return 1
-
- def DefaultHandler(self, userData):
- pass
-
- def DefaultHandlerExpand(self, userData):
- pass
-
- handler_names = [
- 'StartElementHandler', 'EndElementHandler', 'CharacterDataHandler',
- 'ProcessingInstructionHandler', 'UnparsedEntityDeclHandler',
- 'NotationDeclHandler', 'StartNamespaceDeclHandler',
- 'EndNamespaceDeclHandler', 'CommentHandler',
- 'StartCdataSectionHandler', 'EndCdataSectionHandler', 'DefaultHandler',
- 'DefaultHandlerExpand', 'NotStandaloneHandler',
- 'ExternalEntityRefHandler', 'StartDoctypeDeclHandler',
- 'EndDoctypeDeclHandler', 'EntityDeclHandler', 'XmlDeclHandler',
- 'ElementDeclHandler', 'AttlistDeclHandler', 'SkippedEntityHandler',
- ]
-
- def test_utf8(self):
-
- out = self.Outputter()
- parser = expat.ParserCreate(namespace_separator='!')
- for name in self.handler_names:
- setattr(parser, name, getattr(out, name))
- parser.returns_unicode = 0
- parser.Parse(data, 1)
-
- # Verify output
- operations = out.out
- expected_operations = [
- ('XML declaration', (u'1.0', u'iso-8859-1', 0)),
- 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'',
- "Comment: ' comment data '",
- "Not standalone",
- ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
- ('Element declaration', (u'root', (2, 0, None, ()))),
- ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
- 1)),
- ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
- 0)),
- "Notation declared: ('notation', None, 'notation.jpeg', None)",
- ('Entity declaration', ('acirc', 0, '\xc3\xa2', None, None, None, None)),
- ('Entity declaration', ('external_entity', 0, None, None,
- 'entity.file', None, None)),
- "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')",
- "Not standalone",
- "End doctype",
- "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\\xe1\\xbd\\x80'}",
- "NS decl: 'myns' 'http://www.python.org/namespace'",
- "Start element: 'http://www.python.org/namespace!subelement' {}",
- "Character data: 'Contents of subelements'",
- "End element: 'http://www.python.org/namespace!subelement'",
- "End of NS decl: 'myns'",
- "Start element: 'sub2' {}",
- 'Start of CDATA section',
- "Character data: 'contents of CDATA section'",
- 'End of CDATA section',
- "End element: 'sub2'",
- "External entity ref: (None, 'entity.file', None)",
- ('Skipped entity', ('skipped_entity', 0)),
- "End element: 'root'",
- ]
- for operation, expected_operation in zip(operations, expected_operations):
- assert operation == expected_operation
-
- def test_unicode(self):
- # Try the parse again, this time producing Unicode output
- out = self.Outputter()
- parser = expat.ParserCreate(namespace_separator='!')
- parser.returns_unicode = 1
- for name in self.handler_names:
- setattr(parser, name, getattr(out, name))
-
- parser.Parse(data, 1)
-
- operations = out.out
- expected_operations = [
- ('XML declaration', (u'1.0', u'iso-8859-1', 0)),
- 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'',
- "Comment: u' comment data '",
- "Not standalone",
- ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
- ('Element declaration', (u'root', (2, 0, None, ()))),
- ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
- 1)),
- ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
- 0)),
- "Notation declared: (u'notation', None, u'notation.jpeg', None)",
- ('Entity declaration', (u'acirc', 0, u'\xe2', None, None, None,
- None)),
- ('Entity declaration', (u'external_entity', 0, None, None,
- u'entity.file', None, None)),
- "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')",
- "Not standalone",
- "End doctype",
- "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}",
- "NS decl: u'myns' u'http://www.python.org/namespace'",
- "Start element: u'http://www.python.org/namespace!subelement' {}",
- "Character data: u'Contents of subelements'",
- "End element: u'http://www.python.org/namespace!subelement'",
- "End of NS decl: u'myns'",
- "Start element: u'sub2' {}",
- 'Start of CDATA section',
- "Character data: u'contents of CDATA section'",
- 'End of CDATA section',
- "End element: u'sub2'",
- "External entity ref: (None, u'entity.file', None)",
- ('Skipped entity', ('skipped_entity', 0)),
- "End element: u'root'",
- ]
- for operation, expected_operation in zip(operations, expected_operations):
- assert operation == expected_operation
-
- def test_parse_file(self):
- # Try parsing a file
- out = self.Outputter()
- parser = expat.ParserCreate(namespace_separator='!')
- parser.returns_unicode = 1
- for name in self.handler_names:
- setattr(parser, name, getattr(out, name))
- file = StringIO.StringIO(data)
-
- parser.ParseFile(file)
-
- operations = out.out
- expected_operations = [
- ('XML declaration', (u'1.0', u'iso-8859-1', 0)),
- 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'',
- "Comment: u' comment data '",
- "Not standalone",
- ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
- ('Element declaration', (u'root', (2, 0, None, ()))),
- ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
- 1)),
- ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
- 0)),
- "Notation declared: (u'notation', None, u'notation.jpeg', None)",
- ('Entity declaration', ('acirc', 0, u'\xe2', None, None, None, None)),
- ('Entity declaration', (u'external_entity', 0, None, None, u'entity.file', None, None)),
- "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')",
- "Not standalone",
- "End doctype",
- "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}",
- "NS decl: u'myns' u'http://www.python.org/namespace'",
- "Start element: u'http://www.python.org/namespace!subelement' {}",
- "Character data: u'Contents of subelements'",
- "End element: u'http://www.python.org/namespace!subelement'",
- "End of NS decl: u'myns'",
- "Start element: u'sub2' {}",
- 'Start of CDATA section',
- "Character data: u'contents of CDATA section'",
- 'End of CDATA section',
- "End element: u'sub2'",
- "External entity ref: (None, u'entity.file', None)",
- ('Skipped entity', ('skipped_entity', 0)),
- "End element: u'root'",
- ]
- for operation, expected_operation in zip(operations, expected_operations):
- assert operation == expected_operation
-
-
-class TestNamespaceSeparator:
- def test_legal(self):
- # Tests that make sure we get errors when the namespace_separator value
- # is illegal, and that we don't for good values:
- expat.ParserCreate()
- expat.ParserCreate(namespace_separator=None)
- expat.ParserCreate(namespace_separator=' ')
-
- def test_illegal(self):
- try:
- expat.ParserCreate(namespace_separator=42)
- raise AssertionError
- except TypeError, e:
- assert str(e) == (
- 'ParserCreate() argument 2 must be string or None, not int')
-
- try:
- expat.ParserCreate(namespace_separator='too long')
- raise AssertionError
- except ValueError, e:
- assert str(e) == (
- 'namespace_separator must be at most one character, omitted, or None')
-
- def test_zero_length(self):
- # ParserCreate() needs to accept a namespace_separator of zero length
- # to satisfy the requirements of RDF applications that are required
- # to simply glue together the namespace URI and the localname. Though
- # considered a wart of the RDF specifications, it needs to be supported.
- #
- # See XML-SIG mailing list thread starting with
- # http://mail.python.org/pipermail/xml-sig/2001-April/005202.html
- #
- expat.ParserCreate(namespace_separator='') # too short
-
-
-class TestInterning:
- def test(self):
- py.test.skip("Not working")
- # Test the interning machinery.
- p = expat.ParserCreate()
- L = []
- def collector(name, *args):
- L.append(name)
- p.StartElementHandler = collector
- p.EndElementHandler = collector
- p.Parse("<e> <e/> <e></e> </e>", 1)
- tag = L[0]
- assert len(L) == 6
- for entry in L:
- # L should have the same string repeated over and over.
- assert tag is entry
-
-
-class TestBufferText:
- def setup_method(self, meth):
- self.stuff = []
- self.parser = expat.ParserCreate()
- self.parser.buffer_text = 1
- self.parser.CharacterDataHandler = self.CharacterDataHandler
-
- def check(self, expected, label):
- assert self.stuff == expected, (
- "%s\nstuff = %r\nexpected = %r"
- % (label, self.stuff, map(unicode, expected)))
-
- def CharacterDataHandler(self, text):
- self.stuff.append(text)
-
- def StartElementHandler(self, name, attrs):
- self.stuff.append("<%s>" % name)
- bt = attrs.get("buffer-text")
- if bt == "yes":
- self.parser.buffer_text = 1
- elif bt == "no":
- self.parser.buffer_text = 0
-
- def EndElementHandler(self, name):
- self.stuff.append("</%s>" % name)
-
- def CommentHandler(self, data):
- self.stuff.append("<!--%s-->" % data)
-
- def setHandlers(self, handlers=[]):
- for name in handlers:
- setattr(self.parser, name, getattr(self, name))
-
- def test_default_to_disabled(self):
- parser = expat.ParserCreate()
- assert not parser.buffer_text
-
- def test_buffering_enabled(self):
- # Make sure buffering is turned on
- assert self.parser.buffer_text
- self.parser.Parse("<a>1<b/>2<c/>3</a>", 1)
- assert self.stuff == ['123'], (
- "buffered text not properly collapsed")
-
- def test1(self):
- # XXX This test exposes more detail of Expat's text chunking than we
- # XXX like, but it tests what we need to concisely.
- self.setHandlers(["StartElementHandler"])
- self.parser.Parse("<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\n5</a>", 1)
- assert self.stuff == (
- ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"]), (
- "buffering control not reacting as expected")
-
- def test2(self):
- self.parser.Parse("<a>1<b/><2><c/> \n 3</a>", 1)
- assert self.stuff == ["1<2> \n 3"], (
- "buffered text not properly collapsed")
-
- def test3(self):
- self.setHandlers(["StartElementHandler"])
- self.parser.Parse("<a>1<b/>2<c/>3</a>", 1)
- assert self.stuff == ["<a>", "1", "<b>", "2", "<c>", "3"], (
- "buffered text not properly split")
-
- def test4(self):
- self.setHandlers(["StartElementHandler", "EndElementHandler"])
- self.parser.CharacterDataHandler = None
- self.parser.Parse("<a>1<b/>2<c/>3</a>", 1)
- assert self.stuff == (
- ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"])
-
- def test5(self):
- self.setHandlers(["StartElementHandler", "EndElementHandler"])
- self.parser.Parse("<a>1<b></b>2<c/>3</a>", 1)
- assert self.stuff == (
- ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"])
-
- def test6(self):
- self.setHandlers(["CommentHandler", "EndElementHandler",
- "StartElementHandler"])
- self.parser.Parse("<a>1<b/>2<c></c>345</a> ", 1)
- assert self.stuff == (
- ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"]), (
- "buffered text not properly split")
-
- def test7(self):
- self.setHandlers(["CommentHandler", "EndElementHandler",
- "StartElementHandler"])
- self.parser.Parse("<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1)
- assert self.stuff == (
- ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3",
- "<!--abc-->", "4", "<!--def-->", "5", "</a>"]), (
- "buffered text not properly split")
-
-
-# Test handling of exception from callback:
-class TestHandlerException:
- def StartElementHandler(self, name, attrs):
- raise RuntimeError(name)
-
- def test(self):
- parser = expat.ParserCreate()
- parser.StartElementHandler = self.StartElementHandler
- try:
- parser.Parse("<a><b><c/></b></a>", 1)
- raise AssertionError
- except RuntimeError, e:
- assert e.args[0] == 'a', (
- "Expected RuntimeError for element 'a', but" + \
- " found %r" % e.args[0])
-
-
-# Test Current* members:
-class TestPosition:
- def StartElementHandler(self, name, attrs):
- self.check_pos('s')
-
- def EndElementHandler(self, name):
- self.check_pos('e')
-
- def check_pos(self, event):
- pos = (event,
- self.parser.CurrentByteIndex,
- self.parser.CurrentLineNumber,
- self.parser.CurrentColumnNumber)
- assert self.upto < len(self.expected_list)
- expected = self.expected_list[self.upto]
- assert pos == expected, (
- 'Expected position %s, got position %s' %(pos, expected))
- self.upto += 1
-
- def test(self):
- self.parser = expat.ParserCreate()
- self.parser.StartElementHandler = self.StartElementHandler
- self.parser.EndElementHandler = self.EndElementHandler
- self.upto = 0
- self.expected_list = [('s', 0, 1, 0), ('s', 5, 2, 1), ('s', 11, 3, 2),
- ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)]
-
- xml = '<a>\n <b>\n <c/>\n </b>\n</a>'
- self.parser.Parse(xml, 1)
-
-
-class Testsf1296433:
- def test_parse_only_xml_data(self):
- # http://python.org/sf/1296433
- #
- xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025)
- # this one doesn't crash
- #xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000)
-
- class SpecificException(Exception):
- pass
-
- def handler(text):
- raise SpecificException
-
- parser = expat.ParserCreate()
- parser.CharacterDataHandler = handler
-
- py.test.raises(Exception, parser.Parse, xml)
-
-class TestChardataBuffer:
- """
- test setting of chardata buffer size
- """
-
- def test_1025_bytes(self):
- assert self.small_buffer_test(1025) == 2
-
- def test_1000_bytes(self):
- assert self.small_buffer_test(1000) == 1
-
- def test_wrong_size(self):
- parser = expat.ParserCreate()
- parser.buffer_text = 1
- def f(size):
- parser.buffer_size = size
-
- py.test.raises(TypeError, f, sys.maxint+1)
- py.test.raises(ValueError, f, -1)
- py.test.raises(ValueError, f, 0)
-
- def test_unchanged_size(self):
- xml1 = ("<?xml version='1.0' encoding='iso8859'?><s>%s" % ('a' * 512))
- xml2 = 'a'*512 + '</s>'
- parser = expat.ParserCreate()
- parser.CharacterDataHandler = self.counting_handler
- parser.buffer_size = 512
- parser.buffer_text = 1
-
- # Feed 512 bytes of character data: the handler should be called
- # once.
- self.n = 0
- parser.Parse(xml1)
- assert self.n == 1
-
- # Reassign to buffer_size, but assign the same size.
- parser.buffer_size = parser.buffer_size
- assert self.n == 1
-
- # Try parsing rest of the document
- parser.Parse(xml2)
- assert self.n == 2
-
-
- def test_disabling_buffer(self):
- xml1 = "<?xml version='1.0' encoding='iso8859'?><a>%s" % ('a' * 512)
- xml2 = ('b' * 1024)
- xml3 = "%s</a>" % ('c' * 1024)
- parser = expat.ParserCreate()
- parser.CharacterDataHandler = self.counting_handler
- parser.buffer_text = 1
- parser.buffer_size = 1024
- assert parser.buffer_size == 1024
-
- # Parse one chunk of XML
- self.n = 0
- parser.Parse(xml1, 0)
- assert parser.buffer_size == 1024
- assert self.n == 1
-
- # Turn off buffering and parse the next chunk.
- parser.buffer_text = 0
- assert not parser.buffer_text
- assert parser.buffer_size == 1024
- for i in range(10):
- parser.Parse(xml2, 0)
- assert self.n == 11
-
- parser.buffer_text = 1
- assert parser.buffer_text
- assert parser.buffer_size == 1024
- parser.Parse(xml3, 1)
- assert self.n == 12
-
-
-
- def make_document(self, bytes):
- return ("<?xml version='1.0'?><tag>" + bytes * 'a' + '</tag>')
-
- def counting_handler(self, text):
- self.n += 1
-
- def small_buffer_test(self, buffer_len):
- xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * buffer_len)
- parser = expat.ParserCreate()
- parser.CharacterDataHandler = self.counting_handler
- parser.buffer_size = 1024
- parser.buffer_text = 1
-
- self.n = 0
- parser.Parse(xml)
- return self.n
-
- def test_change_size_1(self):
- xml1 = "<?xml version='1.0' encoding='iso8859'?><a><s>%s" % ('a' * 1024)
- xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025)
- parser = expat.ParserCreate()
- parser.CharacterDataHandler = self.counting_handler
- parser.buffer_text = 1
- parser.buffer_size = 1024
- assert parser.buffer_size == 1024
-
- self.n = 0
- parser.Parse(xml1, 0)
- parser.buffer_size *= 2
- assert parser.buffer_size == 2048
- parser.Parse(xml2, 1)
- assert self.n == 2
-
- def test_change_size_2(self):
- xml1 = "<?xml version='1.0' encoding='iso8859'?><a>a<s>%s" % ('a' * 1023)
- xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025)
- parser = expat.ParserCreate()
- parser.CharacterDataHandler = self.counting_handler
- parser.buffer_text = 1
- parser.buffer_size = 2048
- assert parser.buffer_size == 2048
-
- self.n=0
- parser.Parse(xml1, 0)
- parser.buffer_size /= 2
- assert parser.buffer_size == 1024
- parser.Parse(xml2, 1)
- assert self.n == 4
-
- def test_segfault(self):
- py.test.raises(TypeError, expat.ParserCreate, 1234123123)
-
-def test_invalid_data():
- parser = expat.ParserCreate()
- parser.Parse('invalid.xml', 0)
- try:
- parser.Parse("", 1)
- except expat.ExpatError, e:
- assert e.code == 2 # XXX is this reliable?
- assert e.lineno == 1
- assert e.message.startswith('syntax error')
- else:
- py.test.fail("Did not raise")
-
diff --git a/pypy/doc/coding-guide.rst b/pypy/doc/coding-guide.rst
--- a/pypy/doc/coding-guide.rst
+++ b/pypy/doc/coding-guide.rst
@@ -388,7 +388,9 @@
In a few cases (e.g. hash table manipulation), we need machine-sized unsigned
arithmetic. For these cases there is the r_uint class, which is a pure
Python implementation of word-sized unsigned integers that silently wrap
- around. The purpose of this class (as opposed to helper functions as above)
+ around. ("word-sized" and "machine-sized" are used equivalently and mean
+ the native size, which you get using "unsigned long" in C.)
+ The purpose of this class (as opposed to helper functions as above)
is consistent typing: both Python and the annotator will propagate r_uint
instances in the program and interpret all the operations between them as
unsigned. Instances of r_uint are special-cased by the code generators to
diff --git a/pypy/doc/config/objspace.usemodules.pyexpat.txt b/pypy/doc/config/objspace.usemodules.pyexpat.txt
--- a/pypy/doc/config/objspace.usemodules.pyexpat.txt
+++ b/pypy/doc/config/objspace.usemodules.pyexpat.txt
@@ -1,2 +1,1 @@
-Use (experimental) pyexpat module written in RPython, instead of CTypes
-version which is used by default.
+Use the pyexpat module, written in RPython.
diff --git a/pypy/jit/metainterp/optimizeopt/vectorize.py b/pypy/jit/metainterp/optimizeopt/vectorize.py
--- a/pypy/jit/metainterp/optimizeopt/vectorize.py
+++ b/pypy/jit/metainterp/optimizeopt/vectorize.py
@@ -4,6 +4,7 @@
from pypy.jit.metainterp.resoperation import rop, ResOperation
from pypy.jit.metainterp.history import BoxVector
from pypy.jit.codewriter.effectinfo import EffectInfo
+from pypy.rlib.debug import debug_start, debug_print, debug_stop
VECTOR_SIZE = 2
VEC_MAP = {rop.FLOAT_ADD: rop.FLOAT_VECTOR_ADD,
@@ -99,12 +100,19 @@
return self.index == descr.get_field_size()
class OptVectorize(Optimization):
+ track = None
+ full = None
+
def __init__(self):
self.ops_so_far = []
self.reset()
def reset(self):
# deal with reset
+ if self.track or self.full:
+ debug_start("jit-optimizeopt-vectorize")
+ debug_print("aborting vectorizing")
+ debug_stop("jit-optimizeopt-vectorize")
for op in self.ops_so_far:
self.emit_operation(op)
self.ops_so_far = []
@@ -120,6 +128,7 @@
if oopspec == EffectInfo.OS_ASSERT_ALIGNED:
index = self.getvalue(op.getarg(2))
self.tracked_indexes[index] = TrackIndex(index, 0)
+ self.emit_operation(op)
else:
self.optimize_default(op)
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -23,6 +23,7 @@
from pypy.interpreter.function import StaticMethod
from pypy.objspace.std.sliceobject import W_SliceObject
from pypy.module.__builtin__.descriptor import W_Property
+from pypy.module.__builtin__.interp_classobj import W_ClassObject
from pypy.module.__builtin__.interp_memoryview import W_MemoryView
from pypy.rlib.entrypoint import entrypoint
from pypy.rlib.unroll import unrolling_iterable
@@ -397,6 +398,7 @@
'Module': 'space.gettypeobject(Module.typedef)',
'Property': 'space.gettypeobject(W_Property.typedef)',
'Slice': 'space.gettypeobject(W_SliceObject.typedef)',
+ 'Class': 'space.gettypeobject(W_ClassObject.typedef)',
'StaticMethod': 'space.gettypeobject(StaticMethod.typedef)',
'CFunction': 'space.gettypeobject(cpyext.methodobject.W_PyCFunctionObject.typedef)',
'WrapperDescr': 'space.gettypeobject(cpyext.methodobject.W_PyCMethodObject.typedef)'
diff --git a/pypy/module/cpyext/test/test_classobject.py b/pypy/module/cpyext/test/test_classobject.py
--- a/pypy/module/cpyext/test/test_classobject.py
+++ b/pypy/module/cpyext/test/test_classobject.py
@@ -1,4 +1,5 @@
from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.interpreter.function import Function, Method
class TestClassObject(BaseApiTest):
@@ -51,3 +52,14 @@
assert api.PyInstance_Check(w_instance)
assert space.is_true(space.call_method(space.builtin, "isinstance",
w_instance, w_class))
+
+class AppTestStringObject(AppTestCpythonExtensionBase):
+ def test_class_type(self):
+ module = self.import_extension('foo', [
+ ("get_classtype", "METH_NOARGS",
+ """
+ Py_INCREF(&PyClass_Type);
+ return &PyClass_Type;
+ """)])
+ class C: pass
+ assert module.get_classtype() is type(C)
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.gateway import interp2app
+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
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
@@ -29,7 +30,6 @@
def convert_to(self, dtype):
return dtype.box(self.value)
-
class W_GenericBox(Wrappable):
_attrs_ = ()
@@ -38,6 +38,9 @@
w_subtype.getname(space, '?')
)
+ def get_dtype(self, space):
+ return self._get_dtype(space)
+
def descr_str(self, space):
return self.descr_repr(space)
@@ -45,12 +48,12 @@
return space.wrap(self.get_dtype(space).itemtype.str_format(self))
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)
@@ -129,7 +132,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_ = ()
@@ -145,34 +148,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_ = ()
@@ -181,12 +190,50 @@
_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):
+ pass
+
+class W_VoidBox(W_FlexibleBox):
+ def __init__(self, arr, ofs):
+ self.arr = arr # we have to keep array alive
+ self.ofs = ofs
+
+ def get_dtype(self, space):
+ return self.arr.dtype
+
+ @unwrap_spec(item=str)
+ def descr_getitem(self, space, item):
+ try:
+ ofs, dtype = self.arr.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)
+
+ @unwrap_spec(item=str)
+ def descr_setitem(self, space, item, w_value):
+ try:
+ ofs, dtype = self.arr.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):
+ pass
+
+class W_UnicodeBox(W_CharacterBox):
+ pass
W_GenericBox.typedef = TypeDef("generic",
__module__ = "numpypy",
@@ -330,3 +377,26 @@
__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",
+)
+
+W_UnicodeBox.typedef = TypeDef("unicode_", (unicode_typedef, W_CharacterBox.typedef),
+ __module__ = "numpypy",
+)
+
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,28 @@
+
+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):
self.itemtype = itemtype
self.num = num
self.kind = kind
@@ -29,53 +31,27 @@
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
@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 +62,9 @@
def descr_get_itemsize(self, space):
return space.wrap(self.itemtype.get_element_size())
+ def descr_get_alignment(self, space):
+ return space.wrap(self.itemtype.alignment)
+
def descr_get_shape(self, space):
return space.newtuple([])
@@ -99,31 +78,175 @@
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 '<>':
+ # ignore byte order, not sure if it's worth it for unicode only
+ if name[0] != byteorder_prefix and name[1] == 'U':
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "unimplemented non-native unicode"))
+ 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),
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 = '<'
+
class DtypeCache(object):
def __init__(self, space):
self.w_booldtype = W_Dtype(
@@ -211,7 +334,6 @@
name="int64",
char="q",
w_box_type=space.gettypefor(interp_boxes.W_Int64Box),
- alternate_constructors=[space.w_long],
)
self.w_uint64dtype = W_Dtype(
types.UInt64(),
@@ -239,18 +361,147 @@
alternate_constructors=[space.w_float],
aliases=["float"],
)
-
+ self.w_longlongdtype = W_Dtype(
+ types.Int64(),
+ num=9,
+ kind=SIGNEDLTR,
+ name='int64',
+ char='q',
+ w_box_type = space.gettypefor(interp_boxes.W_LongLongBox),
+ alternate_constructors=[space.w_long],
+ )
+ self.w_ulonglongdtype = W_Dtype(
+ types.UInt64(),
+ num=10,
+ kind=UNSIGNEDLTR,
+ name='uint64',
+ char='Q',
+ w_box_type = space.gettypefor(interp_boxes.W_ULongLongBox),
+ )
+ self.w_stringdtype = W_Dtype(
+ types.StringType(0),
+ 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(0),
+ 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_longlongdtype, self.w_ulonglongdtype,
+ self.w_float32dtype,
+ self.w_float64dtype, self.w_stringdtype, self.w_unicodedtype,
+ self.w_voiddtype,
]
self.dtypes_by_num_bytes = sorted(
(dtype.itemtype.get_element_size(), dtype)
for dtype in self.builtin_dtypes
)
+ self.dtypes_by_name = {}
+ for dtype in 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
+ 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)
+ 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_ulonglongdtype,
+ '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
@@ -42,24 +42,65 @@
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(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):
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)
@@ -95,17 +136,20 @@
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
+ element_size = jit.promote(self.element_size)
+ arr.offset = self.offset + x * element_size
+ arr.element_size = element_size
return arr
def next_no_increase(self, shapelen):
@@ -152,7 +196,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
@@ -4,17 +4,16 @@
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.module.micronumpy import (interp_ufuncs, interp_dtype, interp_boxes,
signature, support, loop)
-from pypy.module.micronumpy.strides import (calculate_slice_strides,
- shape_agreement, find_shape_and_elems, get_shape_from_iterable,
- calc_new_strides, to_coords)
-from dot import multidim_dot, match_dot_shapes
+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, ViewIterator, Chunks, RecordChunk)
+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
from pypy.tool.sourcetools import func_with_new_name
-from pypy.rlib.rstring import StringBuilder
-from pypy.module.micronumpy.interp_iter import (ArrayIterator,
- SkipLastAxisIterator, Chunk, ViewIterator)
-from pypy.module.micronumpy.appbridge import get_appbridge_cache
count_driver = jit.JitDriver(
@@ -47,7 +46,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 +78,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):
@@ -223,8 +222,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,
@@ -243,7 +241,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):
@@ -251,13 +249,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)
@@ -277,7 +278,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):
@@ -318,6 +319,8 @@
""" 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 shape_len == 0:
raise OperationError(space.w_IndexError, space.wrap(
@@ -343,34 +346,41 @@
@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]))]
- return [Chunk(*space.decode_index4(w_item, self.shape[i])) for i, w_item in
- enumerate(space.fixedview(w_idx))]
+ return Chunks([Chunk(*space.decode_index4(w_idx, self.shape[0]))])
+ return Chunks([Chunk(*space.decode_index4(w_item, self.shape[i])) for i, w_item in
+ enumerate(space.fixedview(w_idx))])
- 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()
@@ -380,7 +390,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)
@@ -390,23 +400,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()):
@@ -416,7 +409,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()
@@ -434,26 +444,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):
- 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)
@@ -470,12 +463,13 @@
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):
concrete = self.get_concrete()
- # Since we got to here, prod(new_shape) == self.size
+ # Since we got to here, prod(new_shape) == self.get_size()
new_strides = calc_new_strides(new_shape, concrete.shape,
concrete.strides, concrete.order)
if new_strides:
@@ -506,7 +500,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])
@@ -525,7 +519,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()
@@ -604,8 +598,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()
@@ -644,6 +637,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
@@ -665,8 +663,7 @@
"""
Intermediate class representing a literal.
"""
- size = 1
- _attrs_ = ["dtype", "value", "shape"]
+ _attrs_ = ["dtype", "value", "shape", "size"]
def __init__(self, dtype, value):
self.shape = []
@@ -674,6 +671,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
@@ -691,8 +689,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
@@ -705,6 +702,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,
@@ -712,7 +710,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
@@ -740,7 +738,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):
@@ -752,7 +749,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
@@ -787,7 +784,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
@@ -815,15 +811,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.itemsize = dtype.itemtype.get_element_size()
+ self.s = StringBuilder(child.size * self.itemsize)
+ 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)
@@ -897,13 +908,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:
@@ -922,13 +933,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
@@ -936,8 +948,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()
@@ -985,9 +997,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)
@@ -1002,7 +1014,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
@@ -1016,14 +1028,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):
@@ -1038,12 +1051,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 *= sh
if self.order == 'C':
strides.reverse()
@@ -1071,14 +1085,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)
@@ -1086,18 +1102,13 @@
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,
@@ -1131,28 +1142,28 @@
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
+ arr = W_NDimArray(shape[:], dtype=dtype, order=order)
shapelen = len(shape)
- arr_iter = ArrayIterator(arr.size)
+ 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
@@ -1161,22 +1172,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)
@@ -1224,13 +1235,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
@@ -1297,6 +1308,7 @@
nbytes = GetSetProperty(BaseArray.descr_get_nbytes),
T = GetSetProperty(BaseArray.descr_get_transpose),
+ transpose = interp2app(BaseArray.descr_get_transpose),
flat = GetSetProperty(BaseArray.descr_get_flatiter),
ravel = interp2app(BaseArray.descr_ravel),
item = interp2app(BaseArray.descr_item),
@@ -1315,6 +1327,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),
@@ -1337,7 +1350,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):
@@ -1352,7 +1365,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)
@@ -1370,28 +1383,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
@@ -1402,27 +1413,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):
@@ -1430,9 +1442,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,10 +74,14 @@
raise OperationError(space.w_ValueError, space.wrap(
"string is smaller than requested size"))
- a = W_NDimArray(count, [count], dtype=dtype)
+ a = W_NDimArray([count], dtype=dtype)
+ ai = a.create_iter()
for i in range(count):
- val = dtype.itemtype.runpack_str(s[i*itemsize:i*itemsize + itemsize])
- a.dtype.setitem(a.storage, i, val)
+ start = i*itemsize
+ assert start >= 0
+ val = dtype.itemtype.runpack_str(s[start:start + itemsize])
+ a.dtype.setitem(a, ai.offset, val)
+ ai = ai.next(1)
return space.wrap(a)
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)
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
@@ -143,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]
@@ -321,7 +320,23 @@
assert isinstance(arr, ResultArray)
offset = frame.get_final_iter().offset
- arr.left.setitem(offset, self.right.eval(frame, arr.right))
+ res = arr.left
+ if frame.first_iteration:
+ jit.assert_aligned(res, offset)
+ res.setitem(offset, self.right.eval(frame, arr.right))
+
+class ToStringSignature(Call1):
+ def __init__(self, dtype, child):
+ Call1.__init__(self, None, 'tostring', dtype, child)
+
+ 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.itemsize):
+ arr.s.append(arr.res_casted[i])
class BroadcastLeft(Call2):
def _invent_numbering(self, cache, allnumbers):
@@ -389,7 +404,10 @@
assert isinstance(arr, Call2)
ofs = frame.iterators[0].offset
- arr.left.setitem(ofs, self.right.eval(frame, arr.right).convert_to(
+ res = arr.left
+ if frame.first_iteration:
+ jit.assert_aligned(res, ofs)
+ res.setitem(ofs, self.right.eval(frame, arr.right).convert_to(
self.calc_dtype))
def debug_repr(self):
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
@@ -38,22 +38,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
@@ -21,8 +21,8 @@
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 +40,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 +57,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,6 @@
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
-
+from pypy.module.micronumpy.interp_dtype import nonnative_byteorder_prefix
+from pypy.interpreter.gateway import interp2app
class AppTestDtypes(BaseNumpyAppTest):
def test_dtype(self):
@@ -12,7 +13,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 +57,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 +85,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):
@@ -182,16 +186,31 @@
class AppTestTypes(BaseNumpyAppTest):
+ def setup_class(cls):
+ BaseNumpyAppTest.setup_class.im_func(cls)
+ cls.w_non_native_prefix = cls.space.wrap(nonnative_byteorder_prefix)
+ 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))
+
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)
@@ -402,10 +421,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)
@@ -425,9 +463,86 @@
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
+ assert dtype('i8') == dtype('<i8')# XXX should be equal == dtype(long)
+ assert dtype(self.non_native_prefix + 'i8') != dtype('i8')
+
+ 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'))
+
+ 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
+
+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'],
+ })
+
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))
@@ -1037,7 +1045,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)
@@ -1487,6 +1495,7 @@
a = array((range(10), range(20, 30)))
b = a.T
assert(b[:, 0] == a[0, :]).all()
+ assert (a.transpose() == b).all()
def test_flatiter(self):
from _numpypy import array, flatiter, arange
@@ -1543,6 +1552,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)
@@ -1817,6 +1827,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):
@@ -1862,3 +1878,44 @@
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)'
diff --git a/pypy/module/micronumpy/test/test_zjit.py b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -503,7 +503,7 @@
dtype = float64_dtype
else:
dtype = int32_dtype
- ar = W_NDimArray(n, [n], dtype=dtype)
+ ar = W_NDimArray([n], dtype=dtype)
i = 0
while i < n:
ar.get_concrete().setitem(i, int32_dtype.box(7))
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})
def simple_unary_op(func):
specialize.argtype(1)(func)
@@ -60,6 +65,15 @@
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
@@ -74,7 +88,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)
@@ -95,32 +109,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):
+ 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
@@ -204,6 +227,17 @@
def min(self, v1, v2):
return min(v1, v2)
+class NonNativePrimitive(Primitive):
+ _mixin_ = True
+
+ def _read(self, storage, width, i, offset):
+ return byteswap(Primitive._read(self, storage, width, i, offset))
+
+ def _write(self, storage, width, i, offset, value):
+ Primitive._write(self, storage, width, i, offset, byteswap(value))
+
+ def pack_str(self, box):
+ return struct.pack(self.format_code, byteswap(self.unbox(box)))
class Bool(BaseType, Primitive):
T = lltype.Bool
@@ -232,8 +266,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)
@@ -257,15 +290,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)
@@ -329,68 +365,117 @@
def invert(self, v):
return ~v
+class NonNativeInteger(NonNativePrimitive, Integer):
+ _mixin_ = True
+
class Int8(BaseType, Integer):
T = rffi.SIGNEDCHAR
BoxType = interp_boxes.W_Int8Box
format_code = "b"
+NonNativeInt8 = Int8
class UInt8(BaseType, Integer):
T = rffi.UCHAR
BoxType = interp_boxes.W_UInt8Box
format_code = "B"
+NonNativeUInt8 = UInt8
class Int16(BaseType, Integer):
T = rffi.SHORT
BoxType = interp_boxes.W_Int16Box
format_code = "h"
+class NonNativeInt16(BaseType, NonNativeInteger):
+ T = rffi.SHORT
+ BoxType = interp_boxes.W_Int16Box
+ format_code = "h"
+
class UInt16(BaseType, Integer):
T = rffi.USHORT
BoxType = interp_boxes.W_UInt16Box
format_code = "H"
+class NonNativeUInt16(BaseType, NonNativeInteger):
+ T = rffi.USHORT
+ BoxType = interp_boxes.W_UInt16Box
+ format_code = "H"
+
class Int32(BaseType, Integer):
T = rffi.INT
BoxType = interp_boxes.W_Int32Box
format_code = "i"
+class NonNativeInt32(BaseType, NonNativeInteger):
+ T = rffi.INT
+ BoxType = interp_boxes.W_Int32Box
+ format_code = "i"
+
class UInt32(BaseType, Integer):
T = rffi.UINT
BoxType = interp_boxes.W_UInt32Box
format_code = "I"
+class NonNativeUInt32(BaseType, NonNativeInteger):
+ T = rffi.UINT
+ BoxType = interp_boxes.W_UInt32Box
+ format_code = "I"
+
class Long(BaseType, Integer):
T = rffi.LONG
BoxType = interp_boxes.W_LongBox
format_code = "l"
+class NonNativeLong(BaseType, NonNativeInteger):
+ T = rffi.LONG
+ BoxType = interp_boxes.W_LongBox
+ format_code = "l"
+
class ULong(BaseType, Integer):
T = rffi.ULONG
BoxType = interp_boxes.W_ULongBox
format_code = "L"
+class NonNativeULong(BaseType, NonNativeInteger):
+ T = rffi.ULONG
+ BoxType = interp_boxes.W_ULongBox
+ format_code = "L"
+
class Int64(BaseType, Integer):
T = rffi.LONGLONG
BoxType = interp_boxes.W_Int64Box
format_code = "q"
+class NonNativeInt64(BaseType, NonNativeInteger):
+ T = rffi.LONGLONG
+ BoxType = interp_boxes.W_Int64Box
+ format_code = "q"
+
+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):
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):
+ T = rffi.ULONGLONG
+ BoxType = interp_boxes.W_UInt64Box
+ format_code = "Q"
+
+ _coerce = func_with_new_name(_uint64_coerce, '_coerce')
class Float(Primitive):
_mixin_ = True
@@ -399,8 +484,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)
@@ -515,13 +600,126 @@
def isinf(self, v):
return rfloat.isinf(v)
+class NonNativeFloat(NonNativePrimitive, Float):
+ _mixin_ = True
class Float32(BaseType, Float):
T = rffi.FLOAT
BoxType = interp_boxes.W_Float32Box
format_code = "f"
+class NonNativeFloat32(BaseType, NonNativeFloat):
+ T = rffi.FLOAT
+ BoxType = interp_boxes.W_Float32Box
+ format_code = "f"
+
class Float64(BaseType, Float):
T = rffi.DOUBLE
BoxType = interp_boxes.W_Float64Box
format_code = "d"
+
+class NonNativeFloat64(BaseType, NonNativeFloat):
+ T = rffi.DOUBLE
+ BoxType = interp_boxes.W_Float64Box
+ format_code = "d"
+
+class CompositeType(BaseType):
+ 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
+
+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(CompositeType):
+ T = lltype.Char
+
+ def read(self, arr, width, i, offset):
+ return interp_boxes.W_VoidBox(arr, i)
+
+ @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)
+
+ @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/pypyjit/__init__.py b/pypy/module/pypyjit/__init__.py
--- a/pypy/module/pypyjit/__init__.py
+++ b/pypy/module/pypyjit/__init__.py
@@ -13,6 +13,7 @@
'ResOperation': 'interp_resop.WrappedOp',
'DebugMergePoint': 'interp_resop.DebugMergePoint',
'Box': 'interp_resop.WrappedBox',
+ 'PARAMETER_DOCS': 'space.wrap(pypy.rlib.jit.PARAMETER_DOCS)',
}
def setup_after_space_initialization(self):
diff --git a/pypy/module/pypyjit/test/test_jit_setup.py b/pypy/module/pypyjit/test/test_jit_setup.py
--- a/pypy/module/pypyjit/test/test_jit_setup.py
+++ b/pypy/module/pypyjit/test/test_jit_setup.py
@@ -45,6 +45,12 @@
pypyjit.set_compile_hook(None)
pypyjit.set_param('default')
+ def test_doc(self):
+ import pypyjit
+ d = pypyjit.PARAMETER_DOCS
+ assert type(d) is dict
+ assert 'threshold' in d
+
def test_interface_residual_call():
space = gettestobjspace(usemodules=['pypyjit'])
diff --git a/pypy/module/test_lib_pypy/test_datetime.py b/pypy/module/test_lib_pypy/test_datetime.py
--- a/pypy/module/test_lib_pypy/test_datetime.py
+++ b/pypy/module/test_lib_pypy/test_datetime.py
@@ -1,7 +1,10 @@
"""Additional tests for datetime."""
+import py
+
import time
import datetime
+import copy
import os
def test_utcfromtimestamp():
@@ -26,3 +29,18 @@
def test_utcfromtimestamp_microsecond():
dt = datetime.datetime.utcfromtimestamp(0)
assert isinstance(dt.microsecond, int)
+
+
+def test_integer_args():
+ with py.test.raises(TypeError):
+ datetime.datetime(10, 10, 10.)
+ with py.test.raises(TypeError):
+ datetime.datetime(10, 10, 10, 10, 10.)
+ with py.test.raises(TypeError):
+ datetime.datetime(10, 10, 10, 10, 10, 10.)
+
+def test_utcnow_microsecond():
+ dt = datetime.datetime.utcnow()
+ assert type(dt.microsecond) is int
+
+ copy.copy(dt)
\ No newline at end of file
diff --git a/pypy/rlib/clibffi.py b/pypy/rlib/clibffi.py
--- a/pypy/rlib/clibffi.py
+++ b/pypy/rlib/clibffi.py
@@ -228,6 +228,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
@@ -238,7 +238,7 @@
self = jit.promote(self)
if argchain.numargs != len(self.argtypes):
raise TypeError, 'Wrong number of arguments: %d expected, got %d' %\
- (argchain.numargs, len(self.argtypes))
+ (len(self.argtypes), argchain.numargs)
ll_args = self._prepare()
i = 0
arg = argchain.first
@@ -424,6 +424,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):
@@ -434,3 +439,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/rarithmetic.py b/pypy/rlib/rarithmetic.py
--- a/pypy/rlib/rarithmetic.py
+++ b/pypy/rlib/rarithmetic.py
@@ -513,3 +513,31 @@
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)
+ if T != rffi.LONGLONG and T != rffi.ULONGLONG and T != rffi.UINT:
+ arg = rffi.cast(lltype.Signed, arg)
+ # XXX we cannot do arithmetics on small ints
+ 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:
+ a, b, c, d = arg & 0xFF, arg & 0xFF00, arg & 0xFF0000, arg & 0xFF000000
+ res = (a << 24) | (b << 8) | (c >> 8) | (d >> 24)
+ elif rffi.sizeof(T) == 8:
+ a, b, c, d = arg & 0xFF, arg & 0xFF00, arg & 0xFF0000, arg & 0xFF000000
+ e, f, g, h = (arg & (0xFF << 32), arg & (0xFF << 40),
+ arg & (0xFF << 48), arg & (r_uint(0xFF) << 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/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/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
@@ -374,3 +374,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/tool/release/package.py b/pypy/tool/release/package.py
--- a/pypy/tool/release/package.py
+++ b/pypy/tool/release/package.py
@@ -126,7 +126,7 @@
zf.close()
else:
archive = str(builddir.join(name + '.tar.bz2'))
- if sys.platform == 'darwin':
+ if sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
e = os.system('tar --numeric-owner -cvjf ' + archive + " " + name)
else:
e = os.system('tar --owner=root --group=root --numeric-owner -cvjf ' + archive + " " + name)
diff --git a/pypy/translator/goal/app_main.py b/pypy/translator/goal/app_main.py
--- a/pypy/translator/goal/app_main.py
+++ b/pypy/translator/goal/app_main.py
@@ -139,8 +139,14 @@
items = pypyjit.defaults.items()
items.sort()
for key, value in items:
- print ' --jit %s=N %s%s (default %s)' % (
- key, ' '*(18-len(key)), pypyjit.PARAMETER_DOCS[key], value)
+ prefix = ' --jit %s=N %s' % (key, ' '*(18-len(key)))
+ doc = '%s (default %s)' % (pypyjit.PARAMETER_DOCS[key], value)
+ while len(doc) > 51:
+ i = doc[:51].rfind(' ')
+ print prefix + doc[:i]
+ doc = doc[i+1:]
+ prefix = ' '*len(prefix)
+ print prefix + doc
print ' --jit off turn off the JIT'
def print_version(*args):
More information about the pypy-commit
mailing list