diff --git a/bindings/python/lang.py b/bindings/python/lang.py index 7fa168ed..3b8ec92a 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 205e7613..05808979 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 edbfc19f..3e395122 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 bbe1748d..547c9e24 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 30971b10..c8a5b5f1 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 47c7050e..09e999f3 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 -#define PyString_AsString PyUnicode_AsUTF8 +#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_Size PyUnicode_GET_LENGTH +#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 +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 diff --git a/lasso/extract_symbols.py b/lasso/extract_symbols.py index cda7cd0a..6b1f2975 100644 --- a/lasso/extract_symbols.py +++ b/lasso/extract_symbols.py @@ -3,6 +3,7 @@ import glob import re import sys +import six enable_wsf = False @@ -21,7 +22,7 @@ for header_file in glob.glob('%s/*/*.h' % srcdir) + glob.glob('%s/*.h' % srcdir) glob.glob('%s/*/*/*.h' % srcdir): if ('/id-wsf/' in header_file or '/id-wsf-2.0' in header_file) and not enable_wsf: continue - symbols.extend(regex.findall(file(header_file).read().replace('\\\n', ''))) + symbols.extend(regex.findall(open(header_file).read().replace('\\\n', ''))) wsf = ['lasso_disco_', 'lasso_dst_', 'lasso_is_', 'lasso_profile_service_', 'lasso_discovery', 'lasso_wsf', 'lasso_interaction_', 'lasso_utility_', @@ -36,5 +37,5 @@ for s in symbols: if s.startswith(t): break else: - print s + six.print_(s) diff --git a/lasso/extract_types.py b/lasso/extract_types.py index 9f73a68a..2bacbd56 100644 --- a/lasso/extract_types.py +++ b/lasso/extract_types.py @@ -1,10 +1,10 @@ #! /usr/bin/env python -from cStringIO import StringIO import glob import re import sys -import os +from six.moves import cStringIO as StringIO +import six enable_wsf = 0 @@ -26,10 +26,10 @@ if enable_wsf: fd = StringIO() -print >> fd, "/* This file has been autogenerated; changes will be lost */" -print >> fd, "" -print >> fd, "typedef GType (*type_function) ();" -print >> fd, "" +six.print_("/* This file has been autogenerated; changes will be lost */", file=fd) +six.print_("", file=fd) +six.print_("typedef GType (*type_function) ();", file=fd) +six.print_("", file=fd) header_files = [] for header_file in glob.glob('%s/*/*.h' % srcdir) + glob.glob('%s/*/*/*.h' % srcdir): @@ -44,10 +44,10 @@ for header_file in glob.glob('%s/*/*.h' % srcdir) + glob.glob('%s/*/*/*.h' % src if type.startswith(t): break else: - print >> fd, "extern GType %s();" % type + six.print_("extern GType %s();" % type, file=fd) -print >> fd, "" -print >> fd, "type_function functions[] = {" +six.print_("", file=fd) +six.print_("type_function functions[] = {", file=fd) for header_file in header_files: try: type = re.findall('lasso_.*get_type', open(header_file).read())[0] @@ -57,8 +57,8 @@ for header_file in header_files: if type.startswith(t): break else: - print >> fd, "\t%s," % type -print >> fd, "\tNULL" -print >> fd, "};" + six.print_("\t%s," % type, file=fd) +six.print_("\tNULL", file=fd) +six.print_("};", file=fd) -file('types.c', 'w').write(fd.getvalue()) +open('types.c', 'w').write(fd.getvalue())