Author: Matti Picus <[email protected]>
Branch: win32-stdlib
Changeset: r54627:000d3f9fd903
Date: 2012-04-22 23:28 +0300
http://bitbucket.org/pypy/pypy/changeset/000d3f9fd903/
Log: merge default into branch
diff --git a/pypy/module/cpyext/listobject.py b/pypy/module/cpyext/listobject.py
--- a/pypy/module/cpyext/listobject.py
+++ b/pypy/module/cpyext/listobject.py
@@ -110,6 +110,16 @@
space.call_method(w_list, "reverse")
return 0
+@cpython_api([PyObject, Py_ssize_t, Py_ssize_t], PyObject)
+def PyList_GetSlice(space, w_list, low, high):
+ """Return a list of the objects in list containing the objects between low
+ and high. Return NULL and set an exception if unsuccessful. Analogous
+ to list[low:high]. Negative indices, as when slicing from Python, are not
+ supported."""
+ w_start = space.wrap(low)
+ w_stop = space.wrap(high)
+ return space.getslice(w_list, w_start, w_stop)
+
@cpython_api([PyObject, Py_ssize_t, Py_ssize_t, PyObject], rffi.INT_real,
error=-1)
def PyList_SetSlice(space, w_list, low, high, w_sequence):
"""Set the slice of list between low and high to the contents of
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
@@ -381,6 +381,15 @@
This is the equivalent of the Python expression hash(o)."""
return space.int_w(space.hash(w_obj))
+@cpython_api([PyObject], lltype.Signed, error=-1)
+def PyObject_HashNotImplemented(space, o):
+ """Set a TypeError indicating that type(o) is not hashable and return -1.
+ This function receives special treatment when stored in a tp_hash slot,
+ allowing a type to explicitly indicate to the interpreter that it is not
+ hashable.
+ """
+ raise OperationError(space.w_TypeError, space.wrap("unhashable type"))
+
@cpython_api([PyObject], PyObject)
def PyObject_Dir(space, w_o):
"""This is equivalent to the Python expression dir(o), returning a
(possibly
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
@@ -7,7 +7,7 @@
cpython_api, generic_cpy_call, PyObject, Py_ssize_t)
from pypy.module.cpyext.typeobjectdefs import (
unaryfunc, wrapperfunc, ternaryfunc, PyTypeObjectPtr, binaryfunc,
- getattrfunc, getattrofunc, setattrofunc, lenfunc, ssizeargfunc,
+ getattrfunc, getattrofunc, setattrofunc, lenfunc, ssizeargfunc, inquiry,
ssizessizeargfunc, ssizeobjargproc, iternextfunc, initproc, richcmpfunc,
cmpfunc, hashfunc, descrgetfunc, descrsetfunc, objobjproc, objobjargproc,
readbufferproc)
@@ -60,6 +60,15 @@
args_w = space.fixedview(w_args)
return generic_cpy_call(space, func_binary, w_self, args_w[0])
+def wrap_inquirypred(space, w_self, w_args, func):
+ func_inquiry = rffi.cast(inquiry, func)
+ check_num_args(space, w_args, 0)
+ args_w = space.fixedview(w_args)
+ res = generic_cpy_call(space, func_inquiry, w_self)
+ if res == -1:
+ space.fromcache(State).check_and_raise_exception()
+ return space.wrap(bool(res))
+
def wrap_getattr(space, w_self, w_args, func):
func_target = rffi.cast(getattrfunc, func)
check_num_args(space, w_args, 1)
diff --git a/pypy/module/cpyext/stringobject.py
b/pypy/module/cpyext/stringobject.py
--- a/pypy/module/cpyext/stringobject.py
+++ b/pypy/module/cpyext/stringobject.py
@@ -294,6 +294,26 @@
w_errors = space.wrap(rffi.charp2str(errors))
return space.call_method(w_str, 'encode', w_encoding, w_errors)
+@cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
+def PyString_AsDecodedObject(space, w_str, encoding, errors):
+ """Decode a string object by passing it to the codec registered
+ for encoding and return the result as Python object. encoding and
+ errors have the same meaning as the parameters of the same name in
+ the string encode() method. The codec to be used is looked up
+ using the Python codec registry. Return NULL if an exception was
+ raised by the codec.
+
+ This function is not available in 3.x and does not have a PyBytes alias."""
+ if not PyString_Check(space, w_str):
+ PyErr_BadArgument(space)
+
+ w_encoding = w_errors = space.w_None
+ if encoding:
+ w_encoding = space.wrap(rffi.charp2str(encoding))
+ if errors:
+ w_errors = space.wrap(rffi.charp2str(errors))
+ return space.call_method(w_str, "decode", w_encoding, w_errors)
+
@cpython_api([PyObject, PyObject], PyObject)
def _PyString_Join(space, w_sep, w_seq):
return space.call_method(w_sep, 'join', w_seq)
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -1405,17 +1405,6 @@
"""
raise NotImplementedError
-@cpython_api([PyObject, Py_ssize_t, Py_ssize_t], PyObject)
-def PyList_GetSlice(space, list, low, high):
- """Return a list of the objects in list containing the objects between low
- and high. Return NULL and set an exception if unsuccessful. Analogous
- to list[low:high]. Negative indices, as when slicing from Python, are not
- supported.
-
- This function used an int for low and high. This might
- require changes in your code for properly supporting 64-bit systems."""
- raise NotImplementedError
-
@cpython_api([Py_ssize_t], PyObject)
def PyLong_FromSsize_t(space, v):
"""Return a new PyLongObject object from a C Py_ssize_t, or
@@ -1606,15 +1595,6 @@
for PyObject_Str()."""
raise NotImplementedError
-@cpython_api([PyObject], lltype.Signed, error=-1)
-def PyObject_HashNotImplemented(space, o):
- """Set a TypeError indicating that type(o) is not hashable and return -1.
- This function receives special treatment when stored in a tp_hash slot,
- allowing a type to explicitly indicate to the interpreter that it is not
- hashable.
- """
- raise NotImplementedError
-
@cpython_api([], PyFrameObject)
def PyEval_GetFrame(space):
"""Return the current thread state's frame, which is NULL if no frame is
@@ -1737,17 +1717,6 @@
changes in your code for properly supporting 64-bit systems."""
raise NotImplementedError
-@cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
-def PyString_AsDecodedObject(space, str, encoding, errors):
- """Decode a string object by passing it to the codec registered for
encoding and
- return the result as Python object. encoding and errors have the same
- meaning as the parameters of the same name in the string encode() method.
- The codec to be used is looked up using the Python codec registry. Return
NULL
- if an exception was raised by the codec.
-
- This function is not available in 3.x and does not have a PyBytes alias."""
- raise NotImplementedError
-
@cpython_api([rffi.CCHARP, Py_ssize_t, rffi.CCHARP, rffi.CCHARP], PyObject)
def PyString_Encode(space, s, size, encoding, errors):
"""Encode the char buffer of the given size by passing it to the codec
diff --git a/pypy/module/cpyext/test/test_listobject.py
b/pypy/module/cpyext/test/test_listobject.py
--- a/pypy/module/cpyext/test/test_listobject.py
+++ b/pypy/module/cpyext/test/test_listobject.py
@@ -58,6 +58,11 @@
w_t = api.PyList_AsTuple(w_l)
assert space.unwrap(w_t) == (3, 2, 1)
+ def test_list_getslice(self, space, api):
+ w_l = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1)])
+ w_s = api.PyList_GetSlice(w_l, 1, 5)
+ assert space.unwrap(w_s) == [2, 1]
+
class AppTestListObject(AppTestCpythonExtensionBase):
def test_listobject(self):
import sys
diff --git a/pypy/module/cpyext/test/test_stringobject.py
b/pypy/module/cpyext/test/test_stringobject.py
--- a/pypy/module/cpyext/test/test_stringobject.py
+++ b/pypy/module/cpyext/test/test_stringobject.py
@@ -307,6 +307,13 @@
space.wrap(2), lltype.nullptr(rffi.CCHARP.TO),
lltype.nullptr(rffi.CCHARP.TO)
)
+ def test_AsDecodedObject(self, space, api):
+ w_str = space.wrap('caf\xe9')
+ encoding = rffi.str2charp("latin-1")
+ w_res = api.PyString_AsDecodedObject(w_str, encoding, None)
+ rffi.free_charp(encoding)
+ assert space.unwrap(w_res) == u"caf\xe9"
+
def test_eq(self, space, api):
assert 1 == api._PyString_Eq(space.wrap("hello"), space.wrap("hello"))
assert 0 == api._PyString_Eq(space.wrap("hello"), space.wrap("world"))
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
@@ -488,3 +488,55 @@
assert type(it) is type(iter([]))
assert module.tp_iternext(it) == 1
raises(StopIteration, module.tp_iternext, it)
+
+ def test_bool(self):
+ module = self.import_extension('foo', [
+ ("newInt", "METH_VARARGS",
+ """
+ IntLikeObject *intObj;
+ long intval;
+ PyObject *name;
+
+ if (!PyArg_ParseTuple(args, "i", &intval))
+ return NULL;
+
+ IntLike_Type.tp_as_number = &intlike_as_number;
+ intlike_as_number.nb_nonzero = intlike_nb_nonzero;
+ if (PyType_Ready(&IntLike_Type) < 0) return NULL;
+ intObj = PyObject_New(IntLikeObject, &IntLike_Type);
+ if (!intObj) {
+ return NULL;
+ }
+
+ intObj->value = intval;
+ return (PyObject *)intObj;
+ """)],
+ """
+ typedef struct
+ {
+ PyObject_HEAD
+ int value;
+ } IntLikeObject;
+
+ static int
+ intlike_nb_nonzero(IntLikeObject *v)
+ {
+ if (v->value == -42) {
+ PyErr_SetNone(PyExc_ValueError);
+ return -1;
+ }
+ return v->value;
+ }
+
+ PyTypeObject IntLike_Type = {
+ PyObject_HEAD_INIT(0)
+ /*ob_size*/ 0,
+ /*tp_name*/ "IntLike",
+ /*tp_basicsize*/ sizeof(IntLikeObject),
+ };
+ static PyNumberMethods intlike_as_number;
+ """)
+ assert not bool(module.newInt(0))
+ assert bool(module.newInt(1))
+ assert bool(module.newInt(-1))
+ raises(ValueError, bool, module.newInt(-42))
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
@@ -10,6 +10,7 @@
from pypy.module.micronumpy.interp_dtype import get_dtype_cache
from pypy.module.micronumpy.interp_numarray import (Scalar, BaseArray,
scalar_w, W_NDimArray, array)
+from pypy.module.micronumpy.interp_arrayops import where
from pypy.module.micronumpy import interp_ufuncs
from pypy.rlib.objectmodel import specialize, instantiate
@@ -35,6 +36,7 @@
SINGLE_ARG_FUNCTIONS = ["sum", "prod", "max", "min", "all", "any",
"unegative", "flat", "tostring"]
TWO_ARG_FUNCTIONS = ["dot", 'take']
+THREE_ARG_FUNCTIONS = ['where']
class FakeSpace(object):
w_ValueError = None
@@ -445,14 +447,25 @@
arg = self.args[1].execute(interp)
if not isinstance(arg, BaseArray):
raise ArgumentNotAnArray
- if not isinstance(arg, BaseArray):
- raise ArgumentNotAnArray
if self.name == "dot":
w_res = arr.descr_dot(interp.space, arg)
elif self.name == 'take':
w_res = arr.descr_take(interp.space, arg)
else:
assert False # unreachable code
+ elif self.name in THREE_ARG_FUNCTIONS:
+ if len(self.args) != 3:
+ raise ArgumentMismatch
+ arg1 = self.args[1].execute(interp)
+ arg2 = self.args[2].execute(interp)
+ if not isinstance(arg1, BaseArray):
+ raise ArgumentNotAnArray
+ if not isinstance(arg2, BaseArray):
+ raise ArgumentNotAnArray
+ if self.name == "where":
+ w_res = where(interp.space, arr, arg1, arg2)
+ else:
+ assert False
else:
raise WrongFunctionName
if isinstance(w_res, BaseArray):
diff --git a/pypy/module/micronumpy/test/test_compile.py
b/pypy/module/micronumpy/test/test_compile.py
--- a/pypy/module/micronumpy/test/test_compile.py
+++ b/pypy/module/micronumpy/test/test_compile.py
@@ -270,3 +270,13 @@
b -> 2
""")
assert interp.results[0].value == 3
+
+ def test_where(self):
+ interp = self.run('''
+ a = [1, 0, 3, 0]
+ b = [1, 1, 1, 1]
+ c = [0, 0, 0, 0]
+ d = where(a, b, c)
+ d -> 1
+ ''')
+ assert interp.results[0].value == 0
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
@@ -1830,6 +1830,12 @@
a[a & 1 == 1] = array([8, 9, 10])
assert (a == [[0, 8], [2, 9], [4, 10]]).all()
+ def test_array_indexing_bool_setitem_multidim(self):
+ from _numpypy import arange
+ a = arange(10).reshape(5, 2)
+ a[a & 1 == 0] = 15
+ assert (a == [[15, 1], [15, 3], [15, 5], [15, 7], [15, 9]]).all()
+
def test_copy_kwarg(self):
from _numpypy import array
x = array([1, 2, 3])
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit