Skip to content

Commit 3578fa6

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

File tree

17 files changed

+334
-33
lines changed

17 files changed

+334
-33
lines changed

BUILD.bazel

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,50 @@ package(features = ["layering_check"])
55

66
project()
77

8+
SUBSYSTEMS = [
9+
"log",
10+
"memory",
11+
"network",
12+
"random",
13+
"system",
14+
"time",
15+
]
16+
17+
[genrule(
18+
name = "pytox/" + sys,
19+
srcs = [
20+
"pytox/src/%s.pyx" % sys,
21+
"//c-toxcore:tox/toxcore/tox_%s.h" % sys,
22+
],
23+
outs = ["pytox/%s.pyx" % sys],
24+
cmd = " ".join([
25+
"$(location //py_toxcore_c/tools:gen_api)",
26+
"$(location pytox/src/%s.pyx)" % sys,
27+
"$(location //c-toxcore:tox/toxcore/tox_%s.h)" % sys,
28+
"> $@",
29+
]),
30+
tools = ["//py_toxcore_c/tools:gen_api"],
31+
) for sys in SUBSYSTEMS]
32+
833
genrule(
934
name = "pytox/core",
1035
srcs = [
1136
"pytox/src/core.pyx",
12-
"//c-toxcore:tox/tox.h",
37+
"//c-toxcore:tox/toxcore/tox.h",
1338
],
1439
outs = ["pytox/core.pyx"],
15-
cmd = "$(location //py_toxcore_c/tools:gen_api) $(location pytox/src/core.pyx) $(location //c-toxcore:tox/tox.h) > $@",
40+
cmd = "$(location //py_toxcore_c/tools:gen_api) $(location pytox/src/core.pyx) $(location //c-toxcore:tox/toxcore/tox.h) > $@",
1641
tools = ["//py_toxcore_c/tools:gen_api"],
1742
)
1843

1944
pyx_library(
2045
name = "pytox",
2146
srcs = [
47+
"pytox.pxd",
2248
"pytox/av.pyx",
2349
"pytox/core.pyx",
24-
],
50+
"pytox/error.pyx",
51+
] + ["pytox/%s.pxd" % sys for sys in SUBSYSTEMS] + ["pytox/%s.pyx" % sys for sys in SUBSYSTEMS],
2552
cdeps = ["//c-toxcore"],
2653
cython_directives = {"language_level": "3"},
2754
visibility = ["//visibility:public"],

pytox.pxd

Whitespace-only changes.

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/log.pxd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ctypedef struct Tox_Log
2+
3+
cpdef enum Tox_Log_Level:
4+
TOX_LOG_LEVEL_TRACE,
5+
TOX_LOG_LEVEL_DEBUG,
6+
TOX_LOG_LEVEL_INFO,
7+
TOX_LOG_LEVEL_WARNING,
8+
TOX_LOG_LEVEL_ERROR,
9+
10+
cdef class ToxLog:
11+
12+
cdef Tox_Log *_get(self) except *

pytox/memory.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ctypedef struct Tox_Memory
2+
3+
cdef class ToxMemory:
4+
5+
cdef Tox_Memory *_get(self) except *

pytox/network.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ctypedef struct Tox_Network
2+
3+
cdef class ToxNetwork:
4+
5+
cdef Tox_Network *_get(self) except *

pytox/random.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ctypedef struct Tox_Random
2+
3+
cdef class ToxRandom:
4+
5+
cdef Tox_Random *_get(self) except *

pytox/src/core.pyx

Lines changed: 15 additions & 28 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.log cimport Tox_Log_Level
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,37 +32,20 @@ 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-
5441
cdef class ToxOptions:
5542

5643
cdef Tox_Options *_ptr
5744

5845
def __init__(self):
5946
cdef Tox_Err_Options_New err = TOX_ERR_OPTIONS_NEW_OK
6047
self._ptr = tox_options_new(&err)
61-
if err: raise ApiException(Tox_Err_Options_New(err))
48+
if err: raise error.ApiException(Tox_Err_Options_New(err))
6249

6350
def __dealloc__(self):
6451
self.__exit__(None, None, None)
@@ -72,7 +59,7 @@ cdef class ToxOptions:
7259

7360
cdef Tox_Options *_get(self) except *:
7461
if self._ptr is NULL:
75-
raise UseAfterFreeException()
62+
raise error.UseAfterFreeException()
7663
return self._ptr
7764

7865
@property
@@ -93,7 +80,7 @@ cdef class Core:
9380
if options is None:
9481
options = ToxOptions()
9582
self._ptr = tox_new(options._ptr, &err)
96-
if err: raise ApiException(Tox_Err_New(err))
83+
if err: raise error.ApiException(Tox_Err_New(err))
9784

9885
install_handlers(self._get())
9986

@@ -109,7 +96,7 @@ cdef class Core:
10996

11097
cdef Tox *_get(self) except *:
11198
if self._ptr is NULL:
112-
raise UseAfterFreeException()
99+
raise error.UseAfterFreeException()
113100
return self._ptr
114101

115102
@property
@@ -189,7 +176,7 @@ cdef class Core:
189176
def name(self, name: bytes) -> None:
190177
cdef Tox_Err_Set_Info err = TOX_ERR_SET_INFO_OK
191178
tox_self_set_name(self._get(), name, len(name), &err)
192-
if err: raise ApiException(Tox_Err_Set_Info(err))
179+
if err: raise error.ApiException(Tox_Err_Set_Info(err))
193180

194181
@property
195182
def status_message(self) -> bytes:
@@ -205,7 +192,7 @@ cdef class Core:
205192
def status_message(self, status_message: bytes) -> None:
206193
cdef Tox_Err_Set_Info err = TOX_ERR_SET_INFO_OK
207194
tox_self_set_status_message(self._get(), status_message, len(status_message), &err)
208-
if err: raise ApiException(Tox_Err_Set_Info(err))
195+
if err: raise error.ApiException(Tox_Err_Set_Info(err))
209196

210197
@property
211198
def status(self) -> Tox_User_Status:
@@ -219,15 +206,15 @@ cdef class Core:
219206
_check_len("address", address, tox_address_size())
220207
cdef Tox_Err_Friend_Add err = TOX_ERR_FRIEND_ADD_OK
221208
tox_friend_add(self._get(), address, message, len(message), &err)
222-
if err: raise ApiException(Tox_Err_Friend_Add(err))
209+
if err: raise error.ApiException(Tox_Err_Friend_Add(err))
223210

224211
def friend_add_norequest(self, public_key: bytes):
225212
_check_len("public_key", public_key, tox_public_key_size())
226213
cdef Tox_Err_Friend_Add err = TOX_ERR_FRIEND_ADD_OK
227214
tox_friend_add_norequest(self._get(), public_key, &err)
228-
if err: raise ApiException(Tox_Err_Friend_Add(err))
215+
if err: raise error.ApiException(Tox_Err_Friend_Add(err))
229216

230217
def friend_delete(self, friend_number: int):
231218
cdef Tox_Err_Friend_Delete err = TOX_ERR_FRIEND_DELETE_OK
232219
tox_friend_delete(self._get(), friend_number, &err)
233-
if err: raise ApiException(Tox_Err_Friend_Delete(err))
220+
if err: raise error.ApiException(Tox_Err_Friend_Delete(err))

pytox/src/log.pyx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# cython: language_level=3
2+
"""Toxcore bindings.
3+
"""
4+
5+
from libcpp cimport bool
6+
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
7+
from libc.stdlib cimport malloc, free
8+
9+
from pytox import error
10+
from pytox cimport memory
11+
from pytox.memory cimport Tox_Memory
12+
13+
cdef extern from "tox/toxcore/tox_log.h": pass
14+
15+
16+
cdef class ToxLog:
17+
18+
cdef Tox_Log *_ptr
19+
20+
def __init__(self, memory.ToxMemory mem):
21+
self._ptr = tox_log_new(NULL, <void*>self, mem._get())
22+
if self._ptr is NULL: raise MemoryError("tox_log_new")
23+
24+
def __dealloc__(self):
25+
self.__exit__(None, None, None)
26+
27+
def __enter__(self):
28+
return self
29+
30+
def __exit__(self, exc_type, exc_value, exc_traceback):
31+
tox_log_free(self._ptr)
32+
self._ptr = NULL
33+
34+
cdef Tox_Log *_get(self) except *:
35+
if self._ptr is NULL:
36+
raise error.UseAfterFreeException()
37+
return self._ptr

pytox/src/memory.pyx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# cython: language_level=3
2+
"""Toxcore bindings.
3+
"""
4+
5+
from libcpp cimport bool
6+
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
7+
from libc.stdlib cimport malloc, free
8+
9+
from pytox import error
10+
11+
cdef extern from "tox/toxcore/tox_memory.h": pass
12+
13+
14+
cdef class ToxMemory:
15+
16+
cdef Tox_Memory *_ptr
17+
18+
def __init__(self):
19+
self._ptr = tox_memory_new(NULL, <void*>self)
20+
if self._ptr is NULL: raise MemoryError("tox_memory_new")
21+
22+
def __dealloc__(self):
23+
self.__exit__(None, None, None)
24+
25+
def __enter__(self):
26+
return self
27+
28+
def __exit__(self, exc_type, exc_value, exc_traceback):
29+
tox_memory_free(self._ptr)
30+
self._ptr = NULL
31+
32+
cdef Tox_Memory *_get(self) except *:
33+
if self._ptr is NULL:
34+
raise error.UseAfterFreeException()
35+
return self._ptr

0 commit comments

Comments
 (0)