aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Schink <swo-dev@marcschink.de>2017-10-03 15:17:53 +0200
committerMarc Schink <swo-dev@marcschink.de>2017-10-12 08:41:54 +0200
commitdbd7c0044a6039df116c18b8e8c879b57f271095 (patch)
treec62de598307f9ee2e32894ad2d954a586b0b515f
parent417de2250535387977989930b6f05e50c996ac86 (diff)
Python: Workaround for SWIG and C++11 enums
Define simple Python classes for enums manually until SWIG can handle C++11 enums properly.
-rw-r--r--bindings/python/Makefile.am9
-rw-r--r--bindings/python/enums.py62
-rw-r--r--bindings/python/swopy.i45
-rw-r--r--examples/decoder.py52
4 files changed, 102 insertions, 66 deletions
diff --git a/bindings/python/Makefile.am b/bindings/python/Makefile.am
index 7aa683a..b134ad1 100644
--- a/bindings/python/Makefile.am
+++ b/bindings/python/Makefile.am
@@ -20,7 +20,9 @@
CXX_SRCDIR = $(top_srcdir)/bindings/cxx
CXX_BUILDDIR = $(top_builddir)/bindings/cxx
-EXTRA_DIST = swopy.i __init__.py Context.h
+PYTHON_SRCDIR = $(top_srcdir)/bindings/python
+
+EXTRA_DIST = swopy.i enums.py __init__.py Context.h
all-local: swopy build-lock
@@ -31,9 +33,10 @@ swopy:
# Use empty target build-lock to ensure that the Python module is built only if
# one of its source files has changed since the last invocation.
-build-lock: setup.py $(srcdir)/swopy.i Context.h $(CXX_BUILDDIR)/libswocxx.la
+build-lock: setup.py $(srcdir)/swopy.i enums.py Context.h \
+ $(CXX_BUILDDIR)/libswocxx.la
$(SWIG) -python -c++ -Wall -outdir swopy -outcurrentdir \
- -I$(CXX_SRCDIR)/include $(srcdir)/swopy.i
+ -I$(CXX_SRCDIR)/include -I$(PYTHON_SRCDIR) $(srcdir)/swopy.i
$(PYTHON) setup.py --quiet build
touch build-lock
diff --git a/bindings/python/enums.py b/bindings/python/enums.py
new file mode 100644
index 0000000..eab033b
--- /dev/null
+++ b/bindings/python/enums.py
@@ -0,0 +1,62 @@
+##
+## This file is part of the libswo project.
+##
+## Copyright (C) 2017 Marc Schink <swo-dev@marcschink.de>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+
+class LogLevel:
+ NONE = 0
+ ERROR = 1
+ WARNING = 2
+ INFO = 3
+ DEBUG = 4
+
+class PacketType:
+ UNKNOWN = 0
+ SYNC = 1
+ OF = 2
+ LTS = 3
+ GTS1 = 4
+ GTS2 = 5
+ EXT = 6
+ INST = 7
+ HW = 8
+ DWT_EVTCNT = 16
+ DWT_EXCTRC = 17
+ DWT_PC_SAMPLE = 18
+ DWT_PC_VALUE = 19
+ DWT_ADDR_OFFSET = 20
+ DWT_DATA_VALUE = 21
+
+class LocalTimestampRelation:
+ SYNC = 0
+ TS = 1
+ SRC = 2
+ BOTH = 3
+
+class ExtensionSource:
+ ITM = 0
+ HW = 1
+
+class ExceptionTraceFunction:
+ RESERVED = 0
+ ENTER = 1
+ EXIT = 2
+ RETURN = 3
+
+class DecoderFlags:
+ NONE = 0
+ EOS = (1 << 0)
diff --git a/bindings/python/swopy.i b/bindings/python/swopy.i
index 9df39e4..eaa38be 100644
--- a/bindings/python/swopy.i
+++ b/bindings/python/swopy.i
@@ -57,43 +57,14 @@ static int swig_exception_code(int code)
}
%}
-%rename(LOG_LEVEL_NONE) libswo::LogLevel::NONE;
-%rename(LOG_LEVEL_ERROR) libswo::LogLevel::ERROR;
-%rename(LOG_LEVEL_WARNING) libswo::LogLevel::WARNING;
-%rename(LOG_LEVEL_INFO) libswo::LogLevel::INFO;
-%rename(LOG_LEVEL_DEBUG) libswo::LogLevel::DEBUG;
-
-%rename(PACKET_TYPE_UNKNOWN) libswo::PacketType::UNKNOWN;
-%rename(PACKET_TYPE_SYNC) libswo::PacketType::SYNC;
-%rename(PACKET_TYPE_OF) libswo::PacketType::OF;
-%rename(PACKET_TYPE_LTS) libswo::PacketType::LTS;
-%rename(PACKET_TYPE_GTS1) libswo::PacketType::GTS1;
-%rename(PACKET_TYPE_GTS2) libswo::PacketType::GTS2;
-%rename(PACKET_TYPE_EXT) libswo::PacketType::EXT;
-%rename(PACKET_TYPE_INST) libswo::PacketType::INST;
-%rename(PACKET_TYPE_HW) libswo::PacketType::HW;
-%rename(PACKET_TYPE_DWT_EVTCNT) libswo::PacketType::DWT_EVTCNT;
-%rename(PACKET_TYPE_DWT_EXCTRC) libswo::PacketType::DWT_EXCTRC;
-%rename(PACKET_TYPE_DWT_PC_SAMPLE) libswo::PacketType::DWT_PC_SAMPLE;
-%rename(PACKET_TYPE_DWT_PC_VALUE) libswo::PacketType::DWT_PC_VALUE;
-%rename(PACKET_TYPE_DWT_ADDR_OFFSET) libswo::PacketType::DWT_ADDR_OFFSET;
-%rename(PACKET_TYPE_DWT_DATA_VALUE) libswo::PacketType::DWT_DATA_VALUE;
-
-%rename(LTS_REL_SYNC) libswo::LocalTimestampRelation::SYNC;
-%rename(LTS_REL_TS) libswo::LocalTimestampRelation::TS;
-%rename(LTS_REL_SRC) libswo::LocalTimestampRelation::SRC;
-%rename(LTS_REL_BOTH) libswo::LocalTimestampRelation::BOTH;
-
-%rename(EXT_SRC_ITM) libswo::ExtensionSource::ITM;
-%rename(EXT_SRC_HW) libswo::ExtensionSource::HW;
-
-%rename(EXCTRC_FUNC_RESERVED) libswo::ExceptionTraceFunction::RESERVED;
-%rename(EXCTRC_FUNC_ENTER) libswo::ExceptionTraceFunction::ENTER;
-%rename(EXCTRC_FUNC_EXIT) libswo::ExceptionTraceFunction::EXIT;
-%rename(EXCTRC_FUNC_RETURN) libswo::ExceptionTraceFunction::RETURN;
-
-%rename(DF_NONE) libswo::DecoderFlags::NONE;
-%rename(DF_EOS) libswo::DecoderFlags::EOS;
+%ignore libswo::LogLevel;
+%ignore libswo::PacketType;
+%ignore libswo::LocalTimestampRelation;
+%ignore libswo::ExtensionSource;
+%ignore libswo::ExceptionTraceFunction;
+%ignore libswo::DecoderFlags;
+
+%pythoncode "enums.py"
%rename(__str__) libswo::Packet::to_string;
diff --git a/examples/decoder.py b/examples/decoder.py
index 9ae22bc..2416a2e 100644
--- a/examples/decoder.py
+++ b/examples/decoder.py
@@ -32,7 +32,7 @@ class Decoder:
def feed(self, data):
self._ctx.feed(data)
- def decode(self, flags=swopy.DF_NONE):
+ def decode(self, flags=swopy.DecoderFlags.NONE):
self._ctx.decode(flags)
def _handle_unknown_packet(self, packet):
@@ -52,13 +52,13 @@ class Decoder:
def _handle_lts_packet(self, packet):
relation = packet.get_relation()
- if relation == swopy.LTS_REL_SYNC:
+ if relation == swopy.LocalTimestampRelation.SYNC:
tc = 'synchronous'
- elif relation == swopy.LTS_REL_TS:
+ elif relation == swopy.LocalTimestampRelation.TS:
tc = 'timestamp delayed'
- elif relation == swopy.LTS_REL_SRC:
+ elif relation == swopy.LocalTimestampRelation.SRC:
tc = 'data delayed'
- elif relation == swopy.LTS_REL_BOTH:
+ elif relation == swopy.LocalTimestampRelation.BOTH:
tc = 'data and timestamp delayed'
else:
return
@@ -74,9 +74,9 @@ class Decoder:
print('Global timestamp (GTS2) (value = %x)' % packet.get_value())
def _handle_ext_packet(self, packet):
- if packet.get_source() == swopy.EXT_SRC_ITM:
+ if packet.get_source() == swopy.ExtensionSource.ITM:
src = 'ITM'
- elif packet.get_source() == swopy.EXT_SRC_HW:
+ elif packet.get_source() == swopy.ExtensionSource.HW:
src = 'HW'
else:
print('Extension packet with invalid source: %u.' %
@@ -101,11 +101,11 @@ class Decoder:
packet.get_cyc()));
def _handle_exctrc_packet(self, packet):
- if packet.get_function() == swopy.EXCTRC_FUNC_ENTER:
+ if packet.get_function() == swopy.ExceptionTraceFunction.ENTER:
func = 'enter'
- elif packet.get_function() == swopy.EXCTRC_FUNC_EXIT:
+ elif packet.get_function() == swopy.ExceptionTraceFunction.EXIT:
func = 'exit'
- elif packet.get_function() == swopy.EXCTRC_FUNC_RETURN:
+ elif packet.get_function() == swopy.ExceptionTraceFunction.RETURN:
func = 'return'
else:
func = 'reserved'
@@ -135,35 +135,35 @@ class Decoder:
def _packet_callback(self, packet):
packet_type = packet.get_type()
- if packet_type == swopy.PACKET_TYPE_UNKNOWN:
+ if packet_type == swopy.PacketType.UNKNOWN:
self._handle_unknown_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_SYNC:
+ elif packet_type == swopy.PacketType.SYNC:
self._handle_sync_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_OF:
+ elif packet_type == swopy.PacketType.OF:
self._handle_overflow_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_LTS:
+ elif packet_type == swopy.PacketType.LTS:
self._handle_lts_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_GTS1:
+ elif packet_type == swopy.PacketType.GTS1:
self._handle_gts1_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_GTS2:
+ elif packet_type == swopy.PacketType.GTS2:
self._handle_gts2_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_EXT:
+ elif packet_type == swopy.PacketType.EXT:
self._handle_ext_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_INST:
+ elif packet_type == swopy.PacketType.INST:
self._handle_inst_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_HW:
+ elif packet_type == swopy.PacketType.HW:
self._handle_hw_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_DWT_EVTCNT:
+ elif packet_type == swopy.PacketType.DWT_EVTCNT:
self._handle_evtcnt_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_DWT_EXCTRC:
+ elif packet_type == swopy.PacketType.DWT_EXCTRC:
self._handle_exctrc_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_DWT_PC_SAMPLE:
+ elif packet_type == swopy.PacketType.DWT_PC_SAMPLE:
self._handle_pc_sample_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_DWT_PC_VALUE:
+ elif packet_type == swopy.PacketType.DWT_PC_VALUE:
self._handle_pc_value_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_DWT_ADDR_OFFSET:
+ elif packet_type == swopy.PacketType.DWT_ADDR_OFFSET:
self._handle_addr_offset_packet(packet)
- elif packet_type == swopy.PACKET_TYPE_DWT_DATA_VALUE:
+ elif packet_type == swopy.PacketType.DWT_DATA_VALUE:
self._handle_data_value_packet(packet)
else:
print('Unknown packet type %u' % packet_type, file=sys.stderr)
@@ -196,7 +196,7 @@ if __name__ == '__main__':
input_file.close()
try:
- decoder.decode(swopy.DF_EOS)
+ decoder.decode(swopy.DecoderFlags.EOS)
except Exception as e:
print(e, file=sys.stderr)