# como_pybind **Repository Path**: tjopenlab/como_pybind ## Basic Information - **Project Name**: como_pybind - **Description**: COMO python binding library - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2021-03-24 - **Last Updated**: 2026-10-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # como_pybind #### 介绍 como_pybind是COMO python 绑定库。 python通过反射机制调用COMO构件,不需要COMO做任何改变,写出的构件就可以在python中使用。只要是用COMO规范开发的C++程序,就可以象python自己写的程序模块module一样,但效率高得多。 #### 安装教程 首先进 COMO 工作环境: ```shell - como_linux_x64: Switch to build como for linux x64. - como_linux_aarch64: Switch to build como for linux aarch64. - como_linux_riscv64: Switch to build como for linux riscv64. - como_android_aarch64: Switch to build como for android aarch64. - como_openEuler_riscv: Switch to build como for openEuler RISC-V. ``` 然后,进Anaconda工作环境: ```shell ~/home/como/out/target/como.linux.x64.rls$ source ~/anaconda3/bin/activate py36 ``` ```shell pip install como_pybind ``` or ```shell python setup.py install ``` #### 使用说明 ```python import como_pybind # 通过下面这条语句,ReflectionTestUnit.so这个COMO构件中的所有构件类(Coclass)就都加载到python环境中了 mc = como_pybind.como('ReflectionTestUnit.so') name = mc.getName(); print ('name is: ' + name) print ('constants: ', mc.getAllConstants()); # CMethodTester是ReflectionTestUnit.so这个COMO构件中的一个构件类(Coclass) cmt = como_pybind.CMethodTester() print ('load COMO class CMethodTester') print (cmt.TestMethod1(12392021)) print (cmt.getAllConstants()) ``` #### 返回值 COMO 方法通过出参(OUT / IN_OUT)产出结果,绑定层按下面的方式呈现: | 出参个数 | Python 侧得到 | |:--------:|:--------------| | 0 | `None` | | 1 | 该值本身(不必再解包元组) | | 多个 | `dict`,键为 COMO 参数名 | ```python value = cmt.TestMethod1(12392021) # 单个出参,直接拿到值 result = someObj.SomeMethod(1, 2) # 多个出参,result['arg2'] 这样取 ``` 参数既可以按位置传,也可以按 COMO 参数名用关键字传: ```python cmt.TestMethod1(arg=12392021) ``` #### 重载方法 重载方法以其重整后的名字暴露,形如 `名字__<重整签名>`,因此每个重载都仍然 可以从 Python 调到: ```python idt = como_pybind.CMethodIdTester() idt.Add__4III25E(1, 2, 3) # Add(Integer, Integer, Integer) idt.Add__4DDD25E(1.5, 2.5, 3.5) # Add(Double, Double, Double) ``` #### 异常 COMO 的 ECode 会映射成对应的 Python 异常,调用方不需要检查返回的状态码: | ECode | Python 异常 | |:------|:------------| | `E_ILLEGAL_ARGUMENT_EXCEPTION` 等 | `ValueError` | | `E_NOT_FOUND_EXCEPTION` 等 | `KeyError` | | `E_COMPONENT_NOT_FOUND_EXCEPTION` | `ImportError` | | `E_UNSUPPORTED_OPERATION_EXCEPTION` | `TypeError` | | `E_OUT_OF_MEMORY_ERROR` | `MemoryError` | | 其他 | `RuntimeError` | ```python try: cmt.TestMethod1() except ValueError as e: print(e) # missing COMO argument: arg ``` 所有 COMO 对象都是 `como_pybind.ComoObject` 的实例。 #### 测试 `tests/run_tests.sh` 负责全部准备工作:构建扩展、准备 COMO 构件、设置运行环境, 然后执行 `tests/test_*.py`。不需要事先手工构建或安装。 ```bash # 构建并运行全部测试 tests/run_tests.sh # 只跑名字里带 array 的用例 tests/run_tests.sh --no-build -k array # 把扩展安装到用户 site-packages tests/run_tests.sh --install # 把构件复制到 build/components,并从那里加载 tests/run_tests.sh --stage-components # 清理构建目录 tests/run_tests.sh --clean ``` 默认值可以用环境变量覆盖: | 变量 | 默认值 | 作用 | |:-----|:-------|:-----| | `BIN_PATH` | `/home/xilong/como/como/bin/target/como.linux.x64.rls` | `comort.so` 与测试构件所在目录 | | `PYTHON` | `python3` | 构建所用解释器 | | `BUILD_DIR` | `/build` | 构建目录 | | `COMPONENTS` | `ReflectionTestUnit.so CompilerExtraUnit.so` | 测试所需构件 | 构件缺失、`comort.so` 缺失都会在脚本开头直接报错退出,不会跑到一半才失败。 测试用 `unittest` 组织,覆盖:组件加载与缓存、基本类型与关键字传参、重载方法、 ECode 到异常的映射、0 个 / 1 个 / 多个出参的返回形态、以及 `Array` 的入参与返回。 #### "CentOS on AARCH64" 上应用实例 - 安装ninja https://support.huaweicloud.com/prtg-kunpengdbs/kunpenggreenplum_02_0011.html - CentOS7-arm64安装Archiconda3 https://blog.csdn.net/frdevolcqzyxynjds/article/details/105939421 ```shell 进入COMO开发环境 $ source build/envsetup.sh $ como_linux_aarch64 如果Anaconda有虚拟环境,激活相应的环境 $ source ~/anaconda3/bin/activate py36 $ export PYTHONPATH=$PYTHONPATH:${HOME}/como_pybind/pybind11/pybind11 设置COMO构件寻找路径 $ export LIB_PATH=$LIB_PATH:$BIN_PATH 安装como_pybind $ cd ${HOME}/como_pybind $ python setup.py install 运行como_pybind实验程序 $ python tests/como_pybind_basictest.py ``` #### "openEuler on RISC-V" 上应用实例 - 安装python开发包: ```shell $ yum install python3-devel ``` 其它与"CentOS on AARCH64" 相同。 ##### 常用Linux命令行剪贴板 ```shell $ wget https://repo.openeuler.org/openEuler-preview/RISC-V/Image/fw_payload_oe.elf $ wget https://repo.openeuler.org/openEuler-preview/RISC-V/Image/openEuler-preview.riscv64.qcow2 $ wget https://isrc.iscas.ac.cn/mirror/openeuler-sig-riscv/images/fw_payload_oe.elf $ wget https://isrc.iscas.ac.cn/mirror/openeuler-sig-riscv/images/oe-rv-rv64g-30G.qcow2 $ qemu-system-riscv64 -nographic -machine virt -smp 8 -m 2G -kernel fw_payload_oe.elf -drive file=openEuler-preview.riscv64.qcow2,format=qcow2,id=hd0 -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-device,rng=rng0 -device virtio-blk-device,drive=hd0 -device virtio-net-device,netdev=usernet -netdev user,id=usernet,hostfwd=tcp::12055-:22 -append 'root=/dev/vda1 rw console=ttyS0 systemd.default_timeout_start_sec=600 selinux=0 highres=off mem=4096M earlycon' $ ssh -p 12055 root@localhost 默认的登陆用户名/密码是:root/openEuler12#$ ```