From 541c9e76480700f81804e3598a3e3a40d6f45e08 Mon Sep 17 00:00:00 2001 From: Joshua Oreman Date: Fri, 14 Jan 2022 12:58:56 -0700 Subject: [PATCH] Switch back to _impl and fix names --- netfilterqueue/__init__.py | 12 ++++++++++++ netfilterqueue/{__init__.pxd => _impl.pxd} | 0 netfilterqueue/{__init__.pyi => _impl.pyi} | 0 netfilterqueue/{__init__.pyx => _impl.pyx} | 19 +++++++++++++++---- setup.py | 10 +++++----- 5 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 netfilterqueue/__init__.py rename netfilterqueue/{__init__.pxd => _impl.pxd} (100%) rename netfilterqueue/{__init__.pyi => _impl.pyi} (100%) rename netfilterqueue/{__init__.pyx => _impl.pyx} (96%) diff --git a/netfilterqueue/__init__.py b/netfilterqueue/__init__.py new file mode 100644 index 0000000..2a92dec --- /dev/null +++ b/netfilterqueue/__init__.py @@ -0,0 +1,12 @@ +from ._impl import ( + COPY_NONE as COPY_NONE, + COPY_META as COPY_META, + COPY_PACKET as COPY_PACKET, + Packet as Packet, + NetfilterQueue as NetfilterQueue, + PROTOCOLS as PROTOCOLS, +) +from ._version import ( + VERSION as VERSION, + __version__ as __version__, +) diff --git a/netfilterqueue/__init__.pxd b/netfilterqueue/_impl.pxd similarity index 100% rename from netfilterqueue/__init__.pxd rename to netfilterqueue/_impl.pxd diff --git a/netfilterqueue/__init__.pyi b/netfilterqueue/_impl.pyi similarity index 100% rename from netfilterqueue/__init__.pyi rename to netfilterqueue/_impl.pyi diff --git a/netfilterqueue/__init__.pyx b/netfilterqueue/_impl.pyx similarity index 96% rename from netfilterqueue/__init__.pyx rename to netfilterqueue/_impl.pyx index 6cee14e..d656d5b 100644 --- a/netfilterqueue/__init__.pyx +++ b/netfilterqueue/_impl.pyx @@ -23,12 +23,12 @@ DEF SockCopySize = MaxCopySize + SockOverhead # Socket queue should hold max number of packets of copysize bytes DEF SockRcvSize = DEFAULT_MAX_QUEUELEN * SockCopySize // 2 -__package__ = "netfilterqueue" - -from ._version import __version__, VERSION - from cpython.exc cimport PyErr_CheckSignals +cdef extern from "Python.h": + ctypedef struct PyTypeObject: + const char* tp_name + # A negative return value from this callback will stop processing and # make nfq_handle_packet return -1, so we use that as the error flag. cdef int global_callback(nfq_q_handle *qh, nfgenmsg *nfmsg, @@ -346,6 +346,17 @@ cdef class NetfilterQueue: else: nfq_handle_packet(self.h, buf, len(buf)) +cdef void _fix_names(): + # Avoid ._impl showing up in reprs. This doesn't work on PyPy; there we would + # need to modify the name before PyType_Ready(), but I can't find any way to + # write Cython code that would execute at that time. + cdef PyTypeObject* tp = Packet + tp.tp_name = "netfilterqueue.Packet" + tp = NetfilterQueue + tp.tp_name = "netfilterqueue.NetfilterQueue" + +_fix_names() + PROTOCOLS = { 0: "HOPOPT", 1: "ICMP", diff --git a/setup.py b/setup.py index 124036b..81bc2fc 100644 --- a/setup.py +++ b/setup.py @@ -10,8 +10,8 @@ try: ext_modules = cythonize( Extension( - "netfilterqueue.__init__", - ["netfilterqueue/__init__.pyx"], + "netfilterqueue._impl", + ["netfilterqueue/_impl.pyx"], libraries=["netfilter_queue"], ), compiler_directives={"language_level": "3str"}, @@ -23,7 +23,7 @@ except ImportError: # setup_requires below. setup_requires = ["cython"] elif not os.path.exists( - os.path.join(os.path.dirname(__file__), "netfilterqueue/__init__.c") + os.path.join(os.path.dirname(__file__), "netfilterqueue/_impl.c") ): sys.stderr.write( "You must have Cython installed (`pip install cython`) to build this " @@ -34,8 +34,8 @@ except ImportError: sys.exit(1) ext_modules = [ Extension( - "netfilterqueue.__init__", - ["netfilterqueue/__init__.c"], + "netfilterqueue._impl", + ["netfilterqueue/_impl.c"], libraries=["netfilter_queue"], ) ]