Make string conversion stricter (#695)

* Make string conversion stricter

The string conversion logic added in PR #624 for all std::basic_strings
was derived from the old std::wstring logic, but that was underused and
turns out to have had a bug in accepting almost anything convertible to
unicode, while the previous std::string logic was much stricter.  This
restores the previous std::string logic by only allowing actual unicode
or string types.

Fixes #685.

* Added missing 'requires numpy' decorator

(I forgot that the change to a global decorator here is in the
not-yet-merged Eigen PR)
This commit is contained in:
Jason Rhinelander
2017-02-24 05:33:31 -05:00
committed by Wenzel Jakob
parent dd01665e5a
commit ee2e5a5086
3 changed files with 16 additions and 0 deletions

View File

@@ -657,6 +657,8 @@ struct type_caster<std::basic_string<CharT, Traits, Allocator>, enable_if_t<is_s
return false;
// The below is a guaranteed failure in Python 3 when PyUnicode_Check returns false
#else
if (!PYBIND11_BYTES_CHECK(load_src.ptr()))
return false;
temp = reinterpret_steal<object>(PyUnicode_FromObject(load_src.ptr()));
if (!temp) { PyErr_Clear(); return false; }
load_src = temp;