13 | | == Current state of porting |
| 13 | == Python Scripting Library == |
| 14 | Possible approach: |
| 15 | * functions need to accept unicode and return unicode |
| 16 | * functions wrapping Python Popen class (read_command, run_command, ...) will have parameter encoding |
| 17 | * encoding=None means expects and returns bytes (the current state) |
| 18 | * encoding='default' means it takes current encoding using utils._get_encoding() |
| 19 | * encoding='utf-8' takes whatever encoding user specifies, e.g., utf-8 in this case |
| 20 | * this is similar to [https://docs.python.org/3.6/library/subprocess.html#subprocess.Popen Popen class in Python3.6] |
| 21 | * by default encoding='default' to enable expected behavior by users, the following example shows Python3 behavior if we keep using bytes instead of unicode: |
| 22 | |
| 23 | {{{ |
| 24 | # return bytes |
| 25 | ret = read_command('r.what', encoding=None, ... |
| 26 | |
| 27 | for item in ret.splitlines(): |
| 28 | line = item.split('|')[3:] |
| 29 | |
| 30 | Traceback (most recent call last): |
| 31 | File "<stdin>", line 1, in <module> |
| 32 | TypeError: a bytes-like object is required, not 'str' |
| 33 | |
| 34 | # we would have to use: |
| 35 | for item in ret.splitlines(): |
| 36 | line = item.split(b'|')[3:] |
| 37 | }}} |