[pypy-commit] pypy default: test, fix corner case in the fix to #3149 that broke lib-python test

2020-01-19 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r98560:dc4c8f284eee
Date: 2020-01-20 13:44 +1100
http://bitbucket.org/pypy/pypy/changeset/dc4c8f284eee/

Log:test, fix corner case in the fix to #3149 that broke lib-python test

diff --git a/pypy/module/__builtin__/abstractinst.py 
b/pypy/module/__builtin__/abstractinst.py
--- a/pypy/module/__builtin__/abstractinst.py
+++ b/pypy/module/__builtin__/abstractinst.py
@@ -232,7 +232,10 @@
 # many-years issue report: https://bugs.python.org/issue12029), and
 # there are probably tests, so we won't call abstract_issubclass_w()
 # either in PyPy3.
-return abstract_issubclass_w(space, w_cls1, w_cls2, True)
+try:
+return abstract_issubclass_w(space, w_cls1, w_cls2, True)
+except OperationError as e:
+return False
 
 # 
 # App-level interface
diff --git a/pypy/module/__builtin__/test/test_abstractinst.py 
b/pypy/module/__builtin__/test/test_abstractinst.py
--- a/pypy/module/__builtin__/test/test_abstractinst.py
+++ b/pypy/module/__builtin__/test/test_abstractinst.py
@@ -259,3 +259,35 @@
 raise Special, ValueError()
 except ValueError:
 pass
+
+def test_exception_bad_subclasscheck(self):
+import sys
+class Meta(type):
+def __subclasscheck__(cls, subclass):
+raise ValueError()
+
+class MyException(Exception):
+__metaclass__ = Meta
+pass
+
+try:
+raise KeyError()
+except MyException, e:
+assert False, "exception should not be a MyException"
+except KeyError:
+pass
+except:
+assert False, "Should have raised KeyError"
+else:
+assert False, "Should have raised KeyError"
+
+def g():
+try:
+return g()
+except RuntimeError:
+return sys.exc_info()
+e, v, tb = g()
+assert e is RuntimeError, str(e)
+assert "maximum recursion depth exceeded" in str(v)
+
+ 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for importing with unicode in sys.path (issue 3112)

2019-12-01 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r98203:4454c23fcc36
Date: 2019-12-01 18:41 +0200
http://bitbucket.org/pypy/pypy/changeset/4454c23fcc36/

Log:test, fix for importing with unicode in sys.path (issue 3112)

diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -448,14 +448,19 @@
 return w_loader
 
 def _getimporter(space, w_pathitem):
-# the function 'imp._getimporter' is a pypy-only extension
+# 'imp._getimporter' is somewhat like CPython's get_path_importer
 w_path_importer_cache = space.sys.get("path_importer_cache")
 w_importer = space.finditem(w_path_importer_cache, w_pathitem)
 if w_importer is None:
 space.setitem(w_path_importer_cache, w_pathitem, space.w_None)
 for w_hook in space.unpackiterable(space.sys.get("path_hooks")):
+w_pathbytes = w_pathitem
+if space.isinstance_w(w_pathitem, space.w_unicode):
+from pypy.module.sys.interp_encoding import 
getfilesystemencoding
+w_pathbytes = space.call_method(space.w_unicode, 'encode',
+ w_pathitem, getfilesystemencoding(space))
 try:
-w_importer = space.call_function(w_hook, w_pathitem)
+w_importer = space.call_function(w_hook, w_pathbytes)
 except OperationError as e:
 if not e.match(space, space.w_ImportError):
 raise
diff --git a/pypy/module/imp/test/test_app.py b/pypy/module/imp/test/test_app.py
--- a/pypy/module/imp/test/test_app.py
+++ b/pypy/module/imp/test/test_app.py
@@ -4,7 +4,8 @@
 
 class AppTestImpModule:
 spaceconfig = {
-'usemodules': ['binascii', 'imp', 'itertools', 'time', 'struct'],
+'usemodules': ['binascii', 'imp', 'itertools', 'time', 'struct',
+   'zipimport'],
 }
 
 def setup_class(cls):
@@ -246,3 +247,14 @@
 assert marshal.loads == 42
 
 marshal.loads = old
+
+def test_unicode_in_sys_path(self):
+# issue 3112: when _getimporter calls
+# for x in sys.path: for h in sys.path_hooks: h(x)
+# make sure x is properly encoded
+import sys
+import zipimport #  installs a sys.path_hook
+if sys.getfilesystemencoding().lower() == 'utf-8':
+sys.path.insert(0, u'\xef')
+with raises(ImportError):
+import impossible_module
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix CDLL(None) on win32 should raise TypeError

2019-09-17 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r97505:5605eba9586b
Date: 2019-09-17 10:04 +0300
http://bitbucket.org/pypy/pypy/changeset/5605eba9586b/

Log:test, fix CDLL(None) on win32 should raise TypeError

diff --git a/pypy/module/_rawffi/interp_rawffi.py 
b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -239,7 +239,11 @@
 except OSError as e:
 raise wrap_oserror(space, e)
 
-@unwrap_spec(name='fsencode_or_none')
+if _MS_WINDOWS:
+name_spec = 'fsencode'
+else:
+name_spec = 'fsencode_or_none'
+@unwrap_spec(name=name_spec)
 def descr_new_cdll(space, w_type, name):
 cdll = open_cdll(space, name)
 return W_CDLL(space, name, cdll)
diff --git a/pypy/module/_rawffi/test/test__rawffi.py 
b/pypy/module/_rawffi/test/test__rawffi.py
--- a/pypy/module/_rawffi/test/test__rawffi.py
+++ b/pypy/module/_rawffi/test/test__rawffi.py
@@ -262,14 +262,17 @@
 raise AssertionError("did not fail??")
 
 def test_libload_None(self):
-if self.iswin32:
-skip("unix specific")
 import _rawffi
 # this should return *all* loaded libs, dlopen(NULL)
-dll = _rawffi.CDLL(None)
-func = dll.ptr('rand', [], 'i')
-res = func()
-assert res[0] != 0
+try:
+dll = _rawffi.CDLL(None)
+except TypeError:
+if not self.iswin32:
+raise
+else:
+func = dll.ptr('rand', [], 'i')
+res = func()
+assert res[0] != 0
 
 def test_libc_load(self):
 import _rawffi
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix buffer_attach with a CBuffer. Alos add comment that except segfaults

2019-06-26 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r96866:cc5fc1bad809
Date: 2019-06-26 14:15 +0300
http://bitbucket.org/pypy/pypy/changeset/cc5fc1bad809/

Log:test, fix buffer_attach with a CBuffer. Alos add comment that except
segfaults

diff --git a/pypy/module/cpyext/bufferobject.py 
b/pypy/module/cpyext/bufferobject.py
--- a/pypy/module/cpyext/bufferobject.py
+++ b/pypy/module/cpyext/bufferobject.py
@@ -5,6 +5,7 @@
 cpython_api, Py_ssize_t, cpython_struct, bootstrap_function, slot_function,
 PyObjectFields, PyObject)
 from pypy.module.cpyext.pyobject import make_typedescr, decref, make_ref
+from pypy.module.cpyext.buffer import CBuffer
 from pypy.module.array.interp_array import ArrayBuffer
 from pypy.objspace.std.bufferobject import W_Buffer
 
@@ -33,7 +34,7 @@
 
 def buffer_attach(space, py_obj, w_obj, w_userdata=None):
 """
-Fills a newly allocated PyBufferObject with the given (str) buffer object.
+Fills a newly allocated PyBufferObject with the given buffer object.
 """
 py_buf = rffi.cast(PyBufferObject, py_obj)
 py_buf.c_b_offset = 0
@@ -60,7 +61,17 @@
 py_buf.c_b_base = make_ref(space, w_base)
 py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, buf.w_array._charbuf_start())
 py_buf.c_b_size = buf.getlength()
+elif isinstance(buf, CBuffer):
+py_buf.c_b_base = make_ref(space, buf.view.w_obj)
+py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, buf.view.ptr)
+py_buf.c_b_size = buf.getlength()
 else:
+# Raising in attach will segfault.
+# It would be nice if we could handle the error more gracefully
+# with something like this
+# py_buf.c_b_base = lltype.nullptr(PyObject.TO)
+# py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, 0)
+# py_buf.c_b_size = buf.getlength()
 raise oefmt(space.w_NotImplementedError, "buffer flavor not supported")
 
 
diff --git a/pypy/module/cpyext/test/array.c b/pypy/module/cpyext/test/array.c
--- a/pypy/module/cpyext/test/array.c
+++ b/pypy/module/cpyext/test/array.c
@@ -2573,6 +2573,11 @@
 Py_RETURN_NONE;
 }
 
+static PyObject *
+passthrough(PyObject *self, PyObject* args) {
+Py_INCREF(args);
+return args;
+} 
 /*** Install Module **/
 
 static PyMethodDef a_methods[] = {
@@ -2584,6 +2589,7 @@
 {"same_dealloc",   (PyCFunction)same_dealloc, METH_VARARGS, NULL},
 {"getitem", (PyCFunction)getitem, METH_VARARGS, NULL},
 {"subclass_with_attribute", (PyCFunction)subclass_with_attribute, 
METH_VARARGS, NULL},
+{"passthrough", (PyCFunction)passthrough, METH_O, NULL},
 {NULL, NULL, 0, NULL}/* Sentinel */
 };
 
diff --git a/pypy/module/cpyext/test/test_arraymodule.py 
b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -79,6 +79,9 @@
 assert str(buffer('a') + arr) == "a" + expected
 # python2 special cases empty-buffer + obj
 assert str(buffer('') + arr) == "array('i', [1, 2, 3, 4])"
+# make sure buffer_attach is called
+buf2 = module.passthrough(buf)
+assert str(buf2) == str(buf)
 
 def test_releasebuffer(self):
 module = self.import_module(name='array')
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for win32.

2019-02-26 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r96172:25b25fdd6d5c
Date: 2019-02-26 11:18 +0200
http://bitbucket.org/pypy/pypy/changeset/25b25fdd6d5c/

Log:test, fix for win32.

The asserts should be py3.6 only, and theny point to unfinished utf8
changes

diff --git a/pypy/module/posix/test/test_posix2.py 
b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -1190,6 +1190,11 @@
 os = self.posix
 with open(self.path, "w") as f:
 f.write("this is a rename test")
+str_name = str(self.pdir) + '/test_rename.txt'
+os.rename(self.path, str_name)
+with open(str_name) as f:
+assert f.read() == 'this is a rename test'
+os.rename(str_name, self.path)
 unicode_name = str(self.udir) + u'/test\u03be.txt'
 os.rename(self.path, unicode_name)
 with open(unicode_name) as f:
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -1273,8 +1273,6 @@
 win32traits = make_win32_traits(traits)
 path1 = traits.as_str0(path1)
 path2 = traits.as_str0(path2)
-assert isinstance(path1, unicode)
-assert isinstance(path2, unicode)
 if not win32traits.MoveFileEx(path1, path2, 0):
 raise rwin32.lastSavedWindowsError()
 
@@ -1285,8 +1283,6 @@
 win32traits = make_win32_traits(traits)
 path1 = traits.as_str0(path1)
 path2 = traits.as_str0(path2)
-assert isinstance(path1, unicode)
-assert isinstance(path2, unicode)
 ret = win32traits.MoveFileEx(path1, path2,
  win32traits.MOVEFILE_REPLACE_EXISTING)
 if not ret:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for issue #2792 (resizing non-initialized PyTuple), also add fast-path optimization

2018-04-07 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r94260:5014ee1237e8
Date: 2018-04-07 21:40 +0300
http://bitbucket.org/pypy/pypy/changeset/5014ee1237e8/

Log:test, fix for issue #2792 (resizing non-initialized PyTuple), also
add fast-path optimization

diff --git a/pypy/module/cpyext/test/test_tupleobject.py 
b/pypy/module/cpyext/test/test_tupleobject.py
--- a/pypy/module/cpyext/test/test_tupleobject.py
+++ b/pypy/module/cpyext/test/test_tupleobject.py
@@ -67,6 +67,18 @@
 assert space.int_w(space.getitem(w_tuple, space.wrap(i))) == 42 + i
 decref(space, ar[0])
 
+py_tuple = state.ccall("PyTuple_New", 1)
+ar[0] = py_tuple
+api._PyTuple_Resize(ar, 1)
+assert api.PyTuple_Size(ar[0]) == 1
+decref(space, ar[0])
+
+py_tuple = state.ccall("PyTuple_New", 1)
+ar[0] = py_tuple
+api._PyTuple_Resize(ar, 5)
+assert api.PyTuple_Size(ar[0]) == 5
+decref(space, ar[0])
+
 lltype.free(ar, flavor='raw')
 
 def test_setitem(self, space, api):
diff --git a/pypy/module/cpyext/tupleobject.py 
b/pypy/module/cpyext/tupleobject.py
--- a/pypy/module/cpyext/tupleobject.py
+++ b/pypy/module/cpyext/tupleobject.py
@@ -187,6 +187,8 @@
 PyErr_BadInternalCall(space)
 oldref = rffi.cast(PyTupleObject, ref)
 oldsize = oldref.c_ob_size
+if oldsize == newsize:
+return 0
 ptup = state.ccall("PyTuple_New", newsize)
 if not ptup:
 state.check_and_raise_exception(always=True)
@@ -199,8 +201,9 @@
 to_cp = newsize
 for i in range(to_cp):
 ob = oldref.c_ob_item[i]
-incref(space, ob)
-newref.c_ob_item[i] = ob
+if ob:
+incref(space, ob)
+newref.c_ob_item[i] = ob
 except:
 decref(space, p_ref[0])
 p_ref[0] = lltype.nullptr(PyObject.TO)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for PySequence_ITEM accessing sq_item, not mp_subscr. Both are __getitem__, the second takes precedence

2018-03-27 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r94150:abd1818fcde4
Date: 2018-03-28 00:02 +0300
http://bitbucket.org/pypy/pypy/changeset/abd1818fcde4/

Log:test, fix for PySequence_ITEM accessing sq_item, not mp_subscr. Both
are __getitem__, the second takes precedence

diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -5,7 +5,8 @@
 from pypy.objspace.std.listobject import (
 ListStrategy, UNROLL_CUTOFF, W_ListObject, ObjectListStrategy)
 from pypy.module.cpyext.api import (
-cpython_api, CANNOT_FAIL, CONST_STRING, Py_ssize_t, PyObject, PyObjectP)
+cpython_api, CANNOT_FAIL, CONST_STRING, Py_ssize_t, PyObject, PyObjectP,
+generic_cpy_call)
 from pypy.module.cpyext.pyobject import PyObject, make_ref, from_ref
 from pypy.module.cpyext.pyobject import as_pyobj, incref
 from rpython.rtyper.lltypesystem import rffi, lltype
@@ -145,21 +146,26 @@
 # XXX we should call Py*_GET_ITEM() instead of Py*_GetItem()
 # from here, but we cannot because we are also called from
 # PySequence_GetItem()
+py_obj = as_pyobj(space, w_obj)
 if isinstance(w_obj, tupleobject.W_TupleObject):
 from pypy.module.cpyext.tupleobject import PyTuple_GetItem
-py_obj = as_pyobj(space, w_obj)
 py_res = PyTuple_GetItem(space, py_obj, i)
 incref(space, py_res)
 keepalive_until_here(w_obj)
 return py_res
 if isinstance(w_obj, W_ListObject):
 from pypy.module.cpyext.listobject import PyList_GetItem
-py_obj = as_pyobj(space, w_obj)
 py_res = PyList_GetItem(space, py_obj, i)
 incref(space, py_res)
 keepalive_until_here(w_obj)
 return py_res
-return make_ref(space, space.getitem(w_obj, space.newint(i)))
+
+as_sequence = py_obj.c_ob_type.c_tp_as_sequence
+if not as_sequence or not as_sequence.c_sq_item:
+raise oefmt(space.w_TypeError,
+"'%T' object does not support indexing", w_obj)
+ret = generic_cpy_call(space, as_sequence.c_sq_item, w_obj, i)
+return make_ref(space, ret)
 
 @cpython_api([PyObject, Py_ssize_t], PyObject, result_is_ll=True)
 def PySequence_GetItem(space, w_obj, i):
diff --git a/pypy/module/cpyext/test/array.c b/pypy/module/cpyext/test/array.c
--- a/pypy/module/cpyext/test/array.c
+++ b/pypy/module/cpyext/test/array.c
@@ -2202,6 +2202,16 @@
 Py_RETURN_NONE;
 };
 
+static PyObject *
+getitem(PyObject* self, PyObject * args) {
+PyObject * obj;
+int i;
+if (!PyArg_ParseTuple(args, "Oi", , )) {
+return NULL;
+}
+return PySequence_ITEM(obj, i);
+}
+
 PyDoc_STRVAR(module_doc,
 "This module defines an object type which can efficiently represent\n\
 an array of basic values: characters, integers, floating point\n\
@@ -2491,6 +2501,7 @@
 {"get_releasebuffer_cnt",   (PyCFunction)get_releasebuffer_cnt, 
METH_NOARGS, NULL},
 {"create_and_release_buffer",   (PyCFunction)create_and_release_buffer, 
METH_O, NULL},
 {"same_dealloc",   (PyCFunction)same_dealloc, METH_VARARGS, NULL},
+{"getitem", (PyCFunction)getitem, METH_VARARGS, NULL},
 {NULL, NULL, 0, NULL}/* Sentinel */
 };
 
diff --git a/pypy/module/cpyext/test/test_arraymodule.py 
b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -172,3 +172,15 @@
 fd = BytesIO()
 # only test that it works
 fd.write(a)
+
+def test_getitem_via_PySequence_GetItem(self):
+module = self.import_module(name='array')
+a = module.array('i', range(10))
+# call via tp_as_mapping.mp_subscript
+assert 5 == a[-5]
+# PySequence_ITEM used to call space.getitem() which
+# prefers tp_as_mapping.mp_subscript over tp_as_sequence.sq_item
+# Now fixed so this test raises (array_item does not add len(a),
+# array_subscr does)
+raises(IndexError, module.getitem, a, -5)
+
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for cpython2 optimization where "buffer('') + obj is obj"

2018-03-27 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r94151:d044ce2d7d53
Date: 2018-03-28 00:14 +0300
http://bitbucket.org/pypy/pypy/changeset/d044ce2d7d53/

Log:test, fix for cpython2 optimization where "buffer('') + obj is obj"

diff --git a/pypy/module/cpyext/test/test_arraymodule.py 
b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -76,7 +76,9 @@
 else:
 expected = '\x01\0\0\0' '\x02\0\0\0' '\x03\0\0\0' '\x04\0\0\0'
 assert str(buf) == expected
-assert str(buffer('') + arr) == expected
+assert str(buffer('a') + arr) == "a" + expected
+# python2 special cases empty-buffer + obj
+assert str(buffer('') + arr) == "array('i', [1, 2, 3, 4])"
 
 def test_releasebuffer(self):
 module = self.import_module(name='array')
diff --git a/pypy/objspace/std/bufferobject.py 
b/pypy/objspace/std/bufferobject.py
--- a/pypy/objspace/std/bufferobject.py
+++ b/pypy/objspace/std/bufferobject.py
@@ -89,9 +89,14 @@
 def descr_str(self, space):
 return space.newbytes(self.buf.as_str())
 
-@unwrap_spec(other='bufferstr')
-def descr_add(self, space, other):
-return space.newbytes(self.buf.as_str() + other)
+def descr_add(self, space, w_other):
+try:
+other = w_other.readbuf_w(space)
+except BufferInterfaceNotFound:
+raise oefmt(space.w_TypeError, "bad argument type for built-in 
operation")
+if self.buf.getlength() < 1:
+return w_other
+return space.newbytes(self.buf.as_str() + other.as_str())
 
 def _make_descr__cmp(name):
 def descr__cmp(self, space, w_other):
diff --git a/pypy/objspace/std/test/test_bufferobject.py 
b/pypy/objspace/std/test/test_bufferobject.py
--- a/pypy/objspace/std/test/test_bufferobject.py
+++ b/pypy/objspace/std/test/test_bufferobject.py
@@ -29,9 +29,11 @@
 
 def test_array_buffer(self):
 import array
-b = buffer(array.array("B", [1, 2, 3]))
+arr = array.array("B", [1, 2, 3])
+b = buffer(arr)
 assert len(b) == 3
 assert b[0:3] == "\x01\x02\x03"
+assert buffer('') + arr is arr
 
 def test_nonzero(self):
 assert buffer('\x00')
@@ -51,6 +53,7 @@
 assert buffer('abc') + 'def' == 'abcdef'
 import array
 assert buffer('abc') + array.array('c', 'def') == 'abcdef'
+raises(TypeError, buffer('abc').__add__, 3)
 
 def test_cmp(self):
 assert buffer('ab') != 'ab'
@@ -199,6 +202,9 @@
 raises(TypeError, "buf[MyInt(0):MyInt(5)]")
 
 def test_pypy_raw_address_base(self):
+import sys
+if '__pypy__' not in sys.builtin_module_names:
+skip('PyPy only')
 a = buffer("foobar")._pypy_raw_address()
 assert a != 0
 b = buffer(u"foobar")._pypy_raw_address()
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for os.move, os.replace mixing unicode and ascii file names on win32

2018-03-04 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r93952:8b47f49b6df4
Date: 2018-03-04 13:25 -0800
http://bitbucket.org/pypy/pypy/changeset/8b47f49b6df4/

Log:test, fix for os.move, os.replace mixing unicode and ascii file
names on win32

diff --git a/pypy/module/posix/test/test_posix2.py 
b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -1175,6 +1175,19 @@
 if len(e.value.args) > 2:
 assert e.value.args[2] == "\\foo\\bar\\baz"
 
+def test_rename(self):
+os = self.posix
+with open(self.path, "w") as f:
+f.write("this is a rename test")
+unicode_name = str(self.udir) + u'/test\u03be.txt'
+os.rename(self.path, unicode_name)
+with open(unicode_name) as f:
+assert f.read() == 'this is a rename test'
+os.rename(unicode_name, self.path)
+with open(self.path) as f:
+assert f.read() == 'this is a rename test'
+
+
 
 class AppTestEnvironment(object):
 def setup_class(cls):
diff --git a/rpython/rlib/_os_support.py b/rpython/rlib/_os_support.py
--- a/rpython/rlib/_os_support.py
+++ b/rpython/rlib/_os_support.py
@@ -99,6 +99,13 @@
 return unicode_traits
 else:
 return string_traits
+
+@specialize.argtype(0, 1)
+def _preferred_traits2(path1, path2):
+if _prefer_unicode(path1) or _prefer_unicode(path2):
+return unicode_traits
+else:
+return string_traits
 else:
 @specialize.argtype(0)
 def _prefer_unicode(path):
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -8,7 +8,7 @@
 from rpython.rlib import debug, jit, rstring, rthread, types
 from rpython.rlib._os_support import (
 _CYGWIN, _MACRO_ON_POSIX, UNDERSCORE_ON_WIN32, _WIN32,
-_prefer_unicode, _preferred_traits)
+_prefer_unicode, _preferred_traits, _preferred_traits2)
 from rpython.rlib.objectmodel import (
 specialize, enforceargs, register_replacement_for, NOT_CONSTANT)
 from rpython.rlib.rarithmetic import intmask, widen
@@ -1257,7 +1257,7 @@
 handle_posix_error('rename',
c_rename(_as_bytes0(path1), _as_bytes0(path2)))
 else:
-traits = _preferred_traits(path1)
+traits = _preferred_traits2(path1, path2)
 win32traits = make_win32_traits(traits)
 path1 = traits.as_str0(path1)
 path2 = traits.as_str0(path2)
@@ -1267,7 +1267,7 @@
 @specialize.argtype(0, 1)
 def replace(path1, path2):
 if _WIN32:
-traits = _preferred_traits(path1)
+traits = _preferred_traits2(path1, path2)
 win32traits = make_win32_traits(traits)
 path1 = traits.as_str0(path1)
 path2 = traits.as_str0(path2)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for tzinfo. datetime inherits from date, but date has no tzinfo, monkeypatch alloc appropriately

2018-02-17 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r93827:c7ea666563e3
Date: 2018-02-17 22:19 +0200
http://bitbucket.org/pypy/pypy/changeset/c7ea666563e3/

Log:test, fix for tzinfo. datetime inherits from date, but date has no
tzinfo, monkeypatch alloc appropriately

diff --git a/pypy/module/cpyext/cdatetime.py b/pypy/module/cpyext/cdatetime.py
--- a/pypy/module/cpyext/cdatetime.py
+++ b/pypy/module/cpyext/cdatetime.py
@@ -2,9 +2,10 @@
 from rpython.rtyper.annlowlevel import llhelper
 from rpython.rlib.rarithmetic import widen
 from pypy.module.cpyext.pyobject import (PyObject, make_ref, make_typedescr,
-decref)
+decref, as_pyobj, incref)
 from pypy.module.cpyext.api import (cpython_api, CANNOT_FAIL, cpython_struct,
-PyObjectFields, cts, parse_dir, bootstrap_function, slot_function)
+PyObjectFields, cts, parse_dir, bootstrap_function, slot_function,
+Py_TPFLAGS_HEAPTYPE)
 from pypy.module.cpyext.import_ import PyImport_Import
 from pypy.module.cpyext.typeobject import PyTypeObjectPtr
 from pypy.interpreter.error import OperationError
@@ -128,6 +129,8 @@
 # W_DateTime_Date->tp_dealloc
 make_typedescr(W_DateTime_Date.typedef,
basestruct=PyDateTime_DateTime.TO,
+   alloc=date_or_datetime_allocate,
+   attach=type_attach,
dealloc=date_dealloc,
   )
 
@@ -136,10 +139,33 @@
attach=timedeltatype_attach,
   )
 
+def date_or_datetime_allocate(self, space, w_type, itemcount=0, 
immortal=False):
+# allocates a date or datetime object. datetime has a tzinfo field, date 
does not
+pytype = as_pyobj(space, w_type)
+pytype = rffi.cast(PyTypeObjectPtr, pytype)
+if w_type.name == 'date':
+# XXX we should do this where the 'date' and 'datetime' type is 
instantiated
+pytype.c_tp_basicsize = rffi.sizeof(PyObject.TO)
+size = pytype.c_tp_basicsize
+incref(space, pytype)
+assert size >= rffi.sizeof(PyObject.TO)
+buf = lltype.malloc(rffi.VOIDP.TO, size,
+flavor='raw', zero=True,
+add_memory_pressure=True, immortal=immortal)
+pyobj = rffi.cast(PyObject, buf)
+pyobj.c_ob_refcnt = 1
+#pyobj.c_ob_pypy_link should get assigned very quickly
+pyobj.c_ob_type = pytype
+return pyobj
+
 def type_attach(space, py_obj, w_obj, w_userdata=None):
 '''Fills a newly allocated py_obj from the w_obj
-   Can be called with a datetime, or a time
 '''
+if space.type(w_obj).name == 'date':
+# No tzinfo
+return
+# just make sure, should be removed
+assert py_obj.c_ob_type.c_tp_basicsize > rffi.sizeof(PyObject.TO)
 py_datetime = rffi.cast(PyDateTime_Time, py_obj)
 w_tzinfo = space.getattr(w_obj, space.newtext('tzinfo'))
 if space.is_none(w_tzinfo):
diff --git a/pypy/module/cpyext/test/test_datetime.py 
b/pypy/module/cpyext/test/test_datetime.py
--- a/pypy/module/cpyext/test/test_datetime.py
+++ b/pypy/module/cpyext/test/test_datetime.py
@@ -82,16 +82,6 @@
 date = datetime.datetime.fromtimestamp(0)
 assert space.unwrap(space.str(w_date)) == str(date)
 
-def test_tzinfo(self, space):
-w_tzinfo = space.appexec(
-[], """():
-from datetime import tzinfo
-return tzinfo()
-""")
-assert PyTZInfo_Check(space, w_tzinfo)
-assert PyTZInfo_CheckExact(space, w_tzinfo)
-assert not PyTZInfo_Check(space, space.w_None)
-
 class AppTestDatetime(AppTestCpythonExtensionBase):
 def test_CAPI(self):
 module = self.import_extension('foo', [
@@ -272,3 +262,81 @@
 2000, 6, 6, 6, 6, 6, 6)
 assert module.test_time_macros() == datetime.time(6, 6, 6, 6)
 assert module.test_delta_macros() == datetime.timedelta(6, 6, 6)
+
+def test_tzinfo(self):
+module = self.import_extension('foo', [
+("time_with_tzinfo", "METH_O",
+ """ PyDateTime_IMPORT;
+ return PyDateTimeAPI->Time_FromTime(
+6, 6, 6, 6, args, PyDateTimeAPI->TimeType);
+ """),
+("datetime_with_tzinfo", "METH_O",
+ """ 
+ PyObject * obj;
+ int tzrefcnt = args->ob_refcnt; 
+ PyDateTime_IMPORT;
+ obj = PyDateTimeAPI->DateTime_FromDateAndTime(
+2000, 6, 6, 6, 6, 6, 6, args,
+PyDateTimeAPI->DateTimeType);
+if (!((PyDateTime_DateTime*)obj)->hastzinfo)
+{
+Py_DECREF(obj);
+PyErr_SetString(PyExc_ValueError, "missing tzinfo");
+return NULL;
+}
+if (((PyDateTime_DateTime*)obj)->tzinfo->ob_refcnt <= tzrefcnt)
+{
+Py_DECREF(obj);
+PyErr_SetString(PyExc_ValueError, "tzinfo refcnt not 

[pypy-commit] pypy default: test, fix issue #2677 copy pure c PyBuffer_{From, To}Contiguous from cpython

2017-10-25 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92846:94ab09b73962
Date: 2017-10-25 17:50 +0300
http://bitbucket.org/pypy/pypy/changeset/94ab09b73962/

Log:test, fix issue #2677 copy pure c PyBuffer_{From,To}Contiguous from
cpython

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
@@ -577,6 +577,7 @@
 'PyComplex_AsCComplex', 'PyComplex_FromCComplex',
 
 'PyObject_AsReadBuffer', 'PyObject_AsWriteBuffer', 
'PyObject_CheckReadBuffer',
+'PyBuffer_GetPointer', 'PyBuffer_ToContiguous', 'PyBuffer_FromContiguous',
 
 'PyOS_getsig', 'PyOS_setsig',
 'PyThread_get_thread_ident', 'PyThread_allocate_lock', 
'PyThread_free_lock',
diff --git a/pypy/module/cpyext/include/object.h 
b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -317,6 +317,31 @@
 PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *, const void **, Py_ssize_t *);
 PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *, void **, Py_ssize_t *);
 PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *);
+PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
+/* Get the memory area pointed to by the indices for the buffer given.
+   Note that view->ndim is the assumed size of indices
+*/
+
+PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
+   Py_ssize_t len, char fort);
+PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
+ Py_ssize_t len, char fort);
+/* Copy len bytes of data from the contiguous chunk of memory
+   pointed to by buf into the buffer exported by obj.  Return
+   0 on success and return -1 and raise a PyBuffer_Error on
+   error (i.e. the object does not have a buffer interface or
+   it is not working).
+
+   If fort is 'F' and the object is multi-dimensional,
+   then the data will be copied into the array in
+   Fortran-style (first dimension varies the fastest).  If
+   fort is 'C', then the data will be copied into the array
+   in C-style (last dimension varies the fastest).  If fort
+   is 'A', then it does not matter and the copy will be made
+   in whatever way is more efficient.
+
+*/
+
 
 #define PyObject_MALLOC PyObject_Malloc
 #define PyObject_REALLOCPyObject_Realloc
diff --git a/pypy/module/cpyext/src/abstract.c 
b/pypy/module/cpyext/src/abstract.c
--- a/pypy/module/cpyext/src/abstract.c
+++ b/pypy/module/cpyext/src/abstract.c
@@ -101,6 +101,163 @@
 return 0;
 }
 
+void*
+PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
+{
+char* pointer;
+int i;
+pointer = (char *)view->buf;
+for (i = 0; i < view->ndim; i++) {
+pointer += view->strides[i]*indices[i];
+if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
+pointer = *((char**)pointer) + view->suboffsets[i];
+}
+}
+return (void*)pointer;
+}
+
+void
+_Py_add_one_to_index_F(int nd, Py_ssize_t *index, const Py_ssize_t *shape)
+{
+int k;
+
+for (k=0; k=0; k--) {
+if (index[k] < shape[k]-1) {
+index[k]++;
+break;
+}
+else {
+index[k] = 0;
+}
+}
+}
+
+  /* view is not checked for consistency in either of these.  It is
+ assumed that the size of the buffer is view->len in
+ view->len / view->itemsize elements.
+  */
+
+int
+PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
+{
+int k;
+void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
+Py_ssize_t *indices, elements;
+char *dest, *ptr;
+
+if (len > view->len) {
+len = view->len;
+}
+
+if (PyBuffer_IsContiguous(view, fort)) {
+/* simplest copy is all that is needed */
+memcpy(buf, view->buf, len);
+return 0;
+}
+
+/* Otherwise a more elaborate scheme is needed */
+
+/* view->ndim <= 64 */
+indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
+if (indices == NULL) {
+PyErr_NoMemory();
+return -1;
+}
+for (k=0; kndim;k++) {
+indices[k] = 0;
+}
+
+if (fort == 'F') {
+addone = _Py_add_one_to_index_F;
+}
+else {
+addone = _Py_add_one_to_index_C;
+}
+dest = buf;
+/* XXX : This is not going to be the fastest code in the world
+ several optimizations are possible.
+ */
+elements = len / view->itemsize;
+while (elements--) {
+ptr = PyBuffer_GetPointer(view, indices);
+memcpy(dest, ptr, view->itemsize);
+dest += 

[pypy-commit] pypy default: test, fix - any str subtype should never have tp_as_a_number.* functions set

2017-08-27 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92268:e45fdeb7813a
Date: 2017-08-26 20:02 +0300
http://bitbucket.org/pypy/pypy/changeset/e45fdeb7813a/

Log:test, fix - any str subtype should never have tp_as_a_number.*
functions set

diff --git a/pypy/module/cpyext/test/test_bytesobject.py 
b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -367,6 +367,16 @@
  """
 return PyLong_FromLong(PyObject_Size(args));
  """),
+('has_nb_add', "METH_O",
+ '''
+if (args->ob_type->tp_as_number == NULL) {
+Py_RETURN_FALSE;
+}
+if (args->ob_type->tp_as_number->nb_add == NULL) {
+Py_RETURN_FALSE;
+}
+Py_RETURN_TRUE;
+ '''),
 ], prologue="""
 #include 
 PyTypeObject PyStringArrType_Type = {
@@ -447,6 +457,8 @@
 ''')
 
 a = module.newsubstr('abc')
+assert module.has_nb_add('a') is False
+assert module.has_nb_add(a) is False
 assert type(a).__name__ == 'string_'
 assert a == 'abc'
 assert 3 == module.get_len(a)
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -309,13 +309,17 @@
 setattr(pto, slot_names[0], slot_func_helper)
 elif ((w_type is space.w_list or w_type is space.w_tuple) and
   slot_names[0] == 'c_tp_as_number'):
-# XXX hack - hwo can we generalize this? The problem is method
+# XXX hack - how can we generalize this? The problem is method
 # names like __mul__ map to more than one slot, and we have no
 # convenient way to indicate which slots CPython have filled
 #
 # We need at least this special case since Numpy checks that
 # (list, tuple) do __not__ fill tp_as_number
 pass
+elif (space.issubtype_w(w_type, space.w_basestring) and
+slot_names[0] == 'c_tp_as_number'):
+# like above but for any str type
+pass
 else:
 assert len(slot_names) == 2
 struct = getattr(pto, slot_names[0])
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for missing __ne__ on slices

2017-08-24 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92259:5a7118bbfee9
Date: 2017-08-24 23:07 +0300
http://bitbucket.org/pypy/pypy/changeset/5a7118bbfee9/

Log:test, fix for missing __ne__ on slices

diff --git a/pypy/objspace/std/sliceobject.py b/pypy/objspace/std/sliceobject.py
--- a/pypy/objspace/std/sliceobject.py
+++ b/pypy/objspace/std/sliceobject.py
@@ -132,6 +132,18 @@
 else:
 return space.w_False
 
+def descr_ne(self, space, w_other):
+if space.is_w(self, w_other):
+return space.w_False
+if not isinstance(w_other, W_SliceObject):
+return space.w_NotImplemented
+if space.eq_w(self.w_start, w_other.w_start) and \
+   space.eq_w(self.w_stop, w_other.w_stop) and \
+   space.eq_w(self.w_step, w_other.w_step):
+return space.w_False
+else:
+return space.w_True
+
 def descr_lt(self, space, w_other):
 if space.is_w(self, w_other):
 return space.w_False   # see comments in descr_eq()
@@ -177,6 +189,7 @@
 __reduce__ = gateway.interp2app(W_SliceObject.descr__reduce__),
 
 __eq__ = gateway.interp2app(W_SliceObject.descr_eq),
+__ne__ = gateway.interp2app(W_SliceObject.descr_ne),
 __lt__ = gateway.interp2app(W_SliceObject.descr_lt),
 
 start = slicewprop('w_start'),
diff --git a/pypy/objspace/std/test/test_sliceobject.py 
b/pypy/objspace/std/test/test_sliceobject.py
--- a/pypy/objspace/std/test/test_sliceobject.py
+++ b/pypy/objspace/std/test/test_sliceobject.py
@@ -94,6 +94,7 @@
 slice1 = slice(1, 2, 3)
 slice2 = slice(1, 2, 3)
 assert slice1 == slice2
+assert not slice1 != slice2
 slice2 = slice(1, 2)
 assert slice1 != slice2
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for missing userslot tp_iter, tp_iternext, this time via PyObject_Call

2017-08-23 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92239:eb3baada82b7
Date: 2017-08-23 21:28 +0300
http://bitbucket.org/pypy/pypy/changeset/eb3baada82b7/

Log:test, fix for missing userslot tp_iter, tp_iternext, this time via
PyObject_Call

diff --git a/pypy/module/cpyext/test/test_eval.py 
b/pypy/module/cpyext/test/test_eval.py
--- a/pypy/module/cpyext/test/test_eval.py
+++ b/pypy/module/cpyext/test/test_eval.py
@@ -362,3 +362,56 @@
 assert 'while calling recurse' in str(e)
 else:
 assert False, "expected RuntimeError"
+
+def test_build_class(self):
+# make sure PyObject_Call generates a proper PyTypeObject,
+# along the way verify that userslot has iter and next
+module = self.import_extension('foo', [
+("object_call", "METH_O",
+ '''
+return PyObject_Call((PyObject*)_Type, args, NULL);
+ '''),
+('iter', "METH_O",
+ '''
+if (NULL == args->ob_type->tp_iter)
+{
+PyErr_SetString(PyExc_TypeError, "NULL tp_iter");
+return NULL;
+}
+return args->ob_type->tp_iter(args);
+ '''),
+('next', "METH_O",
+ '''
+if (NULL == args->ob_type->tp_iternext)
+{
+PyErr_SetString(PyExc_TypeError, "NULL tp_iternext");
+return NULL;
+}
+return args->ob_type->tp_iternext(args);
+ '''),])
+def __init__(self, N):
+self.N = N
+self.i = 0
+
+def __iter__(self):
+return self
+
+def __next__(self):
+if self.i < self.N:
+i = self.i
+self.i += 1
+return i
+raise StopIteration
+
+d = {'__init__': __init__, '__iter__': __iter__, 'next': __next__,
+ '__next__': next}
+C = module.object_call(('Iterable', (object,), d))
+c = C(5)
+i = module.iter(c)
+out = []
+try:
+while 1:
+out.append(module.next(i))
+except StopIteration:
+pass
+assert out == [0, 1, 2, 3, 4]
diff --git a/pypy/module/cpyext/userslot.py b/pypy/module/cpyext/userslot.py
--- a/pypy/module/cpyext/userslot.py
+++ b/pypy/module/cpyext/userslot.py
@@ -122,3 +122,11 @@
 else:
 space.delete(w_self, w_obj)
 return 0
+
+@slot_function([PyObject], PyObject)
+def slot_tp_iter(space, w_self):
+return space.iter(w_self)
+
+@slot_function([PyObject], PyObject)
+def slot_tp_iternext(space, w_self):
+return space.next(w_self)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for Py_RichCompareBool(float('nan'), float('nan'))

2017-08-21 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92197:3e2341208fe6
Date: 2017-08-21 13:44 +0300
http://bitbucket.org/pypy/pypy/changeset/3e2341208fe6/

Log:test, fix for Py_RichCompareBool(float('nan'), float('nan'))

diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -305,7 +305,7 @@
 PyErr_BadInternalCall(space)
 
 @cpython_api([PyObject, PyObject, rffi.INT_real], rffi.INT_real, error=-1)
-def PyObject_RichCompareBool(space, ref1, ref2, opid_int):
+def PyObject_RichCompareBool(space, w_o1, w_o2, opid_int):
 """Compare the values of o1 and o2 using the operation specified by opid,
 which must be one of Py_LT, Py_LE, Py_EQ,
 Py_NE, Py_GT, or Py_GE, corresponding to <,
@@ -315,13 +315,13 @@
 opid."""
 # Quick result when objects are the same.
 # Guarantees that identity implies equality.
-if ref1 is ref2:
+if space.is_w(w_o1, w_o2):
 opid = rffi.cast(lltype.Signed, opid_int)
 if opid == Py_EQ:
 return 1
 if opid == Py_NE:
 return 0 
-w_res = PyObject_RichCompare(space, ref1, ref2, opid_int)
+w_res = PyObject_RichCompare(space, w_o1, w_o2, opid_int)
 return int(space.is_true(w_res))
 
 @cpython_api([PyObject], PyObject, result_is_ll=True)
diff --git a/pypy/module/cpyext/test/test_object.py 
b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -408,7 +408,7 @@
 Py_buffer passed to it.
 """
 module = self.import_extension('foo', [
-("fillinfo", "METH_VARARGS",
+("fillinfo", "METH_NOARGS",
  """
 Py_buffer buf;
 PyObject *str = PyString_FromString("hello, world.");
@@ -460,7 +460,7 @@
 object.
 """
 module = self.import_extension('foo', [
-("fillinfo", "METH_VARARGS",
+("fillinfo", "METH_NOARGS",
  """
 Py_buffer buf;
 PyObject *str = PyString_FromString("hello, world.");
@@ -506,7 +506,7 @@
 PyBuffer_FillInfo fails if WRITABLE is passed but object is readonly.
 """
 module = self.import_extension('foo', [
-("fillinfo", "METH_VARARGS",
+("fillinfo", "METH_NOARGS",
  """
 Py_buffer buf;
 PyObject *str = PyString_FromString("hello, world.");
@@ -533,7 +533,7 @@
 decremented by PyBuffer_Release.
 """
 module = self.import_extension('foo', [
-("release", "METH_VARARGS",
+("release", "METH_NOARGS",
  """
 Py_buffer buf;
 buf.obj = PyString_FromString("release me!");
@@ -553,3 +553,19 @@
  """)])
 assert module.release() is None
 
+
+class AppTestPyBuffer_Release(AppTestCpythonExtensionBase):
+def test_richcomp_nan(self):
+module = self.import_extension('foo', [
+   ("comp_eq", "METH_VARARGS",
+"""
+PyObject *a = PyTuple_GetItem(args, 0);
+PyObject *b = PyTuple_GetItem(args, 1);
+int res = PyObject_RichCompareBool(a, b, Py_EQ);
+return PyLong_FromLong(res);  
+"""),])
+a = float('nan')
+b = float('nan')
+assert a is b
+res = module.comp_eq(a, b)
+assert res == 1
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for cpython compatibility when PyObject_RichCompareBool(a, a, ...)

2017-08-09 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92118:d6b37b7c15ee
Date: 2017-08-09 18:25 +0300
http://bitbucket.org/pypy/pypy/changeset/d6b37b7c15ee/

Log:test, fix for cpython compatibility when PyObject_RichCompareBool(a,
a, ...)

diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -305,7 +305,7 @@
 PyErr_BadInternalCall(space)
 
 @cpython_api([PyObject, PyObject, rffi.INT_real], rffi.INT_real, error=-1)
-def PyObject_RichCompareBool(space, ref1, ref2, opid):
+def PyObject_RichCompareBool(space, ref1, ref2, opid_int):
 """Compare the values of o1 and o2 using the operation specified by opid,
 which must be one of Py_LT, Py_LE, Py_EQ,
 Py_NE, Py_GT, or Py_GE, corresponding to <,
@@ -313,7 +313,15 @@
 0 if the result is false, 1 otherwise. This is the equivalent of the
 Python expression o1 op o2, where op is the operator corresponding to
 opid."""
-w_res = PyObject_RichCompare(space, ref1, ref2, opid)
+# Quick result when objects are the same.
+# Guarantees that identity implies equality.
+if ref1 is ref2:
+opid = rffi.cast(lltype.Signed, opid_int)
+if opid == Py_EQ:
+return 1
+if opid == Py_NE:
+return 0 
+w_res = PyObject_RichCompare(space, ref1, ref2, opid_int)
 return int(space.is_true(w_res))
 
 @cpython_api([PyObject], PyObject, result_is_ll=True)
diff --git a/pypy/module/cpyext/test/test_object.py 
b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -8,7 +8,7 @@
 from pypy.module.cpyext.object import (
 PyObject_IsTrue, PyObject_Not, PyObject_GetAttrString,
 PyObject_DelAttrString, PyObject_GetAttr, PyObject_DelAttr,
-PyObject_GetItem, PyObject_RichCompareBool,
+PyObject_GetItem, 
 PyObject_IsInstance, PyObject_IsSubclass, PyObject_AsFileDescriptor,
 PyObject_Hash, PyObject_Cmp, PyObject_Unicode
 )
@@ -136,7 +136,18 @@
 
 w_i = space.wrap(1)
 with raises_w(space, SystemError):
-PyObject_RichCompareBool(space, w_i, w_i, 123456)
+api.PyObject_RichCompareBool(w_i, w_i, 123456)
+
+def test_RichCompareNanlike(self, space,api):
+w_obj = space.appexec([], """():
+class Nanlike(object):
+def __eq__(self, other):
+raise RuntimeError('unreachable')
+return Nanlike()""")
+res = api.PyObject_RichCompareBool(w_obj, w_obj, Py_EQ)
+assert res == 1
+res = api.PyObject_RichCompareBool(w_obj, w_obj, Py_NE)
+assert res == 0
 
 def test_IsInstance(self, space, api):
 assert api.PyObject_IsInstance(space.wrap(1), space.w_int) == 1
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix calling unbound object method with no args like np.int32.__array__()

2017-08-09 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r92114:dcea72970920
Date: 2017-08-09 13:26 +0300
http://bitbucket.org/pypy/pypy/changeset/dcea72970920/

Log:test, fix calling unbound object method with no args like
np.int32.__array__()

diff --git a/pypy/module/cpyext/methodobject.py 
b/pypy/module/cpyext/methodobject.py
--- a/pypy/module/cpyext/methodobject.py
+++ b/pypy/module/cpyext/methodobject.py
@@ -210,6 +210,10 @@
 def cmethod_descr_call(space, w_self, __args__):
 self = space.interp_w(W_PyCFunctionObject, w_self)
 args_w, kw_w = __args__.unpack()
+if len(args_w) < 1:
+raise oefmt(space.w_TypeError,
+"descriptor '%s' of '%s' object needs an argument",
+self.name, self.w_objclass.getname(space))
 w_instance = args_w[0] # XXX typecheck missing
 w_args = space.newtuple(args_w[1:])
 w_kw = space.newdict()
diff --git a/pypy/module/cpyext/test/test_arraymodule.py 
b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -21,6 +21,9 @@
 assert arr.itemsize == 4
 assert arr[2] == 3
 assert len(arr.buffer_info()) == 2
+exc = raises(TypeError, module.array.append)
+errstr = str(exc.value)
+assert errstr.startswith("descriptor 'append' of")
 arr.append(4)
 assert arr.tolist() == [1, 2, 3, 4]
 assert len(arr) == 4
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for PyCFunction_Check and builtins (there is no PyCFunction_CheckExact)

2017-07-10 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r91850:ac54af67d3c2
Date: 2017-07-10 22:56 +0300
http://bitbucket.org/pypy/pypy/changeset/ac54af67d3c2/

Log:test, fix for PyCFunction_Check and builtins (there is no
PyCFunction_CheckExact)

diff --git a/pypy/module/cpyext/methodobject.py 
b/pypy/module/cpyext/methodobject.py
--- a/pypy/module/cpyext/methodobject.py
+++ b/pypy/module/cpyext/methodobject.py
@@ -10,7 +10,7 @@
 from pypy.module.cpyext.api import (
 CONST_STRING, METH_CLASS, METH_COEXIST, METH_KEYWORDS, METH_NOARGS, METH_O,
 METH_STATIC, METH_VARARGS, PyObject, bootstrap_function,
-build_type_checkers, cpython_api, generic_cpy_call,
+build_type_checkers, cpython_api, generic_cpy_call, CANNOT_FAIL,
 PyTypeObjectPtr, slot_function, cts)
 from pypy.module.cpyext.pyobject import (
 Py_DecRef, from_ref, make_ref, as_pyobj, make_typedescr)
@@ -113,8 +113,14 @@
 "built-in method '%s' of '%s' object" %
 (self.name, self.w_objclass.getname(self.space)))
 
-PyCFunction_Check, PyCFunction_CheckExact = build_type_checkers(
-"CFunction", W_PyCFunctionObject)
+@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyCFunction_Check(space, w_obj):
+from pypy.interpreter.function import BuiltinFunction
+if w_obj is None:
+return False
+if isinstance(w_obj, W_PyCFunctionObject):
+return True
+return isinstance(w_obj, BuiltinFunction)
 
 class W_PyCClassMethodObject(W_PyCFunctionObject):
 w_self = None
diff --git a/pypy/module/cpyext/test/test_methodobject.py 
b/pypy/module/cpyext/test/test_methodobject.py
--- a/pypy/module/cpyext/test/test_methodobject.py
+++ b/pypy/module/cpyext/test/test_methodobject.py
@@ -93,6 +93,31 @@
 assert mod.isSameFunction(mod.getarg_O)
 raises(SystemError, mod.isSameFunction, 1)
 
+def test_check(self):
+mod = self.import_extension('foo', [
+('check', 'METH_O',
+'''
+return PyLong_FromLong(PyCFunction_Check(args));
+'''),
+])
+from math import degrees
+assert mod.check(degrees) == 1
+assert mod.check(list) == 0
+assert mod.check(sorted) == 1
+def func():
+pass
+class A(object):
+def meth(self):
+pass
+@staticmethod
+def stat():
+pass
+assert mod.check(func) == 0
+assert mod.check(A) == 0
+assert mod.check(A.meth) == 0
+assert mod.check(A.stat) == 0
+ 
+
 class TestPyCMethodObject(BaseApiTest):
 def test_repr(self, space, api):
 """
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for PyObject_Print(NULL, ...), which occurred in Numpy f2py --debug-capi

2017-07-09 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r91848:5a93d0b7ba23
Date: 2017-07-10 05:04 +0300
http://bitbucket.org/pypy/pypy/changeset/5a93d0b7ba23/

Log:test, fix for PyObject_Print(NULL, ...), which occurred in Numpy
f2py --debug-capi

diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -436,15 +436,19 @@
 Py_PRINT_RAW = 1 # No string quotes etc.
 
 @cpython_api([PyObject, FILEP, rffi.INT_real], rffi.INT_real, error=-1)
-def PyObject_Print(space, w_obj, fp, flags):
+def PyObject_Print(space, pyobj, fp, flags):
 """Print an object o, on file fp.  Returns -1 on error.  The flags argument
 is used to enable certain printing options.  The only option currently
 supported is Py_PRINT_RAW; if given, the str() of the object is written
 instead of the repr()."""
-if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
-w_str = space.str(w_obj)
+if not pyobj:
+w_str = space.newtext("")
 else:
-w_str = space.repr(w_obj)
+w_obj = from_ref(space, pyobj)
+if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
+w_str = space.str(w_obj)
+else:
+w_str = space.repr(w_obj)
 
 count = space.len_w(w_str)
 data = space.text_w(w_str)
diff --git a/pypy/module/cpyext/test/test_object.py 
b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -315,13 +315,20 @@
  if (fp == NULL)
  Py_RETURN_NONE;
  ret = PyObject_Print(obj, fp, Py_PRINT_RAW);
+ if (ret < 0) {
+ fclose(fp);
+ return NULL;
+ }
+ ret = PyObject_Print(NULL, fp, Py_PRINT_RAW);
+ if (ret < 0) {
+ fclose(fp);
+ return NULL;
+ }
  fclose(fp);
- if (ret < 0)
- return NULL;
  Py_RETURN_TRUE;
  """)])
 assert module.dump(self.tmpname, None)
-assert open(self.tmpname).read() == 'None'
+assert open(self.tmpname).read() == 'None'
 
 def test_issue1970(self):
 module = self.import_extension('foo', [
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test, fix (filename too long in the jitcodes directory!)

2017-01-05 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89373:606e869ec3f2
Date: 2017-01-05 08:58 +0100
http://bitbucket.org/pypy/pypy/changeset/606e869ec3f2/

Log:Test, fix (filename too long in the jitcodes directory!)

diff --git a/rpython/jit/codewriter/codewriter.py 
b/rpython/jit/codewriter/codewriter.py
--- a/rpython/jit/codewriter/codewriter.py
+++ b/rpython/jit/codewriter/codewriter.py
@@ -106,8 +106,9 @@
 else:
 name = 'unnamed' % id(ssarepr)
 i = 1
-# escape  names for windows
-name = name.replace('', '_(lambda)_')
+# escape names like  for windows by removing any strange
+# character; then make sure the names are not too long
+name = ''.join(c for c in name if c.isalnum() or c == '_')[:60]
 extra = ''
 while dir.join(name+extra).check():
 i += 1
diff --git a/rpython/jit/metainterp/test/test_ajit.py 
b/rpython/jit/metainterp/test/test_ajit.py
--- a/rpython/jit/metainterp/test/test_ajit.py
+++ b/rpython/jit/metainterp/test/test_ajit.py
@@ -4613,3 +4613,10 @@
 self.check_operations_history(guard_nonnull=0, guard_nonnull_class=0,
   guard_class=2,
   assert_not_none=2) # before optimization
+
+def test_call_time_clock(self):
+import time
+def g():
+time.clock()
+return 0
+self.interp_operations(g, [])
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for issue #2245 - unecessarily creating new layouts for PyHeapTypeObjjects

2016-12-07 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r88960:c95db94fc6a8
Date: 2016-12-07 23:26 +0200
http://bitbucket.org/pypy/pypy/changeset/c95db94fc6a8/

Log:test, fix for issue #2245 - unecessarily creating new layouts for
PyHeapTypeObjjects

diff --git a/pypy/module/cpyext/test/test_typeobject.py 
b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -1173,37 +1173,32 @@
("new_obj", "METH_NOARGS",
 '''
 PyObject *obj;
-obj = PyObject_New(PyObject, _Type);
+PyTypeObject *Base1, *Base2, *Base12;
+Base1 =  (PyTypeObject*)PyType_Type.tp_alloc(_Type, 0);
+Base2 =  (PyTypeObject*)PyType_Type.tp_alloc(_Type, 0);
+Base12 =  (PyTypeObject*)PyType_Type.tp_alloc(_Type, 0);
+Base1->tp_name = "Base1";
+Base2->tp_name = "Base2";
+Base12->tp_name = "Base12";
+Base1->tp_basicsize = sizeof(PyHeapTypeObject);
+Base2->tp_basicsize = sizeof(PyHeapTypeObject);
+Base12->tp_basicsize = sizeof(PyHeapTypeObject);
+Base1->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | 
Py_TPFLAGS_HEAPTYPE;
+Base2->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | 
Py_TPFLAGS_HEAPTYPE;
+Base12->tp_flags = Py_TPFLAGS_DEFAULT;
+Base12->tp_base = Base1;
+Base12->tp_bases = PyTuple_Pack(2, Base1, Base2); 
+Base12->tp_doc = "The Base12 type or object";
+if (PyType_Ready(Base1) < 0) return NULL;
+if (PyType_Ready(Base2) < 0) return NULL;
+if (PyType_Ready(Base12) < 0) return NULL;
+obj = PyObject_New(PyObject, Base12);
 return obj;
 '''
-)], prologue='''
-static PyTypeObject Foo1_Type = {
-PyVarObject_HEAD_INIT(NULL, 0)
-"foo.foo1",
-};
-static PyTypeObject Foo2_Type = {
-PyVarObject_HEAD_INIT(NULL, 0)
-"foo.foo2",
-};
-static PyTypeObject Foo12_Type = {
-PyVarObject_HEAD_INIT(NULL, 0)
-"foo.foo12",
-};
-static char doc[]="The foo12 object";
-''', more_init = '''
-Foo1_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
-Foo2_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
-Foo12_Type.tp_flags = Py_TPFLAGS_DEFAULT;
-Foo12_Type.tp_base = _Type;
-Foo12_Type.tp_doc = doc;
-Foo12_Type.tp_bases = PyTuple_Pack(2, _Type, _Type);
-if (PyType_Ready(_Type) < 0) INITERROR;
-if (PyType_Ready(_Type) < 0) INITERROR;
-if (PyType_Ready(_Type) < 0) INITERROR;
-''')
+)])
 obj = module.new_obj()
-assert 'foo.foo12' in str(obj)
-assert type(obj).__doc__ == "The foo12 object"
-assert obj.__doc__ == "The foo12 object"
+assert 'Base12' in str(obj)
+assert type(obj).__doc__ == "The Base12 type or object"
+assert obj.__doc__ == "The Base12 type or object"
 
 
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -465,13 +465,17 @@
 convert_member_defs(space, dict_w, pto.c_tp_members, self)
 
 name = rffi.charp2str(pto.c_tp_name)
-new_layout = (pto.c_tp_basicsize > rffi.sizeof(PyObject.TO) or
-  pto.c_tp_itemsize > 0)
+flag_heaptype = pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE
+if flag_heaptype:
+minsize = rffi.sizeof(PyHeapTypeObject.TO)
+else:
+minsize = rffi.sizeof(PyObject.TO)
+new_layout = (pto.c_tp_basicsize > minsize or pto.c_tp_itemsize > 0)
 
 W_TypeObject.__init__(self, space, name,
 bases_w or [space.w_object], dict_w, force_new_layout=new_layout)
 self.flag_cpytype = True
-self.flag_heaptype = pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE
+self.flag_heaptype = flag_heaptype
 # if a sequence or a mapping, then set the flag to force it
 if pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_item:
 self.flag_map_or_seq = 'S'
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test fix

2016-11-25 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r88651:9f86ca85efa9
Date: 2016-11-25 09:17 +0100
http://bitbucket.org/pypy/pypy/changeset/9f86ca85efa9/

Log:test fix

diff --git a/rpython/rtyper/llinterp.py b/rpython/rtyper/llinterp.py
--- a/rpython/rtyper/llinterp.py
+++ b/rpython/rtyper/llinterp.py
@@ -562,6 +562,9 @@
 def op_jit_conditional_call(self, *args):
 raise NotImplementedError("should not be called while not jitted")
 
+def op_jit_conditional_call_value(self, *args):
+raise NotImplementedError("should not be called while not jitted")
+
 def op_get_exception_addr(self, *args):
 raise NotImplementedError
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix user-created heaptype, issue #2434

2016-11-23 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r88579:98f8c7e783db
Date: 2016-11-23 16:41 +0200
http://bitbucket.org/pypy/pypy/changeset/98f8c7e783db/

Log:test, fix user-created heaptype, issue #2434

diff --git a/pypy/module/cpyext/test/test_typeobject.py 
b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -331,12 +331,34 @@
  PyHeapTypeObject *heaptype = (PyHeapTypeObject *)args;
  Py_INCREF(heaptype->ht_name);
  return heaptype->ht_name;
+ '''),
+("setattr", "METH_O",
  '''
- )
+int ret;
+PyObject* name = PyString_FromString("mymodule");
+PyObject *obj = PyType_Type.tp_alloc(_Type, 0);
+PyHeapTypeObject *type = (PyHeapTypeObject*)obj;
+if ((type->ht_type.tp_flags & Py_TPFLAGS_HEAPTYPE) == 0)
+{
+PyErr_SetString(PyExc_ValueError,
+"Py_TPFLAGS_HEAPTYPE not set");
+return NULL;
+}
+type->ht_type.tp_name = ((PyTypeObject*)args)->tp_name;
+PyType_Ready(>ht_type);
+ret = PyObject_SetAttrString((PyObject*)>ht_type,
+"__module__", name);
+Py_DECREF(name);
+if (ret < 0)
+return NULL;
+return PyLong_FromLong(ret);
+ '''),
 ])
 class C(object):
 pass
 assert module.name_by_heaptype(C) == "C"
+assert module.setattr(C) == 0
+
 
 def test_type_dict(self):
 foo = self.import_module("foo")
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -469,7 +469,7 @@
 W_TypeObject.__init__(self, space, name,
 bases_w or [space.w_object], dict_w, force_new_layout=new_layout)
 self.flag_cpytype = True
-self.flag_heaptype = False
+self.flag_heaptype = pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE
 # if a sequence or a mapping, then set the flag to force it
 if pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_item:
 self.flag_map_or_seq = 'S'
@@ -852,14 +852,14 @@
 w_obj = space.allocate_instance(W_PyCTypeObject, w_metatype)
 track_reference(space, py_obj, w_obj)
 # __init__ wraps all slotdefs functions from py_type via add_operators
-w_obj.__init__(space, py_type) 
+w_obj.__init__(space, py_type)
 w_obj.ready()
 
 finish_type_2(space, py_type, w_obj)
 base = py_type.c_tp_base
 if base:
 # XXX refactor - parts of this are done in finish_type_2 -> 
inherit_slots
-if not py_type.c_tp_as_number: 
+if not py_type.c_tp_as_number:
 py_type.c_tp_as_number = base.c_tp_as_number
 py_type.c_tp_flags |= base.c_tp_flags & Py_TPFLAGS_CHECKTYPES
 py_type.c_tp_flags |= base.c_tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for PyObject_Format('hello', u'') returning unicode (go figure)

2016-11-22 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r88552:299c1e591591
Date: 2016-11-22 22:12 +0200
http://bitbucket.org/pypy/pypy/changeset/299c1e591591/

Log:test, fix for PyObject_Format('hello', u'') returning unicode (go
figure)

diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -252,7 +252,10 @@
 def PyObject_Format(space, w_obj, w_format_spec):
 if w_format_spec is None:
 w_format_spec = space.wrap('')
-return space.call_method(w_obj, '__format__', w_format_spec)
+w_ret = space.call_method(w_obj, '__format__', w_format_spec)
+if space.isinstance_w(w_format_spec, space.w_unicode):
+return space.unicode_from_object(w_ret)
+return w_ret
 
 @cpython_api([PyObject], PyObject)
 def PyObject_Unicode(space, w_obj):
diff --git a/pypy/module/cpyext/test/test_object.py 
b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -312,6 +312,16 @@
 assert isinstance(dict(), collections.Mapping)
 assert module.ismapping(dict())
 
+def test_format_returns_unicode(self):
+module = self.import_extension('foo', [
+("empty_format", "METH_O",
+"""
+PyObject* empty_unicode = PyUnicode_FromStringAndSize("", 0);
+PyObject* obj = PyObject_Format(args, empty_unicode);
+return obj;
+""")])
+a = module.empty_format('hello')
+assert isinstance(a, unicode)
 
 class AppTestPyBuffer_FillInfo(AppTestCpythonExtensionBase):
 """
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix __rmod__ and -A incompatibilities with cpython on str, unicode

2016-10-18 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r87860:20aad7aa5de1
Date: 2016-10-18 18:12 +0300
http://bitbucket.org/pypy/pypy/changeset/20aad7aa5de1/

Log:test, fix __rmod__ and -A incompatibilities with cpython on str,
unicode

diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -605,6 +605,9 @@
 def descr_mod(self, space, w_values):
 return mod_format(space, self, w_values, do_unicode=False)
 
+def descr_rmod(self, space, w_values):
+return mod_format(space, w_values, self, do_unicode=False)
+
 def descr_eq(self, space, w_other):
 if space.config.objspace.std.withstrbuf:
 from pypy.objspace.std.strbufobject import W_StringBufferObject
@@ -937,6 +940,7 @@
 format = interpindirect2app(W_BytesObject.descr_format),
 __format__ = interpindirect2app(W_BytesObject.descr__format__),
 __mod__ = interpindirect2app(W_BytesObject.descr_mod),
+__rmod__ = interpindirect2app(W_BytesObject.descr_rmod),
 __getnewargs__ = interpindirect2app(
 W_AbstractBytesObject.descr_getnewargs),
 _formatter_parser = interp2app(W_BytesObject.descr_formatter_parser),
diff --git a/pypy/objspace/std/stringmethods.py 
b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -649,10 +649,10 @@
 return self._starts_ends_overflow(prefix)
 return endswith(value, prefix, start, end)
 
-def _strip(self, space, w_chars, left, right):
+def _strip(self, space, w_chars, left, right, name='strip'):
 "internal function called by str_xstrip methods"
 value = self._val(space)
-chars = self._op_val(space, w_chars, strict='strip')
+chars = self._op_val(space, w_chars, strict=name)
 
 lpos = 0
 rpos = len(value)
@@ -689,17 +689,17 @@
 def descr_strip(self, space, w_chars=None):
 if space.is_none(w_chars):
 return self._strip_none(space, left=1, right=1)
-return self._strip(space, w_chars, left=1, right=1)
+return self._strip(space, w_chars, left=1, right=1, name='strip')
 
 def descr_lstrip(self, space, w_chars=None):
 if space.is_none(w_chars):
 return self._strip_none(space, left=1, right=0)
-return self._strip(space, w_chars, left=1, right=0)
+return self._strip(space, w_chars, left=1, right=0, name='lstrip')
 
 def descr_rstrip(self, space, w_chars=None):
 if space.is_none(w_chars):
 return self._strip_none(space, left=0, right=1)
-return self._strip(space, w_chars, left=0, right=1)
+return self._strip(space, w_chars, left=0, right=1, name='rstrip')
 
 def descr_swapcase(self, space):
 selfvalue = self._val(space)
diff --git a/pypy/objspace/std/test/test_bytesobject.py 
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -92,6 +92,7 @@
 raises(ValueError, 'a%Zb'.__mod__, ((23,),))
 
 def test_format(self):
+import sys
 raises(TypeError, "foo".__mod__, "bar")
 raises(TypeError, u"foo".__mod__, "bar")
 raises(TypeError, "foo".__mod__, u"bar")
@@ -105,6 +106,22 @@
 assert result == "a foo b"
 assert isinstance(result, cls)
 
+for format, arg, cls in [("a %s b", "foo", str),
+ (u"a %s b", u"foo", unicode)]:
+raises(TypeError, arg.__rmod__, format[:2])
+result = arg.__rmod__(format)
+assert result == "a foo b"
+assert isinstance(result, cls)
+for format, arg, cls in [(u"a %s b", "foo", str),
+ ("a %s b", u"foo", unicode)]:
+result = arg.__rmod__(format)
+if '__pypy__' in sys.builtin_module_names:
+raises(TypeError, arg.__rmod__, format[:2])
+assert result == "a foo b"
+assert isinstance(result, cls)
+else:
+assert result is NotImplemented
+
 def test_format_c_overflow(self):
 raises(OverflowError, b'{0:c}'.format, -1)
 raises(OverflowError, b'{0:c}'.format, 256)
@@ -114,6 +131,7 @@
 exc_info = raises(TypeError, int_format.__mod__, '123')
 expected = int_format + ' format: a number is required, not str'
 assert str(exc_info.value) == expected
+raises(TypeError, "None % 'abc'") # __rmod__
 
 def test_split(self):
 assert b"".split() == []
@@ -258,9 +276,9 @@
 exc = raises(TypeError, s.strip, buffer(' '))
 assert str(exc.value) == 'strip arg must be None, str or unicode'
 exc = raises(TypeError, s.rstrip, buffer(' '))
-assert str(exc.value) == 'strip arg must be None, str or unicode'
+   

[pypy-commit] pypy default: test, fix for returning scalar from reduce function, fixes pypy/numpy #57

2016-10-16 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r87828:d86329133431
Date: 2016-10-16 14:30 +0300
http://bitbucket.org/pypy/pypy/changeset/d86329133431/

Log:test, fix for returning scalar from reduce function, fixes
pypy/numpy #57

diff --git a/pypy/module/micronumpy/test/dummy_module.py 
b/pypy/module/micronumpy/test/dummy_module.py
--- a/pypy/module/micronumpy/test/dummy_module.py
+++ b/pypy/module/micronumpy/test/dummy_module.py
@@ -27,7 +27,8 @@
 globals()['uint'] = dtype('uint').type
 
 types = ['Generic', 'Number', 'Integer', 'SignedInteger', 'UnsignedInteger',
- 'Inexact', 'Floating', 'ComplexFloating', 'Flexible', 'Character']
+ 'Inexact', 'Floating', 'ComplexFloating', 'Flexible', 'Character',
+]
 for t in types:
 globals()[t.lower()] = typeinfo[t]
 
@@ -40,4 +41,4 @@
 return a
 
 def isscalar(a):
-return type(a) in [typeinfo[t] for t in types]
+return any([isinstance(a, typeinfo[t]) for t in types])
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -1486,7 +1486,7 @@
 assert d[1] == 12
 
 def test_sum(self):
-from numpy import array, zeros, float16, complex64, str_
+from numpy import array, zeros, float16, complex64, str_, isscalar, add
 a = array(range(5))
 assert a.sum() == 10
 assert a[:4].sum() == 6
@@ -1515,6 +1515,13 @@
 
 assert list(zeros((0, 2)).sum(axis=1)) == []
 
+a = array([1, 2, 3, 4]).sum()
+s = isscalar(a)
+assert s is True
+a = add.reduce([1.0, 2, 3, 4])
+s = isscalar(a)
+assert s is True,'%r is not a scalar' % type(a)
+
 def test_reduce_nd(self):
 from numpy import arange, array
 a = arange(15).reshape(5, 3)
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -288,10 +288,8 @@
 
 _, dtype, _ = self.find_specialization(space, dtype, dtype, out,
casting='unsafe')
-call__array_wrap__ = True
 if shapelen == len(axes):
 if out:
-call__array_wrap__ = False
 if out.ndims() > 0:
 raise oefmt(space.w_ValueError,
 "output parameter for reduction operation %s 
has "
@@ -302,15 +300,20 @@
 if out:
 out.set_scalar_value(res)
 return out
+w_NDimArray = space.gettypefor(W_NDimArray)
+call__array_wrap__ = False
 if keepdims:
 shape = [1] * len(obj_shape)
 out = W_NDimArray.from_shape(space, shape, dtype, 
w_instance=obj)
 out.implementation.setitem(0, res)
+call__array_wrap__ = True
 res = out
-elif not space.is_w(space.type(w_obj), 
space.gettypefor(W_NDimArray)):
+elif (space.issubtype_w(space.type(w_obj), w_NDimArray) and 
+  not space.is_w(space.type(w_obj), w_NDimArray)):
 # subtypes return a ndarray subtype, not a scalar
 out = W_NDimArray.from_shape(space, [1], dtype, w_instance=obj)
 out.implementation.setitem(0, res)
+call__array_wrap__ = True
 res = out
 if call__array_wrap__:
 res = space.call_method(obj, '__array_wrap__', res, 
space.w_None)
@@ -359,8 +362,7 @@
 return out
 loop.reduce(
 space, self.func, obj, axis_flags, dtype, out, self.identity)
-if call__array_wrap__:
-out = space.call_method(obj, '__array_wrap__', out, 
space.w_None)
+out = space.call_method(obj, '__array_wrap__', out, space.w_None)
 return out
 
 def descr_outer(self, space, args_w):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for returning a W_LongObject

2016-09-17 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r87197:f133ee180e4f
Date: 2016-09-17 23:24 +0300
http://bitbucket.org/pypy/pypy/changeset/f133ee180e4f/

Log:test, fix for returning a W_LongObject

diff --git a/pypy/module/cpyext/longobject.py b/pypy/module/cpyext/longobject.py
--- a/pypy/module/cpyext/longobject.py
+++ b/pypy/module/cpyext/longobject.py
@@ -6,7 +6,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.intobject import PyInt_AsUnsignedLongMask
 from rpython.rlib.rbigint import rbigint
-from rpython.rlib.rarithmetic import intmask
+from rpython.rlib.rarithmetic import widen
 
 
 PyLong_Check, PyLong_CheckExact = build_type_checkers("Long")
@@ -34,7 +34,7 @@
 def PyLong_FromLongLong(space, val):
 """Return a new PyLongObject object from a C long long, or NULL
 on failure."""
-return space.wrap(val)
+return space.newlong(val)
 
 @cpython_api([rffi.ULONG], PyObject)
 def PyLong_FromUnsignedLong(space, val):
@@ -203,7 +203,7 @@
 can be retrieved from the resulting value using PyLong_AsVoidPtr().
 
 If the integer is larger than LONG_MAX, a positive long integer is 
returned."""
-return space.wrap(rffi.cast(ADDR, p))
+return space.newlong(rffi.cast(ADDR, p))
 
 @cpython_api([PyObject], rffi.VOIDP, error=lltype.nullptr(rffi.VOIDP.TO))
 def PyLong_AsVoidPtr(space, w_long):
diff --git a/pypy/module/cpyext/test/test_longobject.py 
b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -8,18 +8,20 @@
 
 class TestLongObject(BaseApiTest):
 def test_FromLong(self, space, api):
-value = api.PyLong_FromLong(3)
-assert isinstance(value, W_LongObject)
-assert space.unwrap(value) == 3
+w_value = api.PyLong_FromLong(3)
+assert isinstance(w_value, W_LongObject)
+assert space.unwrap(w_value) == 3
 
-value = api.PyLong_FromLong(sys.maxint)
-assert isinstance(value, W_LongObject)
-assert space.unwrap(value) == sys.maxint
+w_value = api.PyLong_FromLong(sys.maxint)
+assert isinstance(w_value, W_LongObject)
+assert space.unwrap(w_value) == sys.maxint
 
 def test_aslong(self, space, api):
 w_value = api.PyLong_FromLong((sys.maxint - 1) / 2)
+assert isinstance(w_value, W_LongObject)
 
 w_value = space.mul(w_value, space.wrap(2))
+assert isinstance(w_value, W_LongObject)
 value = api.PyLong_AsLong(w_value)
 assert value == (sys.maxint - 1)
 
@@ -35,12 +37,16 @@
 
 def test_as_ssize_t(self, space, api):
 w_value = space.newlong(2)
+assert isinstance(w_value, W_LongObject)
 value = api.PyLong_AsSsize_t(w_value)
 assert value == 2
-assert space.eq_w(w_value, api.PyLong_FromSsize_t(2))
+w_val2 = api.PyLong_FromSsize_t(2)
+assert isinstance(w_val2, W_LongObject)
+assert space.eq_w(w_value, w_val2)
 
 def test_fromdouble(self, space, api):
 w_value = api.PyLong_FromDouble(-12.74)
+assert isinstance(w_value, W_LongObject)
 assert space.unwrap(w_value) == -12
 assert api.PyLong_AsDouble(w_value) == -12
 
@@ -103,6 +109,7 @@
 
 def test_as_voidptr(self, space, api):
 w_l = api.PyLong_FromVoidPtr(lltype.nullptr(rffi.VOIDP.TO))
+assert isinstance(w_l, W_LongObject)
 assert space.unwrap(w_l) == 0L
 assert api.PyLong_AsVoidPtr(w_l) == lltype.nullptr(rffi.VOIDP.TO)
 
@@ -128,23 +135,58 @@
 module = self.import_extension('foo', [
 ("from_unsignedlong", "METH_NOARGS",
  """
- return PyLong_FromUnsignedLong((unsigned long)-1);
+ PyObject * obj;
+ obj = PyLong_FromUnsignedLong((unsigned long)-1);
+ if (obj->ob_type != _Type)
+ {
+Py_DECREF(obj);
+PyErr_SetString(PyExc_ValueError,
+"PyLong_FromLongLong did not return PyLongObject");
+return NULL;
+ }
+ return obj;
  """)])
 import sys
 assert module.from_unsignedlong() == 2 * sys.maxint + 1
 
 def test_fromlonglong(self):
 module = self.import_extension('foo', [
-("from_longlong", "METH_NOARGS",
+("from_longlong", "METH_VARARGS",
  """
- return PyLong_FromLongLong((long long)-1);
+ int val;
+ PyObject * obj;
+ if (!PyArg_ParseTuple(args, "i", ))
+ return NULL;
+ obj = PyLong_FromLongLong((long long)val);
+ if (obj->ob_type != _Type)
+ {
+Py_DECREF(obj);
+PyErr_SetString(PyExc_ValueError,
+"PyLong_FromLongLong did not return 

[pypy-commit] pypy default: test, fix for raising non-TypeError exception when conversion fails

2016-09-11 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r87008:2e99c5b56c4f
Date: 2016-09-11 22:46 +0300
http://bitbucket.org/pypy/pypy/changeset/2e99c5b56c4f/

Log:test, fix for raising non-TypeError exception when conversion fails

diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -43,16 +43,20 @@
 def PySequence_Fast(space, w_obj, m):
 """Returns the sequence o as a tuple, unless it is already a tuple or 
list, in
 which case o is returned.  Use PySequence_Fast_GET_ITEM() to access the
-members of the result.  Returns NULL on failure.  If the object is not a
-sequence, raises TypeError with m as the message text."""
+members of the result.  Returns NULL on failure.  If the object cannot be
+converted to a sequence, and raises a TypeError, raise a new TypeError with
+m as the message text. If the conversion otherwise, fails, reraise the
+original exception"""
 if isinstance(w_obj, W_ListObject):
 # make sure we can return a borrowed obj from PySequence_Fast_GET_ITEM 
   
 w_obj.convert_to_cpy_strategy(space)
 return w_obj
 try:
 return W_ListObject.newlist_cpyext(space, space.listview(w_obj))
-except OperationError:
-raise OperationError(space.w_TypeError, space.wrap(rffi.charp2str(m)))
+except OperationError as e:
+if e.match(space, space.w_TypeError):
+raise OperationError(space.w_TypeError, 
space.wrap(rffi.charp2str(m)))
+raise e
 
 @cpython_api([rffi.VOIDP, Py_ssize_t], PyObject, result_borrowed=True)
 def PySequence_Fast_GET_ITEM(space, w_obj, index):
diff --git a/pypy/module/cpyext/test/test_sequence.py 
b/pypy/module/cpyext/test/test_sequence.py
--- a/pypy/module/cpyext/test/test_sequence.py
+++ b/pypy/module/cpyext/test/test_sequence.py
@@ -267,3 +267,31 @@
 assert module.test_fast_sequence(s[0:-1])
 assert module.test_fast_sequence(s[::-1])
 
+def test_fast_keyerror(self):
+module = self.import_extension('foo', [
+("test_fast_sequence", "METH_VARARGS",
+ """
+PyObject *foo;
+PyObject * seq = PyTuple_GetItem(args, 0);
+if (seq == NULL)
+Py_RETURN_NONE;
+foo = PySequence_Fast(seq, "Could not convert object to 
sequence");
+if (foo != NULL)
+{
+return foo;
+}
+if (PyErr_ExceptionMatches(PyExc_KeyError)) {
+PyErr_Clear();
+return PyBool_FromLong(1);
+}
+return NULL;
+ """)])
+class Map(object):
+def __len__(self):
+return 1
+
+def __getitem__(self, index):
+raise KeyError()
+
+assert module.test_fast_sequence(Map()) is True
+
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix Py_buffer format, which can be a string (issue #2396 and IRC discussion)

2016-09-09 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r86972:fe0add22fd7e
Date: 2016-09-09 12:37 +0300
http://bitbucket.org/pypy/pypy/changeset/fe0add22fd7e/

Log:test, fix Py_buffer format, which can be a string (issue #2396 and
IRC discussion)

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
@@ -119,7 +119,7 @@
 
 constant_names = """
 Py_TPFLAGS_READY Py_TPFLAGS_READYING Py_TPFLAGS_HAVE_GETCHARBUFFER
-METH_COEXIST METH_STATIC METH_CLASS Py_TPFLAGS_BASETYPE
+METH_COEXIST METH_STATIC METH_CLASS Py_TPFLAGS_BASETYPE Py_MAX_FMT
 METH_NOARGS METH_VARARGS METH_KEYWORDS METH_O Py_TPFLAGS_HAVE_INPLACEOPS
 Py_TPFLAGS_HEAPTYPE Py_TPFLAGS_HAVE_CLASS Py_TPFLAGS_HAVE_NEWBUFFER
 Py_LT Py_LE Py_EQ Py_NE Py_GT Py_GE Py_TPFLAGS_CHECKTYPES Py_MAX_NDIMS
@@ -645,7 +645,7 @@
 ('format', rffi.CCHARP),
 ('shape', Py_ssize_tP),
 ('strides', Py_ssize_tP),
-('_format', rffi.UCHAR),
+('_format', rffi.CFixedArray(rffi.UCHAR, Py_MAX_FMT)),
 ('_shape', rffi.CFixedArray(Py_ssize_t, Py_MAX_NDIMS)),
 ('_strides', rffi.CFixedArray(Py_ssize_t, Py_MAX_NDIMS)),
 ('suboffsets', Py_ssize_tP),
diff --git a/pypy/module/cpyext/include/object.h 
b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -144,6 +144,7 @@
 
 /* Py3k buffer interface, adapted for PyPy */
 #define Py_MAX_NDIMS 32
+#define Py_MAX_FMT 5
 typedef struct bufferinfo {
 void *buf;
 PyObject *obj;/* owned reference */
@@ -158,7 +159,7 @@
 Py_ssize_t *shape;
 Py_ssize_t *strides;
 Py_ssize_t *suboffsets; /* alway NULL for app-level objects*/
-unsigned char _format;
+unsigned char _format[Py_MAX_FMT];
 Py_ssize_t _strides[Py_MAX_NDIMS];
 Py_ssize_t _shape[Py_MAX_NDIMS];
 /* static store for shape and strides of
diff --git a/pypy/module/cpyext/memoryobject.py 
b/pypy/module/cpyext/memoryobject.py
--- a/pypy/module/cpyext/memoryobject.py
+++ b/pypy/module/cpyext/memoryobject.py
@@ -1,5 +1,5 @@
 from pypy.module.cpyext.api import (cpython_api, Py_buffer, CANNOT_FAIL,
-   Py_MAX_NDIMS, build_type_checkers, Py_ssize_tP)
+ Py_MAX_FMT, Py_MAX_NDIMS, build_type_checkers, 
Py_ssize_tP)
 from pypy.module.cpyext.pyobject import PyObject, make_ref, incref
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.objspace.std.memoryobject import W_MemoryView
@@ -41,10 +41,22 @@
 view.c_len = w_obj.getlength()
 view.c_itemsize = w_obj.buf.getitemsize()
 rffi.setintfield(view, 'c_ndim', ndim)
-view.c__format = rffi.cast(rffi.UCHAR, w_obj.buf.getformat())
 view.c_format = rffi.cast(rffi.CCHARP, view.c__format)
 view.c_shape = rffi.cast(Py_ssize_tP, view.c__shape)
 view.c_strides = rffi.cast(Py_ssize_tP, view.c__strides)
+fmt = w_obj.buf.getformat()
+n = Py_MAX_FMT - 1 # NULL terminated buffer
+if len(fmt) > n:
+### WARN?
+pass
+else:
+n = len(fmt)
+for i in range(n):
+if ord(fmt[i]) > 255:
+view.c_format[i] = '*'
+else:
+view.c_format[i] = fmt[i]
+view.c_format[n] = '\x00'
 shape = w_obj.buf.getshape()
 strides = w_obj.buf.getstrides()
 for i in range(ndim):
diff --git a/pypy/module/cpyext/test/test_api.py 
b/pypy/module/cpyext/test/test_api.py
--- a/pypy/module/cpyext/test/test_api.py
+++ b/pypy/module/cpyext/test/test_api.py
@@ -1,5 +1,5 @@
 import py, pytest
-from rpython.rtyper.lltypesystem import rffi, lltype
+from rpython.rtyper.lltypesystem import lltype
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.module.cpyext.state import State
 from pypy.module.cpyext import api
diff --git a/pypy/module/cpyext/test/test_bufferobject.py 
b/pypy/module/cpyext/test/test_bufferobject.py
--- a/pypy/module/cpyext/test/test_bufferobject.py
+++ b/pypy/module/cpyext/test/test_bufferobject.py
@@ -1,4 +1,4 @@
-from rpython.rtyper.lltypesystem import rffi, lltype
+from rpython.rtyper.lltypesystem import lltype
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from pypy.module.cpyext.api import PyObject
diff --git a/pypy/module/cpyext/test/test_memoryobject.py 
b/pypy/module/cpyext/test/test_memoryobject.py
--- a/pypy/module/cpyext/test/test_memoryobject.py
+++ b/pypy/module/cpyext/test/test_memoryobject.py
@@ -1,3 +1,4 @@
+from rpython.rtyper.lltypesystem import rffi
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from rpython.rlib.buffer import StringBuffer
@@ -16,8 +17,12 @@
 w_buf = space.newbuffer(StringBuffer("hello"))
 w_memoryview = api.PyMemoryView_FromObject(w_buf)
 w_view = api.PyMemoryView_GET_BUFFER(w_memoryview)
-

[pypy-commit] pypy default: test, fix issue #2395 - do not force pyobj

2016-09-06 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r86901:9eb6ddd96e0e
Date: 2016-09-06 13:46 +0300
http://bitbucket.org/pypy/pypy/changeset/9eb6ddd96e0e/

Log:test, fix issue #2395 - do not force pyobj

diff --git a/pypy/module/cpyext/bytesobject.py 
b/pypy/module/cpyext/bytesobject.py
--- a/pypy/module/cpyext/bytesobject.py
+++ b/pypy/module/cpyext/bytesobject.py
@@ -156,9 +156,6 @@
 "expected string or Unicode object, %T found",
 from_ref(space, ref))
 ref_str = rffi.cast(PyBytesObject, ref)
-if not pyobj_has_w_obj(ref):
-# XXX Force the ref?
-bytes_realize(space, ref)
 return ref_str.c_ob_sval
 
 @cpython_api([rffi.VOIDP], rffi.CCHARP, error=0)
diff --git a/pypy/module/cpyext/test/test_bytesobject.py 
b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -183,8 +183,27 @@
  Py_INCREF(Py_None);
  return Py_None;
  """),
+("c_only", "METH_NOARGS",
+"""
+int ret;
+char * buf2;
+PyObject * obj = PyBytes_FromStringAndSize(NULL, 1024);
+if (!obj)
+return NULL;
+buf2 = PyBytes_AsString(obj);
+if (!buf2)
+return NULL;
+/* buf should not have been forced, issue #2395 */
+ret = _PyBytes_Resize(, 512);
+if (ret < 0)
+return NULL;
+ Py_DECREF(obj);
+ Py_INCREF(Py_None);
+ return Py_None;
+"""),
 ])
 module.getbytes()
+module.c_only()
 
 def test_py_string_as_string_Unicode(self):
 module = self.import_extension('foo', [
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix PySequence_Fast getslice; remove outdated document

2016-08-09 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r86125:72d14a4de609
Date: 2016-08-10 07:01 +0300
http://bitbucket.org/pypy/pypy/changeset/72d14a4de609/

Log:test, fix PySequence_Fast getslice; remove outdated document

diff --git a/pypy/module/cpyext/c-api.txt b/pypy/module/cpyext/c-api.txt
deleted file mode 100644
--- a/pypy/module/cpyext/c-api.txt
+++ /dev/null
@@ -1,71 +0,0 @@
-Reference Count
-===
-
-XXX
-
-Borrowed References
-===
-
-XXX
-
-PyStringObject support
-==
-
-The problem

-
-PyString_AsString() returns a (non-movable) pointer to the underlying
-buffer, whereas pypy strings are movable.  C code may temporarily
-store this address and use it, as long as it owns a reference to the
-PyObject.  There is no "release" function to specify that the pointer
-is not needed any more.
-
-Note that the pointer may be used to fill the initial value of
-string. This is valid only when the string was just allocated, and is
-not used elsewhere.
-
-Proposed solution
--
-
-Our emulation of the PyStringObject contains an additional member: a
-pointer to a char buffer; it may be NULL.
-
-- A string allocated by pypy will be converted into a PyStringObject
-  with a NULL buffer.  When PyString_AsString() is called, memory is
-  allocated (with flavor='raw') and content is copied.
-
-- A string allocated with PyString_FromStringAndSize(NULL, size) will
-  allocate a buffer with the specified size, but the reference won't
-  be stored in the global map py_objects_r2w; there won't be a
-  corresponding object in pypy.  When from_ref() or Py_INCREF() is
-  called, the pypy string is created, and added in py_objects_r2w.
-  The buffer is then supposed to be immutable.
-
-- _PyString_Resize works only on not-yet-pypy'd strings, and returns a
-  similar object.
-
-- PyString_Size don't need to force the object. (in this case, another
-  "size" member is needed)
-
-- There could be an (expensive!) check in from_ref() that the buffer
-  still corresponds to the pypy gc-managed string.
-
-PySequence_Fast support
-==
-There are five functions for fast sequence access offered by the CPython API:
-
-PyObject* PySequence_Fast(PyObject *o, const char *m)
-
-PyObject* PySequence_Fast_GET_ITEM(PyObject *o, int i)
-
-PyObject** PySequence_Fast_ITEMS(  PyObject *o)
-
-PyObject* PySequence_ITEM( PyObject *o, int i)
-
-int PySequence_Fast_GET_SIZE(  PyObject *o)
-
-PyPy supports four of these, but does not support PySequence_Fast_ITEMS.
-(Various ways to support PySequence_Fast_ITEMS were considered. They all had
-two things in common: they would have taken a lot of work, and they would have
-resulted in incomplete semantics or in poor performance. We decided that a slow
-implementation of PySequence_Fast_ITEMS was not very useful.)
diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -10,7 +10,7 @@
 from pypy.objspace.std import tupleobject
 
 from pypy.module.cpyext.tupleobject import PyTuple_Check, PyTuple_SetItem
-from pypy.module.cpyext.object import Py_IncRef, Py_DecRef
+from pypy.module.cpyext.pyobject import decref
 
 from pypy.module.cpyext.dictobject import PyDict_Check
 
@@ -252,7 +252,7 @@
 def setitem(self, w_list, index, w_obj):
 storage = self.unerase(w_list.lstorage)
 index = self._check_index(index, storage._length)
-Py_DecRef(w_list.space, storage._elems[index])
+decref(w_list.space, storage._elems[index])
 storage._elems[index] = make_ref(w_list.space, w_obj)
 
 def length(self, w_list):
@@ -264,9 +264,8 @@
 return storage._elems
 
 def getslice(self, w_list, start, stop, step, length):
-#storage = self.unerase(w_list.lstorage)
-raise oefmt(w_list.space.w_NotImplementedError,
-"settting a slice of a PySequence_Fast is not supported")
+w_list.switch_to_object_strategy()
+return w_list.strategy.getslice(w_list, start, stop, step, length)
 
 def getitems(self, w_list):
 # called when switching list strategy, so convert storage
@@ -389,5 +388,5 @@
 
 def __del__(self):
 for i in range(self._length):
-Py_DecRef(self.space, self._elems[i])
+decref(self.space, self._elems[i])
 lltype.free(self._elems, flavor='raw')
diff --git a/pypy/module/cpyext/test/test_sequence.py 
b/pypy/module/cpyext/test/test_sequence.py
--- a/pypy/module/cpyext/test/test_sequence.py
+++ b/pypy/module/cpyext/test/test_sequence.py
@@ -78,6 +78,17 @@
 assert api.PySequence_SetSlice(w_t, 1, 1, space.wrap((3,))) == 0
 assert space.eq_w(w_t, space.wrap([1, 3, 5]))
 
+def test_get_slice_fast(self, space, api):
+w_t = space.wrap([1, 2, 3, 4, 5])
+api.PySequence_Fast(w_t, "foo") # converts
+assert 

[pypy-commit] pypy default: test, fix for missing nb_* slot, what else is missing?

2016-07-29 Thread mattip
Author: mattip 
Branch: 
Changeset: r85908:d67646a1ac41
Date: 2016-07-29 10:10 -0500
http://bitbucket.org/pypy/pypy/changeset/d67646a1ac41/

Log:test, fix for missing nb_* slot, what else is missing?

diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -381,6 +381,7 @@
   ('tp_as_number.c_nb_invert', '__invert__'),
   ('tp_as_number.c_nb_index', '__index__'),
   ('tp_as_number.c_nb_hex', '__hex__'),
+  ('tp_as_number.c_nb_oct', '__oct__'),
   ('tp_str', '__str__'),
   ('tp_repr', '__repr__'),
   ('tp_iter', '__iter__'),
diff --git a/pypy/module/cpyext/test/test_longobject.py 
b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -259,8 +259,19 @@
 ret = PyLong_FromLong(-1);
 Py_DECREF(obj);
 return ret;
+ """),
+("has_oct", "METH_NOARGS",
+ """
+PyObject *ret, *obj = PyLong_FromLong(42);
+if (obj->ob_type->tp_as_number->nb_oct)
+ret = obj->ob_type->tp_as_number->nb_oct(obj);
+else
+ret = PyLong_FromLong(-1);
+Py_DECREF(obj);
+return ret;
  """)])
 assert module.has_sub() == 0
 assert module.has_pow() == 0
 assert module.has_hex() == '0x2aL'
+assert module.has_oct() == '052L'
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix for 7087aa60a936

2016-07-27 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r85881:82337b9d2b0a
Date: 2016-07-27 19:27 +0200
http://bitbucket.org/pypy/pypy/changeset/82337b9d2b0a/

Log:Test fix for 7087aa60a936

diff --git a/rpython/jit/metainterp/test/test_memmgr.py 
b/rpython/jit/metainterp/test/test_memmgr.py
--- a/rpython/jit/metainterp/test/test_memmgr.py
+++ b/rpython/jit/metainterp/test/test_memmgr.py
@@ -248,8 +248,8 @@
 tokens = [t() for t in get_stats().jitcell_token_wrefs]
 # Some loops have been freed
 assert None in tokens
-# Loop with number 0, h(), has not been freed
-assert 0 in [t.number for t in tokens if t]
+# Loop with number 1, h(), has not been freed
+assert 1 in [t.number for t in tokens if t]
 
 # 
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test fix for np.array()[...] returning a view (njs)

2016-07-12 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r85666:6b0ba4b3816d
Date: 2016-07-12 09:32 -0500
http://bitbucket.org/pypy/pypy/changeset/6b0ba4b3816d/

Log:test fix for np.array()[...] returning a view (njs)

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -254,7 +254,7 @@
 idx = space.str_w(w_idx)
 return self.getfield(space, idx)
 if space.is_w(w_idx, space.w_Ellipsis):
-return self
+return self.descr_view(space, space.type(self))
 elif isinstance(w_idx, W_NDimArray) and w_idx.get_dtype().is_bool():
 if w_idx.ndims() > 0:
 w_ret = self.getitem_filter(space, w_idx)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2614,17 +2614,11 @@
 import numpy as np
 import sys
 a = np.array(1.5)
-if '__pypy__' in sys.builtin_module_names:
-assert a[...] is a
-else:
-assert a[...].base is a
+assert a[...].base is a
 a[...] = 2.5
 assert a == 2.5
 a = np.array([1, 2, 3])
-if '__pypy__' in sys.builtin_module_names:
-assert a[...] is a
-else:
-assert a[...].base is a
+assert a[...].base is a
 a[...] = 4
 assert (a == [4, 4, 4]).all()
 assert a[..., 0] == 4
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix to not print mandlebrot to log files

2016-04-19 Thread mattip
Author: mattip 
Branch: 
Changeset: r83783:fa743228fbdb
Date: 2016-04-20 07:15 +0300
http://bitbucket.org/pypy/pypy/changeset/fa743228fbdb/

Log:test, fix to not print mandlebrot to log files

diff --git a/rpython/tool/ansi_print.py b/rpython/tool/ansi_print.py
--- a/rpython/tool/ansi_print.py
+++ b/rpython/tool/ansi_print.py
@@ -67,6 +67,8 @@
 
 def dot(self):
 """Output a mandelbrot dot to the terminal."""
+if not isatty():
+return
 global wrote_dot
 if not wrote_dot:
 mandelbrot_driver.reset()
diff --git a/rpython/tool/test/test_ansi_print.py 
b/rpython/tool/test/test_ansi_print.py
--- a/rpython/tool/test/test_ansi_print.py
+++ b/rpython/tool/test/test_ansi_print.py
@@ -65,6 +65,19 @@
 assert output[3] == ('[test:WARNING] maybe?\n', (31,))
 assert len(output[4][0]) == 1# single character
 
+def test_no_tty():
+log = ansi_print.AnsiLogger('test')
+with FakeOutput(tty=False) as output:
+log.dot()
+log.dot()
+log.WARNING('oops')
+log.WARNING('maybe?')
+log.dot()
+assert len(output) == 2
+assert output[0] == ('[test:WARNING] oops\n', ())
+assert output[1] == ('[test:WARNING] maybe?\n', ())
+
+
 def test_unknown_method_names():
 log = ansi_print.AnsiLogger('test')
 with FakeOutput() as output:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix boolean array indexing, extending d77888929462

2016-02-21 Thread mattip
Author: mattip 
Branch: 
Changeset: r82368:85f5eef31a2a
Date: 2016-02-21 15:53 +0100
http://bitbucket.org/pypy/pypy/changeset/85f5eef31a2a/

Log:test, fix boolean array indexing, extending d77888929462

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
@@ -196,6 +196,10 @@
 def newfloat(self, f):
 return self.float(f)
 
+def newslice(self, start, stop, step):
+return SliceObject(self.int_w(start), self.int_w(stop),
+   self.int_w(step))
+
 def le(self, w_obj1, w_obj2):
 assert isinstance(w_obj1, boxes.W_GenericBox)
 assert isinstance(w_obj2, boxes.W_GenericBox)
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
@@ -94,11 +94,13 @@
 dim = i
 break
 if dim >= 0:
-# filter by axis r
-filtr = chunks.pop(dim)
+# filter by axis dim
+filtr = chunks[dim]
 assert isinstance(filtr, BooleanChunk) 
 w_arr = w_arr.getitem_filter(space, filtr.w_idx, axis=dim)
 arr = w_arr.implementation
+chunks[dim] = SliceChunk(space.newslice(space.wrap(0), 
+ space.wrap(-1), space.w_None))
 r = calculate_slice_strides(space, arr.shape, arr.start,
  arr.get_strides(), arr.get_backstrides(), chunks)
 else:
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2550,6 +2550,8 @@
 assert b.base is None
 b = a[:, np.array([True, False, True])]
 assert b.base is not None
+b = a[np.array([True, False]), 0]
+assert (b ==[0]).all()
 
 def test_scalar_indexing(self):
 import numpy as np
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix indexing ndarray with scalar, single boolean array

2016-02-19 Thread mattip
Author: mattip 
Branch: 
Changeset: r82336:d77888929462
Date: 2016-02-19 16:29 +0200
http://bitbucket.org/pypy/pypy/changeset/d77888929462/

Log:test, fix indexing ndarray with scalar, single boolean array

diff --git a/pypy/module/micronumpy/concrete.py 
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -12,8 +12,8 @@
 ArrayArgumentException, W_NumpyObject
 from pypy.module.micronumpy.iterators import ArrayIter
 from pypy.module.micronumpy.strides import (
-IntegerChunk, SliceChunk, NewAxisChunk, EllipsisChunk, new_view,
-calc_strides, calc_new_strides, shape_agreement,
+IntegerChunk, SliceChunk, NewAxisChunk, EllipsisChunk, BooleanChunk,
+new_view, calc_strides, calc_new_strides, shape_agreement,
 calculate_broadcast_strides, calc_backstrides, calc_start, is_c_contiguous,
 is_f_contiguous)
 from rpython.rlib.objectmodel import keepalive_until_here
@@ -236,6 +236,8 @@
 
 @jit.unroll_safe
 def _prepare_slice_args(self, space, w_idx):
+print '_prepare_slice_args', w_idx
+from pypy.module.micronumpy import boxes
 if space.isinstance_w(w_idx, space.w_str):
 raise oefmt(space.w_IndexError, "only integers, slices (`:`), "
 "ellipsis (`...`), numpy.newaxis (`None`) and integer or "
@@ -258,6 +260,7 @@
 result = []
 i = 0
 has_ellipsis = False
+has_filter = False
 for w_item in space.fixedview(w_idx):
 if space.is_w(w_item, space.w_Ellipsis):
 if has_ellipsis:
@@ -272,6 +275,16 @@
 elif space.isinstance_w(w_item, space.w_slice):
 result.append(SliceChunk(w_item))
 i += 1
+elif isinstance(w_item, W_NDimArray) and 
w_item.get_dtype().is_bool():
+if has_filter:
+# in CNumPy, the support for this is incomplete
+raise oefmt(space.w_ValueError,
+"an index can only have a single boolean mask; "
+"use np.take or create a sinlge mask array")
+has_filter = True
+result.append(BooleanChunk(w_item))
+elif isinstance(w_item, boxes.W_GenericBox):
+result.append(IntegerChunk(w_item.descr_int(space)))
 else:
 result.append(IntegerChunk(w_item))
 i += 1
@@ -280,11 +293,14 @@
 return result
 
 def descr_getitem(self, space, orig_arr, w_index):
+print 'concrete descr_gettiem %s' % str(w_index)[:35]
 try:
 item = self._single_item_index(space, w_index)
+print 'concrete descr_gettiem _single_item_index succeeded'
 return self.getitem(item)
 except IndexError:
 # not a single result
+print 'concrete descr_gettiem _single_item_index failed'
 chunks = self._prepare_slice_args(space, w_index)
 return new_view(space, orig_arr, chunks)
 
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -107,8 +107,9 @@
 arr = W_NDimArray(self.implementation.transpose(self, None))
 return space.wrap(loop.tostring(space, arr))
 
-def getitem_filter(self, space, arr):
-if arr.ndims() > 1 and arr.get_shape() != self.get_shape():
+def getitem_filter(self, space, arr, axis=0):
+shape = self.get_shape()
+if arr.ndims() > 1 and arr.get_shape() != shape:
 raise OperationError(space.w_IndexError, space.wrap(
 "boolean index array should have 1 dimension"))
 if arr.get_size() > self.get_size():
@@ -116,14 +117,14 @@
 "index out of range for array"))
 size = loop.count_all_true(arr)
 if arr.ndims() == 1:
-if self.ndims() > 1 and arr.get_shape()[0] != self.get_shape()[0]:
+if self.ndims() > 1 and arr.get_shape()[0] != shape[axis]:
 msg = ("boolean index did not match indexed array along"
-  " dimension 0; dimension is %d but corresponding"
-  " boolean dimension is %d" % (self.get_shape()[0],
+  " dimension %d; dimension is %d but corresponding"
+  " boolean dimension is %d" % (axis, shape[axis],
   arr.get_shape()[0]))
 #warning = 
space.gettypefor(support.W_VisibleDeprecationWarning)
 space.warn(space.wrap(msg), space.w_VisibleDeprecationWarning)
-res_shape = [size] + self.get_shape()[1:]
+res_shape = shape[:axis] + [size] + shape[axis+1:]
 else:
 res_shape = [size]
 w_res = W_NDimArray.from_shape(space, res_shape, self.get_dtype(),
@@ -149,6 +150,8 @@
 def 

[pypy-commit] pypy default: test, fix for disallowing record array with mixed object, non-object values

2015-11-21 Thread mattip
Author: mattip 
Branch: 
Changeset: r80822:fce4c7d4a8b9
Date: 2015-11-22 00:02 +0200
http://bitbucket.org/pypy/pypy/changeset/fce4c7d4a8b9/

Log:test, fix for disallowing record array with mixed object, non-object
values

until we figure out a way to mark the gc pointers in the raw storage

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -15,7 +15,7 @@
 from pypy.module.micronumpy.arrayops import repeat, choose, put
 from pypy.module.micronumpy.base import W_NDimArray, convert_to_array, \
 ArrayArgumentException, wrap_impl
-from pypy.module.micronumpy.concrete import BaseConcreteArray
+from pypy.module.micronumpy.concrete import BaseConcreteArray, V_OBJECTSTORE
 from pypy.module.micronumpy.converters import (
 multi_axis_converter, order_converter, shape_converter,
 searchside_converter, out_converter)
@@ -288,6 +288,9 @@
 raise oefmt(space.w_ValueError, "no field of name %s", field)
 arr = self.implementation
 ofs, subdtype = arr.dtype.fields[field][:2]
+if subdtype.is_object() and arr.gcstruct is V_OBJECTSTORE:
+raise oefmt(space.w_NotImplementedError,
+"cannot read object from array with no gc hook")
 # ofs only changes start
 # create a view of the original array by extending
 # the shape, strides, backstrides of the array
diff --git a/pypy/module/micronumpy/test/test_object_arrays.py 
b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -126,8 +126,6 @@
 interface = dict(a.__array_interface__)
 interface['shape'] = tuple([3])
 interface['strides'] = tuple([0])
-if '__pypy__' in sys.builtin_module_names:
-skip('not implemented yet')
 c = np.array(DummyArray(interface, base=a))
 c.dtype = a.dtype
 #print c
@@ -160,6 +158,9 @@
 import sys
 ytype = np.object_
 if '__pypy__' in sys.builtin_module_names:
+dt = np.dtype([('x', int), ('y', ytype)])
+x = np.empty((4, 0), dtype = dt)
+raises(NotImplementedError, x.__getitem__, 'y')
 ytype = str
 dt = np.dtype([('x', int), ('y', ytype)])
 # Correct way
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
@@ -1851,6 +1851,9 @@
 arr.gcstruct)
 
 def read(self, arr, i, offset, dtype):
+if arr.gcstruct is V_OBJECTSTORE:
+raise oefmt(self.space.w_NotImplementedError,
+"cannot read object from array with no gc hook")
 return self.box(self._read(arr.storage, i, offset))
 
 def byteswap(self, w_v):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix failures due to dtype=(('O', spec)) union, demarcate probable bug in upstream frompyfunc

2015-11-21 Thread mattip
Author: mattip 
Branch: 
Changeset: r80821:dcf9a1751e29
Date: 2015-11-21 22:23 +0200
http://bitbucket.org/pypy/pypy/changeset/dcf9a1751e29/

Log:test, fix failures due to dtype=(('O', spec)) union, demarcate
probable bug in upstream frompyfunc

diff --git a/pypy/module/micronumpy/descriptor.py 
b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -422,6 +422,10 @@
 if space.is_w(self, w_other):
 return True
 if isinstance(w_other, W_Dtype):
+if self.is_object() and w_other.is_object():
+# ignore possible 'record' unions
+# created from dtype(('O', spec))
+return True
 return space.eq_w(self.descr_reduce(space),
   w_other.descr_reduce(space))
 return False
@@ -1017,7 +1021,8 @@
 raise oefmt(space.w_ValueError,
 'mismatch in size of old and new data-descriptor')
 retval = W_Dtype(w_dtype.itemtype, w_dtype.w_box_type,
-names=w_dtype1.names[:], fields=w_dtype1.fields.copy())
+names=w_dtype1.names[:], fields=w_dtype1.fields.copy(),
+elsize=w_dtype1.elsize)
 return retval
 if space.is_none(w_dtype):
 return cache.w_float64dtype
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -1002,7 +1002,7 @@
 # - operations 
 # TODO: support all kwargs like numpy ufunc_object.c
 sig = None
-cast = 'unsafe'
+cast = 'safe'
 extobj = None
 
 
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
@@ -345,7 +345,7 @@
 
 def test_can_subclass(self):
 import numpy as np
-import sys
+import sys, pickle
 class xyz(np.void):
 pass
 assert np.dtype(xyz).name == 'xyz'
@@ -356,12 +356,18 @@
 data = [(1, 'a'), (2, 'bbb')]
 b = np.dtype((xyz, [('a', int), ('b', object)]))
 if '__pypy__' in sys.builtin_module_names:
-raises(NotImplemented, np.array, data, dtype=b)
+raises(NotImplementedError, np.array, data, dtype=b)
 else:
 arr = np.array(data, dtype=b)
 assert arr[0][0] == 1
 assert arr[0][1] == 'a'
-
+b = np.dtype((xyz, [("col1", "

[pypy-commit] pypy default: test, fix deprecated use of arr([True])

2015-11-15 Thread mattip
Author: mattip 
Branch: 
Changeset: r80685:4b5c840d0da2
Date: 2015-11-15 18:06 +0200
http://bitbucket.org/pypy/pypy/changeset/4b5c840d0da2/

Log:test, fix deprecated use of arr([True])

diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -684,8 +684,9 @@
 arr_iter, arr_state = arr.create_iter()
 arr_dtype = arr.get_dtype()
 index_dtype = index.get_dtype()
-# XXX length of shape of index as well?
-while not index_iter.done(index_state):
+# support the deprecated form where arr([True]) will return arr[0, ...]
+# by iterating over res_iter, not index_iter
+while not res_iter.done(res_state):
 getitem_filter_driver.jit_merge_point(shapelen=shapelen,
   index_dtype=index_dtype,
   arr_dtype=arr_dtype,
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2238,6 +2238,9 @@
 c = array([True,False,True],bool)
 b = a[c]
 assert (a[c] == [[1, 2, 3], [7, 8, 9]]).all()
+c = array([True])
+b = a[c]
+assert b.shape == (1, 3)
 
 def test_bool_array_index_setitem(self):
 from numpy import arange, array
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix __array_interface__ for non-compliant keys and values

2015-11-02 Thread mattip
Author: mattip
Branch: 
Changeset: r80502:62330b3e9b7f
Date: 2015-11-02 16:39 +0200
http://bitbucket.org/pypy/pypy/changeset/62330b3e9b7f/

Log:test, fix __array_interface__ for non-compliant keys and values

diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -44,30 +44,43 @@
 def try_interface_method(space, w_object):
 try:
 w_interface = space.getattr(w_object, 
space.wrap("__array_interface__"))
-except OperationError, e:
+if w_interface is None:
+return None
+version_w = space.finditem(w_interface, space.wrap("version"))
+if version_w is None:
+raise oefmt(space.w_ValueError, "__array_interface__ found without"
+" 'version' key")
+if not space.isinstance_w(version_w, space.w_int):
+raise oefmt(space.w_ValueError, "__array_interface__ found with"
+" non-int 'version' key")
+version = space.int_w(version_w)
+if version < 3:
+raise oefmt(space.w_ValueError,
+"__array_interface__ version %d not supported", version)
+# make a view into the data
+w_shape = space.finditem(w_interface, space.wrap('shape'))
+w_dtype = space.finditem(w_interface, space.wrap('typestr'))
+w_descr = space.finditem(w_interface, space.wrap('descr'))
+w_data = space.finditem(w_interface, space.wrap('data'))
+w_strides = space.finditem(w_interface, space.wrap('strides'))
+if w_shape is None or w_dtype is None:
+raise oefmt(space.w_ValueError,
+"__array_interface__ missing one or more required keys: 
shape, typestr"
+)
+raise oefmt(space.w_NotImplementedError,
+"creating array from __array_interface__ not supported 
yet")
+'''
+data_w = space.listview()
+shape = [space.int_w(i) for i in space.listview(w_shape)]
+dtype = descriptor.decode_w_dtype(space, w_dtype)
+rw = space.is_true(data_w[1])
+'''
+#print 'create view from 
shape',shape,'dtype',dtype,'descr',w_descr,'data',data_w[0],'rw',rw
+return None
+except OperationError as e:
 if e.match(space, space.w_AttributeError):
 return None
 raise
-if w_interface is None:
-# happens from compile.py
-return None
-version = space.int_w(space.finditem(w_interface, space.wrap("version")))
-if version < 3:
-raise oefmt(space.w_NotImplementedError,
-"__array_interface__ version %d not supported", version)
-# make a view into the data
-w_shape = space.finditem(w_interface, space.wrap('shape'))
-w_dtype = space.finditem(w_interface, space.wrap('typestr'))
-w_descr = space.finditem(w_interface, space.wrap('descr'))
-data_w = space.listview(space.finditem(w_interface, space.wrap('data')))
-w_strides = space.finditem(w_interface, space.wrap('strides'))
-shape = [space.int_w(i) for i in space.listview(w_shape)]
-dtype = descriptor.decode_w_dtype(space, w_dtype)
-rw = space.is_true(data_w[1])
-#print 'create view from 
shape',shape,'dtype',dtype,'descr',w_descr,'data',data_w[0],'rw',rw
-raise oefmt(space.w_NotImplementedError,
-"creating array from __array_interface__ not supported yet")
-return
 
 
 @unwrap_spec(ndmin=int, copy=bool, subok=bool)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3073,6 +3073,18 @@
 c_data = c.__array_interface__['data'][0]
 assert b_data + 3 * b.dtype.itemsize == c_data
 
+class Dummy(object):
+def __init__(self, aif=None):
+if aif:
+self.__array_interface__ = aif
+
+a = array(Dummy())
+assert a.dtype == object
+raises(ValueError, array, Dummy({'xxx': 0}))
+raises(ValueError), array, Dummy({'version': 0}))
+raises(ValueError, array, Dummy({'version': 'abc'}))
+raises(ValueError, array, Dummy({'version': 3}))
+
 def test_array_indexing_one_elem(self):
 from numpy import array, arange
 raises(IndexError, 'arange(3)[array([3.5])]')
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix failure in ndarray creation from list of unicodes

2015-11-02 Thread mattip
Author: mattip
Branch: 
Changeset: r80500:058bbfa7fe93
Date: 2015-11-02 14:39 +0200
http://bitbucket.org/pypy/pypy/changeset/058bbfa7fe93/

Log:test, fix failure in ndarray creation from list of unicodes

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -669,14 +669,17 @@
 raise oefmt(space.w_NotImplementedError,
 "astype(%s) not implemented yet",
 new_dtype.get_name())
-if new_dtype.is_str() and new_dtype.elsize == 0:
+if new_dtype.is_str_or_unicode() and new_dtype.elsize == 0:
 elsize = 0
+ch = new_dtype.char
 itype = cur_dtype.itemtype
 for i in range(self.get_size()):
-elsize = max(elsize, 
len(itype.str_format(self.implementation.getitem(i), add_quotes=False)))
+elsize = max(elsize, space.len_w(itype.to_builtin_type(space, 
self.implementation.getitem(i
 new_dtype = descriptor.variable_dtype(
-space, 'S' + str(elsize))
-
+space, ch + str(elsize))
+if new_dtype.elsize == 0:
+# XXX Should not happen
+raise oefmt(space.w_ValueError, "new dtype has elsize of 0")
 if not can_cast_array(space, self, new_dtype, casting):
 raise oefmt(space.w_TypeError, "Cannot cast array from %s to %s"
 "according to the rule %s",
diff --git a/pypy/module/micronumpy/test/test_object_arrays.py 
b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -174,6 +174,11 @@
 b = a.astype('S')
 assert b.dtype == 'S100'
 assert 'a' * 100 in str(b)
+a = np.array([u'a' * 100], dtype='O')
+assert 'a' * 100 in str(a)
+b = a.astype('U')
+assert b.dtype == 'U100'
+
 a = np.array([123], dtype='U')
 assert a[0] == u'123'
 b = a.astype('O')
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix extra quotes in astype() for object to str ndarray conversion

2015-10-29 Thread mattip
Author: mattip 
Branch: 
Changeset: r80475:cdb5f895892c
Date: 2015-10-29 09:53 +1100
http://bitbucket.org/pypy/pypy/changeset/cdb5f895892c/

Log:test, fix extra quotes in astype() for object to str ndarray
conversion

diff --git a/pypy/module/micronumpy/test/test_object_arrays.py 
b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -172,6 +172,7 @@
 a = np.array([b'a' * 100], dtype='O')
 assert 'a' * 100 in str(a)
 b = a.astype('S')
+assert b.dtype == 'S100'
 assert 'a' * 100 in str(b)
 a = np.array([123], dtype='U')
 assert a[0] == u'123'
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
@@ -1891,6 +1891,12 @@
 return self.BoxType(w_obj)
 
 def str_format(self, box, add_quotes=True):
+if not add_quotes:
+as_str = self.space.str_w(self.space.repr(self.unbox(box)))
+as_strl = len(as_str) - 1
+if as_strl>1 and as_str[0] == "'" and as_str[as_strl] == "'":
+as_str = as_str[1:as_strl]
+return as_str
 return self.space.str_w(self.space.repr(self.unbox(box)))
 
 def runpack_str(self, space, s, native):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for overrun end of storage when offset, i non-zero

2015-10-03 Thread mattip
Author: mattip 
Branch: 
Changeset: r79957:a3564b6798cb
Date: 2015-10-04 00:25 +0300
http://bitbucket.org/pypy/pypy/changeset/a3564b6798cb/

Log:test, fix for overrun end of storage when offset, i non-zero

diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2475,6 +2475,18 @@
 a.fill(12)
 assert (a == u'1').all()
 
+def test_unicode_record_array(self) :
+from numpy import dtype, array
+t = dtype([('a', 'S3'), ('b', 'U2')])
+x = array([('a', u'b')], dtype=t)
+assert str(x) ==  "[('a', u'b')]"
+
+t = dtype([('a', 'U3'), ('b', 'S2')])
+x = array([(u'a', 'b')], dtype=t)
+x['a'] = u'1'
+assert str(x) ==  "[(u'1', 'b')]"
+
+
 def test_boolean_indexing(self):
 import numpy as np
 a = np.zeros((1, 3))
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
@@ -2231,9 +2231,9 @@
 index = i + offset + 4*k
 data = rffi.cast(Int32.T, ord(box._value[k]))
 raw_storage_setitem_unaligned(storage, index, data)
-for k in range(size, width // 4):
-index = i + offset + 4*k
-data = rffi.cast(Int32.T, 0)
+# zero out the remaining memory
+for index in range(size * 4 + i + offset, width):
+data = rffi.cast(Int8.T, 0)
 raw_storage_setitem_unaligned(storage, index, data)
 
 def read(self, arr, i, offset, dtype):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix

2015-09-27 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r79865:644a9163d222
Date: 2015-09-27 17:29 +0200
http://bitbucket.org/pypy/pypy/changeset/644a9163d222/

Log:Test fix

diff --git a/rpython/jit/metainterp/optimizeopt/optimizer.py 
b/rpython/jit/metainterp/optimizeopt/optimizer.py
--- a/rpython/jit/metainterp/optimizeopt/optimizer.py
+++ b/rpython/jit/metainterp/optimizeopt/optimizer.py
@@ -3,7 +3,7 @@
 from rpython.jit.metainterp.logger import LogOperations
 from rpython.jit.metainterp.history import Const, ConstInt, REF, ConstPtr
 from rpython.jit.metainterp.optimizeopt.intutils import IntBound,\
- ConstIntBound, MININT, MAXINT
+ ConstIntBound, MININT, MAXINT, IntUnbounded
 from rpython.jit.metainterp.optimizeopt.util import make_dispatcher_method
 from rpython.jit.metainterp.resoperation import rop, AbstractResOp, 
GuardResOp,\
  OpHelpers, ResOperation
@@ -57,9 +57,11 @@
 if isinstance(op, ConstInt):
 return ConstIntBound(op.getint())
 fw = op.get_forwarded()
-if isinstance(fw, IntBound):
-return fw
-assert fw is None
+if fw is not None:
+if isinstance(fw, IntBound):
+return fw
+# rare case: fw might be a RawBufferPtrInfo
+return IntUnbounded()
 assert op.type == 'i'
 intbound = IntBound(MININT, MAXINT)
 op.set_forwarded(intbound)
@@ -72,7 +74,8 @@
 return
 cur = op.get_forwarded()
 if cur is not None:
-cur.intersect(bound)
+if isinstance(cur, IntBound):
+cur.intersect(bound)
 else:
 op.set_forwarded(bound)
 
@@ -404,7 +407,8 @@
 box = self.get_box_replacement(box)
 if not we_are_translated():# safety-check
 if (box.get_forwarded() is not None and
-isinstance(constbox, ConstInt)):
+isinstance(constbox, ConstInt) and
+not isinstance(box.get_forwarded(), info.AbstractRawPtrInfo)):
 assert box.get_forwarded().contains(constbox.getint())
 if box.is_constant():
 return
diff --git a/rpython/jit/metainterp/optimizeopt/test/test_optimizeopt.py 
b/rpython/jit/metainterp/optimizeopt/test/test_optimizeopt.py
--- a/rpython/jit/metainterp/optimizeopt/test/test_optimizeopt.py
+++ b/rpython/jit/metainterp/optimizeopt/test/test_optimizeopt.py
@@ -8902,12 +8902,12 @@
 """
 self.optimize_loop(ops, expected)
 
-def test_raw_buffer_ptr_info_intbounds(self):
-ops = """
-[i1]
+def test_raw_buffer_ptr_info_intbounds_bug(self):
+ops = """
+[]
 i2 = call_i('malloc', 10, descr=raw_malloc_descr)
 guard_value(i2, 12345) []
-jump(i2)
+jump()
 """
 self.optimize_loop(ops, ops)
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for broadcasting when we shouldn't

2015-08-13 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r78978:e84ee03a93ab
Date: 2015-08-14 00:12 +0300
http://bitbucket.org/pypy/pypy/changeset/e84ee03a93ab/

Log:test, fix for broadcasting when we shouldn't

diff --git a/pypy/module/micronumpy/concrete.py 
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -52,11 +52,16 @@
 
 @jit.unroll_safe
 def setslice(self, space, arr):
-if len(arr.get_shape())  0 and len(self.get_shape()) == 0:
-raise oefmt(space.w_ValueError,
-could not broadcast input array from shape 
-(%s) into shape (),
-','.join([str(x) for x in arr.get_shape()]))
+if len(arr.get_shape())   len(self.get_shape()):
+# record arrays get one extra dimension
+if not self.dtype.is_record() or \
+len(arr.get_shape())  len(self.get_shape()) + 1:
+raise oefmt(space.w_ValueError,
+could not broadcast input array from shape 
+(%s) into shape (%s),
+','.join([str(x) for x in arr.get_shape()]),
+','.join([str(x) for x in self.get_shape()]),
+)
 shape = shape_agreement(space, self.get_shape(), arr)
 impl = arr.implementation
 if impl.storage == self.storage:
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2668,6 +2668,15 @@
 a[0, :: -1] = numpy.array([11, 12])
 assert (a == [[12, 11], [5, 10]]).all()
 
+a = numpy.zeros((3, 2), int)
+b = numpy.ones((3, 1), int)
+exc = raises(ValueError, 'a[:, 1] = b')
+assert str(exc.value) == could not broadcast  +\
+input array from shape (3,1) into shape (3)
+a[:, 1] = b[:,0]  0.5
+assert (a == [[0, 1], [0, 1], [0, 1]]).all()
+
+
 def test_ufunc(self):
 from numpy import array
 a = array([[1, 2], [3, 4], [5, 6]])
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix unpickling 'V0' (builtin, empty) subarray in record dtype

2015-08-12 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r78954:7860229d39ac
Date: 2015-08-12 23:54 +0300
http://bitbucket.org/pypy/pypy/changeset/7860229d39ac/

Log:test, fix unpickling 'V0' (builtin, empty) subarray in record dtype

diff --git a/pypy/module/micronumpy/descriptor.py 
b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -588,7 +588,8 @@
 return space.newtuple([w_class, builder_args, data])
 
 def descr_setstate(self, space, w_data):
-if self.fields is None:  # if builtin dtype
+if self.fields is None and not isinstance(self.itemtype, 
types.VoidType):  
+# if builtin dtype (but not w_voiddtype)
 return space.w_None
 
 version = space.int_w(space.getitem(w_data, space.wrap(0)))
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3808,7 +3808,7 @@
 assert (a == [1, 2]).all()
 
 def test_pickle(self):
-from numpy import dtype, array
+from numpy import dtype, array, int32
 from cPickle import loads, dumps
 
 d = dtype([('x', str), ('y', 'int32')])
@@ -3825,6 +3825,11 @@
 
 assert a[0]['y'] == 2
 assert a[1]['y'] == 1
+
+a = array([(1, [])], dtype=[('a', int32), ('b', int32, 0)])
+assert a['b'].shape == (1, 0)
+b = loads(dumps(a))
+assert b['b'].shape == (1, 0)
 
 def test_subarrays(self):
 from numpy import dtype, array, zeros
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for issue #2105

2015-08-02 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r78746:f3e27c185636
Date: 2015-08-02 19:22 +0300
http://bitbucket.org/pypy/pypy/changeset/f3e27c185636/

Log:test, fix for issue #2105

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
@@ -106,7 +106,7 @@
 'not find it' % (str(libpypy_c),))
 binaries.append((libpypy_c, libpypy_name))
 #
-builddir = options.builddir
+builddir = py.path.local(options.builddir)
 pypydir = builddir.ensure(name, dir=True)
 includedir = basedir.join('include')
 # Recursively copy all headers, shutil has only ignore
diff --git a/pypy/tool/release/test/test_package.py 
b/pypy/tool/release/test/test_package.py
--- a/pypy/tool/release/test/test_package.py
+++ b/pypy/tool/release/test/test_package.py
@@ -3,28 +3,31 @@
 from pypy.conftest import pypydir
 from pypy.tool.release import package
 from pypy.module.sys.version import  CPYTHON_VERSION
+from rpython.tool.udir import udir
 import tarfile, zipfile, sys
 
-def test_dir_structure(test='test'):
-# make sure we have sort of pypy-c
-if sys.platform == 'win32':
-basename = 'pypy-c.exe'
-rename_pypy_c = 'pypy-c'
-exe_name_in_archive = 'pypy-c.exe'
-else:
-basename = 'pypy-c'
-rename_pypy_c = 'pypy'
-exe_name_in_archive = 'bin/pypy'
-pypy_c = py.path.local(pypydir).join('goal', basename)
-try:
+class TestPackaging:
+def setup_class(cls):
+# make sure we have sort of pypy-c
+if sys.platform == 'win32':
+basename = 'pypy-c.exe'
+cls.rename_pypy_c = 'pypy-c'
+cls.exe_name_in_archive = 'pypy-c.exe'
+else:
+basename = 'pypy-c'
+cls.rename_pypy_c = 'pypy'
+cls.exe_name_in_archive = 'bin/pypy'
+cls.pypy_c = py.path.local(pypydir).join('goal', basename)
+
+def test_dir_structure(self, test='test'):
 retval, builddir = package.package(
 '--without-cffi', str(py.path.local(pypydir).dirpath()),
-test, rename_pypy_c, _fake=True)
+test, self.rename_pypy_c, _fake=True)
 assert retval == 0
 prefix = builddir.join(test)
 cpyver = '%d.%d' % CPYTHON_VERSION[:2]
 assert prefix.join('lib-python', cpyver, 'test').check()
-assert prefix.join(exe_name_in_archive).check()
+assert prefix.join(self.exe_name_in_archive).check()
 assert prefix.join('lib_pypy', 'syslog.py').check()
 assert not prefix.join('lib_pypy', 'py').check()
 assert not prefix.join('lib_pypy', 'ctypes_configure').check()
@@ -36,7 +39,7 @@
 else:
 th = tarfile.open(str(builddir.join('%s.tar.bz2' % test)))
 syslog = th.getmember('%s/lib_pypy/syslog.py' % test)
-exe = th.getmember('%s/%s' % (test, exe_name_in_archive))
+exe = th.getmember('%s/%s' % (test, self.exe_name_in_archive))
 assert syslog.mode == 0644
 assert exe.mode == 0755
 assert exe.uname == ''
@@ -63,16 +66,22 @@
 check_include('modsupport.h')
 check_include('pypy_decl.h')
 check_include('numpy/arrayobject.h')
-finally:
-pass# to keep the indentation
 
-def test_with_zipfile_module():
-prev = package.USE_ZIPFILE_MODULE
-try:
-package.USE_ZIPFILE_MODULE = True
-test_dir_structure(test='testzipfile')
-finally:
-package.USE_ZIPFILE_MODULE = prev
+def test_options(self, test='testoptions'):
+builddir = udir.ensure(build, dir=True)
+retval, builddir = package.package(
+'--without-cffi', '--builddir', str(builddir),
+str(py.path.local(pypydir).dirpath()),
+test, self.rename_pypy_c, _fake=True)
+
+def test_with_zipfile_module(self):
+prev = package.USE_ZIPFILE_MODULE
+try:
+package.USE_ZIPFILE_MODULE = True
+self.test_dir_structure(test='testzipfile')
+finally:
+package.USE_ZIPFILE_MODULE = prev
+
 
 def test_fix_permissions(tmpdir):
 if sys.platform == 'win32':
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix issue #2090

2015-07-21 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r78634:b0a67e1d9a20
Date: 2015-07-21 17:11 -0400
http://bitbucket.org/pypy/pypy/changeset/b0a67e1d9a20/

Log:test, fix issue #2090

diff --git a/pypy/module/micronumpy/concrete.py 
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -400,11 +400,17 @@
 
 class ConcreteArrayNotOwning(BaseConcreteArray):
 def __init__(self, shape, dtype, order, strides, backstrides, storage, 
start=0):
+if len(shape)  NPY.MAXDIMS:
+raise oefmt(dtype.itemtype.space.w_ValueError,
+sequence too large; must be smaller than %d, NPY.MAXDIMS)
 make_sure_not_resized(shape)
 make_sure_not_resized(strides)
 make_sure_not_resized(backstrides)
 self.shape = shape
-self.size = support.product(shape) * dtype.elsize
+try:
+self.size = support.product(shape) * dtype.elsize
+except OverflowError as e:
+raise oefmt(dtype.itemtype.space.w_ValueError, array is too big)
 self.order = order
 self.dtype = dtype
 self.strides = strides
@@ -445,8 +451,14 @@
  storage=lltype.nullptr(RAW_STORAGE), zero=True):
 gcstruct = V_OBJECTSTORE
 flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
+if len(shape)  NPY.MAXDIMS:
+raise oefmt(dtype.itemtype.space.w_ValueError,
+sequence too large; must be smaller than %d, NPY.MAXDIMS)
 if storage == lltype.nullptr(RAW_STORAGE):
-length = support.product(shape)
+try:
+length = support.product(shape)
+except OverflowError as e:
+raise oefmt(dtype.itemtype.space.w_ValueError, array is too 
big)
 if dtype.num == NPY.OBJECT:
 storage = dtype.itemtype.malloc(length * dtype.elsize, 
zero=True)
 gcstruct = _create_objectstore(storage, length, dtype.elsize)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -267,6 +267,11 @@
 y = array(x.T, copy=False)
 assert (y == x.T).all()
 
+exc = raises(ValueError, ndarray, [1,2,256]*1)
+assert exc.value[0] == 'sequence too large; must be smaller than 32'
+exc = raises(ValueError, ndarray, [1,2,256]*10)
+assert exc.value[0] == 'array is too big'
+
 def test_ndmin(self):
 from numpy import array
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for pypy/numpy issue number 34

2015-07-12 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r78540:1f1d7e1c14c2
Date: 2015-07-12 23:48 +0300
http://bitbucket.org/pypy/pypy/changeset/1f1d7e1c14c2/

Log:test, fix for pypy/numpy issue number 34

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
@@ -77,7 +77,7 @@
 stop = 1
 step = 1
 lgt = 1
-axis_step = 0
+axis_step = 0 # both skip this axis in calculate_slice_strides and set 
stride = 0
 
 def __init__(self):
 pass
@@ -127,7 +127,7 @@
 except IndexError:
 continue
 if chunk.step != 0:
-rstrides[j] = s_i * chunk.step
+rstrides[j] = s_i * chunk.step * chunk.axis_step
 rbackstrides[j] = s_i * max(0, chunk.lgt - 1) * chunk.step
 rshape[j] = chunk.lgt
 j += 1
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -764,6 +764,8 @@
 assert (a[1:] == b).all()
 assert (a[1:,newaxis] == d).all()
 assert (a[newaxis,1:] == c).all()
+assert a.strides == (8,)
+assert a[:, newaxis].strides == (8, 0)
 
 def test_newaxis_assign(self):
 from numpy import array, newaxis
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix ndarray.data for slices; pypy/numpy issue number 37.

2015-07-12 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r78541:0a3ca9a1ac09
Date: 2015-07-13 00:35 +0300
http://bitbucket.org/pypy/pypy/changeset/0a3ca9a1ac09/

Log:test, fix ndarray.data for slices; pypy/numpy issue number 37.

This unsafe interface will fail for non-contiguous views and can
cause buffer under/overruns, do we really need it?

diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2347,6 +2347,7 @@
 assert a[1] == 0xff
 assert len(a.data) == 16
 assert type(a.data) is buffer
+assert a[1:].data._pypy_raw_address() - a.data._pypy_raw_address() == 
a.strides[0]
 
 def test_explicit_dtype_conversion(self):
 from numpy import array
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix

2015-06-23 Thread arigo
Author: Armin Rigo ar...@tunes.org
Branch: 
Changeset: r78229:8fca06d46f8c
Date: 2015-06-21 14:30 +0200
http://bitbucket.org/pypy/pypy/changeset/8fca06d46f8c/

Log:Test fix

diff --git a/pypy/module/_socket/test/test_sock_app.py 
b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -316,7 +316,8 @@
 cls.w_udir = space.wrap(str(udir))
 
 def teardown_class(cls):
-cls.space.sys.getmodule('_socket').shutdown(cls.space)
+if not cls.runappdirect:
+cls.space.sys.getmodule('_socket').shutdown(cls.space)
 
 def test_module(self):
 import _socket
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix: this test used to randomly close file descriptor 1!

2015-06-07 Thread arigo
Author: Armin Rigo ar...@tunes.org
Branch: 
Changeset: r77943:d7d00adb03ae
Date: 2015-06-07 22:00 +0200
http://bitbucket.org/pypy/pypy/changeset/d7d00adb03ae/

Log:Test fix: this test used to randomly close file descriptor 1!

diff --git a/pypy/module/_multiprocessing/test/test_connection.py 
b/pypy/module/_multiprocessing/test/test_connection.py
--- a/pypy/module/_multiprocessing/test/test_connection.py
+++ b/pypy/module/_multiprocessing/test/test_connection.py
@@ -189,9 +189,11 @@
 assert data2 == '\x00\x00\x00\x04defg'
 
 def test_repr(self):
-import _multiprocessing
-c = _multiprocessing.Connection(1)
-assert repr(c) == 'read-write Connection, handle 1'
+import _multiprocessing, os
+fd = os.dup(1) # closed by Connection.__del__
+c = _multiprocessing.Connection(fd)
+assert repr(c) == 'read-write Connection, handle %d' % fd
 if hasattr(_multiprocessing, 'PipeConnection'):
-c = _multiprocessing.PipeConnection(1)
-assert repr(c) == 'read-write PipeConnection, handle 1'
+fd = os.dup(1) # closed by PipeConnection.__del__
+c = _multiprocessing.PipeConnection(fd)
+assert repr(c) == 'read-write PipeConnection, handle %d' % fd
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix converting object dtype to str

2015-06-07 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77939:e6db7b9cfa59
Date: 2015-06-07 18:16 +0300
http://bitbucket.org/pypy/pypy/changeset/e6db7b9cfa59/

Log:test, fix converting object dtype to str

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -604,10 +604,14 @@
 raise oefmt(space.w_NotImplementedError,
 astype(%s) not implemented yet,
 new_dtype.get_name())
-if new_dtype.num == NPY.STRING and new_dtype.elsize == 0:
-if cur_dtype.num == NPY.STRING:
-new_dtype = descriptor.variable_dtype(
-space, 'S' + str(cur_dtype.elsize))
+if new_dtype.is_str() and new_dtype.elsize == 0:
+elsize = 0
+itype = cur_dtype.itemtype
+for i in range(self.get_size()):
+elsize = max(elsize, 
len(itype.str_format(self.implementation.getitem(i), add_quotes=False)))
+new_dtype = descriptor.variable_dtype(
+space, 'S' + str(elsize))
+
 if not can_cast_array(space, self, new_dtype, casting):
 raise oefmt(space.w_TypeError, Cannot cast array from %s to %s
 according to the rule %s,
diff --git a/pypy/module/micronumpy/test/test_object_arrays.py 
b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -83,8 +83,8 @@
 def test_complex_op(self):
 import numpy as np
 import sys
-a = np.array(['abc', 'def'], dtype=object) 
-b = np.array([1, 2, 3], dtype=object) 
+a = np.array(['abc', 'def'], dtype=object)
+b = np.array([1, 2, 3], dtype=object)
 c = np.array([complex(1, 1), complex(1, -1)], dtype=object)
 for arg in (a,b,c):
 assert (arg == np.real(arg)).all()
@@ -164,3 +164,11 @@
 a = np.array([(1, 'object')], dt)
 # Wrong way - should complain about writing buffer to object dtype
 raises(ValueError, np.array, [1, 'object'], dt)
+
+def test_astype(self):
+import numpy as np
+a = np.array([b'a' * 100], dtype='O')
+assert 'a' * 100 in str(a)
+b = a.astype('S')
+assert 'a' * 100 in str(b)
+
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix flags.owndata

2015-06-03 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77841:ad5bd147db7f
Date: 2015-06-03 20:37 +0300
http://bitbucket.org/pypy/pypy/changeset/ad5bd147db7f/

Log:test, fix flags.owndata

diff --git a/pypy/module/micronumpy/concrete.py 
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -438,6 +438,7 @@
 def __init__(self, shape, dtype, order, strides, backstrides,
  storage=lltype.nullptr(RAW_STORAGE), zero=True):
 gcstruct = V_OBJECTSTORE
+self.flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
 if storage == lltype.nullptr(RAW_STORAGE):
 length = support.product(shape) 
 if dtype.num == NPY.OBJECT:
@@ -445,11 +446,11 @@
 gcstruct = _create_objectstore(storage, length, dtype.elsize)
 else:
 storage = dtype.itemtype.malloc(length * dtype.elsize, 
zero=zero)
+self.flags |= NPY.ARRAY_OWNDATA
 start = calc_start(shape, strides)
 ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, 
backstrides,
 storage, start=start)
 self.gcstruct = gcstruct
-self.flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
 if is_c_contiguous(self):
 self.flags |= NPY.ARRAY_C_CONTIGUOUS
 if is_f_contiguous(self):
diff --git a/pypy/module/micronumpy/test/test_flagsobj.py 
b/pypy/module/micronumpy/test/test_flagsobj.py
--- a/pypy/module/micronumpy/test/test_flagsobj.py
+++ b/pypy/module/micronumpy/test/test_flagsobj.py
@@ -13,6 +13,8 @@
 assert s == '%s' %('  C_CONTIGUOUS : True\n  F_CONTIGUOUS : True'
  '\n  OWNDATA : True\n  WRITEABLE : False'
  '\n  ALIGNED : True\n  UPDATEIFCOPY : False')
+a = np.array(2)
+assert a.flags.owndata
 
 def test_repr(self):
 import numpy as np
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix to support creating a record array from a different record array

2015-06-02 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77788:0c9994fe9a36
Date: 2015-06-02 21:05 +0300
http://bitbucket.org/pypy/pypy/changeset/0c9994fe9a36/

Log:test, fix to support creating a record array from a different record
array

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
@@ -1129,6 +1129,16 @@
 exc = raises(ValueError, dtype([('a', 'i8'), ('a', 'f8')]))
 assert exc.value[0] == 'two fields with the same name'
 
+def test_array_from_record(self):
+import numpy as np
+a = np.array(('???', -999, -12345678.9), 
+ dtype=[('c', '|S3'), ('a', 'i8'), ('b', 'f8')])
+# Change the order of the keys
+b = np.array(a, dtype=[('a', 'i8'), ('b', 'f8'), ('c', '|S3')])
+assert b.base is None
+assert b.dtype.fields['a'][1] == 0
+assert b['a'] == -999
+
 def test_create_from_dict(self):
 import numpy as np
 import sys
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
@@ -2363,8 +2363,16 @@
 def coerce(self, space, dtype, w_item):
 from pypy.module.micronumpy.base import W_NDimArray
 if isinstance(w_item, boxes.W_VoidBox):
-return w_item
-if w_item is not None:
+if dtype == w_item.dtype:
+return w_item
+else:
+# match up the field names
+items_w = [None] * len(dtype.fields)
+for i in range(len(dtype.fields)):
+name = dtype.names[i]
+if name in w_item.dtype.names:
+items_w[i] = w_item.descr_getitem(space, 
space.wrap(name))
+elif w_item is not None:
 if space.isinstance_w(w_item, space.w_tuple):
 if len(dtype.fields) != space.len_w(w_item):
 raise OperationError(space.w_ValueError, space.wrap(
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix record array creation with int value broadcast to internal array

2015-06-02 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77793:d7275799e550
Date: 2015-06-03 00:43 +0300
http://bitbucket.org/pypy/pypy/changeset/d7275799e550/

Log:test, fix record array creation with int value broadcast to internal
array

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
@@ -1138,6 +1138,10 @@
 assert b.base is None
 assert b.dtype.fields['a'][1] == 0
 assert b['a'] == -999
+a = np.array(('N/A', 1e+20, 1e+20, 99),
+ dtype=[('name', '|S4'), ('x', 'f8'), 
+('y', 'f8'), ('block', 'i8', (2, 3))])
+assert (a['block'] == 99).all()
 
 def test_create_from_dict(self):
 import numpy as np
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
@@ -20,7 +20,7 @@
  cast_gcref_to_instance
 from rpython.rtyper.lltypesystem import lltype, rffi, llmemory
 from rpython.tool.sourcetools import func_with_new_name
-from pypy.module.micronumpy import boxes
+from pypy.module.micronumpy import boxes, support
 from pypy.module.micronumpy.concrete import SliceArray, VoidBoxStorage, 
V_OBJECTSTORE
 from pypy.module.micronumpy.strides import calc_strides
 from . import constants as NPY
@@ -2265,10 +2265,12 @@
 def _coerce(self, space, arr, ofs, dtype, w_items, shape):
 # TODO: Make sure the shape and the array match
 from pypy.module.micronumpy.descriptor import W_Dtype
-if w_items is not None:
+if w_items is None:
+items_w = [None] * shape[0]
+elif support.issequence_w(space, w_items):
 items_w = space.fixedview(w_items)
 else:
-items_w = [None] * shape[0]
+items_w = [w_items] * shape[0]
 subdtype = dtype.subdtype
 assert isinstance(subdtype, W_Dtype)
 itemtype = subdtype.itemtype
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for str(box('abc')) = 'abc' rather than 'abc'

2015-05-19 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77416:6f94cd71078c
Date: 2015-05-20 00:08 +0300
http://bitbucket.org/pypy/pypy/changeset/6f94cd71078c/

Log:test, fix for str(box('abc')) = 'abc' rather than 'abc'

diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -193,7 +193,7 @@
 '%T' object is not iterable, self)
 
 def descr_str(self, space):
-return space.wrap(self.get_dtype(space).itemtype.str_format(self))
+return space.wrap(self.get_dtype(space).itemtype.str_format(self, 
add_quotes=False))
 
 def descr_format(self, space, w_spec):
 return space.format(self.item(space), w_spec)
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -277,7 +277,7 @@
 if self.is_scalar() and dtype.is_str():
 s.append(dtype.itemtype.to_str(i.getitem(state)))
 else:
-s.append(dtype.itemtype.str_format(i.getitem(state)))
+s.append(dtype.itemtype.str_format(i.getitem(state), 
add_quotes=True))
 state = i.next(state)
 if not self.is_scalar():
 s.append(']')
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3468,6 +3468,9 @@
 assert str(array('abc')) == 'abc'
 assert str(array(1.5)) == '1.5'
 assert str(array(1.5).real) == '1.5'
+arr = array(['abc', 'abc'])
+for a in arr.flat:
+ assert str(a) == 'abc'
 
 def test_ndarray_buffer_strides(self):
 from numpy import ndarray, array
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -388,7 +388,7 @@
 def to_builtin_type(self, space, w_item):
 return space.wrap(self.unbox(w_item))
 
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):
 return True if self.unbox(box) else False
 
 @staticmethod
@@ -454,7 +454,7 @@
 def _coerce(self, space, w_item):
 return self._base_coerce(space, w_item)
 
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):
 return str(self.for_computation(self.unbox(box)))
 
 @staticmethod
@@ -727,7 +727,7 @@
 return self.box(rfloat.NAN)
 return self.box(space.float_w(space.call_function(space.w_float, 
w_item)))
 
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):
 return float2string(self.for_computation(self.unbox(box)), g,
 rfloat.DTSF_STR_PRECISION)
 
@@ -1132,7 +1132,7 @@
 w_obj.__init__(w_tmpobj.real, w_tmpobj.imag)
 return w_obj
 
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):
 real, imag = self.for_computation(self.unbox(box))
 imag_str = str_format(imag)
 if not rfloat.isfinite(imag):
@@ -1862,7 +1862,7 @@
 w_obj = self.space.newcomplex(real, imag)
 return self.BoxType(w_obj)
 
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):
 return self.space.str_w(self.space.repr(self.unbox(box)))
 
 def runpack_str(self, space, s):
@@ -2122,11 +2122,13 @@
 dtype = arr.dtype
 return boxes.W_StringBox(arr, i + offset, dtype)
 
-def str_format(self, item):
+def str_format(self, item, add_quotes=True):
 builder = StringBuilder()
-builder.append(')
+if add_quotes:
+builder.append(')
 builder.append(self.to_str(item))
-builder.append(')
+if add_quotes:
+builder.append(')
 return builder.build()
 
 # XXX move the rest of this to base class when UnicodeType is supported
@@ -2209,7 +2211,7 @@
 def read(self, arr, i, offset, dtype=None):
 raise oefmt(self.space.w_NotImplementedError, unicode type not 
completed)
 
-def str_format(self, item):
+def str_format(self, item, add_quotes=True):
 raise oefmt(self.space.w_NotImplementedError, unicode type not 
completed)
 
 def to_builtin_type(self, space, box):
@@ -2314,7 +2316,7 @@
 return boxes.W_VoidBox(arr, i + offset, dtype)
 
 @jit.unroll_safe
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):
 assert isinstance(box, boxes.W_VoidBox)
 arr = self.readarray(box.arr, box.ofs, 0, box.dtype)
 return arr.dump_data(prefix='', suffix='')
@@ -2425,7 +2427,7 @@
 return space.newtuple(items)
 
 @jit.unroll_safe
-def str_format(self, box):
+def str_format(self, box, add_quotes=True):

[pypy-commit] pypy default: test, fix? nditer for negative strides

2015-05-14 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77320:0fdc72f5d9ac
Date: 2015-05-14 16:46 +0300
http://bitbucket.org/pypy/pypy/changeset/0fdc72f5d9ac/

Log:test, fix? nditer for negative strides

diff --git a/pypy/module/micronumpy/nditer.py b/pypy/module/micronumpy/nditer.py
--- a/pypy/module/micronumpy/nditer.py
+++ b/pypy/module/micronumpy/nditer.py
@@ -217,8 +217,8 @@
 backward = is_backward(imp, order)
 if arr.is_scalar():
 return ConcreteIter(imp, 1, [], [], [], op_flags, base)
-if (imp.strides[0]  imp.strides[-1] and not backward) or \
-   (imp.strides[0]  imp.strides[-1] and backward):
+if (abs(imp.strides[0])  abs(imp.strides[-1]) and not backward) or \
+   (abs(imp.strides[0])  abs(imp.strides[-1]) and backward):
 # flip the strides. Is this always true for multidimension?
 strides = imp.strides[:]
 backstrides = imp.backstrides[:]
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -1834,6 +1834,13 @@
 v = s.view(y.__class__)
 assert v.strides == (4, 24)
 
+x = empty([12, 8, 8], 'float64')
+y = x[::-4, :, :]
+assert y.base is x
+assert y.strides == (-2048, 64, 8)
+y[:] = 1000
+assert x[-1, 0, 0] == 1000 
+
 a = empty([3, 2, 1], dtype='float64')
 b = a.view(dtype('uint32'))
 assert b.strides == (16, 8, 4)
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py 
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -246,12 +246,17 @@
 dtypes=[dtype(int), dtype(int)],
 stack_inputs=True,
   )
-ai = arange(18, dtype=int).reshape(2,3,3)
+ai = arange(12*3*3, dtype=int).reshape(12,3,3)
 exc = raises(ValueError, ufunc, ai[:,:,0])
 assert perand 0 has a mismatch in its core dimension 1 in 
exc.value.message
 ai3 = ufunc(ai[0,:,:])
 ai2 = ufunc(ai)
 assert (ai2 == ai * 2).all()
+# view
+aiV = ai[::-2, :, :]
+assert aiV.strides == (-144, 24, 8)
+ai2 = ufunc(aiV)
+assert (ai2 == aiV * 2).all()
 
 def test_frompyfunc_needs_nditer(self):
 def summer(in0):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for issue #2046

2015-05-12 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r77302:30fa3802a882
Date: 2015-05-13 07:41 +0300
http://bitbucket.org/pypy/pypy/changeset/30fa3802a882/

Log:test, fix for issue #2046

diff --git a/pypy/module/micronumpy/concrete.py 
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -519,6 +519,9 @@
 return self.__class__(self.start, new_strides, new_backstrides, 
new_shape,
   self, orig_array)
 
+def sort(self, space, w_axis, w_order):
+from .selection import sort_array
+return sort_array(self, space, w_axis, w_order)
 
 class NonWritableSliceArray(SliceArray):
 def descr_setitem(self, space, orig_array, w_index, w_value):
diff --git a/pypy/module/micronumpy/selection.py 
b/pypy/module/micronumpy/selection.py
--- a/pypy/module/micronumpy/selection.py
+++ b/pypy/module/micronumpy/selection.py
@@ -120,7 +120,7 @@
 ArgSort = make_timsort_class(arg_getitem, arg_setitem, arg_length,
  arg_getitem_slice, arg_lt)
 
-def argsort(arr, space, w_axis, itemsize):
+def argsort(arr, space, w_axis):
 if w_axis is space.w_None:
 # note that it's fine ot pass None here as we're not going
 # to pass the result around (None is the link to base in slices)
@@ -138,7 +138,7 @@
 if len(arr.get_shape()) == 1:
 for i in range(arr.get_size()):
 raw_storage_setitem(storage, i * INT_SIZE, i)
-r = Repr(INT_SIZE, itemsize, arr.get_size(), arr_storage,
+r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage,
  storage, 0, arr.start)
 ArgSort(r).sort()
 else:
@@ -174,8 +174,7 @@
 itemtype = arr.dtype.itemtype
 for tp in all_types:
 if isinstance(itemtype, tp[0]):
-return cache._lookup(tp)(arr, space, w_axis,
- itemtype.get_element_size())
+return cache._lookup(tp)(arr, space, w_axis)
 # XXX this should probably be changed
 raise oefmt(space.w_NotImplementedError,
 sorting of non-numeric types '%s' is not implemented,
@@ -272,7 +271,7 @@
 ArgSort = make_timsort_class(arg_getitem, arg_setitem, arg_length,
  arg_getitem_slice, arg_lt)
 
-def sort(arr, space, w_axis, itemsize):
+def sort(arr, space, w_axis):
 if w_axis is space.w_None:
 # note that it's fine to pass None here as we're not going
 # to pass the result around (None is the link to base in slices)
@@ -284,7 +283,7 @@
 axis = space.int_w(w_axis)
 with arr as storage:
 if len(arr.get_shape()) == 1:
-r = Repr(itemsize, arr.get_size(), storage,
+r = Repr(arr.strides[0], arr.get_size(), storage,
  arr.start)
 ArgSort(r).sort()
 else:
@@ -313,8 +312,7 @@
 sorting of non-native byteorder not supported yet)
 for tp in all_types:
 if isinstance(itemtype, tp[0]):
-return cache._lookup(tp)(arr, space, w_axis,
- itemtype.get_element_size())
+return cache._lookup(tp)(arr, space, w_axis)
 # XXX this should probably be changed
 raise oefmt(space.w_NotImplementedError,
 sorting of non-numeric types '%s' is not implemented,
diff --git a/pypy/module/micronumpy/test/test_selection.py 
b/pypy/module/micronumpy/test/test_selection.py
--- a/pypy/module/micronumpy/test/test_selection.py
+++ b/pypy/module/micronumpy/test/test_selection.py
@@ -82,6 +82,13 @@
 #assert (a == b).all(), \
 #'a,orig,dtype %r,%r,%r' % (a,c,dtype)
 
+def test_sort_noncontiguous(self):
+from numpy import array
+x = array([[2, 10], [1, 11]])
+assert (x[:, 0].argsort() == [1, 0]).all()
+x[:, 0].sort()
+assert (x == [[1, 10], [2, 11]]).all()
+
 # tests from numpy/tests/test_multiarray.py
 def test_sort_corner_cases(self):
 # test ordering for floats and complex containing nans. It is only
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix random.getstate() to return longs so that repr(state) is identical w/cpython

2015-03-07 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r76275:a2a352a3b535
Date: 2015-03-07 22:51 +0200
http://bitbucket.org/pypy/pypy/changeset/a2a352a3b535/

Log:test, fix random.getstate() to return longs so that repr(state) is
identical w/cpython

diff --git a/pypy/module/_random/interp_random.py 
b/pypy/module/_random/interp_random.py
--- a/pypy/module/_random/interp_random.py
+++ b/pypy/module/_random/interp_random.py
@@ -4,7 +4,7 @@
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.baseobjspace import W_Root
-from rpython.rlib.rarithmetic import r_uint, intmask
+from rpython.rlib.rarithmetic import r_uint, intmask, widen
 from rpython.rlib import rbigint, rrandom, rstring
 
 
@@ -54,8 +54,8 @@
 def getstate(self, space):
 state = [None] * (rrandom.N + 1)
 for i in range(rrandom.N):
-state[i] = space.newint(intmask(self._rnd.state[i]))
-state[rrandom.N] = space.newint(self._rnd.index)
+state[i] = space.wrap(widen(self._rnd.state[i]))
+state[rrandom.N] = space.newlong(self._rnd.index)
 return space.newtuple(state)
 
 def setstate(self, space, w_state):
diff --git a/pypy/module/_random/test/test_random.py 
b/pypy/module/_random/test/test_random.py
--- a/pypy/module/_random/test/test_random.py
+++ b/pypy/module/_random/test/test_random.py
@@ -41,6 +41,17 @@
 # does not crash
 rnd1.setstate((-1, ) * 624 + (0, ))
 
+def test_state_repr(self):
+# since app-level jumpahead salts with repr(state),
+# it is important the repr is consistent with cpython
+import _random
+rnd = _random.Random()
+rnd.seed(1234)
+state = rnd.getstate()
+s = repr(state)
+assert len(s) == 7956
+assert s.count('L') == 625
+
 def test_seed(self):
 import _random, sys
 rnd = _random.Random()
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for mmap subtype not writeable; asarray not respecting implementation.start

2015-03-02 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r76225:c5c4df0df240
Date: 2015-03-02 21:05 +0200
http://bitbucket.org/pypy/pypy/changeset/c5c4df0df240/

Log:test, fix for mmap subtype not writeable; asarray not respecting
implementation.start

diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py
--- a/pypy/module/micronumpy/base.py
+++ b/pypy/module/micronumpy/base.py
@@ -46,7 +46,7 @@
 @staticmethod
 def from_shape_and_storage(space, shape, storage, dtype, storage_bytes=-1,
order='C', owning=False, w_subtype=None,
-   w_base=None, writable=True, strides=None):
+   w_base=None, writable=True, strides=None, 
start=0):
 from pypy.module.micronumpy import concrete
 from pypy.module.micronumpy.strides import (calc_strides,
 calc_backstrides)
@@ -75,8 +75,9 @@
 raise OperationError(space.w_ValueError,
 space.wrap(Cannot have owning=True when specifying a 
buffer))
 if writable:
-impl = concrete.ConcreteArrayWithBase(shape, dtype, order, 
strides,
-  backstrides, storage, 
w_base)
+impl = concrete.ConcreteArrayWithBase(shape, dtype, order,
+strides, backstrides, storage, w_base,
+start=start)
 else:
 impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, 
order,
  strides, 
backstrides,
diff --git a/pypy/module/micronumpy/concrete.py 
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -331,7 +331,7 @@
 
 
 class ConcreteArrayNotOwning(BaseConcreteArray):
-def __init__(self, shape, dtype, order, strides, backstrides, storage):
+def __init__(self, shape, dtype, order, strides, backstrides, storage, 
start=0):
 make_sure_not_resized(shape)
 make_sure_not_resized(strides)
 make_sure_not_resized(backstrides)
@@ -342,6 +342,7 @@
 self.strides = strides
 self.backstrides = backstrides
 self.storage = storage
+self.start = start
 
 def fill(self, space, box):
 self.dtype.itemtype.fill(self.storage, self.dtype.elsize,
@@ -350,7 +351,7 @@
 def set_shape(self, space, orig_array, new_shape):
 strides, backstrides = calc_strides(new_shape, self.dtype,
 self.order)
-return SliceArray(0, strides, backstrides, new_shape, self,
+return SliceArray(self.start, strides, backstrides, new_shape, self,
   orig_array)
 
 def set_dtype(self, space, dtype):
@@ -384,9 +385,10 @@
 
 
 class ConcreteArrayWithBase(ConcreteArrayNotOwning):
-def __init__(self, shape, dtype, order, strides, backstrides, storage, 
orig_base):
+def __init__(self, shape, dtype, order, strides, backstrides, storage,
+ orig_base, start=0):
 ConcreteArrayNotOwning.__init__(self, shape, dtype, order,
-strides, backstrides, storage)
+strides, backstrides, storage, start)
 self.orig_base = orig_base
 
 def base(self):
diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -99,10 +99,11 @@
 for i in range(w_object.get_size()):
 elems_w[i] = w_object.implementation.getitem(i * elsize)
 else:
+imp = w_object.implementation
 sz = support.product(w_object.get_shape()) * dtype.elsize
 return W_NDimArray.from_shape_and_storage(space,
-w_object.get_shape(),w_object.implementation.storage,
-dtype, storage_bytes=sz, w_base=w_object)
+w_object.get_shape(),imp.storage, dtype, storage_bytes=sz, 
+w_base=w_object, start=imp.start)
 else:
 # not an array
 shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype)
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -2,7 +2,7 @@
 
 
 class AppTestSupport(BaseNumpyAppTest):
-spaceconfig = dict(usemodules=[micronumpy, struct, binascii])
+spaceconfig = dict(usemodules=[micronumpy, struct, binascii, mmap])
 
 def setup_class(cls):
 BaseNumpyAppTest.setup_class.im_func(cls)
@@ -476,3 +476,120 @@
 a = self.SubType(array([[1, 2], [3, 4]]))
 b = array(a, subok=False)
 assert type(b) is ndarray
+
+

[pypy-commit] pypy default: test, fix load_dh_params get_saved_errno for win32

2015-02-16 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r75929:1bf192278b3f
Date: 2015-02-17 00:11 +0200
http://bitbucket.org/pypy/pypy/changeset/1bf192278b3f/

Log:test, fix load_dh_params get_saved_errno for win32

diff --git a/pypy/module/_ssl/test/test_ssl.py 
b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -361,12 +361,14 @@
 assert ctx.cert_store_stats() == {'x509_ca': 0, 'crl': 0, 'x509': 1}
 
 def test_load_dh_params(self):
-import _ssl
+import _ssl, errno
 ctx = _ssl._SSLContext(_ssl.PROTOCOL_TLSv1)
 ctx.load_dh_params(self.dh512)
 raises(TypeError, ctx.load_dh_params)
 raises(TypeError, ctx.load_dh_params, None)
 raises(_ssl.SSLError, ctx.load_dh_params, self.keycert)
+exc = raises(IOError, ctx.load_dh_params, inexistent.pem)
+assert exc.value.errno == errno.ENOENT
 
 def test_set_ecdh_curve(self):
 import _ssl
diff --git a/rpython/rlib/ropenssl.py b/rpython/rlib/ropenssl.py
--- a/rpython/rlib/ropenssl.py
+++ b/rpython/rlib/ropenssl.py
@@ -5,7 +5,7 @@
 from rpython.translator.platform import platform
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rlib.unroll import unrolling_iterable
-from rpython.rlib._rsocket_rffi import MAX_FD_SIZE, SAVE_ERR
+from rpython.rlib._rsocket_rffi import SAVE_ERR
 
 
 if sys.platform == 'win32' and platform.name != 'mingw32':
@@ -467,7 +467,7 @@
 ssl_external('BIO_new', [BIO_METHOD], BIO)
 ssl_external('BIO_set_nbio', [BIO, rffi.INT], rffi.INT, macro=True)
 ssl_external('BIO_new_file', [rffi.CCHARP, rffi.CCHARP], BIO,
- save_err=SAVE_ERR)
+ save_err=rffi.RFFI_FULL_ERRNO_ZERO)
 ssl_external('BIO_new_mem_buf', [rffi.VOIDP, rffi.INT], BIO)
 ssl_external('BIO_free', [BIO], rffi.INT)
 ssl_external('BIO_reset', [BIO], rffi.INT, macro=True)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for array(..., dtype=xxx, subok=True)

2015-02-14 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r75871:c745a42c6abe
Date: 2015-02-14 20:28 +0200
http://bitbucket.org/pypy/pypy/changeset/c745a42c6abe/

Log:test, fix for array(..., dtype=xxx, subok=True)

diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -81,7 +81,7 @@
 return w_object.descr_copy(space, w_order)
 elif not copy and (subok or type(w_object) is W_NDimArray):
 return w_object
-if subok:
+if subok and not type(w_object) is W_NDimArray:
 raise oefmt(space.w_NotImplementedError, 
 array(..., subok=True) only partially implemented)
 # we have a ndarray, but need to copy or change dtype 
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -26,6 +26,12 @@
 self.called_finalize = True
 return SubType ''')
 
+def test_subtype_ndarray(self):
+from numpy import arange, array
+a = arange(24, dtype='int32').reshape((6,4))
+b = array(a, dtype='float64', subok=True)
+assert (a == b).all()
+
 def test_subtype_base(self):
 from numpy import ndarray, dtype
 class C(ndarray):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix creating an array from a flatiter

2015-02-12 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r75843:5bd4d9346f9a
Date: 2015-02-12 23:01 +0200
http://bitbucket.org/pypy/pypy/changeset/5bd4d9346f9a/

Log:test, fix creating an array from a flatiter

diff --git a/pypy/module/micronumpy/flatiter.py 
b/pypy/module/micronumpy/flatiter.py
--- a/pypy/module/micronumpy/flatiter.py
+++ b/pypy/module/micronumpy/flatiter.py
@@ -15,6 +15,7 @@
 self._base = base
 self.dtype = base.get_dtype()
 self.shape = [base.get_size()]
+self.storage = self._base.implementation.storage
 
 def base(self):
 return self._base
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2852,6 +2852,13 @@
 c.flat = ['defgh', 'ijklmnop']
 assert (c.flatten() == ['def', 'ijk']*5).all()
 
+def test_flatiter_subtype(self):
+from numpy import array
+x = array([[1, 2], [3, 4]]).T
+y = array(x.flat)
+assert (x == [[1, 3], [2, 4]]).all()
+
+
 def test_slice_copy(self):
 from numpy import zeros
 a = zeros((10, 10))
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix and start to optimize a=np.array(b) where b is a ndarray subtype

2015-02-01 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r75638:a0d3e64efd8b
Date: 2015-02-02 09:23 +0200
http://bitbucket.org/pypy/pypy/changeset/a0d3e64efd8b/

Log:test, fix and start to optimize a=np.array(b) where b is a ndarray
subtype

diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -82,9 +82,18 @@
 return w_object.descr_copy(space, w_order)
 elif not copy and (subok or type(w_object) is W_NDimArray):
 return w_object
-
-# not an array or incorrect dtype
-shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype)
+if isinstance(w_object, W_NDimArray) and copy and not subok:
+# TODO do the loop.assign without copying elems_w
+shape = w_object.get_shape()
+_elems_w = w_object.reshape(space, space.wrap(-1))
+elems_w = [None] * w_object.get_size()
+for i in range(len(elems_w)):
+elems_w[i] = _elems_w.descr_getitem(space, space.wrap(i))
+if space.is_none(w_dtype):
+dtype = w_object.get_dtype()
+else:
+# not an array
+shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype)
 if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize  1):
 dtype = strides.find_dtype_for_seq(space, elems_w, dtype)
 if dtype is None:
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -304,10 +304,13 @@
 out.shape = (sh, 1)
 else:
 out.shape = (1, sh)
-print 'out, shape was',old_shape,'now',out.shape
+#print 'out, shape was',old_shape,'now',out.shape,'out',out
 return out
-a = matrix([[1., 2.]])
+a = matrix([[1., 2.], [3., 4.]])
 b = N.array([a])
+assert (b == a).all()
+b = N.array(a)
+assert len(b.shape) == 2
 
 def test_setstate_no_version(self):
 # Some subclasses of ndarray, like MaskedArray, do not use
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix bogus shape, stride, backstride creation in SliceIterator

2015-01-28 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r75575:7e9970164f05
Date: 2015-01-29 07:07 +0200
http://bitbucket.org/pypy/pypy/changeset/7e9970164f05/

Log:test, fix bogus shape,stride,backstride creation in SliceIterator

diff --git a/pypy/module/micronumpy/nditer.py b/pypy/module/micronumpy/nditer.py
--- a/pypy/module/micronumpy/nditer.py
+++ b/pypy/module/micronumpy/nditer.py
@@ -252,10 +252,6 @@
 # Copy logic from npyiter_coalesce_axes, used in ufunc iterators
 # and in nditer's with 'external_loop' flag
 can_coalesce = True
-if it.order == 'F':
-fastest = 0
-else:
-fastest = -1
 for idim in range(it.ndim - 1):
 for op_it, _ in it.iters:
 if op_it is None:
@@ -275,7 +271,7 @@
 if can_coalesce:
 for i in range(len(it.iters)):
 new_iter = coalesce_iter(it.iters[i][0], it.op_flags[i], it,
- it.order, fastest)
+ it.order)
 it.iters[i] = (new_iter, new_iter.reset())
 if len(it.shape)  1:
 if it.order == 'F':
@@ -289,7 +285,7 @@
 break
 # Always coalesce at least one
 for i in range(len(it.iters)):
-new_iter = coalesce_iter(it.iters[i][0], it.op_flags[i], it, 'C', -1)
+new_iter = coalesce_iter(it.iters[i][0], it.op_flags[i], it, 'C')
 it.iters[i] = (new_iter, new_iter.reset())
 if len(it.shape)  1:
 if it.order == 'F':
@@ -300,12 +296,11 @@
 it.shape = [1]
 
 
-def coalesce_iter(old_iter, op_flags, it, order, fastest=-1, flat=True):
+def coalesce_iter(old_iter, op_flags, it, order, flat=True):
 '''
 We usually iterate through an array one value at a time.
 But after coalesce(), getoperand() will return a slice by removing
-the fastest varying dimension from the beginning or end of the shape.
-XXX - what happens on swapaxis arrays?
+the fastest varying dimension(s) from the beginning or end of the shape.
 If flat is true, then the slice will be 1d, otherwise stack up the shape of
 the fastest varying dimension in the slice, so an iterator of a  'C' array 
 of shape (2,4,3) after two calls to coalesce will iterate 2 times over a 
slice
@@ -319,6 +314,9 @@
 new_strides = strides[1:]
 new_backstrides = backstrides[1:]
 _stride = old_iter.slice_stride + [strides[0]]
+_shape =  old_iter.slice_shape + [shape[0]]
+_backstride = old_iter.slice_backstride + [strides[0] * (shape[0] - 1)]
+fastest = shape[0]
 else:
 new_shape = shape[:-1]
 new_strides = strides[:-1]
@@ -326,14 +324,15 @@
 # use the operand's iterator's rightmost stride,
 # even if it is not the fastest (for 'F' or swapped axis)
 _stride = [strides[-1]] + old_iter.slice_stride
-_shape = [shape[fastest]]  + old_iter.slice_shape
-_backstride = [(_shape[fastest] - 1) * _stride[0]] + 
old_iter.slice_backstride
+_shape = [shape[-1]]  + old_iter.slice_shape
+_backstride = [(shape[-1] - 1) * strides[-1]] + 
old_iter.slice_backstride
+fastest = shape[-1]
 if flat:
 _shape = [support.product(_shape)]
 if len(_stride)  1:
 _stride = [min(_stride[0], _stride[1])]
 _backstride = [(shape[0] - 1) * _stride[0]]
-return SliceIter(old_iter.array, old_iter.size / shape[fastest],
+return SliceIter(old_iter.array, old_iter.size / fastest,
 new_shape, new_strides, new_backstrides,
 _shape, _stride, _backstride, op_flags, it)
 
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py 
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -269,6 +269,26 @@
 aout = ufunc_sum(ai)
 assert aout.shape == (3, 3)
 
+def test_frompyfunc_fortran(self):
+import numpy as np
+def tofrom_fortran(in0, out0):
+out0[:] = in0.T
+
+def lapack_like_times2(in0, out0):
+a = np.empty(in0.T.shape, in0.dtype)
+tofrom_fortran(in0, a)
+a *= 2
+tofrom_fortran(a, out0)
+
+times2 = np.frompyfunc([lapack_like_times2], 1, 1,
+signature='(m,n)-(m,n)',
+dtypes=[np.dtype(float), np.dtype(float)],
+stack_inputs=True,
+  )
+in0 = np.arange(3300, dtype=float).reshape(100, 33)
+out0 = times2(in0)
+assert out0.shape == in0.shape
+assert (out0 == in0 * 2).all()
 
 def test_ufunc_kwargs(self):
 from numpy import ufunc, frompyfunc, arange, dtype
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -651,14 +651,10 @@
   

[pypy-commit] pypy default: test, fix overflow on abs(complex), we have no np.seterr - RuntimeWarning yet

2014-12-28 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r75132:035393735c13
Date: 2014-12-28 17:36 +0200
http://bitbucket.org/pypy/pypy/changeset/035393735c13/

Log:test, fix overflow on abs(complex), we have no np.seterr -
RuntimeWarning yet

diff --git a/pypy/module/micronumpy/test/test_complex.py 
b/pypy/module/micronumpy/test/test_complex.py
--- a/pypy/module/micronumpy/test/test_complex.py
+++ b/pypy/module/micronumpy/test/test_complex.py
@@ -478,6 +478,15 @@
 for i in range(4):
 assert c[i] == max(a[i], b[i])
 
+
+def test_abs_overflow(self):
+from numpy import array, absolute, isinf
+a = array(complex(1.5e308,1.5e308))
+# Prints a RuntimeWarning, but does not raise
+b = absolute(a)
+assert isinf(b)
+
+
 def test_basic(self):
 import sys
 from numpy import (dtype, add, array, dtype,
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
@@ -1195,7 +1195,11 @@
 
 @complex_to_real_unary_op
 def abs(self, v):
-return rcomplex.c_abs(v[0], v[1])
+try:
+return rcomplex.c_abs(v[0], v[1])
+except OverflowError:
+# warning ...
+return rfloat.INFINITY
 
 @raw_unary_op
 def isnan(self, v):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix view of a slice

2014-12-11 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r74891:b75a06bcc48d
Date: 2014-12-11 18:36 +0200
http://bitbucket.org/pypy/pypy/changeset/b75a06bcc48d/

Log:test, fix view of a slice

diff --git a/pypy/module/micronumpy/flagsobj.py 
b/pypy/module/micronumpy/flagsobj.py
--- a/pypy/module/micronumpy/flagsobj.py
+++ b/pypy/module/micronumpy/flagsobj.py
@@ -5,46 +5,26 @@
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.module.micronumpy import constants as NPY
-
+from pypy.module.micronumpy.strides import is_c_contiguous, is_f_contiguous
 
 def enable_flags(arr, flags):
 arr.flags |= flags
 
-
 def clear_flags(arr, flags):
 arr.flags = ~flags
 
-
-@jit.unroll_safe
 def _update_contiguous_flags(arr):
-shape = arr.shape
-strides = arr.strides
-
-is_c_contig = True
-sd = arr.dtype.elsize
-for i in range(len(shape) - 1, -1, -1):
-dim = shape[i]
-if strides[i] != sd:
-is_c_contig = False
-break
-if dim == 0:
-break
-sd *= dim
+is_c_contig = is_c_contiguous(arr)
 if is_c_contig:
 enable_flags(arr, NPY.ARRAY_C_CONTIGUOUS)
 else:
 clear_flags(arr, NPY.ARRAY_C_CONTIGUOUS)
 
-sd = arr.dtype.elsize
-for i in range(len(shape)):
-dim = shape[i]
-if strides[i] != sd:
-clear_flags(arr, NPY.ARRAY_F_CONTIGUOUS)
-return
-if dim == 0:
-break
-sd *= dim
-enable_flags(arr, NPY.ARRAY_F_CONTIGUOUS)
+is_f_contig = is_f_contiguous(arr)
+if is_f_contig:
+enable_flags(arr, NPY.ARRAY_F_CONTIGUOUS)
+else:
+clear_flags(arr, NPY.ARRAY_F_CONTIGUOUS)
 
 
 class W_FlagsObject(W_Root):
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -19,7 +19,7 @@
 order_converter, shape_converter, searchside_converter
 from pypy.module.micronumpy.flagsobj import W_FlagsObject
 from pypy.module.micronumpy.strides import get_shape_from_iterable, \
-shape_agreement, shape_agreement_multiple
+shape_agreement, shape_agreement_multiple, is_c_contiguous, is_f_contiguous
 
 
 def _match_dot_shapes(space, left, right):
@@ -837,7 +837,15 @@
 raise OperationError(space.w_ValueError, space.wrap(
 new type not compatible with array.))
 else:
-if dims == 1 or impl.get_strides()[0]  impl.get_strides()[-1]:
+if not is_c_contiguous(impl) and not is_f_contiguous(impl):
+if old_itemsize != new_itemsize:
+raise OperationError(space.w_ValueError, space.wrap(
+new type not compatible with array.))
+# Strides, shape does not change
+v = impl.astype(space, dtype)
+return wrap_impl(space, w_type, self, v) 
+strides = impl.get_strides()
+if dims == 1 or strides[0] strides[-1]:
 # Column-major, resize first dimension
 if new_shape[0] * old_itemsize % new_itemsize != 0:
 raise OperationError(space.w_ValueError, space.wrap(
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
@@ -429,3 +429,35 @@
 n_old_elems_to_use *= old_shape[oldI]
 assert len(new_strides) == len(new_shape)
 return new_strides[:]
+
+@jit.unroll_safe
+def is_c_contiguous(arr):
+shape = arr.get_shape()
+strides = arr.get_strides()
+ret = True
+sd = arr.dtype.elsize
+for i in range(len(shape) - 1, -1, -1):
+dim = shape[i]
+if strides[i] != sd:
+ret = False
+break
+if dim == 0:
+break
+sd *= dim
+return ret
+
+@jit.unroll_safe
+def is_f_contiguous(arr):
+shape = arr.get_shape()
+strides = arr.get_strides()
+ret = True
+sd = arr.dtype.elsize
+for i in range(len(shape)):
+dim = shape[i]
+if strides[i] != sd:
+ret = False
+break
+if dim == 0:
+break
+sd *= dim
+return ret
diff --git a/pypy/module/micronumpy/test/test_iterators.py 
b/pypy/module/micronumpy/test/test_iterators.py
--- a/pypy/module/micronumpy/test/test_iterators.py
+++ b/pypy/module/micronumpy/test/test_iterators.py
@@ -13,6 +13,11 @@
 self.strides = strides
 self.start = start
 
+def get_shape(self):
+return self.shape
+
+def get_strides(self):
+return self.strides
 
 class TestIterDirect(object):
 def test_iterator_basic(self):
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ 

[pypy-commit] pypy default: test/fix ufunc reduce with comparison func when dtype specified

2014-12-04 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r74824:4213885db36d
Date: 2014-12-05 00:23 -0500
http://bitbucket.org/pypy/pypy/changeset/4213885db36d/

Log:test/fix ufunc reduce with comparison func when dtype specified

diff --git a/pypy/module/micronumpy/test/test_ufuncs.py 
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -785,6 +785,7 @@
 assert exc.value[0] == 'axis' entry is out of bounds
 
 def test_reduce_1d(self):
+import numpy as np
 from numpypy import array, add, maximum, less, float16, complex64
 
 assert less.reduce([5, 4, 3, 2, 1])
@@ -799,6 +800,10 @@
 assert type(add.reduce(array([True, False] * 200, dtype='float16'))) 
is float16
 assert type(add.reduce(array([True, False] * 200, dtype='complex64'))) 
is complex64
 
+for dtype in ['bool', 'int']:
+assert np.equal.reduce([1, 2], dtype=dtype) == True
+assert np.equal.reduce([1, 2, 0], dtype=dtype) == False
+
 def test_reduceND(self):
 from numpypy import add, arange
 a = arange(12).reshape(3, 4)
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -196,16 +196,15 @@
 axis += shapelen
 assert axis = 0
 dtype = descriptor.decode_w_dtype(space, dtype)
-if dtype is None:
-if self.comparison_func:
-dtype = descriptor.get_dtype_cache(space).w_booldtype
-else:
-dtype = find_unaryop_result_dtype(
-space, obj.get_dtype(),
-promote_to_float=self.promote_to_float,
-promote_to_largest=self.promote_to_largest,
-promote_bools=self.promote_bools,
-)
+if self.comparison_func:
+dtype = descriptor.get_dtype_cache(space).w_booldtype
+elif dtype is None:
+dtype = find_unaryop_result_dtype(
+space, obj.get_dtype(),
+promote_to_float=self.promote_to_float,
+promote_to_largest=self.promote_to_largest,
+promote_bools=self.promote_bools,
+)
 if self.identity is None:
 for i in range(shapelen):
 if space.is_none(w_axis) or i == axis:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix

2014-09-30 Thread arigo
Author: Armin Rigo ar...@tunes.org
Branch: 
Changeset: r73746:90df6dfc9c89
Date: 2014-09-30 14:32 +0200
http://bitbucket.org/pypy/pypy/changeset/90df6dfc9c89/

Log:Test fix

diff --git a/rpython/memory/gc/incminimark.py b/rpython/memory/gc/incminimark.py
--- a/rpython/memory/gc/incminimark.py
+++ b/rpython/memory/gc/incminimark.py
@@ -787,8 +787,9 @@
 # and clear these bytes.
 i = 0
 while i  cardheadersize:
-llarena.arena_reserve(arena + i, llmemory.sizeof(lltype.Char))
-arena.char[i] = '\x00'
+p = arena + i
+llarena.arena_reserve(p, llmemory.sizeof(lltype.Char))
+p.char[0] = '\x00'
 i += 1
 #
 # Reserve the actual object.  (This is a no-op in C).
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix stdin/stdout/stderr file name

2014-09-10 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73427:f9915af38176
Date: 2014-09-10 19:08 -0400
http://bitbucket.org/pypy/pypy/changeset/f9915af38176/

Log:test/fix stdin/stdout/stderr file name

diff --git a/pypy/module/sys/state.py b/pypy/module/sys/state.py
--- a/pypy/module/sys/state.py
+++ b/pypy/module/sys/state.py
@@ -36,17 +36,17 @@
 
 stdin = W_File(space)
 stdin.file_fdopen(0, r, 1)
-stdin.name = 'stdin'
+stdin.w_name = space.wrap('stdin')
 self.w_stdin = space.wrap(stdin)
 
 stdout = W_File(space)
 stdout.file_fdopen(1, w, 1)
-stdout.name = 'stdout'
+stdout.w_name = space.wrap('stdout')
 self.w_stdout = space.wrap(stdout)
 
 stderr = W_File(space)
 stderr.file_fdopen(2, w, 0)
-stderr.name = 'stderr'
+stderr.w_name = space.wrap('stderr')
 self.w_stderr = space.wrap(stderr)
 
 stdin._when_reading_first_flush(stdout)
diff --git a/pypy/module/sys/test/test_sysmodule.py 
b/pypy/module/sys/test/test_sysmodule.py
--- a/pypy/module/sys/test/test_sysmodule.py
+++ b/pypy/module/sys/test/test_sysmodule.py
@@ -91,6 +91,10 @@
 assert isinstance(sys.__stderr__, file)
 assert isinstance(sys.__stdin__, file)
 
+assert sys.__stdin__.name == stdin
+assert sys.__stdout__.name == stdout
+assert sys.__stderr__.name == stderr
+
 if self.appdirect and not isinstance(sys.stdin, file):
 return
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix fdopen buffering

2014-09-10 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73436:d00e4c12507a
Date: 2014-09-10 21:11 -0400
http://bitbucket.org/pypy/pypy/changeset/d00e4c12507a/

Log:test/fix fdopen buffering

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -153,18 +153,12 @@
 finally:
 lltype.free(ll_name, flavor='raw')
 _dircheck(ll_file)
-if buffering = 0:
-buf = lltype.nullptr(rffi.CCHARP.TO)
-if buffering == 0:
-c_setvbuf(ll_file, buf, _IONBF, 0)
-elif buffering == 1:
-c_setvbuf(ll_file, buf, _IOLBF, BUFSIZ)
-else:
-c_setvbuf(ll_file, buf, _IOFBF, buffering)
-return RFile(ll_file, mode)
+f = RFile(ll_file, mode)
+f._setbufsize(buffering)
+return f
 
 
-def create_fdopen_rfile(fd, mode=r):
+def create_fdopen_rfile(fd, mode=r, buffering=-1):
 newmode = _sanitize_mode(mode)
 fd = rffi.cast(rffi.INT, fd)
 rposix.validate_fd(fd)
@@ -177,7 +171,9 @@
 finally:
 lltype.free(ll_mode, flavor='raw')
 _dircheck(ll_file)
-return RFile(ll_file, mode)
+f = RFile(ll_file, mode)
+f._setbufsize(buffering)
+return f
 
 
 def create_temp_rfile():
@@ -213,6 +209,7 @@
 
 
 class RFile(object):
+_setbuf = lltype.nullptr(rffi.CCHARP.TO)
 _univ_newline = False
 _newlinetypes = NEWLINE_UNKNOWN
 _skipnextlf = False
@@ -223,6 +220,23 @@
 self._univ_newline = 'U' in mode
 self._close2 = close2
 
+def _setbufsize(self, bufsize):
+if bufsize = 0:
+if bufsize == 0:
+mode = _IONBF
+elif bufsize == 1:
+mode = _IOLBF
+bufsize = BUFSIZ
+else:
+mode = _IOFBF
+if self._setbuf:
+lltype.free(self._setbuf, flavor='raw')
+if mode == _IONBF:
+self._setbuf = lltype.nullptr(rffi.CCHARP.TO)
+else:
+self._setbuf = lltype.malloc(rffi.CCHARP.TO, bufsize, 
flavor='raw')
+c_setvbuf(self._ll_file, self._setbuf, mode, bufsize)
+
 def __del__(self):
 Closes the described file when the object's last reference
 goes away.  Unlike an explicit call to close(), this is meant
@@ -233,6 +247,8 @@
 do_close = self._close2[1]
 if do_close:
 do_close(ll_file)   # return value ignored
+if self._setbuf:
+lltype.free(self._setbuf, flavor='raw')
 
 def _cleanup_(self):
 self._ll_file = lltype.nullptr(FILEP.TO)
@@ -251,11 +267,16 @@
 # double close is allowed
 self._ll_file = lltype.nullptr(FILEP.TO)
 do_close = self._close2[0]
-if do_close:
-res = do_close(ll_file)
-if res == -1:
-errno = rposix.get_errno()
-raise IOError(errno, os.strerror(errno))
+try:
+if do_close:
+res = do_close(ll_file)
+if res == -1:
+errno = rposix.get_errno()
+raise IOError(errno, os.strerror(errno))
+finally:
+if self._setbuf:
+lltype.free(self._setbuf, flavor='raw')
+self._setbuf = lltype.nullptr(rffi.CCHARP.TO)
 return res
 
 def _check_closed(self):
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -104,15 +104,53 @@
 f()
 self.interpret(f, [])
 
+@py.test.mark.skipif(sys.platform == 'win32')
+# http://msdn.microsoft.com/en-us/library/86cebhfs.aspx
+def test_fdopen_buffering_line(self):
+fname = str(self.tmpdir.join('file_1a'))
+
+def f():
+g = open(fname, 'w')
+f = os.fdopen(os.dup(g.fileno()), 'w', 1)
+g.close()
+f.write('dupa\ndupb')
+f2 = open(fname, 'r')
+assert f2.read() == 'dupa\n'
+f.close()
+assert f2.read() == 'dupb'
+f2.close()
+
+f()
+self.interpret(f, [])
+
 def test_open_buffering_full(self):
 fname = str(self.tmpdir.join('file_1b'))
 
 def f():
 f = open(fname, 'w', 128)
-f.write('dupa')
+f.write('dupa\ndupb')
 f2 = open(fname, 'r')
 assert f2.read() == ''
-f.write('z' * 5000)
+f.write('z' * 120)
+assert f2.read() != ''
+f.close()
+assert f2.read() != ''
+f2.close()
+
+f()
+self.interpret(f, [])
+
+def test_fdopen_buffering_full(self):
+fname = str(self.tmpdir.join('file_1b'))
+
+def f():
+g = open(fname, 'w')
+f = 

[pypy-commit] pypy default: test/fix rfile seek case with univ newline

2014-09-09 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73393:c4e64d36778a
Date: 2014-09-09 11:34 -0400
http://bitbucket.org/pypy/pypy/changeset/c4e64d36778a/

Log:test/fix rfile seek case with univ newline

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -460,6 +460,7 @@
 if res == -1:
 errno = rposix.get_errno()
 raise IOError(errno, os.strerror(errno))
+self._skipnextlf = False
 
 def tell(self):
 self._check_closed()
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -212,6 +212,11 @@
 assert f.readline() == dupd
 assert f.tell() == 20
 assert f.readline() == 
+f.seek(0)
+assert f.readline() == dupa\n
+assert f.readline() == dupb\n
+f.seek(4)
+assert f.read(1) == \n
 f.close()
 
 f()
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix rfile tell with universal newlines

2014-09-08 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73387:dc540ed089f2
Date: 2014-09-08 19:35 -0400
http://bitbucket.org/pypy/pypy/changeset/dc540ed089f2/

Log:test/fix rfile tell with universal newlines

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -77,6 +77,7 @@
 _pclose2 = (c_pclose, c_pclose_in_del)
 
 c_getc = llexternal('getc', [FILEP], rffi.INT, macro=True)
+c_ungetc = llexternal('ungetc', [rffi.INT, FILEP], rffi.INT)
 c_fgets = llexternal('fgets', [rffi.CCHARP, rffi.INT, FILEP], rffi.CCHARP)
 c_fread = llexternal('fread', [rffi.CCHARP, rffi.SIZE_T, rffi.SIZE_T, FILEP],
  rffi.SIZE_T)
@@ -466,6 +467,14 @@
 if res == -1:
 errno = rposix.get_errno()
 raise IOError(errno, os.strerror(errno))
+if self._skipnextlf:
+c = c_getc(self._ll_file)
+if c == ord('\n'):
+self._newlinetypes |= NEWLINE_CRLF
+res += 1
+self._skipnextlf = False
+elif c != EOF:
+c_ungetc(c, self._ll_file)
 return res
 
 def fileno(self):
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -191,21 +191,26 @@
 
 def test_read_universal(self):
 fname = self.tmpdir.join('read_univ')
-fname.write(dupa\ndupb\r\ndupc)
+fname.write(dupa\ndupb\r\ndupc\rdupd)
 fname = str(fname)
 
 def f():
 f = open(fname, 'U')
-assert f.read() == dupa\ndupb\ndupc
+assert f.read() == dupa\ndupb\ndupc\ndupd
 assert f.read() == 
 f.seek(0)
-assert f.read(9) == dupa\ndupb
-assert f.read(42) == \ndupc
+assert f.read(10) == dupa\ndupb\n
+assert f.read(42) == dupc\ndupd
 assert f.read(1) == 
 f.seek(0)
 assert f.readline() == dupa\n
+assert f.tell() == 5
 assert f.readline() == dupb\n
-assert f.readline() == dupc
+assert f.tell() == 11
+assert f.readline() == dupc\n
+assert f.tell() == 16
+assert f.readline() == dupd
+assert f.tell() == 20
 assert f.readline() == 
 f.close()
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix for test_xpickle

2014-08-30 Thread arigo
Author: Armin Rigo ar...@tunes.org
Branch: 
Changeset: r73199:aba3692914d3
Date: 2014-08-30 06:44 +
http://bitbucket.org/pypy/pypy/changeset/aba3692914d3/

Log:Test fix for test_xpickle

diff --git a/lib-python/2.7/test/pickletester.py 
b/lib-python/2.7/test/pickletester.py
--- a/lib-python/2.7/test/pickletester.py
+++ b/lib-python/2.7/test/pickletester.py
@@ -6,14 +6,16 @@
 import pickletools
 import copy_reg
 
-from test.test_support import TestFailed, verbose, have_unicode, TESTFN, 
impl_detail
+from test.test_support import TestFailed, verbose, have_unicode, TESTFN
 try:
-from test.test_support import _2G, _1M, precisionbigmemtest
+from test.test_support import _2G, _1M, precisionbigmemtest, impl_detail
 except ImportError:
 # this import might fail when run on older Python versions by test_xpickle
 _2G = _1M = 0
 def precisionbigmemtest(*args, **kwargs):
 return lambda self: None
+def impl_detail(*args, **kwargs):
+return lambda self: None
 
 # Tests that try a number of pickle protocols should have a
 # for proto in protocols:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix rfile.truncate

2014-08-28 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73132:c337d9417d2e
Date: 2014-08-28 21:20 -0400
http://bitbucket.org/pypy/pypy/changeset/c337d9417d2e/

Log:test/fix rfile.truncate

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -233,6 +233,7 @@
 if self.ll_file:
 if arg == -1:
 arg = self.tell()
+self.flush()
 res = c_ftruncate(self.fileno(), arg)
 if res == -1:
 errno = rposix.get_errno()
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -140,14 +140,14 @@
 fname = str(self.tmpdir.join('file_trunc'))
 
 def f():
-f = open(fname, w)
-f.write(xyz)
+f = open(fname, w+b)
+f.write(hello world)
+f.seek(7)
+f.truncate()
 f.seek(0)
-f.truncate(2)
+data = f.read()
+assert data == hello w
 f.close()
-f2 = open(fname)
-assert f2.read() == xy
-f2.close()
 
 f()
 self.interpret(f, [])
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix rfile buffering

2014-08-28 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73135:7445b0d7890d
Date: 2014-08-28 18:05 -0400
http://bitbucket.org/pypy/pypy/changeset/7445b0d7890d/

Log:test/fix rfile buffering

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -29,10 +29,19 @@
 
 off_t = platform.SimpleType('off_t')
 
+_IONBF = platform.DefinedConstantInteger('_IONBF')
+_IOLBF = platform.DefinedConstantInteger('_IOLBF')
+_IOFBF = platform.DefinedConstantInteger('_IOFBF')
+BUFSIZ = platform.DefinedConstantInteger('BUFSIZ')
+
 config = platform.configure(CConfig)
 
 OFF_T = config['off_t']
 FILEP = rffi.COpaquePtr(FILE)
+_IONBF = config['_IONBF']
+_IOLBF = config['_IOLBF']
+_IOFBF = config['_IOFBF']
+BUFSIZ = config['BUFSIZ']
 
 c_fopen = llexternal('fopen', [rffi.CCHARP, rffi.CCHARP], FILEP)
 c_fclose = llexternal('fclose', [FILEP], rffi.INT, releasegil=False)
@@ -58,6 +67,7 @@
 
 c_popen = llexternal('popen', [rffi.CCHARP, rffi.CCHARP], FILEP)
 c_pclose = llexternal('pclose', [FILEP], rffi.INT, releasegil=False)
+c_setvbuf = llexternal('setvbuf', [FILEP, rffi.CCHARP, rffi.INT, rffi.SIZE_T], 
rffi.INT)
 
 BASE_BUF_SIZE = 4096
 BASE_LINE_SIZE = 100
@@ -70,7 +80,6 @@
 
 
 def create_file(filename, mode=r, buffering=-1):
-assert buffering == -1
 assert filename is not None
 assert mode is not None
 ll_name = rffi.str2charp(filename)
@@ -85,6 +94,13 @@
 lltype.free(ll_mode, flavor='raw')
 finally:
 lltype.free(ll_name, flavor='raw')
+if buffering = 0:
+if buffering == 0:
+c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IONBF, 0)
+elif buffering == 1:
+c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IOLBF, BUFSIZ)
+else:
+c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IOFBF, buffering)
 return RFile(ll_f)
 
 
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -20,6 +20,38 @@
 self.interpret(f, [])
 assert open(fname, r).read() == dupa
 
+def test_open_buffering_line(self):
+fname = str(self.tmpdir.join('file_1a'))
+
+def f():
+f = open(fname, 'w', 1)
+f.write('dupa\ndupb')
+f2 = open(fname, 'r')
+assert f2.read() == 'dupa\n'
+f.close()
+assert f2.read() == 'dupb'
+f2.close()
+
+f()
+self.interpret(f, [])
+
+def test_open_buffering_full(self):
+fname = str(self.tmpdir.join('file_1b'))
+
+def f():
+f = open(fname, 'w', 128)
+f.write('dupa')
+f2 = open(fname, 'r')
+assert f2.read() == ''
+f.write('z' * 5000)
+assert f2.read() != ''
+f.close()
+assert f2.read() != ''
+f2.close()
+
+f()
+self.interpret(f, [])
+
 def test_read_write(self):
 fname = str(self.tmpdir.join('file_2'))
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix check for directory in rfile open

2014-08-28 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73151:8986be62c395
Date: 2014-08-29 01:17 -0400
http://bitbucket.org/pypy/pypy/changeset/8986be62c395/

Log:test/fix check for directory in rfile open

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -3,7 +3,7 @@
 python builtin open()
 
 
-import os
+import os, stat, errno
 from rpython.rlib import rposix
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib.rstring import StringBuilder
@@ -79,9 +79,20 @@
 
 
 def _error(ll_file):
-errno = c_ferror(ll_file)
+err = c_ferror(ll_file)
 c_clearerr(ll_file)
-raise OSError(errno, os.strerror(errno))
+raise OSError(err, os.strerror(err))
+
+
+def _dircheck(ll_file):
+try:
+st = os.fstat(c_fileno(ll_file))
+except OSError:
+pass
+else:
+if stat.S_ISDIR(st[0]):
+err = errno.EISDIR
+raise OSError(err, os.strerror(err))
 
 
 def create_file(filename, mode=r, buffering=-1):
@@ -97,6 +108,7 @@
 lltype.free(ll_mode, flavor='raw')
 finally:
 lltype.free(ll_name, flavor='raw')
+_dircheck(ll_f)
 if buffering = 0:
 if buffering == 0:
 c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IONBF, 0)
@@ -124,6 +136,7 @@
 raise OSError(errno, os.strerror(errno))
 finally:
 lltype.free(ll_mode, flavor='raw')
+_dircheck(ll_f)
 return RFile(ll_f)
 
 
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -1,4 +1,4 @@
-import os, sys, py
+import os, sys, py, errno
 from rpython.rtyper.test.tool import BaseRtypingTest
 from rpython.tool.udir import udir
 from rpython.rlib import rfile
@@ -17,9 +17,37 @@
 f.write(dupa)
 f.close()
 
+f()
 self.interpret(f, [])
 assert open(fname, r).read() == dupa
 
+def test_open_errors(self):
+def f():
+try:
+open('zzz')
+except OSError as e:
+assert e.errno == errno.ENOENT
+else:
+assert False
+
+try:
+open('.')
+except OSError as e:
+assert e.errno == errno.EISDIR
+else:
+assert False
+
+fd = os.open('.', os.O_RDONLY, 0777)
+try:
+os.fdopen(fd)
+except OSError as e:
+assert e.errno == errno.EISDIR
+else:
+assert False
+os.close(fd)
+
+self.interpret(f, [])
+
 def test_open_buffering_line(self):
 fname = str(self.tmpdir.join('file_1a'))
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix rfile.read(0)

2014-08-28 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r73150:26d170cd6c24
Date: 2014-08-29 01:01 -0400
http://bitbucket.org/pypy/pypy/changeset/26d170cd6c24/

Log:test/fix rfile.read(0)

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -190,7 +190,9 @@
 # XXX CPython uses a more delicate logic here
 self._check_closed()
 ll_file = self.ll_file
-if size  0:
+if size == 0:
+return 
+elif size  0:
 # read the entire contents
 buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw')
 try:
@@ -206,7 +208,7 @@
 s.append_charpsize(buf, returned_size)
 finally:
 lltype.free(buf, flavor='raw')
-else:
+else:  # size  0
 with rffi.scoped_alloc_buffer(size) as buf:
 returned_size = c_fread(buf.raw, 1, size, ll_file)
 returned_size = intmask(returned_size)  # is between 0 and size
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -60,6 +60,8 @@
 f.write(dupa\x00dupb)
 f.close()
 f2 = open(fname)
+dupa = f2.read(0)
+assert dupa == 
 dupa = f2.read()
 assert dupa == dupa\x00dupb
 f2.seek(0)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for #1850

2014-08-20 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r72940:8ad8dd8f0ae3
Date: 2014-08-21 00:27 +0300
http://bitbucket.org/pypy/pypy/changeset/8ad8dd8f0ae3/

Log:test, fix for #1850

diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -78,9 +78,9 @@
 # arrays with correct dtype
 if isinstance(w_object, W_NDimArray) and \
 (space.is_none(w_dtype) or w_object.get_dtype() is dtype):
-if copy:
-return w_object.descr_copy(space)
-else:
+if copy and (subok or type(w_object) is W_NDimArray):
+return w_object.descr_copy(space, w_order)
+elif not copy and (subok or type(w_object) is W_NDimArray):
 return w_object
 
 # not an array or incorrect dtype
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -7,7 +7,7 @@
 def setup_class(cls):
 BaseNumpyAppTest.setup_class.im_func(cls)
 cls.w_NoNew = cls.space.appexec([], '''():
-from numpypy import ndarray
+from numpy import ndarray
 class NoNew(ndarray):
 def __new__(cls, subtype):
 raise ValueError('should not call __new__')
@@ -16,7 +16,7 @@
 self.called_finalize = True
 return NoNew ''')
 cls.w_SubType = cls.space.appexec([], '''():
-from numpypy import ndarray, array
+from numpy import ndarray, array
 class SubType(ndarray):
 def __new__(obj, input_array):
 obj = array(input_array, copy=False).view(obj)
@@ -27,7 +27,7 @@
 return SubType ''')
 
 def test_subtype_base(self):
-from numpypy import ndarray, dtype
+from numpy import ndarray, dtype
 class C(ndarray):
 def __new__(subtype, shape, dtype):
 self = ndarray.__new__(subtype, shape, dtype)
@@ -65,7 +65,7 @@
 assert b.base is a
 
 def test_subtype_view(self):
-from numpypy import ndarray, array
+from numpy import ndarray, array
 class matrix(ndarray):
 def __new__(subtype, data, dtype=None, copy=True):
 if isinstance(data, matrix):
@@ -89,7 +89,7 @@
 
 def test_finalize(self):
 #taken from 
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#simple-example-adding-an-extra-attribute-to-ndarray
-import numpypy as np
+import numpy as np
 class InfoArray(np.ndarray):
 def __new__(subtype, shape, dtype=float, buffer=None, offset=0,
   strides=None, order='C', info=None):
@@ -121,7 +121,7 @@
 assert cast_arr.info is None
 
 def test_sub_where(self):
-from numpypy import where, ones, zeros, array
+from numpy import where, ones, zeros, array
 a = array([1, 2, 3, 0, -3])
 v = a.view(self.NoNew)
 b = where(array(v)  0, ones(5), zeros(5))
@@ -130,14 +130,14 @@
 assert not isinstance(b, self.NoNew)
 
 def test_sub_repeat(self):
-from numpypy import array
+from numpy import array
 a = self.SubType(array([[1, 2], [3, 4]]))
 b = a.repeat(3)
 assert (b == [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]).all()
 assert isinstance(b, self.SubType)
 
 def test_sub_flatiter(self):
-from numpypy import array
+from numpy import array
 a = array(range(9)).reshape(3, 3).view(self.NoNew)
 c = array(range(9)).reshape(3, 3)
 assert isinstance(a.flat[:] + a.flat[:], self.NoNew)
@@ -146,7 +146,7 @@
 assert not isinstance(c.flat[:] + c.flat[:], self.NoNew)
 
 def test_sub_getitem_filter(self):
-from numpypy import array
+from numpy import array
 a = array(range(5))
 b = self.SubType(a)
 c = b[array([False, True, False, True, False])]
@@ -158,7 +158,7 @@
 assert c.called_finalize
 
 def test_sub_getitem_array_int(self):
-from numpypy import array
+from numpy import array
 a = array(range(5))
 b = self.SubType(a)
 assert b.called_new
@@ -169,7 +169,7 @@
 assert c.called_finalize
 
 def test_sub_round(self):
-from numpypy import array
+from numpy import array
 a = array(range(10), dtype=float).view(self.NoNew)
 # numpy compatibility
 b = a.round(decimals=0)
@@ -181,7 +181,7 @@
 
 def test_sub_dot(self):
 # the returned type is that of the first argument
-from numpypy import array
+from numpy import array
 a = array(range(12)).reshape(3,4)
 b = self.SubType(a)
 c = array(range(12)).reshape(4,3).view(self.SubType)
@@ -197,7 +197,7 @@
 

[pypy-commit] pypy default: test, fix to only output IRC topic if nonrelease or if PYPY_IRC_TOPIC set

2014-05-10 Thread mattip
Author: mattip matti.pi...@gmail.com
Branch: 
Changeset: r71440:adbfbb5d604d
Date: 2014-05-10 23:16 +0300
http://bitbucket.org/pypy/pypy/changeset/adbfbb5d604d/

Log:test, fix to only output IRC topic if nonrelease or if
PYPY_IRC_TOPIC set

diff --git a/pypy/interpreter/app_main.py b/pypy/interpreter/app_main.py
--- a/pypy/interpreter/app_main.py
+++ b/pypy/interpreter/app_main.py
@@ -671,7 +671,8 @@
 if inspect_requested():
 try:
 from _pypy_interact import interactive_console
-irc_topic = readenv and os.getenv('PYPY_IRC_TOPIC')
+irc_topic = sys.version_info[3] != 'final' or (
+readenv and os.getenv('PYPY_IRC_TOPIC'))
 success = run_toplevel(interactive_console, mainmodule,
quiet=not irc_topic)
 except SystemExit, e:
diff --git a/pypy/interpreter/test/test_app_main.py 
b/pypy/interpreter/test/test_app_main.py
--- a/pypy/interpreter/test/test_app_main.py
+++ b/pypy/interpreter/test/test_app_main.py
@@ -7,10 +7,9 @@
 from rpython.tool.udir import udir
 from contextlib import contextmanager
 from pypy.conftest import pypydir
-from pypy.module.sys.version import PYPY_VERSION
 from lib_pypy._pypy_interact import irc_header
 
-is_release = PYPY_VERSION[3] == final
+is_release = sys.version_info[3] == final
 
 
 banner = sys.version.splitlines()[0]
@@ -269,6 +268,11 @@
 child.sendline('' in sys.path)
 child.expect(True)
 
+def test_irc_topic(self, monkeypatch):
+monkeypatch.setenv('PYPY_IRC_TOPIC', '1')
+child = self.spawn([])
+child.expect(irc_header)   # banner
+
 def test_help(self):
 # test that -h prints the usage, including the name of the executable
 # which should be /full/path/to/app_main.py in this case
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix quotes in bytearray repr

2014-05-08 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r71419:eefd0674de1d
Date: 2014-05-08 17:05 -0400
http://bitbucket.org/pypy/pypy/changeset/eefd0674de1d/

Log:test/fix quotes in bytearray repr

diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -221,7 +221,15 @@
 # Good default if there are no replacements.
 buf = StringBuilder(len(bytearray(b'')) + len(s))
 
-buf.append(bytearray(b')
+buf.append(bytearray(b)
+quote = '
+for c in s:
+if c == '':
+quote = '
+break
+elif c == ':
+quote = ''
+buf.append(quote)
 
 for i in range(len(s)):
 c = s[i]
@@ -243,7 +251,8 @@
 else:
 buf.append(c)
 
-buf.append('))
+buf.append(quote)
+buf.append())
 
 return space.wrap(buf.build())
 
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py 
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -1,5 +1,6 @@
 from pypy import conftest
 
+
 class AppTestBytesArray:
 def setup_class(cls):
 cls.w_runappdirect = cls.space.wrap(conftest.option.runappdirect)
@@ -49,7 +50,10 @@
 def test_repr(self):
 assert repr(bytearray()) == bytearray(b'')
 assert repr(bytearray('test')) == bytearray(b'test')
-assert repr(bytearray(d'oh)) == rbytearray(b'd\'oh')
+assert repr(bytearray(d'oh)) == r'bytearray(bd\'oh)'
+assert repr(bytearray('doh')) == 'bytearray(b\'doh\')'
+assert repr(bytearray('d\'oh')) == 'bytearray(b\'d\\\'oh\')'
+assert repr(bytearray('d\'oh')) == 'bytearray(b\'d\\\'oh\')'
 
 def test_str(self):
 assert str(bytearray()) == 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix parsing of spaces between nums in complex constructor

2014-05-08 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r71421:84388c3ff677
Date: 2014-05-08 17:14 -0400
http://bitbucket.org/pypy/pypy/changeset/84388c3ff677/

Log:test/fix parsing of spaces between nums in complex constructor

diff --git a/pypy/objspace/std/complextype.py b/pypy/objspace/std/complextype.py
--- a/pypy/objspace/std/complextype.py
+++ b/pypy/objspace/std/complextype.py
@@ -50,10 +50,6 @@
 
 realstop = i
 
-# ignore whitespace
-while i  slen and s[i] == ' ':
-i += 1
-
 # return appropriate strings is only one number is there
 if i = slen:
 newstop = realstop - 1
diff --git a/pypy/objspace/std/test/test_complexobject.py 
b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -82,9 +82,7 @@
 
 
 class AppTestAppComplexTest:
-spaceconfig = {
-usemodules: [binascii, rctime]
-}
+spaceconfig = {usemodules: [binascii, rctime]}
 
 def w_check_div(self, x, y):
 Compute complex z=x*y, and check that z/x==y and z/y==x.
@@ -383,7 +381,6 @@
 #
 assert cmath.polar(1) == (1.0, 0.0)
 raises(TypeError, cmath.polar(Obj(1)))
-
 
 def test_hash(self):
 for x in xrange(-30, 30):
@@ -403,7 +400,9 @@
 assert j(100 + 0j) == 100 + 0j
 assert isinstance(j(100), j)
 assert j(100L + 0j) == 100 + 0j
-assert j(100 + 0j) == 100 + 0j
+assert j(100+0j) == 100 + 0j
+exc = raises(ValueError, j, 100 + 0j)
+assert str(exc.value) == complex() arg is a malformed string
 x = j(1+0j)
 x.foo = 42
 assert x.foo == 42
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix searchsorted return type for scalars

2014-05-05 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r71279:3581f7a906c9
Date: 2014-05-05 15:02 -0400
http://bitbucket.org/pypy/pypy/changeset/3581f7a906c9/

Log:test/fix searchsorted return type for scalars

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -738,6 +738,8 @@
 ret = W_NDimArray.from_shape(
 space, v.get_shape(), 
descriptor.get_dtype_cache(space).w_longdtype)
 app_searchsort(space, self, v, space.wrap(side), ret)
+if ret.is_scalar():
+return ret.get_scalar_value()
 return ret
 
 def descr_setasflat(self, space, w_v):
diff --git a/pypy/module/micronumpy/test/test_sorting.py 
b/pypy/module/micronumpy/test/test_sorting.py
--- a/pypy/module/micronumpy/test/test_sorting.py
+++ b/pypy/module/micronumpy/test/test_sorting.py
@@ -351,13 +351,21 @@
 assert (x.argsort(kind='m') == np.arange(32)).all()
 
 def test_searchsort(self):
-from numpy import arange
+import numpy as np
 import sys
-a = arange(1, 6)
+a = np.arange(1, 6)
 ret = a.searchsorted(3)
 assert ret == 2
+assert isinstance(ret, np.generic)
+ret = a.searchsorted(np.array(3))
+assert ret == 2
+assert isinstance(ret, np.generic)
+ret = a.searchsorted(np.array([3]))
+assert ret == 2
+assert isinstance(ret, np.ndarray)
 ret = a.searchsorted(3, side='right')
 assert ret == 3
+assert isinstance(ret, np.generic)
 ret = a.searchsorted([-10, 10, 2, 3])
 assert (ret == [0, 5, 1, 2]).all()
 if '__pypy__' in sys.builtin_module_names:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix cpyext version number

2014-05-02 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r71220:12b3395cfa84
Date: 2014-05-02 21:59 -0400
http://bitbucket.org/pypy/pypy/changeset/12b3395cfa84/

Log:test/fix cpyext version number

diff --git a/pypy/module/cpyext/include/patchlevel.h 
b/pypy/module/cpyext/include/patchlevel.h
--- a/pypy/module/cpyext/include/patchlevel.h
+++ b/pypy/module/cpyext/include/patchlevel.h
@@ -21,7 +21,7 @@
 /* Version parsed out into numeric values */
 #define PY_MAJOR_VERSION   2
 #define PY_MINOR_VERSION   7
-#define PY_MICRO_VERSION   3
+#define PY_MICRO_VERSION   6
 #define PY_RELEASE_LEVEL   PY_RELEASE_LEVEL_FINAL
 #define PY_RELEASE_SERIAL  0
 
diff --git a/pypy/module/cpyext/test/test_version.py 
b/pypy/module/cpyext/test/test_version.py
--- a/pypy/module/cpyext/test/test_version.py
+++ b/pypy/module/cpyext/test/test_version.py
@@ -9,11 +9,17 @@
 if (Py_IsInitialized()) {
 PyObject *m = Py_InitModule(foo, NULL);
 PyModule_AddStringConstant(m, py_version, PY_VERSION);
+PyModule_AddIntConstant(m, py_major_version, PY_MAJOR_VERSION);
+PyModule_AddIntConstant(m, py_minor_version, PY_MINOR_VERSION);
+PyModule_AddIntConstant(m, py_micro_version, PY_MICRO_VERSION);
 PyModule_AddStringConstant(m, pypy_version, PYPY_VERSION);
 }
 
 module = self.import_module(name='foo', init=init)
 assert module.py_version == sys.version[:5]
+assert module.py_major_version == sys.version_info.major
+assert module.py_minor_version == sys.version_info.minor
+assert module.py_micro_version == sys.version_info.micro
 v = sys.pypy_version_info
 s = '%d.%d.%d' % (v[0], v[1], v[2])
 if v.releaselevel != 'final':
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix xrange rejecting floats

2014-04-26 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r70999:171d134c9340
Date: 2014-04-26 02:14 -0400
http://bitbucket.org/pypy/pypy/changeset/171d134c9340/

Log:test/fix xrange rejecting floats

diff --git a/pypy/module/__builtin__/functional.py 
b/pypy/module/__builtin__/functional.py
--- a/pypy/module/__builtin__/functional.py
+++ b/pypy/module/__builtin__/functional.py
@@ -351,17 +351,17 @@
 self.promote_step = promote_step
 
 def descr_new(space, w_subtype, w_start, w_stop=None, w_step=None):
-start = _toint(space, w_start)
+start = space.int_w(w_start)
 if space.is_none(w_step):  # no step argument provided
 step = 1
 promote_step = True
 else:
-step  = _toint(space, w_step)
+step  = space.int_w(w_step)
 promote_step = False
 if space.is_none(w_stop):  # only 1 argument provided
 start, stop = 0, start
 else:
-stop = _toint(space, w_stop)
+stop = space.int_w(w_stop)
 howmany = get_len_of_range(space, start, stop, step)
 obj = space.allocate_instance(W_XRange, w_subtype)
 W_XRange.__init__(obj, space, start, howmany, step, promote_step)
@@ -425,11 +425,6 @@
 minint = -sys.maxint - 1
 return minint if last  minint - step else last + step
 
-def _toint(space, w_obj):
-# this also supports float arguments.  CPython still does, too.
-# needs a bit more thinking in general...
-return space.int_w(space.int(w_obj))
-
 W_XRange.typedef = TypeDef(xrange,
 __new__  = interp2app(W_XRange.descr_new.im_func),
 __repr__ = interp2app(W_XRange.descr_repr),
diff --git a/pypy/module/__builtin__/test/test_builtin.py 
b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -311,14 +311,14 @@
 def test_xrange_len(self):
 x = xrange(33)
 assert len(x) == 33
-x = xrange(33.2)
-assert len(x) == 33
+exc = raises(TypeError, xrange, 33.2)
+assert integer in str(exc.value)
 x = xrange(33,0,-1)
 assert len(x) == 33
 x = xrange(33,0)
 assert len(x) == 0
-x = xrange(33,0.2)
-assert len(x) == 0
+exc = raises(TypeError, xrange, 33, 0.2)
+assert integer in str(exc.value)
 x = xrange(0,33)
 assert len(x) == 33
 x = xrange(0,33,-1)
diff --git a/pypy/module/__builtin__/test/test_functional.py 
b/pypy/module/__builtin__/test/test_functional.py
--- a/pypy/module/__builtin__/test/test_functional.py
+++ b/pypy/module/__builtin__/test/test_functional.py
@@ -1,5 +1,4 @@
 class AppTestMap:
-
 def test_trivial_map_one_seq(self):
 assert map(lambda x: x+2, [1, 2, 3, 4]) == [3, 4, 5, 6]
 
@@ -77,6 +76,7 @@
 assert result == [(2, 7), (1, 6), (None, 5), (None, 4),
   (None, 3), (None, 2), (None, 1)]
 
+
 class AppTestZip:
 def test_one_list(self):
 assert zip([1,2,3]) == [(1,), (2,), (3,)]
@@ -93,6 +93,7 @@
 yield None
 assert zip(Foo()) == []
 
+
 class AppTestReduce:
 def test_None(self):
 raises(TypeError, reduce, lambda x, y: x+y, [1,2,3], None)
@@ -105,6 +106,7 @@
 assert reduce(lambda x, y: x-y, [10, 2, 8]) == 0
 assert reduce(lambda x, y: x-y, [2, 8], 10) == 0
 
+
 class AppTestFilter:
 def test_None(self):
 assert filter(None, ['a', 'b', 1, 0, None]) == ['a', 'b', 1]
@@ -125,6 +127,7 @@
 return i * 10
 assert filter(lambda x: x != 20, T(abcd)) == (0, 10, 30)
 
+
 class AppTestXRange:
 def test_xrange(self):
 x = xrange(2, 9, 3)
@@ -155,7 +158,8 @@
 assert list(xrange(0, 10, A())) == [0, 5]
 
 def test_xrange_float(self):
-assert list(xrange(0.1, 2.0, 1.1)) == [0, 1]
+exc = raises(TypeError, xrange, 0.1, 2.0, 1.1)
+assert integer in str(exc.value)
 
 def test_xrange_long(self):
 import sys
@@ -218,6 +222,7 @@
 assert list(reversed(list(reversed(hello == ['h','e','l','l','o']
 raises(TypeError, reversed, reversed(hello))
 
+
 class AppTestApply:
 def test_apply(self):
 def f(*args, **kw):
@@ -228,6 +233,7 @@
 assert apply(f, args) == (args, {})
 assert apply(f, args, kw) == (args, kw)
 
+
 class AppTestAllAny:
 
 These are copied directly and replicated from the Python 2.5 source code.
@@ -277,6 +283,7 @@
 S = [10, 20, 30]
 assert any([x  42 for x in S]) == False
 
+
 class AppTestMinMax:
 def test_min(self):
 assert min(1, 2) == 1
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix file.writelines(buffer) behavior to match CPython

2014-04-25 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r70975:f8870a4ed20a
Date: 2014-04-25 12:54 -0400
http://bitbucket.org/pypy/pypy/changeset/f8870a4ed20a/

Log:test/fix file.writelines(buffer) behavior to match CPython

diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py
--- a/pypy/module/_file/interp_file.py
+++ b/pypy/module/_file/interp_file.py
@@ -468,7 +468,12 @@
 if not e.match(space, space.w_StopIteration):
 raise
 break  # done
-self.file_write(w_line)
+try:
+line = w_line.charbuf_w(space)
+except TypeError:
+raise OperationError(space.w_TypeError, space.wrap(
+writelines() argument must be a sequence of strings))
+self.file_write(space.wrap(line))
 
 def file_readinto(self, w_rwbuffer):
 readinto() - Undocumented.  Don't use this; it may go away.
diff --git a/pypy/module/_file/test/test_file_extra.py 
b/pypy/module/_file/test/test_file_extra.py
--- a/pypy/module/_file/test/test_file_extra.py
+++ b/pypy/module/_file/test/test_file_extra.py
@@ -386,6 +386,27 @@
 assert len(somelines)  200
 assert somelines == lines[:len(somelines)]
 
+def test_writelines(self):
+import array
+fn = self.temptestfile
+with file(fn, 'w') as f:
+f.writelines(['abc'])
+f.writelines([u'def'])
+exc = raises(TypeError, f.writelines, [array.array('c', 'ghi')])
+assert str(exc.value) == writelines() argument must be a sequence 
of strings
+exc = raises(TypeError, f.writelines, [memoryview('jkl')])
+assert str(exc.value) == writelines() argument must be a sequence 
of strings
+assert open(fn, 'r').readlines() == ['abcdef']
+
+with file(fn, 'wb') as f:
+f.writelines(['abc'])
+f.writelines([u'def'])
+exc = raises(TypeError, f.writelines, [array.array('c', 'ghi')])
+assert str(exc.value) == writelines() argument must be a sequence 
of strings
+exc = raises(TypeError, f.writelines, [memoryview('jkl')])
+assert str(exc.value) == writelines() argument must be a sequence 
of strings
+assert open(fn, 'rb').readlines() == ['abcdef']
+
 def test_nasty_writelines(self):
 # The stream lock should be released between writes
 fn = self.temptestfile
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix some compile() behaviors

2014-04-25 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r70978:0b124540057c
Date: 2014-04-25 13:33 -0400
http://bitbucket.org/pypy/pypy/changeset/0b124540057c/

Log:test/fix some compile() behaviors

diff --git a/lib-python/2.7/test/test_builtin.py 
b/lib-python/2.7/test/test_builtin.py
--- a/lib-python/2.7/test/test_builtin.py
+++ b/lib-python/2.7/test/test_builtin.py
@@ -250,14 +250,12 @@
 self.assertRaises(TypeError, compile)
 self.assertRaises(ValueError, compile, 'print 42\n', 'string', 
'badmode')
 self.assertRaises(ValueError, compile, 'print 42\n', 'string', 
'single', 0xff)
-if check_impl_detail(cpython=True):
-self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
+self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
 self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
   mode='eval', source='0', filename='tmp')
 if have_unicode:
 compile(unicode('print u\xc3\xa5\n', 'utf8'), '', 'exec')
-if check_impl_detail(cpython=True):
-self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec')
+self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec')
 self.assertRaises(ValueError, compile, unicode('a = 1'), 'f', 
'bad')
 
 
diff --git a/pypy/module/__builtin__/compiling.py 
b/pypy/module/__builtin__/compiling.py
--- a/pypy/module/__builtin__/compiling.py
+++ b/pypy/module/__builtin__/compiling.py
@@ -22,22 +22,6 @@
 compile; if absent or zero these statements do influence the compilation,
 in addition to any features explicitly specified.
 
-
-ast_node = None
-w_ast_type = space.gettypeobject(ast.AST.typedef)
-str_ = None
-if space.isinstance_w(w_source, w_ast_type):
-ast_node = space.interp_w(ast.mod, w_source)
-ast_node.sync_app_attrs(space)
-elif space.isinstance_w(w_source, space.w_unicode):
-w_utf_8_source = space.call_method(w_source, encode,
-   space.wrap(utf-8))
-str_ = space.str_w(w_utf_8_source)
-# This flag tells the parser to reject any coding cookies it sees.
-flags |= consts.PyCF_SOURCE_IS_UTF8
-else:
-str_ = space.str_w(w_source)
-
 ec = space.getexecutioncontext()
 if flags  ~(ec.compiler.compiler_flags | consts.PyCF_ONLY_AST |
  consts.PyCF_DONT_IMPLY_DEDENT | consts.PyCF_SOURCE_IS_UTF8):
@@ -53,14 +37,30 @@
  space.wrap(compile() arg 3 must be 'exec' 
 or 'eval' or 'single'))
 
-if ast_node is None:
-if flags  consts.PyCF_ONLY_AST:
-mod = ec.compiler.compile_to_ast(str_, filename, mode, flags)
-return space.wrap(mod)
-else:
-code = ec.compiler.compile(str_, filename, mode, flags)
+w_ast_type = space.gettypeobject(ast.AST.typedef)
+if space.isinstance_w(w_source, w_ast_type):
+ast_node = space.interp_w(ast.mod, w_source)
+ast_node.sync_app_attrs(space)
+code = ec.compiler.compile_ast(ast_node, filename, mode, flags)
+return space.wrap(code)
+
+if space.isinstance_w(w_source, space.w_unicode):
+w_utf_8_source = space.call_method(w_source, encode,
+   space.wrap(utf-8))
+str_ = space.str_w(w_utf_8_source)
+# This flag tells the parser to reject any coding cookies it sees.
+flags |= consts.PyCF_SOURCE_IS_UTF8
 else:
-code = ec.compiler.compile_ast(ast_node, filename, mode, flags)
+str_ = space.readbuf_w(w_source).as_str()
+
+if '\x00' in str_:
+raise OperationError(space.w_TypeError, space.wrap(
+compile() expected string without null bytes))
+
+if flags  consts.PyCF_ONLY_AST:
+code = ec.compiler.compile_to_ast(str_, filename, mode, flags)
+else:
+code = ec.compiler.compile(str_, filename, mode, flags)
 return space.wrap(code)
 
 
diff --git a/pypy/module/__builtin__/test/test_builtin.py 
b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -490,6 +490,14 @@
 def test_compile(self):
 co = compile('1+2', '?', 'eval')
 assert eval(co) == 3
+co = compile(buffer('1+2'), '?', 'eval')
+assert eval(co) == 3
+exc = raises(TypeError, compile, chr(0), '?', 'eval')
+assert str(exc.value) == compile() expected string without null bytes
+exc = raises(TypeError, compile, unichr(0), '?', 'eval')
+assert str(exc.value) == compile() expected string without null bytes
+exc = raises(TypeError, compile, memoryview('1+2'), '?', 'eval')
+assert str(exc.value) == expected a readable buffer object
 compile(from __future__ import with_statement, test, exec)
 raises(SyntaxError, compile, 

[pypy-commit] pypy default: test/fix tcsetattr validation of attributes

2014-04-25 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r70993:e87d8dddb7f3
Date: 2014-04-25 19:38 -0400
http://bitbucket.org/pypy/pypy/changeset/e87d8dddb7f3/

Log:test/fix tcsetattr validation of attributes

diff --git a/pypy/module/termios/interp_termios.py 
b/pypy/module/termios/interp_termios.py
--- a/pypy/module/termios/interp_termios.py
+++ b/pypy/module/termios/interp_termios.py
@@ -4,7 +4,7 @@
 
 
 from pypy.interpreter.gateway import unwrap_spec
-from pypy.interpreter.error import wrap_oserror
+from pypy.interpreter.error import wrap_oserror, OperationError
 from rpython.rlib import rtermios
 import termios
 
@@ -19,6 +19,10 @@
 @unwrap_spec(when=int)
 def tcsetattr(space, w_fd, when, w_attributes):
 fd = space.c_filedescriptor_w(w_fd)
+if not space.isinstance_w(w_attributes, space.w_list) or \
+space.len_w(w_attributes) != 7:
+raise OperationError(space.w_TypeError, space.wrap(
+tcsetattr, arg 3: must be 7 element list))
 w_iflag, w_oflag, w_cflag, w_lflag, w_ispeed, w_ospeed, w_cc = \
  space.unpackiterable(w_attributes, expected_length=7)
 w_builtin = space.getbuiltinmodule('__builtin__')
diff --git a/pypy/module/termios/test/test_termios.py 
b/pypy/module/termios/test/test_termios.py
--- a/pypy/module/termios/test/test_termios.py
+++ b/pypy/module/termios/test/test_termios.py
@@ -149,4 +149,7 @@
 
 def test_error_tcsetattr(self):
 import termios
-raises(ValueError, termios.tcsetattr, 0, 1, (1, 2))
+exc = raises(TypeError, termios.tcsetattr, 0, 1, (1, 2))
+assert str(exc.value) == tcsetattr, arg 3: must be 7 element list
+exc = raises(TypeError, termios.tcsetattr, 0, 1, (1, 2, 3, 4, 5, 6, 7))
+assert str(exc.value) == tcsetattr, arg 3: must be 7 element list
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Test fix (OS/X, Win)

2014-03-23 Thread arigo
Author: Armin Rigo ar...@tunes.org
Branch: 
Changeset: r70200:99ea05cf50b8
Date: 2014-03-23 15:15 +0100
http://bitbucket.org/pypy/pypy/changeset/99ea05cf50b8/

Log:Test fix (OS/X, Win)

diff --git a/rpython/jit/backend/llsupport/test/test_rewrite.py 
b/rpython/jit/backend/llsupport/test/test_rewrite.py
--- a/rpython/jit/backend/llsupport/test/test_rewrite.py
+++ b/rpython/jit/backend/llsupport/test/test_rewrite.py
@@ -545,7 +545,7 @@
 p1 = int_add(p0, %(strdescr.basesize + 16 * strdescr.itemsize)d)
 setfield_gc(p1, %(unicodedescr.tid)d, descr=tiddescr)
 setfield_gc(p1, 10, descr=unicodelendescr)
-p2 = call_malloc_nursery_varsize(2, 4, i2, \
+p2 = call_malloc_nursery_varsize(2, %(unicodedescr.itemsize)d, i2,\
 descr=unicodedescr)
 setfield_gc(p2, i2, descr=unicodelendescr)
 p3 = call_malloc_nursery_varsize(1, 1, i2, \
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix buffer init

2014-03-18 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r70062:3c1786c06a69
Date: 2014-03-18 15:24 -0400
http://bitbucket.org/pypy/pypy/changeset/3c1786c06a69/

Log:test/fix buffer init

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -195,11 +195,6 @@
 return None
 
 def buffer_w(self, space):
-w_impl = space.lookup(self, '__buffer__')
-if w_impl is not None:
-w_result = space.get_and_call_function(w_impl, self)
-if space.isinstance_w(w_result, space.w_buffer):
-return w_result.buf
 self._typed_unwrap_error(space, buffer)
 
 def str_w(self, space):
diff --git a/pypy/module/__builtin__/test/test_buffer.py 
b/pypy/module/__builtin__/test/test_buffer.py
--- a/pypy/module/__builtin__/test/test_buffer.py
+++ b/pypy/module/__builtin__/test/test_buffer.py
@@ -4,6 +4,12 @@
 class AppTestBuffer:
 spaceconfig = dict(usemodules=['array'])
 
+def test_init(self):
+class A(object):
+def __buffer__(self):
+return buffer('123')
+raises(TypeError, buffer, A())
+
 def test_unicode_buffer(self):
 import sys
 b = buffer(uab)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix marshal behavior wrt buffers

2014-03-18 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r70071:07f40b57d13c
Date: 2014-03-18 20:35 -0400
http://bitbucket.org/pypy/pypy/changeset/07f40b57d13c/

Log:test/fix marshal behavior wrt buffers

diff --git a/pypy/module/marshal/interp_marshal.py 
b/pypy/module/marshal/interp_marshal.py
--- a/pypy/module/marshal/interp_marshal.py
+++ b/pypy/module/marshal/interp_marshal.py
@@ -217,11 +217,6 @@
 self.space.marshal_w(w_obj, self)
 
 def dump_w_obj(self, w_obj):
-space = self.space
-if (space.type(w_obj).is_heaptype() and
-space.lookup(w_obj, __buffer__) is None):
-w_err = space.wrap(only builtins can be marshaled)
-raise OperationError(space.w_ValueError, w_err)
 try:
 self.put_w_obj(w_obj)
 except rstackovf.StackOverflow:
diff --git a/pypy/module/marshal/test/test_marshal.py 
b/pypy/module/marshal/test/test_marshal.py
--- a/pypy/module/marshal/test/test_marshal.py
+++ b/pypy/module/marshal/test/test_marshal.py
@@ -2,6 +2,8 @@
 
 
 class AppTestMarshal:
+spaceconfig = {'usemodules': ['array']}
+
 def setup_class(cls):
 tmpfile = udir.join('AppTestMarshal.tmp')
 cls.w_tmpfile = cls.space.wrap(str(tmpfile))
@@ -173,7 +175,15 @@
 for cls in types:
 class subtype(cls):
 pass
-raises(ValueError, marshal.dumps, subtype)
+exc = raises(ValueError, marshal.dumps, subtype)
+assert str(exc.value) == 'unmarshallable object'
+
+def test_valid_subtypes(self):
+import marshal
+from array import array
+class subtype(array):
+pass
+assert marshal.dumps(subtype('c', 'test')) == marshal.dumps(array('c', 
'test'))
 
 def test_bad_typecode(self):
 import marshal
@@ -182,7 +192,8 @@
 
 
 class AppTestSmallLong(AppTestMarshal):
-spaceconfig = {objspace.std.withsmalllong: True}
+spaceconfig = AppTestMarshal.spaceconfig.copy()
+spaceconfig[objspace.std.withsmalllong] = True
 
 def test_smalllong(self):
 import __pypy__
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix promote_to_largest wrt 32bit, though demonstrates another problem

2013-12-18 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r68481:84c4bc069470
Date: 2013-12-18 21:27 -0500
http://bitbucket.org/pypy/pypy/changeset/84c4bc069470/

Log:test/fix promote_to_largest wrt 32bit, though demonstrates another
problem

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
@@ -498,13 +498,14 @@
 promote_bools=False, promote_to_largest=False):
 if promote_to_largest:
 if dt.kind == NPY_GENBOOLLTR or dt.kind == NPY_SIGNEDLTR:
-return interp_dtype.get_dtype_cache(space).w_int64dtype
+if dt.get_size() * 8  LONG_BIT:
+return interp_dtype.get_dtype_cache(space).w_longdtype
 elif dt.kind == NPY_UNSIGNEDLTR:
-return interp_dtype.get_dtype_cache(space).w_uint64dtype
-elif dt.kind == NPY_FLOATINGLTR or dt.kind == NPY_COMPLEXLTR:
-return dt
+if dt.get_size() * 8  LONG_BIT:
+return interp_dtype.get_dtype_cache(space).w_ulongdtype
 else:
-assert False
+assert dt.kind == NPY_FLOATINGLTR or dt.kind == NPY_COMPLEXLTR
+return dt
 if promote_bools and (dt.kind == NPY_GENBOOLLTR):
 return interp_dtype.get_dtype_cache(space).w_int8dtype
 if promote_to_float:
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
@@ -1404,12 +1404,14 @@
 a = array(range(1, 6))
 assert a.prod() == 120.0
 assert a[:4].prod() == 24.0
-a = array([True, False])
-assert a.prod() == 0
-assert type(a.prod()) is int_
-a = array([True, False], dtype='uint')
-assert a.prod() == 0
-assert type(a.prod()) is dtype('uint').type
+for dt in ['bool', 'int8', 'uint8', 'int16', 'uint16']:
+a = array([True, False], dtype=dt)
+assert a.prod() == 0
+assert a.prod().dtype is dtype('uint' if dt[0] == 'u' else 'int')
+for dt in ['l', 'L', 'q', 'Q', 'e', 'f', 'd', 'F', 'D']:
+a = array([True, False], dtype=dt)
+assert a.prod() == 0
+assert a.prod().dtype is dtype(dt)
 
 def test_max(self):
 from numpypy import array, zeros
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix conversion of str arrays

2013-12-17 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r68452:ccbbf6d54188
Date: 2013-12-17 19:36 -0500
http://bitbucket.org/pypy/pypy/changeset/ccbbf6d54188/

Log:test/fix conversion of str arrays

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
@@ -988,28 +988,43 @@
 shape = self.get_shape()
 if len(shape) == 0:
 assert isinstance(self.implementation, scalar.Scalar)
-return 
space.int(space.wrap(self.implementation.get_scalar_value()))
-if shape == [1]:
-return space.int(self.descr_getitem(space, space.wrap(0)))
-raise OperationError(space.w_TypeError, space.wrap(only length-1 
arrays can be converted to Python scalars))
+value = space.wrap(self.implementation.get_scalar_value())
+elif shape == [1]:
+value = self.descr_getitem(space, space.wrap(0))
+else:
+raise OperationError(space.w_TypeError, space.wrap(only length-1 
arrays can be converted to Python scalars))
+if self.get_dtype().is_str_or_unicode():
+raise OperationError(space.w_TypeError, space.wrap(
+don't know how to convert scalar number to int))
+return space.int(value)
 
 def descr_long(self, space):
 shape = self.get_shape()
 if len(shape) == 0:
 assert isinstance(self.implementation, scalar.Scalar)
-return 
space.long(space.wrap(self.implementation.get_scalar_value()))
-if shape == [1]:
-return space.int(self.descr_getitem(space, space.wrap(0)))
-raise OperationError(space.w_TypeError, space.wrap(only length-1 
arrays can be converted to Python scalars))
+value = space.wrap(self.implementation.get_scalar_value())
+elif shape == [1]:
+value = self.descr_getitem(space, space.wrap(0))
+else:
+raise OperationError(space.w_TypeError, space.wrap(only length-1 
arrays can be converted to Python scalars))
+if self.get_dtype().is_str_or_unicode():
+raise OperationError(space.w_TypeError, space.wrap(
+don't know how to convert scalar number to long))
+return space.long(value)
 
 def descr_float(self, space):
 shape = self.get_shape()
 if len(shape) == 0:
 assert isinstance(self.implementation, scalar.Scalar)
-return 
space.float(space.wrap(self.implementation.get_scalar_value()))
-if shape == [1]:
-return space.float(self.descr_getitem(space, space.wrap(0)))
-raise OperationError(space.w_TypeError, space.wrap(only length-1 
arrays can be converted to Python scalars))
+value = space.wrap(self.implementation.get_scalar_value())
+elif shape == [1]:
+value = self.descr_getitem(space, space.wrap(0))
+else:
+raise OperationError(space.w_TypeError, space.wrap(only length-1 
arrays can be converted to Python scalars))
+if self.get_dtype().is_str_or_unicode():
+raise OperationError(space.w_TypeError, space.wrap(
+don't know how to convert scalar number to float))
+return space.float(value)
 
 def descr_reduce(self, space):
 from rpython.rlib.rstring import StringBuilder
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
@@ -2083,6 +2083,11 @@
 assert int(array([1])) == 1
 assert raises(TypeError, int(array([1, 2])))
 assert int(array([1.5])) == 1
+for op in [int, float, long]:
+for a in [array('123'), array(['123'])]:
+exc = raises(TypeError, %s(a) % op)
+assert exc.value.message == don't know how to convert  \
+scalar number to %s % op
 
 def test__reduce__(self):
 from numpypy import array, dtype
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix conversion of np.str to numeric types

2013-12-17 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r68451:6b8b4188b7ee
Date: 2013-12-17 19:28 -0500
http://bitbucket.org/pypy/pypy/changeset/6b8b4188b7ee/

Log:test/fix conversion of np.str to numeric types

diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py 
b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -47,7 +47,7 @@
 def setslice(self, space, arr):
 impl = arr.implementation
 if impl.is_scalar():
-self.fill(impl.get_scalar_value())
+self.fill(space, impl.get_scalar_value())
 return
 shape = shape_agreement(space, self.get_shape(), arr)
 if impl.storage == self.storage:
@@ -100,7 +100,7 @@
 tmp = self.get_real(orig_array)
 tmp.setslice(space, convert_to_array(space, w_value))
 
-def get_imag(self, orig_array):
+def get_imag(self, space, orig_array):
 strides = self.get_strides()
 backstrides = self.get_backstrides()
 if self.dtype.is_complex_type():
@@ -110,11 +110,11 @@
 impl = NonWritableArray(self.get_shape(), self.dtype, self.order, 
strides,
  backstrides)
 if not self.dtype.is_flexible_type():
-impl.fill(self.dtype.box(0))
+impl.fill(space, self.dtype.box(0))
 return impl
 
 def set_imag(self, space, orig_array, w_value):
-tmp = self.get_imag(orig_array)
+tmp = self.get_imag(space, orig_array)
 tmp.setslice(space, convert_to_array(space, w_value))
 
 #  applevel get/setitem ---
@@ -357,7 +357,7 @@
  self.get_backstrides(),
  self.get_shape())
 
-def fill(self, box):
+def fill(self, space, box):
 self.dtype.itemtype.fill(self.storage, self.dtype.get_size(),
  box, 0, self.size, 0)
 
@@ -435,8 +435,8 @@
 def base(self):
 return self.orig_arr
 
-def fill(self, box):
-loop.fill(self, box.convert_to(self.dtype))
+def fill(self, space, box):
+loop.fill(self, box.convert_to(space, self.dtype))
 
 def create_iter(self, shape=None, backward_broadcast=False, 
require_index=False):
 if shape is not None and \
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py 
b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -54,8 +54,7 @@
 return self.value
 
 def set_scalar_value(self, w_val):
-assert isinstance(w_val, W_GenericBox)
-self.value = w_val.convert_to(self.dtype)
+self.value = w_val
 
 def copy(self, space):
 scalar = Scalar(self.dtype)
@@ -96,12 +95,12 @@
 ','.join([str(x) for x in w_arr.get_shape()],
 if self.dtype.is_complex_type():
 self.value = self.dtype.itemtype.composite(
-   w_arr.get_scalar_value().convert_to(dtype),
+   w_arr.get_scalar_value().convert_to(space, 
dtype),
self.value.convert_imag_to(dtype))
 else:
 self.value = w_arr.get_scalar_value()
 
-def get_imag(self, orig_array):
+def get_imag(self, space, orig_array):
 if self.dtype.is_complex_type():
 scalar = Scalar(self.dtype.float_type)
 scalar.value = self.value.convert_imag_to(scalar.dtype)
@@ -125,7 +124,7 @@
 ','.join([str(x) for x in w_arr.get_shape()],
 self.value = self.dtype.itemtype.composite(
 self.value.convert_real_to(dtype),
-w_arr.get_scalar_value().convert_to(dtype),
+w_arr.get_scalar_value().convert_to(space, dtype),
 )
 
 def descr_getitem(self, space, _, w_idx):
@@ -180,7 +179,7 @@
 w_res.implementation.setitem(0, index_type.itemtype.box(0)) 
 return space.newtuple([w_res])
 
-def fill(self, w_value):
+def fill(self, space, w_value):
 self.value = w_value
 
 def get_storage_as_int(self, space):
diff --git a/pypy/module/micronumpy/interp_arrayops.py 
b/pypy/module/micronumpy/interp_arrayops.py
--- a/pypy/module/micronumpy/interp_arrayops.py
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -89,7 +89,7 @@
 shape = shape_agreement(space, arr.get_shape(), x)
 shape = shape_agreement(space, shape, y)
 out = W_NDimArray.from_shape(space, shape, dtype)
-return loop.where(out, shape, arr, x, y, dtype)
+return loop.where(space, out, shape, arr, x, y, dtype)
 
 def dot(space, w_obj1, w_obj2, w_out=None):
 w_arr = convert_to_array(space, w_obj1)
diff --git a/pypy/module/micronumpy/interp_boxes.py 

[pypy-commit] pypy default: test/fix buffer(numpy.scalar)

2013-12-17 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r68456:2ec4090a85d4
Date: 2013-12-17 21:17 -0500
http://bitbucket.org/pypy/pypy/changeset/2ec4090a85d4/

Log:test/fix buffer(numpy.scalar)

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
@@ -313,6 +313,9 @@
 def descr_copy(self, space):
 return self.convert_to(space, self.get_dtype(space))
 
+def descr_buffer(self, space):
+return self.descr_ravel(space).descr_get_data(space)
+
 w_flags = None
 def descr_get_flags(self, space):
 if self.w_flags is None:
@@ -518,6 +521,7 @@
 __nonzero__ = interp2app(W_GenericBox.descr_nonzero),
 __oct__ = interp2app(W_GenericBox.descr_oct),
 __hex__ = interp2app(W_GenericBox.descr_hex),
+__buffer__ = interp2app(W_GenericBox.descr_buffer),
 
 __add__ = interp2app(W_GenericBox.descr_add),
 __sub__ = interp2app(W_GenericBox.descr_sub),
diff --git a/pypy/module/micronumpy/test/test_scalar.py 
b/pypy/module/micronumpy/test/test_scalar.py
--- a/pypy/module/micronumpy/test/test_scalar.py
+++ b/pypy/module/micronumpy/test/test_scalar.py
@@ -92,6 +92,15 @@
 assert b == a
 assert b is not a
 
+def test_buffer(self):
+import numpy as np
+a = np.int32(123)
+b = buffer(a)
+assert type(b) is buffer
+a = np.string_('abc')
+b = buffer(a)
+assert str(b) == a
+
 def test_squeeze(self):
 import numpy as np
 assert np.True_.squeeze() is np.True_
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test/fix ndarray init from list of array scalars

2013-12-17 Thread bdkearns
Author: Brian Kearns bdkea...@gmail.com
Branch: 
Changeset: r68461:c8e4f9503987
Date: 2013-12-18 01:31 -0500
http://bitbucket.org/pypy/pypy/changeset/c8e4f9503987/

Log:test/fix ndarray init from list of array scalars

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
@@ -1425,6 +1425,8 @@
 if dtype is None or (
  dtype.is_str_or_unicode() and dtype.get_size()  1):
 for w_elem in elems_w:
+if isinstance(w_elem, W_NDimArray) and w_elem.is_scalar():
+w_elem = w_elem.get_scalar_value()
 dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
 dtype)
 #if dtype is interp_dtype.get_dtype_cache(space).w_float64dtype:
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
@@ -62,9 +62,10 @@
 if (is_rec_type and space.isinstance_w(w_elem, space.w_tuple)):
 return True
 if (space.isinstance_w(w_elem, space.w_tuple) or
-isinstance(w_elem, W_NDimArray) or
 space.isinstance_w(w_elem, space.w_list)):
 return False
+if isinstance(w_elem, W_NDimArray) and not w_elem.is_scalar():
+return False
 return True
 
 def find_shape_and_elems(space, w_iterable, dtype):
@@ -72,7 +73,6 @@
 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 is_single_elem(space, batch[0], is_rec_type):
@@ -81,6 +81,7 @@
 raise OperationError(space.w_ValueError, space.wrap(
 setting an array element with a sequence))
 return shape[:], batch
+new_batch = []
 size = space.len_w(batch[0])
 for w_elem in batch:
 if (is_single_elem(space, w_elem, is_rec_type) or
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
@@ -455,6 +455,25 @@
 a = array(range(5))
 assert a[3] == 3
 
+def test_list_of_array_init(self):
+import numpy as np
+a = np.array([np.array(True), np.array(False)])
+assert a.shape == (2,)
+assert a.dtype == np.bool_
+assert (a == [True, False]).all()
+a = np.array([np.array(True), np.array(2)])
+assert a.shape == (2,)
+assert a.dtype == np.int_
+assert (a == [1, 2]).all()
+a = np.array([np.array(True), np.int_(2)])
+assert a.shape == (2,)
+assert a.dtype == np.int_
+assert (a == [1, 2]).all()
+a = np.array([np.array([True]), np.array([2])])
+assert a.shape == (2, 1)
+assert a.dtype == np.int_
+assert (a == [[1], [2]]).all()
+
 def test_getitem(self):
 from numpypy import array
 a = array(range(5))
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: test, fix for subtype pickle numpy compatability, including quirks

2013-12-16 Thread mattip
Author: Matti Picus matti.pi...@gmail.com
Branch: 
Changeset: r68443:d5e489e07679
Date: 2013-12-16 19:14 +0200
http://bitbucket.org/pypy/pypy/changeset/d5e489e07679/

Log:test, fix for subtype pickle numpy compatability, including quirks

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
@@ -1021,8 +1021,8 @@
 multiarray = numpypy.get(multiarray)
 assert isinstance(multiarray, MixedModule)
 reconstruct = multiarray.get(_reconstruct)
-
-parameters = space.newtuple([space.gettypefor(W_NDimArray), 
space.newtuple([space.wrap(0)]), space.wrap(b)])
+parameters = space.newtuple([self.getclass(space),
+space.newtuple([space.wrap(0)]), space.wrap(b)])
 
 builder = StringBuilder()
 if isinstance(self.implementation, SliceArray):
@@ -1045,14 +1045,22 @@
 return space.newtuple([reconstruct, parameters, state])
 
 def descr_setstate(self, space, w_state):
-from rpython.rtyper.lltypesystem import rffi
-
-shape = space.getitem(w_state, space.wrap(1))
-dtype = space.getitem(w_state, space.wrap(2))
-assert isinstance(dtype, interp_dtype.W_Dtype)
-isfortran = space.getitem(w_state, space.wrap(3))
-storage = space.getitem(w_state, space.wrap(4))
-
+lens = space.len_w(w_state)
+# numpy compatability, see multiarray/methods.c
+if lens == 5:
+base_index = 1
+elif lens == 4:
+base_index = 0
+else:
+raise OperationError(space.w_ValueError, space.wrap(
+ __setstate__ called with len(args[1])==%d, not 5 or 4 % 
lens))
+shape = space.getitem(w_state, space.wrap(base_index))
+dtype = space.getitem(w_state, space.wrap(base_index+1))
+isfortran = space.getitem(w_state, space.wrap(base_index+2))
+storage = space.getitem(w_state, space.wrap(base_index+3))
+if not isinstance(dtype, interp_dtype.W_Dtype):
+raise OperationError(space.w_ValueError, space.wrap(
+ __setstate__(self, (shape, dtype, .. called with improper 
dtype '%r' % dtype))
 self.implementation = W_NDimArray.from_shape_and_storage(space,
 [space.int_w(i) for i in space.listview(shape)],
 rffi.str2charp(space.str_w(storage), track_allocation=False),
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -3,6 +3,7 @@
 
 
 class AppTestSupport(BaseNumpyAppTest):
+spaceconfig = dict(usemodules=[micronumpy, struct, binascii])
 def setup_class(cls):
 BaseNumpyAppTest.setup_class.im_func(cls)
 cls.w_NoNew = cls.space.appexec([], '''():
@@ -300,4 +301,75 @@
 a = matrix([[1., 2.]])
 b = N.array([a])
 
+def test_setstate_no_version(self):
+# Some subclasses of ndarray, like MaskedArray, do not use
+# version in __setstare__
+from numpy import ndarray, array
+from pickle import loads, dumps
+import sys, new
+class D(ndarray):
+''' A subtype with a constructor that accepts a list of
+data values, where ndarray accepts a shape
+'''
+def __new__(subtype, data, dtype=None, copy=True):
+arr = array(data, dtype=dtype, copy=copy)
+shape = arr.shape
+ret = ndarray.__new__(subtype, shape, arr.dtype,
+buffer=arr,
+order=True)
+return ret
+def __setstate__(self, state):
+(version, shp, typ, isf, raw) = state
+ndarray.__setstate__(self, (shp, typ, isf, raw))
 
+D.__module__ = 'mod'
+mod = new.module('mod')
+mod.D = D
+sys.modules['mod'] = mod
+a = D([1., 2.])
+s = dumps(a)
+#Taken from numpy version 1.8
+s_from_numpy = '''ignore this line
+_reconstruct
+p0
+(cmod
+D
+p1
+(I0
+tp2
+S'b'
+p3
+tp4
+Rp5
+(I1
+(I2
+tp6
+cnumpy
+dtype
+p7
+(S'f8'
+p8
+I0
+I1
+tp9
+Rp10
+(I3
+S''
+p11
+NNNI-1
+I-1
+I0
+tp12
+bI00
+S'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@'
+p13
+tp14
+b.'''.replace('','')
+for ss,sn in 

[pypy-commit] pypy default: test, fix for matrix subtype which modfies shape in __getitem__

2013-12-14 Thread mattip
Author: Matti Picus matti.pi...@gmail.com
Branch: 
Changeset: r68441:bacd9679adbf
Date: 2013-12-15 07:43 +0200
http://bitbucket.org/pypy/pypy/changeset/bacd9679adbf/

Log:test, fix for matrix subtype which modfies shape in __getitem__
causing an infinite loop in find_shape_and_elems

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
@@ -51,8 +51,8 @@
 rstrides.append(strides[i])
 rbackstrides.append(backstrides[i])
 if backwards:
-rstrides = rstrides + [0] * (len(res_shape) - len(orig_shape))  
-rbackstrides = rbackstrides + [0] * (len(res_shape) - len(orig_shape)) 
+rstrides = rstrides + [0] * (len(res_shape) - len(orig_shape))
+rbackstrides = rbackstrides + [0] * (len(res_shape) - len(orig_shape))
 else:
 rstrides = [0] * (len(res_shape) - len(orig_shape)) + rstrides
 rbackstrides = [0] * (len(res_shape) - len(orig_shape)) + rbackstrides
@@ -62,7 +62,7 @@
 if (is_rec_type and space.isinstance_w(w_elem, space.w_tuple)):
 return True
 if (space.isinstance_w(w_elem, space.w_tuple) or
-isinstance(w_elem, W_NDimArray) or
+isinstance(w_elem, W_NDimArray) or
 space.isinstance_w(w_elem, space.w_list)):
 return False
 return True
@@ -87,6 +87,12 @@
 space.len_w(w_elem) != size):
 raise OperationError(space.w_ValueError, space.wrap(
 setting an array element with a sequence))
+w_array = space.lookup(w_elem, '__array__')
+if w_array is not None:
+# Make sure we call the array implementation of listview,
+# since for some ndarray subclasses (matrix, for instance)
+# listview does not reduce but rather returns the same class
+w_elem = space.get_and_call_function(w_array, w_elem, 
space.w_None)
 new_batch += space.listview(w_elem)
 shape.append(size)
 batch = new_batch
diff --git a/pypy/module/micronumpy/test/test_subtype.py 
b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -258,3 +258,46 @@
 assert isinstance(b, D)
 c = array(a, float)
 assert c.dtype is dtype(float)
+
+def test__getitem_modifies_shape(self):
+import numpypy as N
+# numpy's matrix class caused an infinite loop
+class matrix(N.ndarray):
+getcnt = 0
+def __new__(subtype, data, dtype=None, copy=True):
+arr = N.array(data, dtype=dtype, copy=copy)
+shape = arr.shape
+
+ret = N.ndarray.__new__(subtype, shape, arr.dtype,
+buffer=arr,
+order=True)
+return ret
+
+def __getitem__(self, index):
+matrix.getcnt += 1
+if matrix.getcnt  10:
+# XXX strides.find_shape_and_elems is sensitive
+# to shape modification
+xxx
+out = N.ndarray.__getitem__(self, index)
+
+if not isinstance(out, N.ndarray):
+return out
+# Determine when we should have a column array
+old_shape = out.shape
+if out.ndim  2:
+sh = out.shape[0]
+try:
+n = len(index)
+except:
+n = 0
+if n  1:
+out.shape = (sh, 1)
+else:
+out.shape = (1, sh)
+print 'out, shape was',old_shape,'now',out.shape
+return out
+a = matrix([[1., 2.]])
+b = N.array([a])
+
+
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


  1   2   >