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?)

This commit is contained in:
Alberto Valverde 2010-03-19 13:42:58 +00:00
parent 3f47c813cf
commit 17b61d16c4
2 changed files with 29 additions and 24 deletions

View file

@ -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<UnicodeString>());
boost::python::converter::registry::push_back(
&convertible,
&construct,
boost::python::type_id<UnicodeString>());
}
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<UnicodeString>*)
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<UnicodeString>*)
data)->storage.bytes;
new (storage) UnicodeString(value);
data->convertible = storage;
}
};
*/
void export_feature()
{
@ -258,12 +268,12 @@ void export_feature()
implicitly_convertible<int,mapnik::value>();
implicitly_convertible<double,mapnik::value>();
//implicitly_convertible<UnicodeString,mapnik::value>();
implicitly_convertible<UnicodeString,mapnik::value>();
implicitly_convertible<bool,mapnik::value>();
std_pair_to_python_converter<std::string const,mapnik::value>();
to_python_converter<mapnik::value,mapnik_value_to_python>();
//UnicodeString_from_python_str();
UnicodeString_from_python_str();
class_<Feature,boost::shared_ptr<Feature>,
boost::noncopyable>("Feature",init<int>("Default ctor."))

View file

@ -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):