From 7bf964b1ce1fb77a46840ba84dc1bd762dd0667d Mon Sep 17 00:00:00 2001 From: Haoyu Bai Date: Wed, 22 Jul 2009 05:38:59 +0000 Subject: [PATCH] implemented list.sort with keyword argument support, by using args_proxy and kwds_proxy. Not a nice solution though... [SVN r55078] --- include/boost/python/list.hpp | 10 ++++++++-- src/list.cpp | 8 ++++++++ test/list.cpp | 6 ++++++ test/list.py | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/boost/python/list.hpp b/include/boost/python/list.hpp index 9b9611c8..e3dbf711 100644 --- a/include/boost/python/list.hpp +++ b/include/boost/python/list.hpp @@ -37,8 +37,12 @@ namespace detail void reverse(); // reverse *IN PLACE* void sort(); // sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1 +#if PY_VERSION_HEX >= 0x03000000 + void sort(args_proxy const &args, + kwds_proxy const &kwds); +#else void sort(object_cref cmpfunc); - +#endif protected: list_base(); // new list @@ -113,13 +117,15 @@ class list : public detail::list_base base::remove(object(value)); } +#if PY_VERSION_HEX <= 0x03000000 void sort() { base::sort(); } - + template void sort(T const& value) { base::sort(object(value)); } +#endif public: // implementation detail -- for internal use only BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list, base) diff --git a/src/list.cpp b/src/list.cpp index ead51848..7d273ade 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -132,10 +132,18 @@ void list_base::sort() } } +#if PY_VERSION_HEX >= 0x03000000 +void list_base::sort(args_proxy const &args, + kwds_proxy const &kwds) +{ + this->attr("sort")(args, kwds); +} +#else void list_base::sort(object_cref cmpfunc) { this->attr("sort")(cmpfunc); } +#endif // For some reason, moving this to the end of the TU suppresses an ICE // with vc6. diff --git a/test/list.cpp b/test/list.cpp index 8ddf8574..3e9fcbe6 100644 --- a/test/list.cpp +++ b/test/list.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #define BOOST_ENABLE_ASSERT_HANDLER @@ -114,7 +116,11 @@ void exercise(list x, object y, object print) print(x); print("reverse sorted:"); +#if PY_VERSION_HEX >= 0x03000000 + x.sort(*tuple(), **dict(make_tuple(make_tuple("reverse", true)))); +#else x.sort(¬cmp); +#endif print(x); list w; diff --git a/test/list.py b/test/list.py index 6c1b8aef..8be21123 100644 --- a/test/list.py +++ b/test/list.py @@ -73,7 +73,7 @@ X(22) ... >>> y = X(42) ->>> exercise(letters, y, printer) +>>> exercise(letters, y, printer) #doctest: +NORMALIZE_WHITESPACE after append: ['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3)] number of X(42) instances: 1