Author: Maciej Fijalkowski <fij...@gmail.com> Branch: Changeset: r54650:92a93f454539 Date: 2012-04-23 10:28 +0200 http://bitbucket.org/pypy/pypy/changeset/92a93f454539/
Log: merge 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,16 @@ 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) + res = rffi.cast(lltype.Signed, res) + 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)) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit