Skip to content

Commit

Permalink
refactor: Use apigen to generate cython.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 7, 2024
1 parent 3a9e66b commit f83f0b6
Show file tree
Hide file tree
Showing 23 changed files with 1,709 additions and 619 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
*.o
*.tox
.mypy_cache
# TODO(iphydf): Remove once .gen is removed from the name.
*.gen.pyx
47 changes: 32 additions & 15 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@ load("//tools/project:build_defs.bzl", "project")

project(license = "gpl3-https")

genrule(
name = "pytox/core",
srcs = [
"pytox/src/core.pyx",
"//c-toxcore:tox/tox.h",
],
outs = ["pytox/core.pyx"],
cmd = "$(location //py_toxcore_c/tools:gen_api) $(location pytox/src/core.pyx) $(location //c-toxcore:tox/tox.h) > $@",
tools = ["//py_toxcore_c/tools:gen_api"],
)
#genrule(
# name = "pytox/core",
# srcs = [
# "pytox/src/core.pyx",
# "//c-toxcore:tox/tox.h",
# ],
# outs = ["pytox/core.pyx"],
# cmd = "$(location //py_toxcore_c/tools:gen_api) $(location pytox/src/core.pyx) $(location //c-toxcore:tox/tox.h) > $@",
# tools = ["//py_toxcore_c/tools:gen_api"],
#)

pyx_library(
name = "pytox",
srcs = [
"pytox/av.pyx",
"pytox/core.pyx",
],
srcs = glob(
[
"pytox/**/*.pxd",
"pytox/**/*.pyx",
"pytox/**/*.py",
],
# TODO(iphydf): Remove.
exclude = ["**/*.gen.pyx"],
),
cdeps = ["//c-toxcore"],
cython_directives = {"language_level": "3"},
cython_directives = {
"embedsignature": "True",
"embedsignature.format": "python",
"language_level": "3",
},
tags = ["no-cross"],
visibility = ["//visibility:public"],
)

genrule(
name = "pytox_stubs",
outs = [mod[:-4] + ".pyi" for mod in glob(["pytox/**/*.pyx"])],
cmd = "$(location //py_toxcore_c/tools:stubgen) -o $(GENDIR)/py_toxcore_c" + " ".join([" -m %s" % mod[:-4].replace("/", ".") for mod in glob(["pytox/**/*.pyx"])]),
tags = ["manual"],
tools = ["//py_toxcore_c/tools:stubgen"],
)
9 changes: 3 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ RUN git clone --depth=1 --recursive https://github.com/TokTok/c-toxcore /build/c
&& ldconfig

COPY pytox /build/pytox
COPY tools /build/tools

RUN mypy --strict tools/gen_api.py \
&& tools/gen_api.py pytox/src/core.pyx /usr/local/include/tox/tox.h > pytox/core.pyx \
&& cython pytox/av.pyx pytox/core.pyx
RUN cython -I. $(find pytox -name "*.pyx")

COPY setup.py /build/
RUN python3 setup.py install \
&& python3 -c 'from pytox import core; print(core.__doc__)'
&& python3 -c 'import pytox.toxcore.tox as core; print(core.__doc__)'

COPY test /build/test
RUN python3 test/core_test.py
RUN python3 test/tox_test.py
6 changes: 0 additions & 6 deletions pytox/av.pyx

This file was deleted.

34 changes: 34 additions & 0 deletions pytox/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from enum import Enum
from typing import Sized
from typing import TypeVar

T = TypeVar("T", bytes, str, Sized)


class PytoxException(Exception):
pass


class ApiException(PytoxException):
def __init__(self, err: Enum):
super().__init__(err.name)
self.error = err


class LengthException(PytoxException):
pass


class UseAfterFreeException(Exception):
def __init__(self):
super().__init__(
"object used after it was killed/freed (or it was never initialised)"
)


def _check_len(name: str, data: T, expected_length: int) -> T:
if len(data) < expected_length:
raise LengthException(
f"parameter '{name}' received bytes of invalid"
f"length {len(data)}, expected at least {expected_length}")
return data
233 changes: 0 additions & 233 deletions pytox/src/core.pyx

This file was deleted.

Loading

0 comments on commit f83f0b6

Please sign in to comment.