Skip to content

Commit

Permalink
Use functools.partial instead of lambdas where it makes sense.
Browse files Browse the repository at this point in the history
GC few python 2.7 shims while I am here.
  • Loading branch information
sobomax committed Mar 26, 2024
1 parent 33b4327 commit a782428
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 38 deletions.
9 changes: 4 additions & 5 deletions sippy/Core/EventDispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@

from __future__ import print_function

from functools import partial
from datetime import datetime
from heapq import heappush, heappop, heapify
from threading import Lock
from random import random
import sys, traceback, signal
if sys.version_info[0] < 3:
from thread import get_ident
else:
from _thread import get_ident
from _thread import get_ident
from sippy.Time.MonoTime import MonoTime
from sippy.Core.Exceptions import dump_exception, StdException

Expand Down Expand Up @@ -67,7 +65,8 @@ def cleanup(self):
self.randomize_runs = None

def get_randomizer(self, p):
return lambda x: x * (1.0 + p * (1.0 - 2.0 * random()))
def randomizer(p, x): x * (1.0 + p * (1.0 - 2.0 * random()))
return partial(randomizer, p)

def spread_runs(self, p):
self.randomize_runs = self.get_randomizer(p)
Expand Down
16 changes: 3 additions & 13 deletions sippy/Security/SipNonce.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,16 @@

#import sys; sys.path.append('..')

from functools import partial
from base64 import b64encode, b64decode

from Crypto import Random
from Crypto.Cipher import AES

from sippy.Time.clock_dtime import clock_getntime, CLOCK_MONOTONIC

# Python 2.7 compat shims
to_bytes_be = lambda x, sz: x.to_bytes(sz, 'big')
try:
to_bytes_be(int(1), 1)
except AttributeError:
to_bytes_be = lambda x, sz: '{:0{}x}'.format(x, sz * 2).decode('hex')

from_bytes_be = lambda x: int.from_bytes(x, 'big')
try:
from_bytes_be(to_bytes_be(1, 1))
except AttributeError:
from_bytes_be = lambda x: int(x.encode('hex'), 16)

to_bytes_be = partial(int.to_bytes, byteorder='big')
from_bytes_be = partial(int.from_bytes, byteorder='big')

def bxor(ba1, ba2):
olen = len(ba1)
Expand Down
10 changes: 6 additions & 4 deletions sippy/UaStateConnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.Time.Timeout import Timeout
from sippy.UaStateGeneric import UaStateGeneric
from sippy.SipAlso import SipAlso
Expand Down Expand Up @@ -85,7 +87,7 @@ def recvRequest(self, req):
pass
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
return (UasStateUpdating,)
else:
self.ua.rSDP = body.getCopy()
Expand Down Expand Up @@ -137,7 +139,7 @@ def recvACK(self, req):
callback(self.ua, req.rtime, self.ua.origin)
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
return None
else:
self.ua.rSDP = body.getCopy()
Expand Down Expand Up @@ -188,7 +190,7 @@ def recvEvent(self, event):
return None
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
try:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event), en_excpt = True)
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event), en_excpt = True)
except Exception as e:
event = CCEventFail((400, 'Malformed SDP Body'), rtime = event.rtime)
event.setWarning(str(e))
Expand Down Expand Up @@ -222,7 +224,7 @@ def recvEvent(self, event):
self.ua.expire_timer = None
code, reason, body = event.getData()
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
self.ua.startCreditTimer(event.rtime)
self.ua.connect_ts = event.rtime
Expand Down
4 changes: 3 additions & 1 deletion sippy/UacStateIdle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.Time.Timeout import TimeoutAbsMono
from sippy.UaStateGeneric import UaStateGeneric
from sippy.CCEvents import CCEventTry, CCEventFail, CCEventRedirect, CCEventDisconnect
Expand All @@ -46,7 +48,7 @@ def recvEvent(self, event):
cId, callingID, calledID, body, auth, callingName = event.getData()
if body != None:
if self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
else:
self.ua.late_media = True
Expand Down
6 changes: 4 additions & 2 deletions sippy/UacStateRinging.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.SipAddress import SipAddress
from sippy.SipRoute import SipRoute
from sippy.UaStateGeneric import UaStateGeneric
Expand All @@ -47,7 +49,7 @@ def recvResponse(self, resp, tr):
ring_cb(self.ua, resp.rtime, self.ua.origin, code)
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
return None
else:
self.ua.rSDP = body.getCopy()
Expand Down Expand Up @@ -121,7 +123,7 @@ def recvResponse(self, resp, tr):
rval = (UaStateConnected,)
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
return rval
else:
self.ua.rSDP = body.getCopy()
Expand Down
6 changes: 4 additions & 2 deletions sippy/UacStateTrying.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.SipAddress import SipAddress
from sippy.SipRoute import SipRoute
from sippy.UaStateGeneric import UaStateGeneric
Expand Down Expand Up @@ -62,7 +64,7 @@ def recvResponse(self, resp, tr):
event = CCEventRing(scode, rtime = resp.rtime, origin = self.ua.origin)
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
self.ua.p1xx_ts = resp.rtime
return (UacStateRinging, self.ua.ring_cbs, resp.rtime, self.ua.origin, code)
else:
Expand Down Expand Up @@ -139,7 +141,7 @@ def recvResponse(self, resp, tr):
rval = (UaStateConnected,)
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
return rval
else:
self.ua.rSDP = body.getCopy()
Expand Down
4 changes: 3 additions & 1 deletion sippy/UacStateUpdating.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.UaStateGeneric import UaStateGeneric
from sippy.CCEvents import CCEventDisconnect, CCEventRing, CCEventConnect, CCEventFail, CCEventRedirect

Expand Down Expand Up @@ -63,7 +65,7 @@ def recvResponse(self, resp, tr):
event = CCEventConnect(scode, rtime = resp.rtime, origin = self.ua.origin)
if body != None:
if self.ua.on_remote_sdp_change != None:
cb_func = lambda x: self.ua.delayed_remote_sdp_update(event, x)
cb_func = partial(self.ua.delayed_remote_sdp_update, event)
try:
self.ua.on_remote_sdp_change(body, cb_func, en_excpt = True)
except Exception as e:
Expand Down
4 changes: 3 additions & 1 deletion sippy/UasStateIdle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.Time.Timeout import TimeoutAbsMono
from sippy.SipAddress import SipAddress
from sippy.SipRoute import SipRoute
Expand Down Expand Up @@ -98,7 +100,7 @@ def recvRequest(self, req):
self.ua.expire_timer = TimeoutAbsMono(self.ua.expires, self.ua.expire_mtime)
if body != None:
if self.ua.on_remote_sdp_change != None:
self.ua.on_remote_sdp_change(body, lambda x: self.ua.delayed_remote_sdp_update(event, x))
self.ua.on_remote_sdp_change(body, partial(self.ua.delayed_remote_sdp_update, event))
self.ua.setup_ts = req.rtime
return (UasStateTrying,)
else:
Expand Down
7 changes: 4 additions & 3 deletions sippy/UasStateRinging.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.UaStateGeneric import UaStateGeneric
from sippy.CCEvents import CCEventRing, CCEventConnect, CCEventFail, CCEventRedirect, \
CCEventDisconnect, CCEventPreConnect
from sippy.SipContact import SipContact
from sippy.SipAddress import SipAddress

class UasStateRinging(UaStateGeneric):
sname = 'Ringing(UAS)'
Expand All @@ -44,7 +45,7 @@ def recvEvent(self, event):
if code == 100:
return None
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
self.ua.lSDP = body
if self.ua.p1xx_ts == None:
Expand All @@ -56,7 +57,7 @@ def recvEvent(self, event):
elif isinstance(event, CCEventConnect) or isinstance(event, CCEventPreConnect):
code, reason, body = event.getData()
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
if event.extra_headers != None:
extra_headers = tuple(event.extra_headers)
Expand Down
7 changes: 4 additions & 3 deletions sippy/UasStateTrying.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.UaStateGeneric import UaStateGeneric
from sippy.CCEvents import CCEventRing, CCEventConnect, CCEventFail, CCEventRedirect, \
CCEventDisconnect, CCEventPreConnect
from sippy.SipContact import SipContact
from sippy.SipAddress import SipAddress
from sippy.Time.Timeout import TimeoutAbsMono

class UasStateTrying(UaStateGeneric):
Expand All @@ -44,7 +45,7 @@ def recvEvent(self, event):
if code == 100:
return None
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
self.ua.lSDP = body
self.ua.sendUasResponse(code, reason, body)
Expand All @@ -59,7 +60,7 @@ def recvEvent(self, event):
elif isinstance(event, CCEventConnect) or isinstance(event, CCEventPreConnect):
code, reason, body = event.getData()
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
if event.extra_headers != None:
extra_headers = tuple(event.extra_headers)
Expand Down
7 changes: 4 additions & 3 deletions sippy/UasStateUpdating.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from functools import partial

from sippy.SipContact import SipContact
from sippy.SipAddress import SipAddress
from sippy.UaStateGeneric import UaStateGeneric
from sippy.CCEvents import CCEventDisconnect, CCEventRing, CCEventConnect, CCEventFail, CCEventRedirect

Expand Down Expand Up @@ -75,15 +76,15 @@ def recvEvent(self, event):
scode = (180, 'Ringing', None)
body = scode[2]
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
self.ua.lSDP = body
self.ua.sendUasResponse(scode[0], scode[1], body)
return None
elif isinstance(event, CCEventConnect):
code, reason, body = event.getData()
if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
self.ua.on_local_sdp_change(body, lambda x: self.ua.recvEvent(event))
self.ua.on_local_sdp_change(body, partial(self.ua.recvEvent, event))
return None
self.ua.lSDP = body
self.ua.sendUasResponse(code, reason, body, (self.ua.lContact,))
Expand Down

0 comments on commit a782428

Please sign in to comment.