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

refactor: Use apigen to generate cython. #88

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ bazel-opt_task:
- /src/workspace/tools/inject-repo py_toxcore_c
test_all_script:
- cd /src/workspace && bazel test -k
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST
//py_toxcore_c/...
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
Loading