From 17b61d16c4a7ac29cd27ea4665ce2b968d857f9a Mon Sep 17 00:00:00 2001 From: Alberto Valverde Date: Fri, 19 Mar 2010 13:42:58 +0000 Subject: [PATCH] More progress on #528: Made setting Feature properties with values of type unicode or str possible. I've modified the UnicodeString_from_python_str from_python_converter so it can also convert python unicode objects (not just encocded strs). It was originally commented out so but it seems to work fine on my tests. Is there anything I0ve overlooked? (Artem?) --- bindings/python/mapnik_feature.cpp | 46 ++++++++++++++++++------------ tests/python_tests/feature_test.py | 7 +---- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/bindings/python/mapnik_feature.cpp b/bindings/python/mapnik_feature.cpp index e49ab2fff..d8fdebaea 100644 --- a/bindings/python/mapnik_feature.cpp +++ b/bindings/python/mapnik_feature.cpp @@ -219,37 +219,47 @@ mapnik::feature_ptr create_feature_(int id) { return mapnik::feature_ptr(new mapnik::Feature(id)); } +*/ struct UnicodeString_from_python_str { UnicodeString_from_python_str() { - boost::python::converter::registry::push_back( - &convertible, - &construct, - boost::python::type_id()); + boost::python::converter::registry::push_back( + &convertible, + &construct, + boost::python::type_id()); } static void* convertible(PyObject* obj_ptr) { - if (!PyString_Check(obj_ptr)) return 0; - return obj_ptr; + if (!(PyString_Check(obj_ptr) || PyUnicode_Check(obj_ptr))) + return 0; + return obj_ptr; } static void construct( - PyObject* obj_ptr, - boost::python::converter::rvalue_from_python_stage1_data* data) + PyObject* obj_ptr, + boost::python::converter::rvalue_from_python_stage1_data* data) { - const char* value = PyString_AsString(obj_ptr); - if (value == 0) boost::python::throw_error_already_set(); - void* storage = ( - (boost::python::converter::rvalue_from_python_storage*) - data)->storage.bytes; - new (storage) UnicodeString(value); - data->convertible = storage; + char * value=0; + if (PyUnicode_Check(obj_ptr)) { + PyObject *encoded = PyUnicode_AsEncodedString(obj_ptr, "utf8", "replace"); + if (encoded) { + value = PyString_AsString(encoded); + Py_DecRef(encoded); + } + } else { + value = PyString_AsString(obj_ptr); + } + if (value == 0) boost::python::throw_error_already_set(); + void* storage = ( + (boost::python::converter::rvalue_from_python_storage*) + data)->storage.bytes; + new (storage) UnicodeString(value); + data->convertible = storage; } }; -*/ void export_feature() { @@ -258,12 +268,12 @@ void export_feature() implicitly_convertible(); implicitly_convertible(); - //implicitly_convertible(); + implicitly_convertible(); implicitly_convertible(); std_pair_to_python_converter(); to_python_converter(); - //UnicodeString_from_python_str(); + UnicodeString_from_python_str(); class_, boost::noncopyable>("Feature",init("Default ctor.")) diff --git a/tests/python_tests/feature_test.py b/tests/python_tests/feature_test.py index 86d5f1894..c760b38da 100644 --- a/tests/python_tests/feature_test.py +++ b/tests/python_tests/feature_test.py @@ -23,12 +23,7 @@ class FeatureTest(unittest.TestCase): except TypeError: self.fail("%r (%s)"%(expected, type(expected))) self.failUnlessEqual(f[key], expected) - for v in (1, True, 1.4): - test_val(v) - - raise Todo("Support setting unicode and string properties") - - for v in ("foo", u"foó"): + for v in (1, True, 1.4, "foo", u"avión"): test_val(v) def test_add_wkb_geometry_(self):