Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lacks a type converter from python utf string or bytes to std::string #448

Open
inter1965 opened this issue Jan 19, 2020 · 0 comments
Open

Comments

@inter1965
Copy link

diff --git libpyEM/libpyTypeConverter2.cpp libpyEM/libpyTypeConverter2.cpp
index c9c4461fa..5aa4ff983 100644
--- libpyEM/libpyTypeConverter2.cpp
+++ libpyEM/libpyTypeConverter2.cpp
@@ -112,6 +112,7 @@ BOOST_PYTHON_MODULE(libpyTypeConverter2)
EMAN::EMObject_to_python();
EMAN::Dict_to_python();
EMAN::String_to_python();

  • EMAN::String_from_python();
    EMAN::Dict_from_python();

    EMAN::tuple3_to_pythonEMAN::IntPoint();
    diff --git libpyEM/typeconverter.h libpyEM/typeconverter.h
    index ace5ab315..34b474b55 100644
    --- libpyEM/typeconverter.h
    +++ libpyEM/typeconverter.h
    @@ -915,6 +916,51 @@ namespace EMAN {

    };

  • struct String_from_python {

  •   String_from_python() {
    
  •   	boost::python::converter::registry::push_back(
    
  •   		&convertible,
    
  •   		&construct,
    
  •   		boost::python::type_id<std::string>());
    
  •   }
    
  •   // Determine if obj_ptr can be converted in a std::string
    
  •   static void* convertible(PyObject* obj_ptr) {
    
  •   	if (!PyUnicode_Check(obj_ptr) && !PyBytes_Check(obj_ptr)) {
    
  •   		return 0;
    
  •   	}
    
  •   	return obj_ptr;
    
  •   }
    
  •   // Convert obj_ptr into a std::string
    
  •   static void construct(
    
  •   	PyObject* obj_ptr,
    
  •   	boost::python::converter::rvalue_from_python_stage1_data* data)	{
    
  •   	const char* value(nullptr);
    
  •   	boost::python::object temp_bytes_obj;
    
  •   	if (PyUnicode_Check(obj_ptr)) {
    
  •   		PyObject * temp_bytes(PyUnicode_AsEncodedString(obj_ptr, "UTF-8", "strict")); // Owned reference
    
  •   		if (temp_bytes != nullptr) {
    
  •   			// Take over lifetime management of temp_bytes
    
  •   			temp_bytes_obj = boost::python::object(boost::python::handle<>(temp_bytes));
    
  •   			value = PyBytes_AS_STRING(temp_bytes);
    
  •   		} else {
    
  •   			boost::python::throw_error_already_set();
    
  •   		}
    
  •   	} else if (PyBytes_Check(obj_ptr)) {
    
  •   		value = PyBytes_AS_STRING(obj_ptr);
    
  •   	} else {
    
  •   		boost::python::throw_error_already_set();
    
  •   	}
    
  •   	if (value == nullptr) boost::python::throw_error_already_set();
    
  •   	void* storage = (
    
  •   		(boost::python::converter::rvalue_from_python_storage<std::string>*)
    
  •   		data)->storage.bytes;
    
  •   	new (storage) std::string(value);
    
  •   	data->convertible = storage;		
    
  • 	}
    
  • };

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant