exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
13.95k stars 498 forks source link

using protobuf case #547

Open lilothar opened 4 months ago

lilothar commented 4 months ago

defines test.proto

syntax = "proto3";
package tutorial;

enum PhoneType {
  MOBILE = 0;
  HOME = 1;
  WORK = 2;
}

message Person {
  string name = 1;
  int32 id = 2;
  PhoneNumber phone = 4;
  string email = 3;
}

message PhoneNumber {
  string number = 1;
  PhoneType type = 2;
}

message AddressBook {
  repeated Person people = 1;
}

run generation cmd protoc ./test.proto --python_out=./

write proto.codon

from python import test_pb2

def test_proto():
    person = test_pb2.Person()
    person.id = 123
    person.name = "Jeff"
    person.email = "123"
    print(person)

test_proto()

run codon run proto.codon

proto.codon:5:5-11: error: 'pyobj' object has no attribute 'id'
╰─ proto.codon:10:1-11: error: during the realization of test_proto()
lilothar commented 4 months ago

how to import protobuf generations?

elisbyberi commented 4 months ago

@lilothar Try: import test_pb2

lilothar commented 4 months ago

@elisbyberi I have try that, but raise error too

test_pb2.py:5:4-23: error: cannot import name 'version_info' from 'sys'
test_pb2.py:6:6-30: error: no module named 'google.protobuf.internal'
test_pb2.py:7:6-21: error: no module named 'google.protobuf'
test_pb2.py:8:6-21: error: no module named 'google.protobuf'
test_pb2.py:9:6-21: error: no module named 'google.protobuf'
test_pb2.py:10:6-21: error: no module named 'google.protobuf'
test_pb2.py:13:11-37: error: no module named '_symbol_database'

another example that define a class A,

class A:
    x: int
    def __init__(self, x):
        self.x = x

codon test file: a_test.codon

from python import ftest

print(ftest.A(1).x)

codon run a_test.codon will run ok if define a as

class A:
    def __init__(self, y: int):
        self.x: int = y

this will case the same type error with using protobuf as bellow

error: 'pyobj' object has no attribute 'x'

I guss if I want to import a python class, I should definitely define the data member with type in python, thus to protobuf, I need to write same plugin to regenerate the code as required or same proxy to adapter it?

elisbyberi commented 4 months ago

@lilothar Refer to: https://docs.exaloop.io/codon/interoperability/python for information on how to work with the google.protobuf library. TL;DR: You have to import it from Python using from python import [specific_module]

Note that a 'Python module' is a pip-installed Python module (importable from the Python runtime). If you want to import a Python script (from the same directory), you can simply use import script_file.

lilothar commented 4 months ago

@elisbyberi I have try that method, but no effect and raise


test_pb2.py:5:4-23: error: cannot import name 'version_info' from 'sys'
test_pb2.py:6:6-30: error: no module named 'google.protobuf.internal'
test_pb2.py:7:6-21: error: no module named 'google.protobuf'
test_pb2.py:8:6-21: error: no module named 'google.protobuf'
test_pb2.py:9:6-21: error: no module named 'google.protobuf'
test_pb2.py:10:6-21: error: no module named 'google.protobuf'
test_pb2.py:13:11-37: error: no module named '_symbol_database'
``` error