[pypy-svn] r74174 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test
afa at codespeak.net
afa at codespeak.net
Wed Apr 28 15:04:48 CEST 2010
Author: afa
Date: Wed Apr 28 15:04:47 2010
New Revision: 74174
Modified:
pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py
Log:
PyDict_GetItem never raises: it returns NULL when the key does not exists *and* on any other error.
+ add PyDict_Clear
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py Wed Apr 28 15:04:47 2010
@@ -11,12 +11,14 @@
PyDict_Check, PyDict_CheckExact = build_type_checkers("Dict")
- at cpython_api([PyObject, PyObject], PyObject)
+ at cpython_api([PyObject, PyObject], PyObject, borrowed=True, error=CANNOT_FAIL)
def PyDict_GetItem(space, w_dict, w_key):
- if PyDict_Check(space, w_dict):
- return space.getitem(w_dict, w_key)
- else:
- PyErr_BadInternalCall(space)
+ try:
+ w_res = space.getitem(w_dict, w_key)
+ except:
+ return None
+ register_container(space, w_dict)
+ return w_res
@cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1)
def PyDict_SetItem(space, w_dict, w_key, w_obj):
@@ -45,15 +47,16 @@
else:
PyErr_BadInternalCall(space)
- at cpython_api([PyObject, CONST_STRING], PyObject, borrowed=True)
+ at cpython_api([PyObject, CONST_STRING], PyObject, borrowed=True, error=CANNOT_FAIL)
def PyDict_GetItemString(space, w_dict, key):
"""This is the same as PyDict_GetItem(), but key is specified as a
char*, rather than a PyObject*."""
- if not PyDict_Check(space, w_dict):
- PyErr_BadInternalCall(space)
- w_res = space.finditem_str(w_dict, rffi.charp2str(key))
+ try:
+ w_res = space.finditem_str(w_dict, rffi.charp2str(key))
+ except:
+ w_res = None
if w_res is None:
- raise OperationError(space.w_KeyError, space.wrap("Key not found"))
+ return None
register_container(space, w_dict)
return w_res
@@ -64,6 +67,11 @@
len(p) on a dictionary."""
return space.int_w(space.len(w_obj))
+ at cpython_api([PyObject], lltype.Void)
+def PyDict_Clear(space, w_obj):
+ """Empty an existing dictionary of all key-value pairs."""
+ space.call_method(w_obj, "clear")
+
@cpython_api([PyObject, Py_ssize_t, PyObjectP, PyObjectP], rffi.INT_real, error=CANNOT_FAIL)
def PyDict_Next(space, w_obj, ppos, pkey, pvalue):
"""Iterate over all key-value pairs in the dictionary p. The
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py Wed Apr 28 15:04:47 2010
@@ -1,3 +1,4 @@
+from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.test.test_api import BaseApiTest
class TestDictObject(BaseApiTest):
@@ -19,8 +20,12 @@
space.delitem(d, space.wrap("name"))
assert not api.PyDict_GetItem(d, space.wrap("name"))
- assert api.PyErr_Occurred() is space.w_KeyError
- api.PyErr_Clear()
+ assert not api.PyErr_Occurred()
+
+ buf = rffi.str2charp("name")
+ assert not api.PyDict_GetItemString(d, buf)
+ rffi.free_charp(buf)
+ assert not api.PyErr_Occurred()
assert api.PyDict_DelItem(d, space.wrap("c")) == 0
assert api.PyDict_DelItem(d, space.wrap("name")) < 0
@@ -28,6 +33,10 @@
api.PyErr_Clear()
assert api.PyDict_Size(d) == 0
+ d = space.wrap({'a': 'b'})
+ api.PyDict_Clear(d)
+ assert api.PyDict_Size(d) == 0
+
def test_check(self, space, api):
d = api.PyDict_New()
assert api.PyDict_Check(d)
More information about the Pypy-commit
mailing list