Skip to content

Commit ccbbbb0

Browse files
committed
Initial port to Cython 3 with backwards compatibilty
Closes D94
1 parent d0cb8c4 commit ccbbbb0

14 files changed

Lines changed: 410 additions & 106 deletions

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import glob
44
import os
5-
65
from distutils.core import setup
76
from distutils.extension import Extension
7+
88
from setup_pjsip import PJSIP_build_ext
99

1010

@@ -54,7 +54,7 @@ def __getattribute__(self, name): # this is here to silence the IDE about missi
5454
},
5555

5656
ext_modules=[
57-
Extension(name="sipsimple.core._core", sources=["sipsimple/core/_core.pyx", "sipsimple/core/_core.pxd"] + glob.glob(os.path.join("sipsimple", "core", "_core.*.pxi"))),
57+
Extension(name="sipsimple.core._core", sources=["sipsimple/core/_core.pyx"]),
5858
Extension(name="sipsimple.util._sha1", sources=["sipsimple/util/_sha1.pyx"], depends=["sipsimple/util/_sha1.h"])
5959
],
6060

setup_pjsip.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
from distutils import log
6868
from distutils.dir_util import copy_tree
6969
from distutils.errors import DistutilsError
70+
71+
from Cython import __version__ as cython_version
7072
from Cython.Distutils import build_ext
7173

7274

@@ -143,6 +145,7 @@ def get_make_cmd():
143145
else:
144146
return "make"
145147

148+
146149
@staticmethod
147150
def get_opts_from_string(line, prefix):
148151
"""Returns all options that have a particular prefix on a commandline"""
@@ -274,3 +277,19 @@ def cython_sources(self, sources, extension, silent=True):
274277
log.info("Error building %s: %s" % (extension.name, str(e)))
275278
return None
276279
return build_ext.cython_sources(self, sources, extension)
280+
281+
def build_extension(self, ext):
282+
log.info(f"Compiling Cython extension {ext.name}")
283+
if cython_version.startswith('0.2'):
284+
return super().build_extension(ext)
285+
286+
if ext.name == "sipsimple.core._core":
287+
self.build_dir = os.path.join(self.build_temp, "pjsip")
288+
if self.clean:
289+
self.clean_pjsip()
290+
copy_tree(self.pjsip_dir, self.build_dir, verbose=0)
291+
if not os.path.exists(os.path.join(self.build_dir, "build.mak")):
292+
self.configure_pjsip()
293+
self.update_extension(ext)
294+
self.compile_pjsip()
295+
return super().build_extension(ext)

sipsimple/core/_core.event.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cdef struct _handler_queue:
2121

2222
# callback functions
2323

24-
cdef void _cb_log(int level, char_ptr_const data, int len):
24+
cdef void _cb_log(int level, char_ptr_const data, int len) noexcept:
2525
cdef _core_event *event
2626
event = <_core_event *> malloc(sizeof(_core_event))
2727
if event != NULL:

sipsimple/core/_core.invitation.pxi

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ cdef class Invitation:
13911391
# Callback functions
13921392
#
13931393

1394-
cdef void _Invitation_cb_state(pjsip_inv_session *inv, pjsip_event *e) with gil:
1394+
cdef void _Invitation_cb_state_impl(pjsip_inv_session *inv, pjsip_event *e) with gil:
13951395
cdef pjsip_rx_data *rdata = NULL
13961396
cdef pjsip_tx_data *tdata = NULL
13971397
cdef object state
@@ -1466,7 +1466,11 @@ cdef void _Invitation_cb_state(pjsip_inv_session *inv, pjsip_event *e) with gil:
14661466
except:
14671467
ua._handle_exception(1)
14681468

1469-
cdef void _Invitation_cb_sdp_done(pjsip_inv_session *inv, int status) with gil:
1469+
cdef void _Invitation_cb_state(pjsip_inv_session *inv, pjsip_event *e) noexcept nogil:
1470+
with gil:
1471+
_Invitation_cb_state_impl(inv, e)
1472+
1473+
cdef void _Invitation_cb_sdp_done_impl(pjsip_inv_session *inv, int status) with gil:
14701474
cdef Invitation invitation
14711475
cdef PJSIPUA ua
14721476
cdef SDPCallbackTimer timer
@@ -1516,7 +1520,11 @@ cdef void _Invitation_cb_sdp_done(pjsip_inv_session *inv, int status) with gil:
15161520
except:
15171521
ua._handle_exception(1)
15181522

1519-
cdef int _Invitation_cb_rx_reinvite(pjsip_inv_session *inv, pjmedia_sdp_session_ptr_const offer, pjsip_rx_data *rdata) with gil:
1523+
cdef void _Invitation_cb_sdp_done(pjsip_inv_session *inv, int status) noexcept nogil:
1524+
with gil:
1525+
_Invitation_cb_sdp_done_impl(inv, status)
1526+
1527+
cdef int _Invitation_cb_rx_reinvite_impl(pjsip_inv_session *inv, pjmedia_sdp_session_ptr_const offer, pjsip_rx_data *rdata) with gil:
15201528
cdef int status
15211529
cdef pjsip_tx_data *answer_tdata
15221530
cdef object rdata_dict = None
@@ -1560,7 +1568,13 @@ cdef int _Invitation_cb_rx_reinvite(pjsip_inv_session *inv, pjmedia_sdp_session_
15601568
ua._handle_exception(1)
15611569
return 1
15621570

1563-
cdef void _Invitation_cb_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e) with gil:
1571+
cdef int _Invitation_cb_rx_reinvite(pjsip_inv_session *inv, pjmedia_sdp_session_ptr_const offer, pjsip_rx_data *rdata) noexcept nogil:
1572+
cdef int result
1573+
with gil:
1574+
result = _Invitation_cb_rx_reinvite_impl(inv, offer, rdata)
1575+
return result
1576+
1577+
cdef void _Invitation_cb_tsx_state_changed_impl(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e) with gil:
15641578
cdef pjsip_rx_data *rdata = NULL
15651579
cdef pjsip_tx_data *tdata = NULL
15661580
cdef object rdata_dict = None
@@ -1628,11 +1642,19 @@ cdef void _Invitation_cb_tsx_state_changed(pjsip_inv_session *inv, pjsip_transac
16281642
except:
16291643
ua._handle_exception(1)
16301644

1631-
cdef void _Invitation_cb_new(pjsip_inv_session *inv, pjsip_event *e) with gil:
1645+
cdef void _Invitation_cb_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e) noexcept nogil:
1646+
with gil:
1647+
_Invitation_cb_tsx_state_changed_impl(inv, tsx, e)
1648+
1649+
cdef void _Invitation_cb_new_impl(pjsip_inv_session *inv, pjsip_event *e) with gil:
16321650
# As far as I can tell this is never actually called!
16331651
pass
16341652

1635-
cdef void _Invitation_transfer_cb_state(pjsip_evsub *sub, pjsip_event *event) with gil:
1653+
cdef void _Invitation_cb_new(pjsip_inv_session *inv, pjsip_event *e) noexcept nogil:
1654+
with gil:
1655+
_Invitation_cb_new_impl(inv, e)
1656+
1657+
cdef void _Invitation_transfer_cb_state_impl(pjsip_evsub *sub, pjsip_event *event) with gil:
16361658
cdef void *invitation_void
16371659
cdef Invitation invitation
16381660
cdef object state
@@ -1680,7 +1702,11 @@ cdef void _Invitation_transfer_cb_state(pjsip_evsub *sub, pjsip_event *event) wi
16801702
except:
16811703
ua._handle_exception(1)
16821704

1683-
cdef void _Invitation_transfer_cb_tsx(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) with gil:
1705+
cdef void _Invitation_transfer_cb_state(pjsip_evsub *sub, pjsip_event *event) noexcept nogil:
1706+
with gil:
1707+
_Invitation_transfer_cb_state_impl(sub, event)
1708+
1709+
cdef void _Invitation_transfer_cb_tsx_impl(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) with gil:
16841710
cdef void *invitation_void
16851711
cdef Invitation invitation
16861712
cdef pjsip_rx_data *rdata
@@ -1714,7 +1740,11 @@ cdef void _Invitation_transfer_cb_tsx(pjsip_evsub *sub, pjsip_transaction *tsx,
17141740
except:
17151741
ua._handle_exception(1)
17161742

1717-
cdef void _Invitation_transfer_cb_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
1743+
cdef void _Invitation_transfer_cb_tsx(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) noexcept nogil:
1744+
with gil:
1745+
_Invitation_transfer_cb_tsx_impl(sub, tsx, event)
1746+
1747+
cdef void _Invitation_transfer_cb_notify_impl(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
17181748
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body) with gil:
17191749
cdef void *invitation_void
17201750
cdef Invitation invitation
@@ -1742,11 +1772,20 @@ cdef void _Invitation_transfer_cb_notify(pjsip_evsub *sub, pjsip_rx_data *rdata,
17421772
except:
17431773
ua._handle_exception(1)
17441774

1745-
cdef void _Invitation_transfer_cb_refresh(pjsip_evsub *sub) with gil:
1775+
cdef void _Invitation_transfer_cb_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
1776+
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body) noexcept nogil:
1777+
with gil:
1778+
_Invitation_transfer_cb_notify_impl(sub, rdata, p_st_code, p_st_text, res_hdr, p_body)
1779+
1780+
cdef void _Invitation_transfer_cb_refresh_impl(pjsip_evsub *sub) with gil:
17461781
# We want to handle the refresh timer oursevles, ignore the PJSIP provided timer
17471782
pass
17481783

1749-
cdef void _Invitation_transfer_in_cb_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
1784+
cdef void _Invitation_transfer_cb_refresh(pjsip_evsub *sub) noexcept nogil:
1785+
with gil:
1786+
_Invitation_transfer_cb_refresh_impl(sub)
1787+
1788+
cdef void _Invitation_transfer_in_cb_rx_refresh_impl(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
17501789
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body) with gil:
17511790
cdef void *invitation_void
17521791
cdef dict rdata_dict
@@ -1780,7 +1819,12 @@ cdef void _Invitation_transfer_in_cb_rx_refresh(pjsip_evsub *sub, pjsip_rx_data
17801819
except:
17811820
ua._handle_exception(1)
17821821

1783-
cdef void _Invitation_transfer_in_cb_server_timeout(pjsip_evsub *sub) with gil:
1822+
cdef void _Invitation_transfer_in_cb_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
1823+
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body) noexcept nogil:
1824+
with gil:
1825+
_Invitation_transfer_in_cb_rx_refresh_impl(sub, rdata, p_st_code, p_st_text, res_hdr, p_body)
1826+
1827+
cdef void _Invitation_transfer_in_cb_server_timeout_impl(pjsip_evsub *sub) with gil:
17841828
cdef void *invitation_void
17851829
cdef Invitation invitation
17861830
cdef Timer timer
@@ -1804,7 +1848,11 @@ cdef void _Invitation_transfer_in_cb_server_timeout(pjsip_evsub *sub) with gil:
18041848
except:
18051849
ua._handle_exception(1)
18061850

1807-
cdef void _Invitation_transfer_in_cb_tsx(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) with gil:
1851+
cdef void _Invitation_transfer_in_cb_server_timeout(pjsip_evsub *sub) noexcept nogil:
1852+
with gil:
1853+
_Invitation_transfer_in_cb_server_timeout_impl(sub)
1854+
1855+
cdef void _Invitation_transfer_in_cb_tsx_impl(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) with gil:
18081856
cdef void *invitation_void
18091857
cdef Invitation invitation
18101858
cdef PJSIPUA ua
@@ -1840,6 +1888,10 @@ cdef void _Invitation_transfer_in_cb_tsx(pjsip_evsub *sub, pjsip_transaction *ts
18401888
ua._handle_exception(1)
18411889

18421890

1891+
cdef void _Invitation_transfer_in_cb_tsx(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) noexcept nogil:
1892+
with gil:
1893+
_Invitation_transfer_in_cb_tsx_impl(sub, tsx, event)
1894+
18431895
# Globals
18441896
#
18451897

sipsimple/core/_core.lib.pxi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ cdef class PJMEDIAEndpoint:
513513
raise PJSIPError("Could not set video options", status)
514514

515515

516-
cdef void _transport_state_cb(pjsip_transport *tp, pjsip_transport_state state, pjsip_transport_state_info_ptr_const info) with gil:
516+
cdef void _transport_state_cb_impl(pjsip_transport *tp, pjsip_transport_state state, pjsip_transport_state_info_ptr_const info) with gil:
517517
cdef PJSIPUA ua
518518
cdef str local_address
519519
cdef str remote_address
@@ -542,6 +542,10 @@ cdef void _transport_state_cb(pjsip_transport *tp, pjsip_transport_state state,
542542
_add_event("SIPEngineTransportDidDisconnect", event_dict)
543543

544544

545+
cdef void _transport_state_cb(pjsip_transport *tp, pjsip_transport_state state, pjsip_transport_state_info_ptr_const info) noexcept nogil:
546+
with gil:
547+
_transport_state_cb_impl(tp, state, info)
548+
545549
# globals
546550
cdef PJSTR h264_profile_level_id = PJSTR(b"profile-level-id")
547551
cdef PJSTR h264_packetization_mode = PJSTR(b"packetization-mode")

0 commit comments

Comments
 (0)