Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E.g. while loop not working #7

Open
PallHaraldsson opened this issue Aug 9, 2019 · 9 comments
Open

E.g. while loop not working #7

PallHaraldsson opened this issue Aug 9, 2019 · 9 comments

Comments

@PallHaraldsson
Copy link

PallHaraldsson commented Aug 9, 2019

E.g. what doesn't work (and predictably "unexpected EOF while parsing" with one line shorter, and different error with, from the original, "while True:" ..):

julia> py2jl"""
       def __BOND_repl():
           SENTINEL = 1
           while SENTINEL>1:
             SENTINEL = 2
       """
ERROR: LoadError: "class: Compare, attributes: Symbol[:left, :comparators, :class, :lineno, :colno, :ops]."
Stacktrace:
 [1] (::getfield(Py2Jl.ASTGen, Symbol("##12#122")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}}})(::Dict{Symbol,Any}) at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:392
 [2] ##Py2Jl.ASTGen 90#478 at /home/qwerty/.julia/packages/MLStyle/ksIXg/src/MLStyle.jl:282 [inlined]
 [3] #12 at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:282 [inlined]
 [4] (::getfield(Py2Jl.ASTGen, Symbol("##12#122")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [5] (::getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}})(::Array{Dict{Symbol,Any},1}) at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:187
 [6] macro expansion at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:240 [inlined]
 [7] ##Py2Jl.ASTGen 124#512 at /home/qwerty/.julia/packages/MLStyle/ksIXg/src/MLStyle.jl:229 [inlined]
 [8] (::getfield(Py2Jl.ASTGen, Symbol("###Py2Jl.ASTGen 124#512#152")){String,Dict{Symbol,Any},getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [9] (::getfield(Py2Jl.ASTGen, Symbol("##12#122")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [10] (::getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}})(::Array{Dict{Symbol,Any},1}) at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:187
 [11] ##Py2Jl.ASTGen 156#544 at /home/qwerty/.julia/packages/MLStyle/ksIXg/src/MLStyle.jl:197 [inlined]
 [12] #12 at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:197 [inlined]
 [13] (::getfield(Py2Jl.ASTGen, Symbol("##12#122")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#121")){getfield(Py2Jl.ASTGen, Symbol("##7#114")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [14] to_ast(::Nothing, ::Dict{Symbol,Any}) at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/ASTGen.jl:398
 [15] py2jl(::Nothing, ::String) at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/Py2Jl.jl:43
 [16] @py2jl_str(::LineNumberNode, ::Module, ::Any) at /home/qwerty/.julia/packages/Py2Jl/G6hOG/src/Py2Jl.jl:51
in expression starting at REPL[5]:1
@thautwarm
Copy link
Member

Wait for several minutes. I'm going out for some food.

@thautwarm
Copy link
Member

That's not caused by while, instead of compare...

thautwarm added a commit that referenced this issue Aug 9, 2019
@thautwarm
Copy link
Member

Updated , check if resolved?

@PallHaraldsson
Copy link
Author

Yes, forks (checked in 1.1.0), while not for "True" (not too important for me I think, I can work around):

julia> py2jl"""
              def __BOND_repl():
                  SENTINEL = 1
                  while True:
                    SENTINEL = 2
              """
Dict{Symbol,Any}
ERROR: LoadError: "class: NameConstant, attributes: Symbol[:value, :class, :lineno, :colno]."
Stacktrace:
 [1] (::getfield(Py2Jl.ASTGen, Symbol("##12#131")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}}})(::Dict{Symbol,Any}) at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:440
 [2] ##Py2Jl.ASTGen 108#473 at /home/qwerty/.julia/packages/MLStyle/ksIXg/src/MLStyle.jl:312 [inlined]
 [3] #12 at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:312 [inlined]
 [4] (::getfield(Py2Jl.ASTGen, Symbol("##12#131")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [5] (::getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}})(::Array{Dict{Symbol,Any},1}) at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:217
 [6] macro expansion at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:270 [inlined]
 [7] ##Py2Jl.ASTGen 142#507 at /home/qwerty/.julia/packages/MLStyle/ksIXg/src/MLStyle.jl:259 [inlined]
 [8] (::getfield(Py2Jl.ASTGen, Symbol("###Py2Jl.ASTGen 142#507#161")){String,Dict{Symbol,Any},getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [9] (::getfield(Py2Jl.ASTGen, Symbol("##12#131")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [10] (::getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}})(::Array{Dict{Symbol,Any},1}) at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:217
 [11] ##Py2Jl.ASTGen 174#539 at /home/qwerty/.julia/packages/MLStyle/ksIXg/src/MLStyle.jl:227 [inlined]
 [12] #12 at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:227 [inlined]
 [13] (::getfield(Py2Jl.ASTGen, Symbol("##12#131")){Nothing,getfield(Py2Jl.ASTGen, Symbol("#trans_block#130")){getfield(Py2Jl.ASTGen, Symbol("##7#123")){Nothing}}})(::Dict{Symbol,Any}) at ./none:0
 [14] to_ast(::Nothing, ::Dict{Symbol,Any}) at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/ASTGen.jl:450
 [15] py2jl(::Nothing, ::String) at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/Py2Jl.jl:43
 [16] @py2jl_str(::LineNumberNode, ::Module, ::Any) at /home/qwerty/.julia/packages/Py2Jl/LUvcL/src/Py2Jl.jl:51
in expression starting at REPL[4]:1

FYI. I'll look more at your package, but even though abandoned, I will try out too https://github.com/TotalVerb/PythonSyntax.jl

It's not a substitute, while in a way if it supports more syntax, but I'll look at it at least for fun, and maybe there's something of value to know there?

@thautwarm
Copy link
Member

thautwarm commented Aug 9, 2019

Okay, in fact you can check https://github.com/python/cpython/blob/master/Parser/Python.asdl to see what I haven't implemented yet.

@thautwarm
Copy link
Member

To support True, firstly we search which role the True performs in Python ASDL.

In Python 3.8+, True is stored in Constant,
https://github.com/python/cpython/blob/master/Parser/Python.asdl#L81

| Constant(constant value, string? kind)

in other Python 3.x versions, it's a NameConstant:
https://github.com/python/cpython/blob/afe3a4975cf93c97e5d6eb8800e48f368011d37a/Parser/Python.asdl#L75

| NameConstant(singleton value)

And you can install astpretty in PyPI to check what is the AST for specific codes:

Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.

> from ast import parse as p
> from astpretty import pprint as pp
> def ppp(x): pp(p(x))
> ppp("True")
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            value=NameConstant(lineno=1, col_offset=0, value=True),
        ),
    ],
)

Then it'll be translated into such a json in Py2Jl.jl:

Dict(
   :class => "Module", 
   :body =>  [
            Dict(:value =>
                   Dict(:value => true,
                          :class => "NameConstant",
                          :lineno => 1,
                          :colno => 0
                   ),
                :class => "Expr",
                :lineno => 1,
                :colno => 0)
])

I then make a translation for such a Python 3.7 AST of True by adding such a block at
https://github.com/JuliaCN/Py2Jl.jl/blob/master/src/ASTGen.jl#L420

Record(:class => "NameConstant", value) -> value

In fact for Python 3.8+ , the way to support True is almost the same:

Record(:class => "Constant", value) -> value

Then I add a corresponding test:

py2jl"""
def __BOND_repl():
        SENTINEL = 10
        while True:
            break
        return SENTINEL
    """
    @testset "True" begin
            @test __BOND_repl() == 10
    end
end

@thautwarm
Copy link
Member

That's quite simple, isn't it?
Gonna sleep, wouldn't reply in 12 hrs.

thautwarm added a commit that referenced this issue Aug 9, 2019
@PallHaraldsson
Copy link
Author

PallHaraldsson commented Aug 13, 2019

I thought I should let you know (and also that 1.2.0-rc3 is out):

I saw in PR 31882 at Julia:
"LoadError is deprecated because it is no longer emitted by the runtime as of Julia 1.3."

I never really found out why "LoadError" was happening. or was sure what it means in general. I may look more into there there (or here). [I didn't want to put in a link here, only the number, in case it makes a link and distracts discussion there. So you have to copy paste to see the PR.]

@thautwarm
Copy link
Member

thautwarm commented Aug 13, 2019

I just throw the message out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants