#!/usr/bin/env python2 # -*- coding: utf-8 -*- from gnuradio import gr, blocks import pmt import numpy as np from time import sleep ''' This is a demo of how accessing PMT variables from python can easily lead to segmentation faults. ''' class minimal_error_block(gr.sync_block): def __init__(self): gr.sync_block.__init__(self, name='error_showcase_block', in_sig=[(np.complex64, 1)], out_sig=None) self.message_port = pmt.intern('message_in') self.message_port_register_in(self.message_port) self.messages = [] self.tags = [] self.set_msg_handler(self.message_port, self.process_message) def process_message(self, message): self.messages.append(message) # Workaround to fix segfault: # self.messages.append(pmt.to_python(message)) def work(self, input_items, output_items): # Reading PMTs we're not supposed to read happens easily in two ways: # - saving tag keys and reading them later # - saving messages and reading them later offs = self.nitems_read(0)+1 key = self.get_tags_in_range(0, offs, offs+1)[0].key self.tags.append(key) # Workaround to fix segfault: # self.tags.append(pmt.to_python(key)) # May fail the second time this runs print self.tags # May even fail the first time this runs print self.messages return len(input_items[0]) class top_block(gr.top_block): def __init__(self): gr.top_block.__init__(self, "Top Block") self.message_strobe = blocks.message_strobe(pmt.intern("TEST"), 1) self.tag_strobe = blocks.tags_strobe(gr.sizeof_gr_complex*1, pmt.intern("TEST"), 1, pmt.intern("strobe")) self.min_err = minimal_error_block() self.throttle = blocks.throttle(gr.sizeof_gr_complex*1, 10e3,True) self.msg_connect((self.message_strobe, 'strobe'), (self.min_err, 'message_in')) self.connect((self.tag_strobe, 0), self.throttle, (self.min_err, 0)) tb = top_block() tb.start() sleep(5) tb.stop() tb.wait()