Comments (4)
I agree this is an issue, though I'm not yet sure what the right fix is. std::wstring
is defined as std::basic_string<wchar_t>
, with wchar_t
being of non-portable compiler-dependent width. So it isn't even clear what data it can hold, much less what encoding the user choses to store in it.
I think it would be best to not provide a default conversion for it, rather than second-guessing what the user may want / need.
from python.
I think the issue is not with std::wstring, but std::string.
The following workaround works for me:
#if PY_MAJOR_VERSION >= 3
object getString()
{
return object( handle<>( PyBytes_FromString( "string" ) ) );
}
#else /* python 2.x */
std::string getString()
{
return "string";
}
#endif
Then it produces expected results:
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from str_test import getString, getWString
>>> type(getString())
<class 'bytes'>
>>> type(getWString())
<class 'str'>
from python.
OK, fair enough. :-)
Still, I think the handling of getWString()
is wrong, too, i.e. Boost.Python shouldn't provide a default converter for std::wstring
.
from python.
I think the handling is already correct in both cases, because std::string
and std::wstring
are both supposed to handle text, not bytes. std::wstring
is supposed to be text encoded in UTF-16 when sizeof(wchar_t) == 2
(i.e., in Windows) and in UTF-32 when sizeof(wchar_t) == 4
(i.e., in Unix with default compiler flags). By extension, the universally accepted encoding for std::string
would be UTF-8.
In Python 3 philosophy, bytes
is supposed to be just that, an array of bytes, not text. Sometimes this array of bytes may be holding an encoded representation of text, but this is just a special case.
from python.
Related Issues (20)
- warning: missing field 'tp_version_tag' initializer [-Wmissing-field-initializers]
- `no_proxy_helper` should not access `Container::value_type`
- Boost.Python doesn't build under MSVC... HOT 4
- NULL ptr deref in instance_dealloc
- About `No Python class registered for C++ class`,throw errors. HOT 1
- Is it possible to programmatically retrieve C++ class name / Python class name mapping ? HOT 1
- __qualname__ on embedded/inner classes is wrong
- Outdated reference to bjam in examples dir
- Broken URL in build/Jamfile HOT 2
- Qt binding to python - undefined symbol HOT 2
- Compilation failure using numpy 2.0.0b1 HOT 4
- Is Boost.Python is bound to specific python version? HOT 1
- Jupyter Kernel Notebook Dies After Starting Threaded Class HOT 4
- Cannot import Boost.Python from Python 3.11 HOT 4
- Minimal Boost Python example not working HOT 7
- Segfault when importing Python Module HOT 1
- How to Properly Expose Enum Comparison Operators to Python Using Boost.Python? HOT 1
- Expose shared_ptr to const
- missing lib boost_numpy312-vc143-mt-x64-1_85.lib HOT 1
- Boost Python 1.83.0 doesn't support NumPy 2.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python.