TranscryptOrg/Transcrypt

Python file ran through transcrypt fails to work in GraalJS

Yu-Vitaqua-fer-Chronos opened this issue · 18 comments

Heya, I'm trying to run a generated python file in a GraalJS environment but get this error:

Exception in thread "main" SyntaxError: Unnamed:1:13 Expected an operand but found import
'use strict';import{AssertionError,AttributeError,BaseException,DeprecationWarning,Exception,IndexError,IterableError,KeyError,NotImplementedError,RuntimeWarning,StopIteration,UserWarning,ValueError,Warning,__JsIterator__,__PyIterator__,__Terminal__,__add__,__and__,__call__,__class__,__envir__,__eq__,__floordiv__,__ge__,__get__,__getcm__,__getitem__,__getslice__,__getsm__,__gt__,__i__,__iadd__,__iand__,__idiv__,__ijsmod__,__ilshift__,__imatmul__,__imod__,__imul__,__in__,__init__,__ior__,__ipow__,
             ^
Unnamed:3:207 Expected eof but found }
hasattr,input,int,isinstance,issubclass,len,list,map,max,min,object,ord,pow,print,property,py_TypeError,py_iter,py_metatype,py_next,py_reversed,py_typeof,range,repr,round,set,setattr,sorted,str,sum,tuple,zip}from"./org.transcrypt.__runtime__.js";var __name__="__main__";print("Ara ara~");

The code of the python file is simply print("Ara ara~") (don't ask why xD) but it for some reason doesn't work

On a side note, is it possible to make Transcrypt use require instead of import?

What version of Python and Transcrypt are you using?

I'm using python 3.10 and the latest version of Transcrypt available from PyPi

The code works in NodeJS so i have no idea what the issue could be

Because Transcrypt uses the AST module, it is important to make sure that the version of Python being used matches the version of Transcrypt. Even though it doesn't make any difference with you're Python source code, the implementation details of the Python language itself affect the structure of the AST. As such, it may need to be parsed in a different way between versions.

The latest version of Transcrypt (3.9.0) expects a Python version 3.9 AST. Can you try running Transcrypt using Python 3.9 and see if that makes any difference?

But the code runs on NodeJS perfectly fine? But yeah I'll try that

Ah, I'm unable to install a Python 3.9 distribution currently, sorry

Screenshot_2022-02-25-20-50-31-958_com termux
I've just attached a screenshot of the code being ran in Python and NodeJS successfully, but failing in GraalJS (I've made a program to run JS code from GraalJS in the stock JVM)

There is a chance it may not make a difference, but it's the first thing to try. The mismatch of Python/Transcrypt versions can cause unpredictable transpilation errors to creep in. Node may be more forgiving with generated JavaScript that is malformed.

If you happen to have Python 3.7 available, you can try that as well. You would just need to use Transcrypt==3.7.16 in that case. You only need the right Python version for transpiling to JS, so even a cloud instance or docker container running the matching Python version would work.

Ah okay then, I'll try using GitHub actions then on a test repo

I've done it with Replit before, but that platform currently has Python 3.8 as a default so there a few extra steps to get 3.9 installed there that you may not want to hassle with. I have a demo repl available with the required setup if you want to play around with it though.

Ah that'll be useful, thanks!

Now that I look at the error in more detail, while the Python version is still an issue I'd address, you may be right in that it has more to do with graaljs and the handling of the transcrypt runtime import.

I've also now ran it in a GitHub action (can be found here: https://github.com/Mythical-Forest-Collective/test-repo/runs/5339470717)

Also yeah, i have no idea why it's an issue with graal... Is there a way to make Transcrypt use require instead of import?

I use require in my own Python code to import JS libraries, but in this case I don't think so. It looks like it's the actual import for the transpiled Transcrypt JS runtime and that is likely baked in to Transcrypt.

I'm not a JS expert (which is why I like Transcrypt so much!), but is there anything in graaljs related to ES6 support that might need to be turned on?

Ah okay, and no I don't think so, GraalJS should support ES6 already?

It does support ES6 for sure, i double checked that, so i have no idea what the issue could possibly be now, but no matter what I'm getting a syntax error despite me trying other options

I've managed to figure it out now!

What did the issue end up being? How did you solve it?