Skip to content

Commit

Permalink
py-pypcap: update exception syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jmroot committed Apr 1, 2023
1 parent c7aca39 commit 71f0a31
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
1 change: 1 addition & 0 deletions python/py-pypcap/Portfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PortGroup python 1.0

name py-pypcap
version 1.1
revision 1
categories-append net
license BSD
maintainers gmail.com:andrew.reusch openmaintainer
Expand Down
102 changes: 98 additions & 4 deletions python/py-pypcap/files/py3.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- pcap.pyx.orig 2022-10-20 23:22:40.000000000 +1100
+++ pcap.pyx 2022-10-20 23:25:38.000000000 +1100
--- pcap.pyx.orig 2023-04-01 11:07:48
+++ pcap.pyx 2023-04-01 11:21:06
@@ -20,7 +20,8 @@
import struct

Expand All @@ -19,7 +19,74 @@
*(<object>ctx.args))
except:
ctx.got_exc = 1
@@ -318,7 +319,7 @@
@@ -142,7 +143,7 @@
cdef bpf_program fcode
def __init__(self, char *filter, dlt=DLT_RAW):
if pcap_ex_compile_nopcap(65535, dlt, &self.fcode, filter, 1, 0) < 0:
- raise IOError, 'bad filter'
+ raise IOError('bad filter')
def filter(self, buf):
"""Return boolean match for buf against our filter."""
cdef char *p
@@ -184,7 +185,7 @@
if not name:
p = pcap_ex_lookupdev(self.__ebuf)
if p == NULL:
- raise OSError, self.__ebuf
+ raise OSError(self.__ebuf)
else:
p = name

@@ -193,14 +194,14 @@
self.__pcap = pcap_open_live(pcap_ex_name(p), snaplen, promisc,
timeout_ms, self.__ebuf)
if not self.__pcap:
- raise OSError, self.__ebuf
+ raise OSError(self.__ebuf)

self.__name = strdup(p)
self.__filter = strdup("")
try: self.__dloff = dltoff[pcap_datalink(self.__pcap)]
except KeyError: pass
if immediate and pcap_ex_immediate(self.__pcap) < 0:
- raise OSError, "couldn't enable immediate mode"
+ raise OSError("couldn't enable immediate mode")

property name:
"""Network interface or dumpfile name."""
@@ -237,9 +238,9 @@
free(self.__filter)
self.__filter = strdup(value)
if pcap_compile(self.__pcap, &fcode, self.__filter, optimize, 0) < 0:
- raise OSError, pcap_geterr(self.__pcap)
+ raise OSError(pcap_geterr(self.__pcap))
if pcap_setfilter(self.__pcap, &fcode) < 0:
- raise OSError, pcap_geterr(self.__pcap)
+ raise OSError(pcap_geterr(self.__pcap))
pcap_freecode(&fcode)

def setnonblock(self, nonblock=True):
@@ -250,7 +251,7 @@
"""Return non-blocking capture mode as boolean."""
ret = pcap_ex_getnonblock(self.__pcap, self.__ebuf)
if ret < 0:
- raise OSError, self.__ebuf
+ raise OSError(self.__ebuf)
elif ret:
return True
return False
@@ -291,7 +292,9 @@
<unsigned char *>&ctx)
if ctx.got_exc:
exc = sys.exc_info()
- raise exc[0], exc[1], exc[2]
+ e = exc[0](exc[1])
+ e.__traceback__ = exc[2]
+ raise e
return n

def loop(self, cnt, callback, *args):
@@ -318,7 +321,7 @@
n = pcap_ex_next(self.__pcap, &hdr, &pkt)
if n == 1:
callback(hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0),
Expand All @@ -28,7 +95,25 @@
elif n == 0:
break
elif n == -1:
@@ -361,7 +362,7 @@
@@ -333,7 +336,7 @@
"""Send a raw network packet on the interface."""
ret = pcap_sendpacket(self.__pcap, buf, len(buf))
if ret == -1:
- raise OSError, pcap_geterr(self.__pcap)
+ raise OSError(pcap_geterr(self.__pcap))
return len(buf)

def geterr(self):
@@ -345,7 +348,7 @@
dropped, and dropped by the interface."""
cdef pcap_stat pstat
if pcap_stats(self.__pcap, &pstat) < 0:
- raise OSError, pcap_geterr(self.__pcap)
+ raise OSError(pcap_geterr(self.__pcap))
return (pstat.ps_recv, pstat.ps_drop, pstat.ps_ifdrop)

def __iter__(self):
@@ -361,7 +364,7 @@
n = pcap_ex_next(self.__pcap, &hdr, &pkt)
if n == 1:
return (hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0),
Expand All @@ -37,3 +122,12 @@
elif n == 0:
return None
elif n == -1:
@@ -385,7 +388,7 @@
cdef char *p, ebuf[256]
p = pcap_ex_lookupdev(ebuf)
if p == NULL:
- raise OSError, ebuf
+ raise OSError(ebuf)
return p

def findalldevs():

0 comments on commit 71f0a31

Please sign in to comment.