uweschmitt/autowrap — pull request #5 by hroest (2014-03-20 19:26:40+00:00)
Title: Feature/python3

Support for Python 3 and 2 (shared codebase)

- exchange print functions
- exchange relative imports
- exchange exception handling
- add imports from __future__
- switch to next() function

String handling:
How to deal with Python strings was the most difficult part and there are several options on how to handle them. I chose to accept only bytes() as an input to the C++ code since I did not see any other way how to decide the encoding from within Cython and convert incoming strings to std::string in any sensible way. Since std::string are encoding-unaware containers of 8 bit entities, there is a direct correspondence to the Python bytes structure. This means that it will be up to the user to convert potential str input first to bytes (e.g. encode it with an appropriate encoding). Many function calls will then become
```
# Python 2
python_string = some_cpp_function( input )
# Python 3
python_string = some_cpp_function( input.encode() ).decode()
```

we could choose to perform the encoding and decoding within Cython and thus "hide" the abstraction away. This however would mean that we make an assumption about the encoding of incoming strings which is probably not a good idea.

Another solution would be to allow bytes and string input and silently convert str to bytes using the default encoding. This would require some more work from autowrap but would have the advantage that most code would work exactly the same way in Python 2 and Python 3 - but it would loose the advantage of Python 3 of distinguishing between encoded strings and non encoded.