Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'binary': 0.05; 'tool,': 0.07; 'blue': 0.09; 'parsed': 0.09; 'specifying': 0.09; 'typedef': 0.09; 'xml.': 0.09; 'index': 0.13; '"list': 0.16; '(thank': 0.16; 'arrays.': 0.16; 'fly': 0.16; 'mess,': 0.16; 'received:87.54': 0.16; 'sorting': 0.16; 'unusable.': 0.16; 'string': 0.17; 'byte': 0.18; 'debugging': 0.18; '(see': 0.20; 'arrays': 0.22; 'struct': 0.22; 'attached.': 0.23; 'this:': 0.23; 'header': 0.24; "i've": 0.25; 'mostly': 0.27; 'updating': 0.27; 'data,': 0.27; 'projects,': 0.27; 'actual': 0.28; 'dumps': 0.29; 'received:dk': 0.29; 'structure,': 0.29; 'array': 0.29; 'random': 0.29; 'there.': 0.30; "i'm": 0.30; 'code': 0.30; "can't": 0.32; 'especially': 0.32; 'getting': 0.33; 'done,': 0.33; 'flags': 0.33; 'limitations': 0.33; 'values.': 0.33; 'definition': 0.34; 'structure': 0.34; 'file': 0.34; 'except': 0.34; 'next': 0.35; 'involving': 0.35; 'something': 0.35; 'should': 0.36; 'project': 0.36; 'data.': 0.36; 'form,': 0.36; 'to:addr:python-list': 0.36; 'display': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'things': 0.38; 'stuff': 0.38; 'end': 0.39; 'data': 0.39; 'format': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'waiting': 0.60; 'avoid': 0.61; 'real': 0.62; 'information': 0.63; 'due': 0.65; 'six': 0.65; 'plan,': 0.66; 'member,': 0.67; 'day': 0.67; 'shop': 0.70; 'prime': 0.72; 'funny': 0.83; 'luggage,': 0.84; 'pain': 0.84; 'sometimes.': 0.84; 'subject:intelligent': 0.84; 'barcode': 0.91; 'norwegian': 0.91; 'light.': 0.93 X-Virus-Scanned: Debian amavisd-new at rollo.jernurt.dk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wegge.dk; s=mail; t=1437168621; bh=s9dVG5pay7g5LvdozaYNfXs2FUi1vrWlTyVayxpnZCw=; h=Date:From:To:Subject:From; b=GXvDIEIxi1idlk69k6uxkKQ3HoJ6WPF7Bm8iNJHTlLtJpnKV3WqVAGyBv/W7QZBdU 3wn/yyoRd9xdSZbxZqkZxQOCpgG6/IH7dv7DhEvMtE3RNmdl8QJnmHFa09J8QQF2P3 U45ArkPNHHk3CdBPPtXUr0dlB7gy4O3XI2FoN3Xs= Date: Fri, 17 Jul 2015 23:30:19 +0200 From: Anders Wegge Keller To: python-list@python.org Subject: Parsing and displaying C structs in a semi-intelligent way. Organization: Disorganized X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.28; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437169020 news.xs4all.nl 2940 [2001:888:2000:d::a6]:49549 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94034 In my day job, we have a large code base of mostly identical projects, each with their own customizations. The customizations can be a real pain sometimes. Especially when debugging binary data. The interesting part of the binary dumps are most often the stuff that are tweaked from project to project. The prime suspect is a structure like this: typedef struct { byte next ; /* Index to next wedge */ byte flags ; /* ricd-control flags */ word alt [MAX_alt] ; /* Alternative wedges */ ... } ConvResult ; typedef struct { byte state ; /* UCART_ (see cart.h) */ word headcart ; /* Cart number for header cart */ ConvResult conv ; ... } cartUTable_t ; The actual structure with substructures are much larger. This structure contains all the information used when sorting stuff[1]. For historical reasons, the data is dumped to a binary file at the end of the items life-cycle. We do have a debugging tool, that reads an in-house file format specifying the members of the structure, and how to display them. However, updating this description is error-prone, and just about as funny as waiting for the Norwegian Blue to fly out over the fiords. So most often it's either not done, or is unusable. To clean up this mess, I've devised a cunning plan, involving a C-parser, that can write a structure definition as XML. Reading this is simple enough, and going from that to a format string for struct.unpack() is mostly trivial. Except for the arrays of some C-type, that happens to be something else than an array of char. Due to the normal use case[2] of dumping this binary data, I need to get the parsed data munged into a form, that can be put into a namedtuple as single member, even when I have an array of (typical) six conv.alt values. I'm getting stuck with the limitations of struct and namedtuple. While inheriting either of them to get the job done, seem to be feasible, I still feel that I somehow should be able to do something "list comprehension"-ish, to avoid going there. I just can't see the light. (Thank you for reading all the way to here!) 1. Luggage, parcels, shop replenishments ... In short: mostly rectangular things that has a barcode attached. 2. Random ordering, not displaying all members, and only a part of the arrays. -- //Wegge