From caa6f2b9fa8577bb86646268e99250b0b06dc8aa Mon Sep 17 00:00:00 2001 From: Haoyu Bai Date: Mon, 2 Mar 2009 17:56:29 +0000 Subject: [PATCH] Fixes to support Python 3.0.1 and higher. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11143 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- CHANGES.current | 6 ++++ .../test-suite/python/swigobject_runme.py | 13 ++++----- Lib/python/pycontainer.swg | 2 +- Lib/python/pyrun.swg | 28 +++++++++++++------ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 1c37bc20d..085a80bca 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,6 +1,12 @@ Version 1.3.39 (in progress) ============================ +2008-03-01: bhy + [Python] Some fixes for Python 3.0.1 and higher support. In 3.0.1, the C API function + PyObject_Compare is removed, so PyObject_RichCompareBool is used for replacement. + Struct initilization of SwigPyObject and SwigPyObject_as_number changed to reflect + the drop of tp_compare and nb_long. + 2008-03-01: bhy [Python] Fix SF#2583160. Now the importer in Python shadow wrapper take care of the case that module already imported at other place. diff --git a/Examples/test-suite/python/swigobject_runme.py b/Examples/test-suite/python/swigobject_runme.py index 70219a57c..a906108e3 100644 --- a/Examples/test-suite/python/swigobject_runme.py +++ b/Examples/test-suite/python/swigobject_runme.py @@ -1,8 +1,5 @@ from swigobject import * -from string import replace -from string import upper -from string import lstrip a = A() @@ -17,12 +14,12 @@ if a1.this != a2.this: lthis = long(a.this) # match pointer value, but deal with leading zeros on 8/16 bit systems and different C++ compilers interpretation of %p xstr1 = "%016X" % (lthis,) -xstr1 = lstrip(xstr1, '0') +xstr1 = str.lstrip(xstr1, '0') xstr2 = pointer_str(a) -xstr2 = replace(xstr2, "0x", "") -xstr2 = replace(xstr2, "0X", "") -xstr2 = lstrip(xstr2, '0') -xstr2 = upper(xstr2) +xstr2 = str.replace(xstr2, "0x", "") +xstr2 = str.replace(xstr2, "0X", "") +xstr2 = str.lstrip(xstr2, '0') +xstr2 = str.upper(xstr2) if xstr1 != xstr2: print xstr1, xstr2 diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 5f09357f8..b74c18e99 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -115,7 +115,7 @@ namespace std { { bool res; SWIG_PYTHON_THREAD_BEGIN_BLOCK; - res = PyObject_Compare(v, w) < 0; + res = PyObject_RichCompareBool(v, w, Py_LT); /* This may fall into a case of inconsistent eg. ObjA > ObjX > ObjB but ObjA < ObjB diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index bc5490960..897fa7fb3 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -660,7 +660,11 @@ _PySwigObject_type(void) { 0, /*nb_coerce*/ #endif (unaryfunc)SwigPyObject_long, /*nb_int*/ +#if PY_VERSION_HEX < 0x03000000 (unaryfunc)SwigPyObject_long, /*nb_long*/ +#else + 0, /*nb_reserved*/ +#endif (unaryfunc)0, /*nb_float*/ #if PY_VERSION_HEX < 0x03000000 (unaryfunc)SwigPyObject_oct, /*nb_oct*/ @@ -700,7 +704,11 @@ _PySwigObject_type(void) { (getattrfunc)0, /* tp_getattr */ #endif (setattrfunc)0, /* tp_setattr */ - (cmpfunc)SwigPyObject_compare, /* tp_compare */ +#if PY_VERSION_HEX >= 0x03000000 + 0, /* tp_reserved in 3.0.1 */ +#else + (cmpfunc)SwigPyObject_compare, /* tp_compare */ +#endif (reprfunc)SwigPyObject_repr, /* tp_repr */ &SwigPyObject_as_number, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -871,14 +879,18 @@ _PySwigPacked_type(void) { (printfunc)SwigPyPacked_print, /* tp_print */ (getattrfunc)0, /* tp_getattr */ (setattrfunc)0, /* tp_setattr */ - (cmpfunc)SwigPyPacked_compare, /* tp_compare */ - (reprfunc)SwigPyPacked_repr, /* tp_repr */ - 0, /* tp_as_number */ +#if PY_VERSION_HEX>=0x03000000 + 0, /* tp_reserved in 3.0.1 */ +#else + (cmpfunc)SwigPyPacked_compare, /* tp_compare */ +#endif + (reprfunc)SwigPyPacked_repr, /* tp_repr */ + 0, /* tp_as_number */ 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - (hashfunc)0, /* tp_hash */ - (ternaryfunc)0, /* tp_call */ - (reprfunc)SwigPyPacked_str, /* tp_str */ + 0, /* tp_as_mapping */ + (hashfunc)0, /* tp_hash */ + (ternaryfunc)0, /* tp_call */ + (reprfunc)SwigPyPacked_str, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */