From 734b19cf153fa3f981aa602f27c2bac815cbf084 Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <albertito@blitiri.com.ar>
Date: Fri, 27 Mar 2009 01:44:22 -0300
Subject: [PATCH 05/48] Add Python 3 bindings, based on the Python 2 bindings

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
---
 .gitignore                             |    3 +++
 Makefile                               |   13 +++++++++++--
 bindings/{python2 => python3}/fiu.py   |    8 ++++----
 bindings/{python2 => python3}/fiu_ll.c |   20 +++++++++++++++-----
 bindings/{python2 => python3}/setup.py |    0
 5 files changed, 33 insertions(+), 11 deletions(-)
 copy bindings/{python2 => python3}/fiu.py (95%)
 copy bindings/{python2 => python3}/fiu_ll.c (93%)
 copy bindings/{python2 => python3}/setup.py (100%)

diff --git a/.gitignore b/.gitignore
index e0c7c3d..7e4eb4e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,9 @@
 bindings/python2/build
 bindings/python2/*.pyc
 bindings/python2/*.pyo
+bindings/python3/build
+bindings/python3/*.pyc
+bindings/python3/*.pyo
 libfiu/*.o
 libfiu/libfiu.a
 libfiu/libfiu.pc
diff --git a/Makefile b/Makefile
index a05295c..31ae8e0 100644
--- a/Makefile
+++ b/Makefile
@@ -19,12 +19,21 @@ python2_install:
 python2_clean:
 	cd bindings/python2 && rm -rf build/
 
+python3:
+	cd bindings/python3 && python3 setup.py build
 
-clean: python2_clean
+python3_install:
+	cd bindings/python3 && python3 setup.py install
+
+python3_clean:
+	cd bindings/python3 && rm -rf build/
+
+clean: python2_clean python3_clean
 	$(MAKE) -C libfiu clean
 
 
 .PHONY: default all clean libfiu utils \
-	python2 python2_install python2_clean
+	python2 python2_install python2_clean \
+	python3 python3_install python3_clean
 
 
diff --git a/bindings/python2/fiu.py b/bindings/python3/fiu.py
similarity index 95%
copy from bindings/python2/fiu.py
copy to bindings/python3/fiu.py
index f4e6ece..822ad9b 100644
--- a/bindings/python2/fiu.py
+++ b/bindings/python3/fiu.py
@@ -36,7 +36,7 @@ def enable(name, failnum = 1, failinfo = None, flags = 0):
 	r = _ll.enable(name, failnum, failinfo, flags)
 	if r != 0:
 		del _fi_table[name]
-		raise RuntimeError, r
+		raise RuntimeError(r)
 
 def enable_random(name, probability, failnum = 1, failinfo = None, flags = 0):
 	"Enables the given point of failure, with the given probability."
@@ -44,7 +44,7 @@ def enable_random(name, probability, failnum = 1, failinfo = None, flags = 0):
 	r = _ll.enable_random(name, failnum, failinfo, flags, probability)
 	if r != 0:
 		del _fi_table[name]
-		raise RuntimeError, r
+		raise RuntimeError(r)
 
 def enable_external(name, cb, failnum = 1, flags = 0):
 	"""Enables the given point of failure, leaving the decision whether to
@@ -61,7 +61,7 @@ def enable_external(name, cb, failnum = 1, flags = 0):
 	_fi_table[name] = cb
 	r = _ll.enable_external(name, failnum, flags, cb)
 	if r != 0:
-		raise RuntimeError, r
+		raise RuntimeError(r)
 
 def disable(name):
 	"""Disables the given point of failure, undoing the actions of the
@@ -70,6 +70,6 @@ def disable(name):
 		del _fi_table[name]
 	r = _ll.disable(name)
 	if r != 0:
-		raise RuntimeError, r
+		raise RuntimeError(r)
 
 
diff --git a/bindings/python2/fiu_ll.c b/bindings/python3/fiu_ll.c
similarity index 93%
copy from bindings/python2/fiu_ll.c
copy to bindings/python3/fiu_ll.c
index f0c9a49..cff30ed 100644
--- a/bindings/python2/fiu_ll.c
+++ b/bindings/python3/fiu_ll.c
@@ -119,10 +119,10 @@ static int external_callback(const char *name, int *failnum, void **failinfo,
 		return 0;
 	}
 
-	/* If PyInt_AsLong() causes an error, it will be handled by the
+	/* If PyLong_AsLong() causes an error, it will be handled by the
 	 * PyErr_Occurred() check in fail(), so we don't need to worry about
 	 * it now. */
-	rv = PyInt_AsLong(cbrv);
+	rv = PyLong_AsLong(cbrv);
 	Py_DECREF(cbrv);
 
 	PyGILState_Release(gil_state);
@@ -168,7 +168,7 @@ static PyObject *disable(PyObject *self, PyObject *args)
 }
 
 
-static PyMethodDef fiu_functions[] = {
+static PyMethodDef fiu_methods[] = {
 	{ "fail", (PyCFunction) fail, METH_VARARGS, NULL },
 	{ "failinfo", (PyCFunction) failinfo, METH_VARARGS, NULL },
 	{ "enable", (PyCFunction) enable, METH_VARARGS, NULL },
@@ -178,14 +178,24 @@ static PyMethodDef fiu_functions[] = {
 	{ NULL }
 };
 
-PyMODINIT_FUNC initfiu_ll(void)
+static PyModuleDef fiu_module = {
+	PyModuleDef_HEAD_INIT,
+	.m_name = "libfiu",
+	.m_size = -1,
+	.m_methods = fiu_methods,
+};
+
+
+PyMODINIT_FUNC PyInit_fiu_ll(void)
 {
 	PyObject *m;
 
-	m = Py_InitModule("fiu_ll", fiu_functions);
+	m = PyModule_Create(&fiu_module);
 
 	PyModule_AddIntConstant(m, "FIU_ONETIME", FIU_ONETIME);
 
 	fiu_init(0);
+
+	return m;
 }
 
diff --git a/bindings/python2/setup.py b/bindings/python3/setup.py
similarity index 100%
copy from bindings/python2/setup.py
copy to bindings/python3/setup.py
-- 
1.6.2.2.646.gb214

