Skip to content

Commit e37680a

Browse files
committed
refactor: Adapt pytox to the new tox_system stuff.
1 parent 1c574ed commit e37680a

25 files changed

+524
-114
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
pkg-config
3131
python3-dev
3232
python3-setuptools
33-
- run: git clone --depth=1 https://github.com/TokTok/c-toxcore
33+
- run: git clone --recursive --depth=1 https://github.com/TokTok/c-toxcore
3434
- run: cd c-toxcore;
3535
. .github/scripts/flags-gcc.sh;
3636
add_flag -fsanitize=address;

.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
@@ -5,23 +5,59 @@ package(features = ["layering_check"])
55

66
project()
77

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

1953
pyx_library(
2054
name = "pytox",
2155
srcs = [
56+
"pytox.pxd",
2257
"pytox/av.pyx",
2358
"pytox/core.pyx",
24-
],
59+
"pytox/error.pyx",
60+
] + ["pytox/%s.pxd" % sys for sys in SUBSYSTEMS] + ["pytox/%s.pyx" % sys for sys in SUBSYSTEMS],
2561
cdeps = ["//c-toxcore"],
2662
cython_directives = {"language_level": "3"},
2763
visibility = ["//visibility:public"],

Dockerfile

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM ubuntu:20.04
1+
FROM ubuntu:22.04
22
LABEL maintainer="iphydf@gmail.com"
33

44
RUN apt-get update \
55
&& DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y \
66
ca-certificates \
77
cmake \
8-
cython \
8+
cython3 \
99
gcc \
1010
g++ \
1111
git \
@@ -22,20 +22,40 @@ RUN apt-get update \
2222
&& pip3 install mypy
2323

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

33-
COPY pytox /build/pytox
33+
# Tools first, they change less.
3434
COPY tools /build/tools
35+
COPY pytox.pxd /build/
36+
COPY pytox /build/pytox
3537

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

4060
COPY setup.py /build/
4161
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))

0 commit comments

Comments
 (0)