From ac885a907525589aa56266d9a2527cdc7127c9cb Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 10 Nov 2009 18:44:08 -0500 Subject: All strings passed to Python are now Unicode objects --- src/py_utils.cc | 105 ++++++++++++++++++++++---------------------------------- 1 file changed, 41 insertions(+), 64 deletions(-) (limited to 'src/py_utils.cc') diff --git a/src/py_utils.cc b/src/py_utils.cc index 364e575f..2736ed3e 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -75,13 +75,19 @@ typedef register_python_conversion bool_python_conversion; -#if defined(STRING_VERIFY_ON) - struct string_to_python { - static PyObject* convert(const ledger::string& str) + static PyObject* convert(const string& str) { +#if 1 + // Return a Unicode object + PyObject * pstr = PyString_FromString(str.c_str()); + PyObject * uni = PyUnicode_FromEncodedObject(pstr, "UTF-8", NULL); + return object(handle<>(borrowed(uni))).ptr(); +#else + // Return a 7-bit ASCII string return incref(object(static_cast(str)).ptr()); +#endif } }; @@ -89,75 +95,49 @@ struct string_from_python { static void* convertible(PyObject* obj_ptr) { - if (!PyString_Check(obj_ptr)) return 0; + if (!PyUnicode_Check(obj_ptr) && + !PyString_Check(obj_ptr)) return 0; return obj_ptr; } static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) { - const char* value = PyString_AsString(obj_ptr); - if (value == 0) throw_error_already_set(); - void* storage = - reinterpret_cast *> - (data)->storage.bytes; - new (storage) ledger::string(value); - data->convertible = storage; + if (PyString_Check(obj_ptr)) { + const char* value = PyString_AsString(obj_ptr); + if (value == 0) throw_error_already_set(); + void* storage = + reinterpret_cast *> + (data)->storage.bytes; + new (storage) string(value); + data->convertible = storage; + } else { + VERIFY(PyUnicode_Check(obj_ptr)); + + Py_ssize_t size = PyUnicode_GET_SIZE(obj_ptr); + const Py_UNICODE* value = PyUnicode_AS_UNICODE(obj_ptr); + + string str; + if (sizeof(Py_UNICODE) == 2) // UTF-16 + utf8::unchecked::utf16to8(value, value + size, std::back_inserter(str)); + else if (sizeof(Py_UNICODE) == 4) // UTF-32 + utf8::unchecked::utf32to8(value, value + size, std::back_inserter(str)); + else + assert(! "Py_UNICODE has an unexpected size"); + + if (value == 0) throw_error_already_set(); + void* storage = + reinterpret_cast *> + (data)->storage.bytes; + new (storage) string(str); + data->convertible = storage; + } } }; -typedef register_python_conversion +typedef register_python_conversion string_python_conversion; -#endif // STRING_VERIFY_ON - - -struct unicode_to_python -{ - static PyObject* convert(const std::string& utf8str) - { - PyObject * pstr = PyString_FromString(utf8str.c_str()); - PyObject * uni = PyUnicode_FromEncodedObject(pstr, "UTF-8", NULL); - return object(handle<>(borrowed(uni))).ptr(); - } -}; - -struct unicode_from_python -{ - static void* convertible(PyObject* obj_ptr) - { - if (!PyUnicode_Check(obj_ptr)) return 0; - return obj_ptr; - } - - static void construct(PyObject* obj_ptr, - converter::rvalue_from_python_stage1_data* data) - { - Py_ssize_t size = PyUnicode_GET_SIZE(obj_ptr); - const Py_UNICODE* value = PyUnicode_AS_UNICODE(obj_ptr); - - std::string str; - if (sizeof(Py_UNICODE) == 2) // UTF-16 - utf8::unchecked::utf16to8(value, value + size, std::back_inserter(str)); - else if (sizeof(Py_UNICODE) == 4) // UTF-32 - utf8::unchecked::utf32to8(value, value + size, std::back_inserter(str)); - else - assert(! "Py_UNICODE has an unexpected size"); - - if (value == 0) throw_error_already_set(); - void* storage = - reinterpret_cast *> - (data)->storage.bytes; - new (storage) std::string(str); - data->convertible = storage; - } -}; - -typedef register_python_conversion - unicode_python_conversion; - struct istream_to_python { @@ -273,10 +253,7 @@ void export_utils() ; bool_python_conversion(); -#if defined(STRING_VERIFY_ON) string_python_conversion(); -#endif - unicode_python_conversion(); istream_python_conversion(); ostream_python_conversion(); } -- cgit v1.2.3