Skip to content

Commit 4824e9d

Browse files
authored
use cython for python bindings (#7)
1 parent d7d8b9c commit 4824e9d

File tree

9 files changed

+44
-108
lines changed

9 files changed

+44
-108
lines changed

python/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
*.pyc
22
*.so
3+
pylibra.c
4+
build/
35
/*.egg-info
46
/_skbuild
57
/.eggs
68
/dist
7-
/venv
9+
/venv

python/CMakeLists.txt

-26
This file was deleted.

python/pylibra.pyx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# distutils: include_dirs = ../libra-dev/include/
2+
3+
cdef extern from "data.h":
4+
ctypedef struct CDevAccountResource:
5+
long balance
6+
7+
CDevAccountResource account_resource_from_lcs(char* buffer, int length)
8+
9+
10+
cdef class DevAccountResource:
11+
cdef public long balance
12+
13+
def __cinit__(self, lcs_bytes):
14+
self.balance = account_resource_from_lcs(lcs_bytes, len(lcs_bytes)).balance

python/pylibra/__init__.py

-1
This file was deleted.

python/pylibra/_pylibra.c

-61
This file was deleted.

python/pyproject.toml

-2
This file was deleted.

python/setup.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
import sys
22

3-
from skbuild import setup # This line replaces 'from setuptools import setup'
3+
from setuptools import setup, Extension
4+
from Cython.Build import cythonize
45

56
# Require pytest-runner only when running tests
6-
pytest_runner = (['pytest-runner']
7-
if any(arg in sys.argv for arg in ('pytest', 'test'))
8-
else [])
7+
pytest_runner = (
8+
["pytest-runner"] if any(arg in sys.argv for arg in ("pytest", "test")) else []
9+
)
910

1011
setup_requires = pytest_runner
1112

12-
setup(name="pylibra",
13-
version="1.0.0",
14-
description="Python interface for the libra-dev library function",
15-
author="Yucong Sun",
16-
author_email="[email protected]",
17-
packages=['pylibra'],
18-
tests_require=['pytest', 'pytest-runner'],
19-
setup_requires=setup_requires
20-
)
13+
setup(
14+
name="pylibra",
15+
version="1.0.0",
16+
description="Python interface for the libra-dev library function",
17+
author="Yucong Sun",
18+
author_email="[email protected]",
19+
tests_require=["pytest", "pytest-runner"],
20+
ext_modules=cythonize(
21+
[
22+
Extension(
23+
"pylibra",
24+
["pylibra.pyx"],
25+
extra_link_args=["-L../libra-dev/target/debug"],
26+
libraries=["libra_dev"],
27+
)
28+
]
29+
),
30+
setup_requires=setup_requires,
31+
)

python/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44
python3 -m venv ./venv
55

66
./venv/bin/pip3 install --upgrade pip
7-
./venv/bin/pip3 install --upgrade scikit-build pytest
7+
./venv/bin/pip3 install --upgrade pytest cython
88

99
./venv/bin/python3 setup.py develop
1010
./venv/bin/python3 setup.py pytest

python/test_pylibra.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import pylibra
1+
from pylibra import DevAccountResource
22

33
BLOB = bytes.fromhex("020000002100000001674deac5e7fca75f00ca92b1ba3697f5f01ef585011beea7b361150f4504638f0800000002000000000000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e0000002000000036ccb9ba8b4f0cd1f3e2d99338806893dff7478c69acee9b8e1247c053783a4800e876481700000000000200000000000000200000000b14ed4f5af8f8f077c7ec4313c6d395b9a7eb5f41eab9ec15367215ca9e420a01000000000000002000000032f56f77b09773aa64c78ee39943da7ec73f91cd757e325098e11b3edc4eccb10100000000000000")
44

55
def test_account_state_blob():
6-
res = pylibra.account_resource_from_lcs(BLOB)
7-
assert res["balance"] == 100000000000
6+
assert DevAccountResource(BLOB).balance == 100000000000

0 commit comments

Comments
 (0)