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

'PyError: invalid syntax' error when running python compiled code #447

Open
aresper opened this issue Aug 8, 2023 · 4 comments
Open

'PyError: invalid syntax' error when running python compiled code #447

aresper opened this issue Aug 8, 2023 · 4 comments

Comments

@aresper
Copy link

aresper commented Aug 8, 2023

I can successfully compile my python code, but when I try to run the binary I get the following error:

me@desktop:~/Python-sandbox/23-07-01-codon-typecheck-fix$ ./main_test 0 0
PyError: invalid syntax (graph_0_0_test.py, line 8)

Raised from: pyobj.exc_check:0
/home/alexandre/.codon/lib/codon/stdlib/internal/python.codon:1166:13
Abortado (imagem do núcleo gravada)
alexandre@insys-desktop:~/Python-sandbox/23-07-01-codon-typecheck-fix$ 

Since the code compiles, I was not expecting to get syntax error during execution.

Here are the snippets of the code I am trying to run:

This is the code from file main_0_0_test.py:

#!/usr/bin/env python
#import pprint
#import cProfile
#from heapq import heapify
import python
#from collections import OrderedDict
from python import collections
#from itertools import product
from operator import itemgetter
import copy
import sys
from typing import Dict, List, Union
#from python import sys.argv
#from time import process_time
from python import time
from python import graph_0_0_test.JobGraph
from python import graph_0_0_test.IntervalGraph
from python import aux_0_0.ComputeJobs



# initialize job graph vertices and create list of events
def initializeJobGraph(in_jobs, in_job_graph, in_events_dict):

    for each_job in in_jobs:

        # create vertex
        in_job_graph.addVertex(each_job, in_jobs[each_job]["budget"], in_jobs[each_job]["release"], in_jobs[each_job]["deadline"])

        # get job_id
        job_id = int(in_job_graph.vertList[each_job].getId())

        # get job release and deadline
        release_time = int(in_job_graph.vertList[each_job].getRelease())
        deadline_time = int(in_job_graph.vertList[each_job].getDeadline())


        # check if the release event already exists in the events list and add it
        if release_time in list(in_events_dict.keys()):

            in_events_dict[release_time].append({'event': Union[int, str]("release"), 'job_id': Union[int, str](job_id)})

        else:
            in_events_dict[release_time] = [{'event': Union[int, str]("release"), 'job_id': Union[int, str](job_id)}]

        # check if the deadline event already exists in the events list and add it
        if deadline_time in list(in_events_dict.keys()):

            in_events_dict[deadline_time].append({'event': Union[int, str]("deadline"), 'job_id': Union[int, str](job_id)})

        else:
            in_events_dict[deadline_time] = [{'event': Union[int, str]("deadline"), 'job_id': Union[int, str](job_id)}]

    # build ordered event dict
    ordered_event_dict_tmp = collections.OrderedDict(sorted(in_events_dict.items(), key=lambda t: t[0]))

    return ordered_event_dict_tmp


##############################################################################################
#                                      MAIN FUNCTION                                         #
##############################################################################################
def main(arg1: str, arg2: str) -> None:

    # debug flag
    debug_flag = False

    #profiler_flag = False

    if debug_flag:
        input_arg_1 = str(0)
        input_arg_2 = str(0)
    else:
        input_arg_1 = arg1
        input_arg_2 = arg2

    ####################################################################################################################
    # Prepare Log Files                                                                                                #
    ####################################################################################################################

    # define paths
    input_data_path = "/home/alexandre/git-repositories/input_data/"
    detailed_outputs_path = "/home/alexandre/git-repositories/detailed_outputs/"
    summary_outputs_path = "/home/alexandre/git-repositories/summary_outputs/"

    detailed_outputs = detailed_outputs_path + "detailed_outputs_" + input_arg_1 + "_" + input_arg_2 + ".txt"
    summary_outputs = summary_outputs_path + "summary_outputs_" + input_arg_1 + "_" + input_arg_2 + ".txt"

    ####################################################################################################################
    # Get input parameters                                                                                             #
    ####################################################################################################################

    # get jobs dict
    jobs, total_util, total_real_util, job_counter, num_proc = ComputeJobs(input_arg_1, input_arg_2, input_data_path)

    ####################################################################################################################
    # Initialize data structures                                                                                       #
    ####################################################################################################################

    # create jobs graph
    job_graph = JobGraph()

    # initialize events dict
    #events_dict = Dict[int, List[Dict[str, int]]]()
    events_dict = Dict[int, List[Dict[str, Union[str, int]]]]()



    ####################################################################################################################
    # INFRASTRUCTURE CONSTRUCTION PHASE                                                                                #
    ####################################################################################################################

    # initialize job graph vertices and create list of events
    t_initializeJobGraph_start = time.process_time()
    ordered_event_dict = initializeJobGraph(jobs, job_graph, events_dict)
    t_initializeJobGraph_stop = time.process_time()



####################################################################################################################
# Program entry point                                                                                              #
####################################################################################################################
if __name__ == "__main__":
    #print(py_sys.argv)
    print(sys.argv)

    arg1 = sys.argv[1]
    arg2 = sys.argv[2]
    main(arg1, arg2)

And this is the code from file graph_0_0_test.py, where I am getting the error in line 8 in the import statement:

image

And this is the code for file vertex_0_0_test.py:

image

@elisbyberi
Copy link

@aresper Why are you importing:

from python import graph_0_0_test.JobGraph
from python import graph_0_0_test.IntervalGraph
from python import aux_0_0.ComputeJobs

Did you install graph_0_0_test as a python package using pip?

If it exists in the same directory as main_0_0_test.py, you can import it:

from .graph_0_0_test import JobGraph
from .graph_0_0_test import IntervalGraph
from .aux_0_0 import ComputeJobs

@aresper
Copy link
Author

aresper commented Aug 10, 2023

Hello @elisbyberi,

To import my python modules I used the procedure described here:
https://docs.exaloop.io/codon/interoperability/python
image

If I use the import statement as suggested I get the following compilation error:

me@desktop:~/Python-sandbox/23-07-01-codon-typecheck-fix$ codon build -release -exe main_0_0.codon
python.codon:1373:17-37: error: 'JobVertex' object has no attribute '__from_py__'
├─ main_0_0.codon:3044:17-37: error: during the realization of __from_py__(d: Ptr[byte])
╰─ main_0_0.codon:3097:5-9: error: during the realization of main(arg1: str, arg2: str)

@elisbyberi
Copy link

@aresper Be sure the PYTHONPATH environment variable includes the path of mymodule.py!

image

Use from python import x only if "x" module has been installed using pip or python3 setup.py install. Note that import python has no effect and is not documented.

The error python.codon:1373:17-37: error: 'JobVertex' object has no attribute '__from_py__' indicates that the import is functioning. What isn't working is attempting to import a Python class definition in Codon. You cannot directly import a class definition from Python to Codon. However, you can call a Python function to return an object:

@python
def from_py():
    from dataclasses import dataclass

    @dataclass
    class T:
        a = 1

        def method(self):
            print(self.a + 4)

    return T()


t = from_py()


print(t.a)  # 1
t.method()  # 5

The equivalent in Codon would be:

@tuple
class T:
    a = 1

    def method(self):
        print(self.a + 6)


t = T()

print(t.a)  # 1
t.method()  # 7

@aresper
Copy link
Author

aresper commented Aug 19, 2023

Hello @elisbyberi,
I modified my files as suggested, thx.
I confirm my modules were not installed using pip and PYTHONPATH is pointing to the right directory.
However, I am having issues with the import:

me@desktop:~/Python-sandbox/23-07-01-codon-typecheck-fix$ codon build -release -exe main_0_0.py 
main_0_0.py:14:24-40: error: cannot import name 'JobGraph_from_py' from 'graph_0_0'
main_0_0.py:15:24-45: error: cannot import name 'IntervalGraph_from_py' from 'graph_0_0'

The initial part of my files currently look like this:
main_0_0.py
image

graph_0_0.py
image

vertex_0_0.py
image

Any ideas?

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