diff options
author | 2013-12-13 01:47:46 +0400 | |
---|---|---|
committer | 2013-12-29 19:59:08 +0000 | |
commit | ff990637e2397690ce911a762d1ce089acd602ad (patch) | |
tree | 00e2d13bd96a6a4bb90761bdb3d2d9e11b39e9a4 | |
parent | fddcb26bc121d802d79b67034ef90cb7a99338ff (diff) | |
download | paludis-ff990637e2397690ce911a762d1ce089acd602ad.tar.gz paludis-ff990637e2397690ce911a762d1ce089acd602ad.tar.xz |
Make it Python3 compatible
-rw-r--r-- | configure.ac | 4 | ||||
-rwxr-xr-x | doc/api/python/example_version_spec.py | 15 | ||||
-rw-r--r-- | python/choices_TEST.py | 12 | ||||
-rwxr-xr-x | python/dep_spec_TEST.py | 6 | ||||
-rw-r--r-- | python/exception.hh | 13 | ||||
-rw-r--r-- | python/iterable.hh | 24 | ||||
-rwxr-xr-x | python/mask_TEST.py | 16 | ||||
-rwxr-xr-x | python/metadata_key_TEST.py | 6 | ||||
-rw-r--r-- | python/name.cc | 9 | ||||
-rwxr-xr-x | python/package_id_TEST.py | 10 | ||||
-rw-r--r-- | python/paludis_python.hh | 14 | ||||
-rw-r--r-- | python/version_spec.cc | 9 | ||||
-rw-r--r-- | python/wrapped_value.hh | 18 |
13 files changed, 109 insertions, 47 deletions
diff --git a/configure.ac b/configure.ac index ebbc5f6cf..9c11264ec 100644 --- a/configure.ac +++ b/configure.ac @@ -1051,8 +1051,8 @@ if test "x$enable_python" = "xyes" ; then AC_MSG_ERROR([Your gcc is too old for Python support])) AC_MSG_CHECKING(for headers required to compile python extensions) - py_prefix=`$PYTHON -c "import sys; print sys.prefix"` - py_exec_prefix=`$PYTHON -c "import sys; print sys.exec_prefix"` + py_prefix=`$PYTHON -c "import sys; print(sys.prefix)"` + py_exec_prefix=`$PYTHON -c "import sys; print(sys.exec_prefix)"` PYTHON_INCLUDE_DIR="${py_prefix}/include/python${PYTHON_VERSION}" if test "$py_prefix" != "$py_exec_prefix"; then PYTHON_INCLUDE_DIR="$PYTHON_INCLUDE_DIR -I${py_exec_prefix}/include/python${PYTHON_VERSION}" diff --git a/doc/api/python/example_version_spec.py b/doc/api/python/example_version_spec.py index c479d7332..bd6110266 100755 --- a/doc/api/python/example_version_spec.py +++ b/doc/api/python/example_version_spec.py @@ -10,13 +10,12 @@ versions = [paludis.VersionSpec(v) for v in "1.0 1.1 1.2 1.2-r1 2.0 2.0-try1 2.0 # For each version... for v in versions: - print str(v) + ":" + print(str(v) + ":") # Show the output of various members. - print " Remove revision: %s" % v.remove_revision() - print " Revision only: %s" % v.revision_only() - print " Bump: %s" % v.bump() - print " Is SCM? %s" % v.is_scm - print " Has -try? %s" % v.has_try_part - print " Has -scm? %s" % v.has_scm_part - print + print(" Remove revision: %s" % v.remove_revision()) + print(" Revision only: %s" % v.revision_only()) + print(" Bump: %s" % v.bump()) + print(" Is SCM? %s" % v.is_scm) + print(" Has -try? %s" % v.has_try_part) + print(" Has -scm? %s" % v.has_scm_part) diff --git a/python/choices_TEST.py b/python/choices_TEST.py index 161cbef09..161b9aa83 100644 --- a/python/choices_TEST.py +++ b/python/choices_TEST.py @@ -34,7 +34,7 @@ Log.instance.log_level = LogLevel.WARNING class TestCase_01_Choices(unittest.TestCase): def setUp(self): self.e = EnvironmentFactory.instance.create("") - self.pid = iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", [])).next() + self.pid = next(iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", []))) self.choices = self.pid.find_metadata("PALUDIS_CHOICES").parse_value() def test_01_choices(self): @@ -50,7 +50,7 @@ class TestCase_01_Choices(unittest.TestCase): self.assert_(not self.choices.has_matching_contains_every_value_prefix("foo")) def test_04_iter(self): - self.assert_(isinstance(iter(self.choices).next(), Choice)) + self.assert_(isinstance(next(iter(self.choices)), Choice)) found = False for f in self.choices: if f.raw_name == "USE": @@ -60,7 +60,7 @@ class TestCase_01_Choices(unittest.TestCase): class TestCase_02_Choice(unittest.TestCase): def setUp(self): self.e = EnvironmentFactory.instance.create("") - self.pid = iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", [])).next() + self.pid = next(iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", []))) self.choices = self.pid.find_metadata("PALUDIS_CHOICES").parse_value() self.use = None self.linguas = None @@ -91,7 +91,7 @@ class TestCase_02_Choice(unittest.TestCase): self.assertEquals(self.linguas.consider_added_or_changed, True) def test_03_use_iter(self): - self.assert_(isinstance(iter(self.use).next(), ChoiceValue)) + self.assert_(isinstance(next(iter(self.use)), ChoiceValue)) found = False for f in self.use: if f.name_with_prefix == "testflag": @@ -99,7 +99,7 @@ class TestCase_02_Choice(unittest.TestCase): self.assert_(found) def test_04_linguas_iter(self): - self.assert_(isinstance(iter(self.linguas).next(), ChoiceValue)) + self.assert_(isinstance(next(iter(self.linguas)), ChoiceValue)) found = False for f in self.linguas: if f.name_with_prefix == "linguas_en": @@ -109,7 +109,7 @@ class TestCase_02_Choice(unittest.TestCase): class TestCase_03_ChoiceValue(unittest.TestCase): def setUp(self): self.e = EnvironmentFactory.instance.create("") - self.pid = iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", [])).next() + self.pid = next(iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", []))) self.choices = self.pid.find_metadata("PALUDIS_CHOICES").parse_value() self.use_testflag = self.choices.find_by_name_with_prefix("testflag") self.linguas_en = self.choices.find_by_name_with_prefix("linguas_en") diff --git a/python/dep_spec_TEST.py b/python/dep_spec_TEST.py index b34a88f66..28b26ca10 100755 --- a/python/dep_spec_TEST.py +++ b/python/dep_spec_TEST.py @@ -79,8 +79,8 @@ class TestCase_1_DepSpecs(unittest.TestCase): vrc = self.pds.version_requirements self.assertEquals(len(list(vrc)), 1) - self.assertEquals(iter(vrc).next().version_spec, VersionSpec("1")) - self.assertEquals(iter(vrc).next().version_operator.value, VersionOperator(">=").value) + self.assertEquals(next(iter(vrc)).version_spec, VersionSpec("1")) + self.assertEquals(next(iter(vrc)).version_operator.value, VersionOperator(">=").value) def test_08_version_requirements_mode(self): self.get_depspecs() @@ -88,7 +88,7 @@ class TestCase_1_DepSpecs(unittest.TestCase): ### def test_09_additional_requirements(self): ### spec = parse_user_package_dep_spec("foo/monkey[foo]", UserPackageDepSpecOptions()) -### ur = iter(spec.additional_requirements).next() +### ur = next(iter(spec.additional_requirements)) ### self.assert_(isinstance(ur, EnabledUseRequirement)) def test_11_name(self): diff --git a/python/exception.hh b/python/exception.hh index 767ab9aab..b81755194 100644 --- a/python/exception.hh +++ b/python/exception.hh @@ -68,7 +68,12 @@ namespace paludis _e(PyErr_NewException(const_cast<char*>(_longname.c_str()), base, NULL)) { PyModule_AddObject(boost::python::detail::current_scope, const_cast<char*>(_name.c_str()), _e); - PyObject * doc_string = PyString_FromString(doc.c_str()); + PyObject * doc_string = +#if PY_MAJOR_VERSION < 3 + PyString_FromString(doc.c_str()); +# else + PyUnicode_FromString(doc.c_str()); +# endif PyObject_SetAttrString(_e, "__doc__", doc_string); boost::python::register_exception_translator<Ex_>( std::bind(std::mem_fn(&RegisteredException<Ex_>::translator), this, std::placeholders::_1)); @@ -78,9 +83,15 @@ namespace paludis void RegisteredException<Ex_>::translator(const Ex_ & x) const { +#if PY_MAJOR_VERSION < 3 PyObject * backtrace = PyString_FromString(x.backtrace("\n").c_str()); PyObject * message = PyString_FromString(x.message().c_str()); PyObject * what = PyString_FromString(x.what()); +#else + PyObject * backtrace = PyUnicode_FromString(x.backtrace("\n").c_str()); + PyObject * message = PyUnicode_FromString(x.message().c_str()); + PyObject * what = PyUnicode_FromString(x.what()); +#endif PyObject_SetAttrString(_e, "backtrace", backtrace); PyObject_SetAttrString(_e, "message", message); PyObject_SetAttrString(_e, "what", what); diff --git a/python/iterable.hh b/python/iterable.hh index 8dc35891c..770213aa6 100644 --- a/python/iterable.hh +++ b/python/iterable.hh @@ -63,8 +63,7 @@ namespace paludis { static void add(Set<To_> & c, PyObject * ptr) { - const char * str = PyString_AsString(ptr); - c.insert(To_(std::string(str))); + c.insert(To_(boost::python::extract<std::string>(ptr))); } }; @@ -73,8 +72,7 @@ namespace paludis { static void add(Sequence<To_> & c, PyObject * ptr) { - const char * str = PyString_AsString(ptr); - c.push_back(To_(std::string(str))); + c.push_back(To_(boost::python::extract<std::string>(ptr))); } }; @@ -130,7 +128,14 @@ namespace paludis V_ * ptr = boost::python::extract<V_ *>(o); s->push_back(*ptr); } - else if (IsConvertible<std::string, V_>::value && PyString_Check(o.ptr())) + else if ( + IsConvertible<std::string, V_>::value +#if PY_MAJOR_VERSION < 3 + && PyString_Check(o.ptr()) +#else + && PyUnicode_Check(o.ptr()) +#endif + ) { ConditionalAdd<V_, std::string, Sequence<V_>, IsConvertible<std::string, V_>::value>::add(*s, o.ptr()); @@ -200,7 +205,14 @@ namespace paludis V_ * ptr = boost::python::extract<V_ *>(o); s->insert(*ptr); } - else if (IsConvertible<std::string, V_>::value && PyString_Check(o.ptr())) + else if ( + IsConvertible<std::string, V_>::value +#if PY_MAJOR_VERSION < 3 + && PyString_Check(o.ptr()) +#else + && PyUnicode_Check(o.ptr()) +#endif + ) { ConditionalAdd<V_, std::string, Set<V_>, IsConvertible<std::string, V_>::value>::add(*s, o.ptr()); diff --git a/python/mask_TEST.py b/python/mask_TEST.py index 83660b5e2..d272f2bdd 100755 --- a/python/mask_TEST.py +++ b/python/mask_TEST.py @@ -36,8 +36,8 @@ class TestCase_01_Masks(unittest.TestCase): def test_01_user_mask(self): q = Selection.RequireExactlyOne(Generator.Matches( parse_user_package_dep_spec("=masked/user-1.0", self.e, []), [])) - pid = iter(self.e[q]).next() - m = iter(pid.masks).next() + pid = next(iter(self.e[q])) + m = next(iter(pid.masks)) self.assert_(isinstance(m, Mask)) self.assert_(isinstance(m, UserMask)) @@ -48,8 +48,8 @@ class TestCase_01_Masks(unittest.TestCase): def test_02_unaccepted_mask(self): q = Selection.RequireExactlyOne(Generator.Matches( parse_user_package_dep_spec("=masked/unaccepted-1.0", self.e, []), [])) - pid = iter(self.e[q]).next() - m = iter(pid.masks).next() + pid = next(iter(self.e[q])) + m = next(iter(pid.masks)) self.assert_(isinstance(m, Mask)) self.assert_(isinstance(m, UnacceptedMask)) @@ -61,8 +61,8 @@ class TestCase_01_Masks(unittest.TestCase): def test_03_repository_mask(self): q = Selection.RequireExactlyOne(Generator.Matches( parse_user_package_dep_spec("=masked/repo-1.0", self.e, []), [])) - pid = iter(self.e[q]).next() - m = iter(pid.masks).next() + pid = next(iter(self.e[q])) + m = next(iter(pid.masks)) self.assert_(isinstance(m, Mask)) self.assert_(isinstance(m, RepositoryMask)) @@ -76,8 +76,8 @@ class TestCase_01_Masks(unittest.TestCase): def test_04_unsupported_mask(self): q = Selection.RequireExactlyOne(Generator.Matches( parse_user_package_dep_spec("=masked/unsupported-1.0", self.e, []), [])) - pid = iter(self.e[q]).next() - m = iter(pid.masks).next() + pid = next(iter(self.e[q])) + m = next(iter(pid.masks)) self.assert_(isinstance(m, Mask)) self.assert_(isinstance(m, UnsupportedMask)) diff --git a/python/metadata_key_TEST.py b/python/metadata_key_TEST.py index 0261e4ab2..e9ab7f247 100755 --- a/python/metadata_key_TEST.py +++ b/python/metadata_key_TEST.py @@ -35,8 +35,8 @@ Log.instance.log_level = LogLevel.WARNING class TestCase_01_MetadataKeys(unittest.TestCase): def setUp(self): self.e = EnvironmentFactory.instance.create("") - self.pid = iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", [])).next() - self.ipid = iter(self.e.fetch_repository("installed").package_ids("cat-one/pkg-one", [])).next() + self.pid = next(iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", []))) + self.ipid = next(iter(self.e.fetch_repository("installed").package_ids("cat-one/pkg-one", []))) def test_02_installed_time(self): self.assertEquals(self.pid.find_metadata("INSTALLED_TIME"), None) @@ -70,7 +70,7 @@ class TestCase_02_MetadataKeys_suclassing(unittest.TestCase): def parse_value(self): e = EnvironmentFactory.instance.create("") - pid = iter(e.fetch_repository("testrepo").package_ids("foo/bar", [])).next() + pid = next(iter(e.fetch_repository("testrepo").package_ids("foo/bar", []))) return pid def raw_name(self): diff --git a/python/name.cc b/python/name.cc index 5d98ebfba..32a22fc1c 100644 --- a/python/name.cc +++ b/python/name.cc @@ -188,7 +188,16 @@ void expose_name() "[ro] PackageNamePart" ) +#if PY_MAJOR_VERSION < 3 .def("__cmp__", &py_cmp<QualifiedPackageName>) +# else + .def(bp::self == bp::self) + .def(bp::self != bp::self) + .def(bp::self < bp::self) + .def(bp::self <= bp::self) + .def(bp::self > bp::self) + .def(bp::self >= bp::self) +# endif .def(bp::self_ns::str(bp::self)) ; diff --git a/python/package_id_TEST.py b/python/package_id_TEST.py index 0c652b1d3..15b55c16c 100755 --- a/python/package_id_TEST.py +++ b/python/package_id_TEST.py @@ -33,9 +33,9 @@ Log.instance.log_level = LogLevel.WARNING class TestCase_01_PackageID(unittest.TestCase): def setUp(self): self.e = EnvironmentFactory.instance.create("") - self.pid = iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", [])).next() - self.ipid = iter(self.e.fetch_repository("installed").package_ids("cat-one/pkg-one", [])).next() - self.mpid = iter(self.e.fetch_repository("testrepo").package_ids("cat/masked", [])).next() + self.pid = next(iter(self.e.fetch_repository("testrepo").package_ids("foo/bar", []))) + self.ipid = next(iter(self.e.fetch_repository("installed").package_ids("cat-one/pkg-one", []))) + self.mpid = next(iter(self.e.fetch_repository("testrepo").package_ids("cat/masked", []))) def test_01_get(self): pass @@ -102,7 +102,7 @@ class TestCase_01_PackageID(unittest.TestCase): self.assert_(self.mpid.masked) def test_13_masks(self): - mask = iter(self.mpid.masks).next() + mask = next(iter(self.mpid.masks)) self.assert_(isinstance(mask, UnacceptedMask)) def test_18_build_dependencies_key(self): @@ -139,7 +139,7 @@ class TestCase_01_PackageID(unittest.TestCase): def test_28_from_repositories_key(self): self.assertEquals(self.pid.from_repositories_key(), None) - self.assertEquals(iter(self.ipid.from_repositories_key().parse_value()).next(), "origin_test") + self.assertEquals(next(iter(self.ipid.from_repositories_key().parse_value())), "origin_test") def test_30_fs_location_key(self): self.assert_(isinstance(self.ipid.fs_location_key(), MetadataFSPathKey)) diff --git a/python/paludis_python.hh b/python/paludis_python.hh index 0691fee57..feed60328 100644 --- a/python/paludis_python.hh +++ b/python/paludis_python.hh @@ -92,7 +92,15 @@ namespace paludis doc += "\n\t" + e_name_up; } PyTypeObject * pto = reinterpret_cast<PyTypeObject *>(enum_.ptr()); - PyDict_SetItemString(pto->tp_dict, "__doc__", PyString_FromString(doc.c_str())); + PyDict_SetItemString( + pto->tp_dict + , "__doc__" +#if PY_MAJOR_VERSION < 3 + , PyString_FromString(doc.c_str()) +#else + , PyUnicode_FromString(doc.c_str()) +#endif + ); } // Compare @@ -128,7 +136,11 @@ namespace paludis static PyObject * convert(const T_ & x) { +#if PY_MAJOR_VERSION < 3 return PyString_FromString(stringify<T_>(x).c_str()); +#else + return PyUnicode_FromString(stringify<T_>(x).c_str()); +#endif } }; diff --git a/python/version_spec.cc b/python/version_spec.cc index 17e6d7260..1619a6c3d 100644 --- a/python/version_spec.cc +++ b/python/version_spec.cc @@ -95,7 +95,16 @@ void expose_version_spec() "Revision part only (or \"r0\")." ) +#if PY_MAJOR_VERSION < 3 .def("__cmp__", &VersionSpec::compare) +#else + .def(bp::self == bp::self) + .def(bp::self != bp::self) + .def(bp::self < bp::self) + .def(bp::self <= bp::self) + .def(bp::self > bp::self) + .def(bp::self >= bp::self) +#endif .def(bp::self_ns::str(bp::self)) ; diff --git a/python/wrapped_value.hh b/python/wrapped_value.hh index b390b4e7a..b3cba5270 100644 --- a/python/wrapped_value.hh +++ b/python/wrapped_value.hh @@ -43,10 +43,20 @@ namespace paludis boost::python::init<const typename WrappedValueTraits<Tag_>::UnderlyingType &>(("__init__("+init_arg+")").c_str()) ) { - this->def(boost::python::self_ns::str(boost::python::self)); - this->def("__cmp__", - &paludis::python::py_cmp<WrappedValue<Tag_> >); - boost::python::implicitly_convertible<typename WrappedValueTraits<Tag_>::UnderlyingType, + namespace bp = boost::python; + this->def(bp::self_ns::str(bp::self)) +#if PY_MAJOR_VERSION < 3 + .def("__cmp__", &paludis::python::py_cmp<WrappedValue<Tag_> >) +# else + .def(bp::self == bp::self) + .def(bp::self != bp::self) + .def(bp::self < bp::self) + .def(bp::self <= bp::self) + .def(bp::self > bp::self) + .def(bp::self >= bp::self) +# endif + ; + bp::implicitly_convertible<typename WrappedValueTraits<Tag_>::UnderlyingType, WrappedValue<Tag_> >(); } }; |