Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make iter.c work, and add a test suite for Iterators.

This change adds a common module, which will hold exceptions definitions and
utilities. It also shows how exceptions from a Python module would be used from
within a C extension.

The test suite requires a fresh checkout of Pry from here:

    http://github.com/cortesi/pry
  • Loading branch information...
commit f910f6d13de23ba6a81945068b453c3922ff9b30 1 parent 5d368ad
@cortesi cortesi authored
Showing with 64 additions and 7 deletions.
  1. +30 −0 test/test_iter.py
  2. +21 −0 xcb/common.py
  3. +13 −7 xcb/iter.c
View
30 test/test_iter.py
@@ -0,0 +1,30 @@
+import libpry
+from xcb import iter, common
+
+class uIter(libpry.AutoTree):
+ def test_simple(self):
+ i = iter.Iterator([1, 2, 3, 4], 2, "test", True)
+ assert list(i) == [(1, 2), (3, 4)]
+
+ i = iter.Iterator([1, 2, 3, 4], 2, "test", False)
+ assert list(i) == [(1, 2), (3, 4)]
+
+ i = iter.Iterator([], 2, "test", True)
+ assert list(i) == []
+
+ i = iter.Iterator([1], 2, "test", True)
+ libpry.raises("extra items", list, i)
+
+ i = iter.Iterator([1], 2, "test", False)
+ libpry.raises("too few items", list, i)
+
+ i = iter.Iterator([1, 2, 3, 4], 3, "test", True)
+ libpry.raises("extra items", list, i)
+
+ i = iter.Iterator([1, 2, 3, 4], 3, "test", False)
+ libpry.raises("too few items", list, i)
+
+
+tests = [
+ uIter(),
+]
View
21 xcb/common.py
@@ -0,0 +1,21 @@
+
+# xpybExcept_base
+class Exception(Exception):
+ pass
+
+
+# xpybExcept_conn
+class ConnectException(Exception):
+ pass
+
+
+# xpybExcept_ext
+class ExtensionException(Exception):
+ pass
+
+
+# xpybExcept_proto
+class ProtocolException(Exception):
+ pass
+
+
View
20 xcb/iter.c
@@ -5,6 +5,8 @@
/*
* Helpers
*/
+PyObject *xpybExcept_base;
+
static void
xpybIter_err(xpybIter *self)
@@ -169,13 +171,17 @@ PyTypeObject xpybIter_type = {
/*
* Module init
*/
-int xpybIter_modinit(PyObject *m)
-{
+void inititer(void){
+ PyObject *module, *common;
+
+ module = Py_InitModule("iter", NULL);
if (PyType_Ready(&xpybIter_type) < 0)
- return -1;
+ return;
Py_INCREF(&xpybIter_type);
- if (PyModule_AddObject(m, "Iterator", (PyObject *)&xpybIter_type) < 0)
- return -1;
-
- return 0;
+ if (PyModule_AddObject(module, "Iterator", (PyObject *)&xpybIter_type) < 0)
+ return;
+ common = PyImport_ImportModule("xcb.common");
+ if (!common)
+ return;
+ xpybExcept_base = PyObject_GetAttrString(common, "Exception");
}
Please sign in to comment.
Something went wrong with that request. Please try again.