flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
256 stars 25 forks source link

Python 3.9 Support #50

Closed nathanielatom closed 3 years ago

nathanielatom commented 3 years ago

In Python 3.9.1

from pscript import py2js

results in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.9/site-packages/pscript/__init__.py", line 272, in <module>
    from .parser0 import Parser0, JSError
  File ".../lib/python3.9/site-packages/pscript/parser0.py", line 19, in <module>
    from . import commonast as ast
  File ".../lib/python3.9/site-packages/pscript/commonast.py", line 15, in <module>
    from base64 import encodestring as encodebytes, decodestring as decodebytes
ImportError: cannot import name 'encodestring' from 'base64' (.../lib/python3.9/base64.py)
ed2050 commented 3 years ago

base64.encodestring has been deprecated since python3.1. It still existed in python 3.8, seems it was removed in 3.9. Damn python devs don't understand that minor version increments don't introduce breaking changes...

The quick and dirty fix is to define it in your code before invoking pscript:

import base64
base64.encodestring = base64.encodebytes
base64.decodestring = base64.decodebytes
from pscript import py2js

That should be it. The docs say encodestring is an alias of encodebytes, and my quick test indicates they return the same type (a bytes object rather than a string). If the return value were different then you might need to transform it between the two, but that doesn't seem to be the case.

Note: I have no affiliation with pscript. Just answering on my python knowledge. If pscript is still being maintained, the devs may want to fix this so the above mitigation is no longer necessary.

Hope this helps.

almarklein commented 3 years ago

python devs don't understand that minor version increments don't introduce breaking changes...

I don't think that Python uses semver versioning. Python does occasionally break backwards compatibility in dot-releases.

the devs may want to fix this so the above mitigation is no longer necessary.

Good point. I'll add a CI build for py39 and make sure it works again. There may be other problems, since the ast spec can also be changed between Python versions.

almarklein commented 3 years ago

done! will release in a minute