Skip to content

Commit 5f7739c

Browse files
committed
refactor: Adapt pytox to the new tox_system stuff.
1 parent 1e75057 commit 5f7739c

26 files changed

+544
-122
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ jobs:
4444
-DTRACE=ON
4545
-DMUST_BUILD_TOXAV=ON
4646
- run: cd c-toxcore/_build && ninja install -j$(nproc)
47-
# - run:
48-
# export CFLAGS="-I$PWD/c-toxcore/_install/include -fsanitize=address";
49-
# export LDFLAGS="-L$PWD/c-toxcore/_install/lib -Wl,-rpath,$PWD/c-toxcore/_install/lib";
50-
# python3 setup.py build_ext --inplace
47+
- run:
48+
export CFLAGS="-I$PWD/c-toxcore/_install/include -fsanitize=address";
49+
export LDFLAGS="-L$PWD/c-toxcore/_install/lib -Wl,-rpath,$PWD/c-toxcore/_install/lib";
50+
python3 setup.py build_ext --inplace
5151
# - run: ASAN_OPTIONS=detect_leaks=0
5252
# LD_PRELOAD=libasan.so.5
5353
# PYTHONPATH=.

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.mypy_cache

BUILD.bazel

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,59 @@ load("//tools/project:build_defs.bzl", "project")
33

44
project()
55

6+
SUBSYSTEMS = [
7+
"log",
8+
"memory",
9+
"network",
10+
"options",
11+
"random",
12+
"system",
13+
"time",
14+
]
15+
16+
[genrule(
17+
name = "pytox/" + sys,
18+
srcs = [
19+
"pytox/src/%s.pxd" % sys,
20+
"pytox/src/%s.pyx" % sys,
21+
"//c-toxcore:public",
22+
],
23+
outs = [
24+
"pytox/%s.pxd" % sys,
25+
"pytox/%s.pyx" % sys,
26+
],
27+
cmd = " ".join([
28+
"$(location //py_toxcore_c/tools:gen_api)",
29+
"$(location pytox/src/%s.pyx)" % sys,
30+
"$(GENDIR)/c-toxcore",
31+
"> $(location pytox/%s.pyx);" % sys,
32+
"$(location //py_toxcore_c/tools:gen_api)",
33+
"$(location pytox/src/%s.pxd)" % sys,
34+
"$(GENDIR)/c-toxcore",
35+
"> $(location pytox/%s.pxd)" % sys,
36+
]),
37+
tools = ["//py_toxcore_c/tools:gen_api"],
38+
) for sys in SUBSYSTEMS]
39+
640
genrule(
741
name = "pytox/core",
842
srcs = [
943
"pytox/src/core.pyx",
10-
"//c-toxcore:tox/tox.h",
44+
"//c-toxcore:tox/toxcore/tox.h",
1145
],
1246
outs = ["pytox/core.pyx"],
13-
cmd = "$(location //py_toxcore_c/tools:gen_api) $(location pytox/src/core.pyx) $(location //c-toxcore:tox/tox.h) > $@",
47+
cmd = "$(location //py_toxcore_c/tools:gen_api) $(location pytox/src/core.pyx) $(GENDIR)/c-toxcore > $@",
1448
tools = ["//py_toxcore_c/tools:gen_api"],
1549
)
1650

1751
pyx_library(
1852
name = "pytox",
1953
srcs = [
54+
"pytox.pxd",
2055
"pytox/av.pyx",
2156
"pytox/core.pyx",
22-
],
57+
"pytox/error.pyx",
58+
] + ["pytox/%s.pxd" % sys for sys in SUBSYSTEMS] + ["pytox/%s.pyx" % sys for sys in SUBSYSTEMS],
2359
cdeps = ["//c-toxcore"],
2460
cython_directives = {"language_level": "3"},
2561
tags = ["no-cross"],

Dockerfile

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,44 @@ RUN apt-get update \
2121
&& pip3 install cython mypy
2222

2323
WORKDIR /build
24-
RUN git clone --depth=1 --recursive https://github.com/TokTok/c-toxcore /build/c-toxcore \
24+
RUN git clone --recursive --depth=1 --branch=system https://github.com/iphydf/c-toxcore /build/c-toxcore \
2525
&& cmake -GNinja -B/build/c-toxcore/_build -H/build/c-toxcore \
2626
-DBOOTSTRAP_DAEMON=OFF \
2727
-DENABLE_STATIC=OFF \
2828
-DMUST_BUILD_TOXAV=ON \
2929
&& cmake --build /build/c-toxcore/_build --target install --parallel "$(nproc)" \
30-
&& ldconfig
30+
&& ldconfig && echo 2
3131

32-
COPY pytox /build/pytox
32+
# Tools first, they change less.
3333
COPY tools /build/tools
34+
COPY pytox.pxd /build/
35+
COPY pytox /build/pytox
3436

3537
RUN mypy --strict tools/gen_api.py \
36-
&& tools/gen_api.py pytox/src/core.pyx /usr/local/include/tox/tox.h > pytox/core.pyx \
37-
&& cython pytox/av.pyx pytox/core.pyx
38+
&& tools/gen_api.py pytox/src/core.pyx /usr/local/include > pytox/core.pyx \
39+
&& tools/gen_api.py pytox/src/log.pxd /usr/local/include > pytox/log.pxd \
40+
&& tools/gen_api.py pytox/src/log.pyx /usr/local/include > pytox/log.pyx \
41+
&& tools/gen_api.py pytox/src/memory.pxd /usr/local/include > pytox/memory.pxd \
42+
&& tools/gen_api.py pytox/src/memory.pyx /usr/local/include > pytox/memory.pyx \
43+
&& tools/gen_api.py pytox/src/network.pxd /usr/local/include > pytox/network.pxd \
44+
&& tools/gen_api.py pytox/src/network.pyx /usr/local/include > pytox/network.pyx \
45+
&& tools/gen_api.py pytox/src/options.pxd /usr/local/include > pytox/options.pxd \
46+
&& tools/gen_api.py pytox/src/options.pyx /usr/local/include > pytox/options.pyx \
47+
&& tools/gen_api.py pytox/src/random.pxd /usr/local/include > pytox/random.pxd \
48+
&& tools/gen_api.py pytox/src/random.pyx /usr/local/include > pytox/random.pyx \
49+
&& tools/gen_api.py pytox/src/system.pxd /usr/local/include > pytox/system.pxd \
50+
&& tools/gen_api.py pytox/src/system.pyx /usr/local/include > pytox/system.pyx \
51+
&& tools/gen_api.py pytox/src/time.pxd /usr/local/include > pytox/time.pxd \
52+
&& tools/gen_api.py pytox/src/time.pyx /usr/local/include > pytox/time.pyx \
53+
&& cython -I $PWD -X "language_level=3" --line-directives pytox/av.pyx pytox/core.pyx \
54+
pytox/error.pyx \
55+
pytox/log.pyx \
56+
pytox/memory.pyx \
57+
pytox/network.pyx \
58+
pytox/options.pyx \
59+
pytox/random.pyx \
60+
pytox/system.pyx \
61+
pytox/time.pyx
3862

3963
COPY setup.py /build/
4064
RUN python3 setup.py install \

pytox.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

pytox/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/core.pxd
2+
/core.pyx
3+
/log.pxd
4+
/log.pyx
5+
/memory.pxd
6+
/memory.pyx
7+
/network.pxd
8+
/network.pyx
9+
/random.pxd
10+
/random.pyx
11+
/system.pxd
12+
/system.pyx
13+
/time.pxd
14+
/time.pyx

pytox/error.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class UseAfterFreeException(Exception):
2+
def __init__(self):
3+
super().__init__(
4+
"object used after it was killed/freed (or it was never initialised)")
5+
6+
class ToxException(Exception):
7+
pass
8+
9+
class ApiException(ToxException):
10+
def __init__(self, err):
11+
super().__init__(err)
12+
self.error = err
13+
14+
class LengthException(ToxException):
15+
pass

pytox/src/core.pyx

Lines changed: 33 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ from libcpp cimport bool
66
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
77
from libc.stdlib cimport malloc, free
88

9-
cdef extern from "tox/tox.h": pass
9+
from pytox import error
10+
from pytox.options cimport Tox_Options, ToxOptions
11+
from pytox.system cimport Tox_System
12+
13+
cdef extern from "tox/toxcore/tox.h": pass
1014

1115

1216
VERSION: str = "%d.%d.%d" % (tox_version_major(), tox_version_minor(), tox_version_patch())
@@ -28,72 +32,22 @@ MAX_FILENAME_LENGTH: int = tox_max_filename_length()
2832
MAX_HOSTNAME_LENGTH: int = tox_max_hostname_length()
2933

3034

31-
class UseAfterFreeException(Exception):
32-
def __init__(self):
33-
super().__init__(
34-
"object used after it was killed/freed (or it was never initialised)")
35-
36-
class ToxException(Exception):
37-
pass
38-
39-
class ApiException(ToxException):
40-
def __init__(self, err):
41-
super().__init__(err)
42-
self.error = err
43-
44-
class LengthException(ToxException):
45-
pass
46-
4735
cdef void _check_len(str name, bytes data, int expected_length) except *:
4836
if len(data) != expected_length:
49-
raise LengthException(
37+
raise error.LengthException(
5038
f"parameter '{name}' received bytes of invalid"
5139
f"length {len(data)}, expected {expected_length}")
5240

53-
54-
cdef class ToxOptions:
55-
56-
cdef Tox_Options *_ptr
57-
58-
def __init__(self):
59-
cdef Tox_Err_Options_New err = TOX_ERR_OPTIONS_NEW_OK
60-
self._ptr = tox_options_new(&err)
61-
if err: raise ApiException(Tox_Err_Options_New(err))
62-
63-
def __dealloc__(self):
64-
self.__exit__(None, None, None)
65-
66-
def __enter__(self):
67-
return self
68-
69-
def __exit__(self, exc_type, exc_value, exc_traceback):
70-
tox_options_free(self._ptr)
71-
self._ptr = NULL
72-
73-
cdef Tox_Options *_get(self) except *:
74-
if self._ptr is NULL:
75-
raise UseAfterFreeException()
76-
return self._ptr
77-
78-
@property
79-
def ipv6_enabled(self) -> bool:
80-
return tox_options_get_ipv6_enabled(self._get())
81-
82-
@ipv6_enabled.setter
83-
def ipv6_enabled(self, value: bool):
84-
tox_options_set_ipv6_enabled(self._get(), value)
85-
86-
8741
cdef class Core:
8842

8943
cdef Tox *_ptr
9044

9145
def __init__(self, options: ToxOptions = None):
92-
cdef Tox_Err_New err = TOX_ERR_NEW_OK
46+
err = TOX_ERR_NEW_OK
9347
if options is None:
9448
options = ToxOptions()
95-
self._ptr = tox_new(options._ptr, &err)
96-
if err: raise ApiException(Tox_Err_New(err))
49+
self._ptr = tox_new(options._get(), &err)
50+
if err: raise error.ApiException(Tox_Err_New(err))
9751

9852
install_handlers(self._get())
9953

@@ -109,13 +63,13 @@ cdef class Core:
10963

11064
cdef Tox *_get(self) except *:
11165
if self._ptr is NULL:
112-
raise UseAfterFreeException()
66+
raise error.UseAfterFreeException()
11367
return self._ptr
11468

11569
@property
11670
def savedata(self) -> bytes:
117-
cdef size_t size = tox_get_savedata_size(self._get())
118-
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
71+
size = tox_get_savedata_size(self._get())
72+
data = <uint8_t*> malloc(size * sizeof(uint8_t))
11973
try:
12074
tox_get_savedata(self._get(), data)
12175
return data[:size]
@@ -124,7 +78,7 @@ cdef class Core:
12478

12579
def bootstrap(self, host: str, port: int, public_key: bytes) -> bool:
12680
_check_len("public_key", public_key, tox_public_key_size())
127-
cdef Tox_Err_Bootstrap err = TOX_ERR_BOOTSTRAP_OK
81+
err = TOX_ERR_BOOTSTRAP_OK
12882
return tox_bootstrap(self._get(), host.encode("utf-8"), port, public_key, &err)
12983

13084
@property
@@ -143,8 +97,8 @@ cdef class Core:
14397

14498
@property
14599
def address(self) -> bytes:
146-
cdef size_t size = tox_address_size()
147-
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
100+
size = tox_address_size()
101+
data = <uint8_t*> malloc(size * sizeof(uint8_t))
148102
try:
149103
tox_self_get_address(self._get(), data)
150104
return data[:size]
@@ -157,8 +111,8 @@ cdef class Core:
157111

158112
@property
159113
def public_key(self) -> bytes:
160-
cdef size_t size = tox_public_key_size()
161-
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
114+
size = tox_public_key_size()
115+
data = <uint8_t*> malloc(size * sizeof(uint8_t))
162116
try:
163117
tox_self_get_public_key(self._get(), data)
164118
return data[:tox_public_key_size()]
@@ -167,8 +121,8 @@ cdef class Core:
167121

168122
@property
169123
def secret_key(self) -> bytes:
170-
cdef size_t size = tox_secret_key_size()
171-
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
124+
size = tox_secret_key_size()
125+
data = <uint8_t*> malloc(size * sizeof(uint8_t))
172126
try:
173127
tox_self_get_secret_key(self._get(), data)
174128
return data[:tox_secret_key_size()]
@@ -177,8 +131,8 @@ cdef class Core:
177131

178132
@property
179133
def name(self) -> bytes:
180-
cdef size_t size = tox_self_get_name_size(self._get())
181-
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
134+
size = tox_self_get_name_size(self._get())
135+
data = <uint8_t*> malloc(size * sizeof(uint8_t))
182136
try:
183137
tox_self_get_name(self._get(), data)
184138
return data[:size]
@@ -187,14 +141,14 @@ cdef class Core:
187141

188142
@name.setter
189143
def name(self, name: bytes) -> None:
190-
cdef Tox_Err_Set_Info err = TOX_ERR_SET_INFO_OK
144+
err = TOX_ERR_SET_INFO_OK
191145
tox_self_set_name(self._get(), name, len(name), &err)
192-
if err: raise ApiException(Tox_Err_Set_Info(err))
146+
if err: raise error.ApiException(Tox_Err_Set_Info(err))
193147

194148
@property
195149
def status_message(self) -> bytes:
196-
cdef size_t size = tox_self_get_status_message_size(self._get())
197-
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
150+
size = tox_self_get_status_message_size(self._get())
151+
data = <uint8_t*> malloc(size * sizeof(uint8_t))
198152
try:
199153
tox_self_get_status_message(self._get(), data)
200154
return data[:size]
@@ -203,9 +157,9 @@ cdef class Core:
203157

204158
@status_message.setter
205159
def status_message(self, status_message: bytes) -> None:
206-
cdef Tox_Err_Set_Info err = TOX_ERR_SET_INFO_OK
160+
err = TOX_ERR_SET_INFO_OK
207161
tox_self_set_status_message(self._get(), status_message, len(status_message), &err)
208-
if err: raise ApiException(Tox_Err_Set_Info(err))
162+
if err: raise error.ApiException(Tox_Err_Set_Info(err))
209163

210164
@property
211165
def status(self) -> Tox_User_Status:
@@ -217,17 +171,17 @@ cdef class Core:
217171

218172
def friend_add(self, address: bytes, message: bytes):
219173
_check_len("address", address, tox_address_size())
220-
cdef Tox_Err_Friend_Add err = TOX_ERR_FRIEND_ADD_OK
174+
err = TOX_ERR_FRIEND_ADD_OK
221175
tox_friend_add(self._get(), address, message, len(message), &err)
222-
if err: raise ApiException(Tox_Err_Friend_Add(err))
176+
if err: raise error.ApiException(Tox_Err_Friend_Add(err))
223177

224178
def friend_add_norequest(self, public_key: bytes):
225179
_check_len("public_key", public_key, tox_public_key_size())
226-
cdef Tox_Err_Friend_Add err = TOX_ERR_FRIEND_ADD_OK
180+
err = TOX_ERR_FRIEND_ADD_OK
227181
tox_friend_add_norequest(self._get(), public_key, &err)
228-
if err: raise ApiException(Tox_Err_Friend_Add(err))
182+
if err: raise error.ApiException(Tox_Err_Friend_Add(err))
229183

230184
def friend_delete(self, friend_number: int):
231-
cdef Tox_Err_Friend_Delete err = TOX_ERR_FRIEND_DELETE_OK
185+
err = TOX_ERR_FRIEND_DELETE_OK
232186
tox_friend_delete(self._get(), friend_number, &err)
233-
if err: raise ApiException(Tox_Err_Friend_Delete(err))
187+
if err: raise error.ApiException(Tox_Err_Friend_Delete(err))

pytox/src/log.pxd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# cython: language_level=3
2+
cdef extern from "tox/toxcore/tox_log.h":
3+
ctypedef struct Tox_Log
4+
5+
cpdef enum Tox_Log_Level:
6+
TOX_LOG_LEVEL_TRACE,
7+
TOX_LOG_LEVEL_DEBUG,
8+
TOX_LOG_LEVEL_INFO,
9+
TOX_LOG_LEVEL_WARNING,
10+
TOX_LOG_LEVEL_ERROR,
11+
12+
cdef extern from "tox/toxcore/os_log.h": pass
13+
14+
cdef class ToxLog:
15+
16+
cdef Tox_Log *_ptr
17+
18+
cdef Tox_Log *_get(self) except *

0 commit comments

Comments
 (0)