Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r89502:6ba9b4214af5
Date: 2017-01-11 19:01 +0000
http://bitbucket.org/pypy/pypy/changeset/6ba9b4214af5/

Log:    Remove uses of api.XXX magic in test_dictobject

diff --git a/pypy/module/cpyext/test/test_dictobject.py 
b/pypy/module/cpyext/test/test_dictobject.py
--- a/pypy/module/cpyext/test/test_dictobject.py
+++ b/pypy/module/cpyext/test/test_dictobject.py
@@ -1,83 +1,81 @@
 import py
 from rpython.rtyper.lltypesystem import rffi, lltype
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
 from pypy.module.cpyext.api import Py_ssize_tP, PyObjectP, PyTypeObjectPtr
 from pypy.module.cpyext.pyobject import make_ref, from_ref
 from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+from pypy.module.cpyext.dictobject import *
+from pypy.module.cpyext.pyobject import decref
 
 class TestDictObject(BaseApiTest):
-    def test_dict(self, space, api):
-        d = api.PyDict_New()
+    def test_dict(self, space):
+        d = PyDict_New(space)
         assert space.eq_w(d, space.newdict())
 
-        assert space.eq_w(api.PyDict_GetItem(space.wrap({"a": 72}),
+        assert space.eq_w(PyDict_GetItem(space, space.wrap({"a": 72}),
                                              space.wrap("a")),
                           space.wrap(72))
 
-        assert api.PyDict_SetItem(d, space.wrap("c"), space.wrap(42)) >= 0
+        PyDict_SetItem(space, d, space.wrap("c"), space.wrap(42))
         assert space.eq_w(space.getitem(d, space.wrap("c")),
                           space.wrap(42))
 
         space.setitem(d, space.wrap("name"), space.wrap(3))
-        assert space.eq_w(api.PyDict_GetItem(d, space.wrap("name")),
+        assert space.eq_w(PyDict_GetItem(space, d, space.wrap("name")),
                           space.wrap(3))
 
         space.delitem(d, space.wrap("name"))
-        assert not api.PyDict_GetItem(d, space.wrap("name"))
-        assert not api.PyErr_Occurred()
+        assert not PyDict_GetItem(space, d, space.wrap("name"))
 
         buf = rffi.str2charp("name")
-        assert not api.PyDict_GetItemString(d, buf)
+        assert not PyDict_GetItemString(space, d, buf)
         rffi.free_charp(buf)
-        assert not api.PyErr_Occurred()
 
-        assert api.PyDict_Contains(d, space.wrap("c"))
-        assert not api.PyDict_Contains(d, space.wrap("z"))
+        assert PyDict_Contains(space, d, space.wrap("c"))
+        assert not PyDict_Contains(space, d, space.wrap("z"))
 
-        assert api.PyDict_DelItem(d, space.wrap("c")) == 0
-        assert api.PyDict_DelItem(d, space.wrap("name")) < 0
-        assert api.PyErr_Occurred() is space.w_KeyError
-        api.PyErr_Clear()
-        assert api.PyDict_Size(d) == 0
+        PyDict_DelItem(space, d, space.wrap("c"))
+        with raises_w(space, KeyError):
+            PyDict_DelItem(space, d, space.wrap("name"))
+        assert PyDict_Size(space, d) == 0
 
         space.setitem(d, space.wrap("some_key"), space.wrap(3))
         buf = rffi.str2charp("some_key")
-        assert api.PyDict_DelItemString(d, buf) == 0
-        assert api.PyDict_Size(d) == 0
-        assert api.PyDict_DelItemString(d, buf) < 0
-        assert api.PyErr_Occurred() is space.w_KeyError
-        api.PyErr_Clear()
+        PyDict_DelItemString(space, d, buf)
+        assert PyDict_Size(space, d) == 0
+        with raises_w(space, KeyError):
+            PyDict_DelItemString(space, d, buf)
         rffi.free_charp(buf)
 
         d = space.wrap({'a': 'b'})
-        api.PyDict_Clear(d)
-        assert api.PyDict_Size(d) == 0
+        PyDict_Clear(space, d)
+        assert PyDict_Size(space, d) == 0
 
-    def test_check(self, space, api):
-        d = api.PyDict_New()
-        assert api.PyDict_Check(d)
-        assert api.PyDict_CheckExact(d)
+    def test_check(self, space):
+        d = PyDict_New(space, )
+        assert PyDict_Check(space, d)
+        assert PyDict_CheckExact(space, d)
         sub = space.appexec([], """():
             class D(dict):
                 pass
             return D""")
         d = space.call_function(sub)
-        assert api.PyDict_Check(d)
-        assert not api.PyDict_CheckExact(d)
+        assert PyDict_Check(space, d)
+        assert not PyDict_CheckExact(space, d)
         i = space.wrap(2)
-        assert not api.PyDict_Check(i)
-        assert not api.PyDict_CheckExact(i)
+        assert not PyDict_Check(space, i)
+        assert not PyDict_CheckExact(space, i)
 
-    def test_keys(self, space, api):
+    def test_keys(self, space):
         w_d = space.newdict()
         space.setitem(w_d, space.wrap("a"), space.wrap("b"))
 
-        assert space.eq_w(api.PyDict_Keys(w_d), space.wrap(["a"]))
-        assert space.eq_w(api.PyDict_Values(w_d), space.wrap(["b"]))
-        assert space.eq_w(api.PyDict_Items(w_d), space.wrap([("a", "b")]))
+        assert space.eq_w(PyDict_Keys(space, w_d), space.wrap(["a"]))
+        assert space.eq_w(PyDict_Values(space, w_d), space.wrap(["b"]))
+        assert space.eq_w(PyDict_Items(space, w_d), space.wrap([("a", "b")]))
 
-    def test_merge(self, space, api):
+    def test_merge(self, space):
         w_d = space.newdict()
         space.setitem(w_d, space.wrap("a"), space.wrap("b"))
 
@@ -86,35 +84,34 @@
         space.setitem(w_d2, space.wrap("c"), space.wrap("d"))
         space.setitem(w_d2, space.wrap("e"), space.wrap("f"))
 
-        api.PyDict_Merge(w_d, w_d2, 0)
+        PyDict_Merge(space, w_d, w_d2, 0)
         assert space.unwrap(w_d) == dict(a='b', c='d', e='f')
-        api.PyDict_Merge(w_d, w_d2, 1)
+        PyDict_Merge(space, w_d, w_d2, 1)
         assert space.unwrap(w_d) == dict(a='c', c='d', e='f')
 
-    def test_update(self, space, api):
+    def test_update(self, space):
         w_d = space.newdict()
         space.setitem(w_d, space.wrap("a"), space.wrap("b"))
 
-        w_d2 = api.PyDict_Copy(w_d)
+        w_d2 = PyDict_Copy(space, w_d)
         assert not space.is_w(w_d2, w_d)
         space.setitem(w_d, space.wrap("c"), space.wrap("d"))
         space.setitem(w_d2, space.wrap("e"), space.wrap("f"))
 
-        api.PyDict_Update(w_d, w_d2)
+        PyDict_Update(space, w_d, w_d2)
         assert space.unwrap(w_d) == dict(a='b', c='d', e='f')
 
-    def test_update_doesnt_accept_list_of_tuples(self, space, api):
+    def test_update_doesnt_accept_list_of_tuples(self, space):
         w_d = space.newdict()
         space.setitem(w_d, space.wrap("a"), space.wrap("b"))
 
         w_d2 = space.wrap([("c", "d"), ("e", "f")])
 
-        api.PyDict_Update(w_d, w_d2)
-        assert api.PyErr_Occurred() is space.w_AttributeError
-        api.PyErr_Clear()
+        with raises_w(space, AttributeError):
+            PyDict_Update(space, w_d, w_d2)
         assert space.unwrap(w_d) == dict(a='b') # unchanged
 
-    def test_iter(self, space, api):
+    def test_iter(self, space):
         w_dict = space.sys.getdict(space)
         py_dict = make_ref(space, w_dict)
 
@@ -125,7 +122,7 @@
 
         try:
             w_copy = space.newdict()
-            while api.PyDict_Next(w_dict, ppos, pkey, pvalue):
+            while PyDict_Next(space, w_dict, ppos, pkey, pvalue):
                 w_key = from_ref(space, pkey[0])
                 w_value = from_ref(space, pvalue[0])
                 space.setitem(w_copy, w_key, w_value)
@@ -134,12 +131,12 @@
             lltype.free(pkey, flavor='raw')
             lltype.free(pvalue, flavor='raw')
 
-        api.Py_DecRef(py_dict) # release borrowed references
+        decref(space, py_dict) # release borrowed references
 
         assert space.eq_w(space.len(w_copy), space.len(w_dict))
         assert space.eq_w(w_copy, w_dict)
 
-    def test_iterkeys(self, space, api):
+    def test_iterkeys(self, space):
         w_dict = space.sys.getdict(space)
         py_dict = make_ref(space, w_dict)
 
@@ -151,11 +148,11 @@
         values_w = []
         try:
             ppos[0] = 0
-            while api.PyDict_Next(w_dict, ppos, pkey, None):
+            while PyDict_Next(space, w_dict, ppos, pkey, None):
                 w_key = from_ref(space, pkey[0])
                 keys_w.append(w_key)
             ppos[0] = 0
-            while api.PyDict_Next(w_dict, ppos, None, pvalue):
+            while PyDict_Next(space, w_dict, ppos, None, pvalue):
                 w_value = from_ref(space, pvalue[0])
                 values_w.append(w_value)
         finally:
@@ -163,25 +160,25 @@
             lltype.free(pkey, flavor='raw')
             lltype.free(pvalue, flavor='raw')
 
-        api.Py_DecRef(py_dict) # release borrowed references
+        decref(space, py_dict) # release borrowed references
 
         assert space.eq_w(space.newlist(keys_w),
                           space.call_method(w_dict, "keys"))
         assert space.eq_w(space.newlist(values_w),
                           space.call_method(w_dict, "values"))
 
-    def test_dictproxy(self, space, api):
+    def test_dictproxy(self, space):
         w_dict = space.sys.get('modules')
-        w_proxy = api.PyDictProxy_New(w_dict)
+        w_proxy = PyDictProxy_New(space, w_dict)
         assert space.contains_w(w_proxy, space.wrap('sys'))
         raises(OperationError, space.setitem,
                w_proxy, space.wrap('sys'), space.w_None)
         raises(OperationError, space.delitem,
                w_proxy, space.wrap('sys'))
         raises(OperationError, space.call_method, w_proxy, 'clear')
-        assert api.PyDictProxy_Check(w_proxy)
+        assert PyDictProxy_Check(space, w_proxy)
 
-    def test_typedict1(self, space, api):
+    def test_typedict1(self, space):
         py_type = make_ref(space, space.w_int)
         py_dict = rffi.cast(PyTypeObjectPtr, py_type).c_tp_dict
         ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
@@ -191,7 +188,7 @@
         pvalue = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
         try:
             w_copy = space.newdict()
-            while api.PyDict_Next(py_dict, ppos, pkey, pvalue):
+            while PyDict_Next(space, py_dict, ppos, pkey, pvalue):
                 w_key = from_ref(space, pkey[0])
                 w_value = from_ref(space, pvalue[0])
                 space.setitem(w_copy, w_key, w_value)
@@ -199,7 +196,7 @@
             lltype.free(ppos, flavor='raw')
             lltype.free(pkey, flavor='raw')
             lltype.free(pvalue, flavor='raw')
-        api.Py_DecRef(py_type) # release borrowed references
+        decref(space, py_type) # release borrowed references
         # do something with w_copy ?
 
 class AppTestDictObject(AppTestCpythonExtensionBase):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to