moskytw / mosql

Build SQL with native Python data structure smoothly.
http://mosql.mosky.tw
MIT License
140 stars 17 forks source link

Python 3 fix #38

Closed uranusjr closed 9 years ago

uranusjr commented 10 years ago

This makes all internals in util use unicode instead of byte strings, so that everything works on both Python 2 and 3. Since the Statement object encodes strings in format, the outputs stay as byte strings, maintaining backward compatibility.

I would probably recommend using from __future__ import unicode_literals in all modules using literals so that things are less likely to break in the future. It’s not necessary now though.

I also added a feature detection in db so that tests can run on Python 3 (which does not have izip).

uranusjr commented 10 years ago

Oops I missed some cases when param is used. Will fix it and update this later.

uranusjr commented 10 years ago

OK, tests should pass now. My initial intentions of making Query still return a byte string does not work with subqueries, so now MoSQL returns unicode always. Not sure if that’s acceptable though.

Tests totally does not pass on Python 3 mainly because there are tons of print statements in the docs; I’m now using a custom build to test my changes (they pass), but the diff is so extensive it needs another PR. Or you can just use sed yourself to make the changes. There are a couple of cases that require manual editing, but it won’t take more than 5 minutes.

But that still won’t make tests pass. Python 3’s dict ordering is different from Python 2’s, so things like

insert('person', {'person_id': 'mosky', 'name': 'Mosky Liu'})

generates different outputs on Python 2

INSERT INTO "person" ("person_id", "name") VALUES ('mosky', 'Mosky Liu')

and Python 3

INSERT INTO "person" ("name", "person_id") VALUES ('Mosky Liu', 'mosky')

So I guess it’s not feasible to run tests on Python 3. But the code itself now should work on both Python 2 and 3.

uranusjr commented 9 years ago

Closed in favour of #40

moskytw commented 9 years ago

sorry for missing this PR ... XD