[GRASS-dev] question about Python 3 and temporal database

Anna Petrášová kratochanna at gmail.com
Thu Jan 3 14:09:09 PST 2019


Hi,

I got stuck with how we should handle strings in temporal database in
respect to Python 2 and 3. Here is some relevant info from stack overflow
[1]:

"If you insert a Python unicode object (or str object in 3.x), the Python
sqlite3 library will automatically convert it to UTF-8. But if you insert a
str object, it will just assume the string is UTF-8, because Python 2.x
"str" doesn't know its encoding"

Regarding getting text from the database, there is some info here [2]:

"[text_factory] Using this attribute you can control what objects are
returned for the TEXT data type. By default, this attribute is set to
unicode and the sqlite3 module will return Unicode objects for TEXT. If you
want to return bytestrings instead, you can set it to str.
...
You can also set it to any other callable that accepts a single bytestring
parameter and returns the resulting object."

text_factory is currently set to 'str' so with Python 2 we get bytes, with
Python 3 unicode.
I am not sure what do we want:
1. if we use bytes (input and output), do we use utf-8 encoding or locale
specific? That could be problem if you move the database to different
computer with different default encoding.
2. use unicode
3. ?

This is for sqlite, I haven't really considered the other db backend.

Any idea?

Anna


[1]
https://stackoverflow.com/questions/2392732/sqlite-python-unicode-and-non-utf-data
[2]
https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.text_factory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20190103/584cc317/attachment.html>


More information about the grass-dev mailing list