summaryrefslogtreecommitdiffstats
path: root/bindings
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2014-12-03 00:22:44 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2015-02-12 18:21:13 (GMT)
commita231eaff33d784b19f416f855f112c1fd0fd2b1d (patch)
treea0322a2156ae60eb41793456eb57f4640d24719c /bindings
parent8938f8722022aa4d3e514b62b4af94886a31bb88 (diff)
downloadlasso-a231eaff33d784b19f416f855f112c1fd0fd2b1d.zip
lasso-a231eaff33d784b19f416f855f112c1fd0fd2b1d.tar.gz
lasso-a231eaff33d784b19f416f855f112c1fd0fd2b1d.tar.bz2
Make python generator scripts and tests run with python >= 3.2
Diffstat (limited to 'bindings')
-rw-r--r--bindings/python/lang.py15
-rw-r--r--bindings/python/tests/Makefile.am2
-rwxr-xr-xbindings/python/tests/binding_tests.py4
-rwxr-xr-xbindings/python/tests/profiles_tests.py14
-rw-r--r--bindings/python/wrapper_bottom.c3
-rw-r--r--bindings/python/wrapper_top.c64
6 files changed, 79 insertions, 23 deletions
diff --git a/bindings/python/lang.py b/bindings/python/lang.py
index 7fa168e..3b8ec92 100644
--- a/bindings/python/lang.py
+++ b/bindings/python/lang.py
@@ -114,6 +114,8 @@ class Binding:
# this file has been generated automatically; do not edit
import _lasso
+import sys
+
def cptrToPy(cptr):
if cptr is None:
@@ -123,11 +125,14 @@ def cptrToPy(cptr):
o._cptr = cptr
return o
-def str2lasso(s):
- if s is not None and not isinstance(s, str):
- # Assume this is a Python 2 unicode string.
- return s.encode('utf-8')
- return s
+if sys.version_info >= (3,):
+ def str2lasso(s):
+ return s
+else: # Python 2.x
+ def str2lasso(s):
+ if isinstance(s, unicode):
+ return s.encode('utf-8')
+ return s
class frozendict(dict):
\'\'\'Immutable dict\'\'\'
diff --git a/bindings/python/tests/Makefile.am b/bindings/python/tests/Makefile.am
index 205e761..0580897 100644
--- a/bindings/python/tests/Makefile.am
+++ b/bindings/python/tests/Makefile.am
@@ -1,7 +1,7 @@
MAINTAINERCLEANFILES = Makefile.in
TESTS = #
-TESTS_ENVIRONMENT=TOP_SRCDIR=$(top_srcdir)
+TESTS_ENVIRONMENT=TOP_SRCDIR=$(top_srcdir) $(PYTHON)
if PYTHON_ENABLED
TESTS += profiles_tests.py binding_tests.py
diff --git a/bindings/python/tests/binding_tests.py b/bindings/python/tests/binding_tests.py
index edbfc19..3e39512 100755
--- a/bindings/python/tests/binding_tests.py
+++ b/bindings/python/tests/binding_tests.py
@@ -268,7 +268,7 @@ class BindingTestCase(unittest.TestCase):
def test09(self):
'''Test dictionary attributes'''
- identity = lasso.Identity.newFromDump(file(
+ identity = lasso.Identity.newFromDump(open(
os.path.join(dataDir, 'sample-identity-dump-1.xml')).read())
self.failUnlessEqual(len(identity.federations.keys()), 2)
self.failIf(not 'http://idp1.lasso.lan' in identity.federations.keys())
@@ -293,7 +293,7 @@ class BindingTestCase(unittest.TestCase):
'''Test saving and reloading a Server using an encrypted private key'''
pkey = os.path.join(dataDir, 'sp7-saml2', 'private-key.pem')
mdata = os.path.join(dataDir, 'sp7-saml2', 'metadata.xml')
- password = file(os.path.join(dataDir, 'sp7-saml2', 'password')).read().strip()
+ password = open(os.path.join(dataDir, 'sp7-saml2', 'password')).read().strip()
server = lasso.Server(mdata, pkey, password)
assert isinstance(server, lasso.Server)
server_dump = server.dump()
diff --git a/bindings/python/tests/profiles_tests.py b/bindings/python/tests/profiles_tests.py
index bbe1748..547c9e2 100755
--- a/bindings/python/tests/profiles_tests.py
+++ b/bindings/python/tests/profiles_tests.py
@@ -49,7 +49,7 @@ def server(local_name, remote_role, remote_name):
pwd = os.path.join(dataDir, local_name, 'password')
password = None
if os.path.exists(pwd):
- password = file(pwd).read()
+ password = open(pwd).read()
s = lasso.Server(os.path.join(dataDir, local_name, 'metadata.xml'),
os.path.join(dataDir, local_name, 'private-key.pem'),
password)
@@ -126,7 +126,7 @@ class LoginTestCase(unittest.TestCase):
login = lasso.Login(lassoServer)
try:
login.processResponseMsg('')
- except lasso.Error, error:
+ except lasso.Error as error:
if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
raise
@@ -318,7 +318,7 @@ class LogoutTestCase(unittest.TestCase):
logout = lasso.Logout(lassoServer)
try:
logout.initRequest()
- except lasso.Error, error:
+ except lasso.Error as error:
if error[0] != lasso.PROFILE_ERROR_SESSION_NOT_FOUND:
raise
else:
@@ -357,7 +357,7 @@ class LogoutTestCase(unittest.TestCase):
# The processRequestMsg should fail but not abort.
try:
logout.processRequestMsg('passport=0&lasso=1')
- except lasso.Error, error:
+ except lasso.Error as error:
if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
raise
else:
@@ -380,7 +380,7 @@ class LogoutTestCase(unittest.TestCase):
# The processResponseMsg should fail but not abort.
try:
logout.processResponseMsg('liberty=&alliance')
- except lasso.Error, error:
+ except lasso.Error as error:
if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
raise
else:
@@ -404,7 +404,7 @@ class DefederationTestCase(unittest.TestCase):
# The processNotificationMsg should fail but not abort.
try:
defederation.processNotificationMsg('nonLibertyQuery=1')
- except lasso.Error, error:
+ except lasso.Error as error:
if error[0] != lasso.PROFILE_ERROR_INVALID_MSG:
raise
else:
@@ -437,7 +437,7 @@ class AttributeAuthorityTestCase(unittest.TestCase):
os.path.join(dataDir, 'sp5-saml2/metadata.xml'))
aq = lasso.AssertionQuery(s)
- rpid = s.providers.keys()[0]
+ rpid = list(s.providers.keys())[0]
aq.initRequest(rpid,
lasso.HTTP_METHOD_SOAP,
lasso.ASSERTION_QUERY_REQUEST_TYPE_ATTRIBUTE)
diff --git a/bindings/python/wrapper_bottom.c b/bindings/python/wrapper_bottom.c
index 30971b1..c8a5b5f 100644
--- a/bindings/python/wrapper_bottom.c
+++ b/bindings/python/wrapper_bottom.c
@@ -7,7 +7,7 @@
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define MOD_DEF(ob, name, doc, methods) \
static struct PyModuleDef moduledef = { \
- PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
+ PyModuleDef_HEAD_INIT, name, doc, -1, methods, NULL, NULL, NULL, NULL}; \
ob = PyModule_Create(&moduledef);
#else
#define MOD_ERROR_VAL
@@ -32,6 +32,7 @@ MOD_INIT(_lasso)
Py_INCREF(&PyGObjectPtrType);
PyModule_AddObject(m, "PyGObjectPtr", (PyObject *)&PyGObjectPtrType);
+
lasso_init();
lasso_log_set_handler(G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION | G_LOG_LEVEL_MASK,
lasso_python_log, NULL);
diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c
index 47c7050..09e999f 100644
--- a/bindings/python/wrapper_top.c
+++ b/bindings/python/wrapper_top.c
@@ -23,17 +23,62 @@ typedef int Py_ssize_t;
#endif
// Python 3 has removed PyString and related functions, in favor of PyBytes & PyUnicode.
-#if PY_MAJOR_VERSION >= 3
+#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3)
+#define PyString_Check PyUnicode_Check
+#define PyString_FromFormat PyUnicode_FromFormat
+#define PyString_FromString PyUnicode_FromString
#define PyString_AsString PyUnicode_AsUTF8
+static Py_ssize_t PyString_Size(PyObject *string) {
+ Py_ssize_t size;
+ char *ret = PyUnicode_AsUTF8AndSize(string, &size);
+ if (! ret) {
+ return -1;
+ }
+ return size;
+}
+#define get_pystring PyUnicode_AsUTF8AndSize
+#define PyStringFree(string) ;
+#elif PY_MAJOR_VERSION >= 3
#define PyString_Check PyUnicode_Check
#define PyString_FromFormat PyUnicode_FromFormat
#define PyString_FromString PyUnicode_FromString
-#define PyString_Size PyUnicode_GET_LENGTH
+static char *PyString_AsString(PyObject *string) {
+ PyObject *bytes = PyUnicode_AsUTF8String(string);
+ char *ret = NULL;
+ if (! bytes)
+ return NULL;
+ ret = g_strdup(PyBytes_AsString(bytes));
+ Py_DECREF(bytes);
+ return ret;
+}
+static char *get_pystring(PyObject *string, Py_ssize_t *size) {
+ char *ret = NULL;
+
+ ret = PyString_AsString(string);
+ *size = strlen(ret);
+ return ret;
+}
+#define PyStringFree(string) g_free(string)
+#elif PY_MAJOR_VERSION >= 3
+#else
+static char *get_pystring(PyObject *string, Py_ssize_t *size) {
+ char *ret = NULL;
+
+ PyString_AsStringAndSize(string, &ret, size);
+
+ return ret;
+}
+#define PyStringFree(string) ;
#endif
GQuark lasso_wrapper_key;
+
+#if PY_MAJOR_VERSION > 3
+PyMODINIT_FUNC PyInit_lasso(void);
+#else
PyMODINIT_FUNC init_lasso(void);
+#endif
G_GNUC_UNUSED static PyObject* get_pystring_from_xml_node(xmlNode *xmlnode);
G_GNUC_UNUSED static xmlNode* get_xml_node_from_pystring(PyObject *string);
G_GNUC_UNUSED static PyObject* get_dict_from_hashtable_of_objects(GHashTable *value);
@@ -213,7 +258,7 @@ set_hashtable_of_pygobject(GHashTable *a_hash, PyObject *dict) {
g_hash_table_remove_all (a_hash);
i = 0;
while (PyDict_Next(dict, &i, &key, &value)) {
- char *ckey = g_strdup(PyString_AsString(key));
+ char *ckey = PyString_AsString(key);
g_hash_table_replace (a_hash, ckey, ((PyGObjectPtr*)value)->obj);
}
return;
@@ -258,7 +303,7 @@ set_hashtable_of_strings(GHashTable *a_hash, PyObject *dict)
while (PyDict_Next(dict, &i, &key, &value)) {
char *ckey = PyString_AsString(key);
char *cvalue = PyString_AsString(value);
- g_hash_table_insert (a_hash, g_strdup(ckey), g_strdup(cvalue));
+ g_hash_table_insert (a_hash, ckey, cvalue);
}
failure:
return;
@@ -354,8 +399,13 @@ failure:
static xmlNode*
get_xml_node_from_pystring(PyObject *string) {
- return lasso_string_fragment_to_xmlnode(PyString_AsString(string),
- PyString_Size(string));
+ char *utf8 = NULL;
+ Py_ssize_t size;
+ xmlNode *xml_node;
+ utf8 = get_pystring(string, &size);
+ xml_node = lasso_string_fragment_to_xmlnode(utf8, size);
+ PyStringFree(utf8);
+ return xml_node;
}
/** Return a tuple containing the string contained in a_list */
@@ -603,7 +653,7 @@ static PyTypeObject PyGObjectPtrType = {
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
- PyGObjectPtr_members, /* tp_members */
+ .tp_members=PyGObjectPtr_members, /* tp_members */
PyGObjectPtr_getseters, /* tp_getset */
NULL,
NULL