← Back to team overview

kicad-developers team mailing list archive

Re: PATCH: fix crash in netlist updater

 

I think I know what's up. You are probably on python 3.7 and my distro is
still on 3.6.

https://www.python.org/dev/peps/pep-0479/

That proposal changed how StopIteration is handled inside generators, they
are no longer handled internally. Change took effect in 3.7.

Please see new patch that should work on any python version.


On Tue, Jun 4, 2019 at 4:36 PM Andrew Lutsenko <anlutsenko@xxxxxxxxx> wrote:

> I just tried your code on linux, python3 and I don't get any exceptions:
>
> import pcbnew
> b = pcbnew.GetBoard()
> d = b.GetDrawings()
> d
> <pcbnew.DRAWINGS; proxy of <Swig Object of type 'std::deque< BOARD_ITEM *
> > *' at 0x7fe9769e6d80> >
> for a in d:
>     print(a)
>
> <pcbnew.TEXTE_PCB; proxy of <Swig Object of type 'TEXTE_PCB *' at
> 0x7fe9769e6de0> >
> <pcbnew.TEXTE_PCB; proxy of <Swig Object of type 'TEXTE_PCB *' at
> 0x7fe9769e6e40> >
> ...
>
> m = b.GetModules()
> for mod in m:
>     print(mod)
>
> <pcbnew.MODULE; proxy of <Swig Object of type 'MODULE *' at
> 0x7fe9769e6d50> >
> <pcbnew.MODULE; proxy of <Swig Object of type 'MODULE *' at
> 0x7fe9769e6d20> >
> ...
>
> If you look at generated pcbnew.py (and make sure it's installed in
> dist-packages) do you see new __iter__ method in DRAWINGS class?
>
>
> On Tue, Jun 4, 2019 at 4:27 PM Seth Hillbrand <seth@xxxxxxxxxxxxx> wrote:
>
>> On 2019-06-04 14:23, Andrew Lutsenko wrote:
>> > Seth,
>> >
>> > 1. No, it's not needed. At first I didn't find Cast() in BOARD_ITEMS
>> > cpp code so thought it's implemented in descendant classes only and
>> > added the check as safeguard like it was done in DList. But now I
>> > found that Cast() is in swig python extension for BOARD_ITEM so it
>> > will always be defined. See amended patch attached.
>> > 2. Yes, it works and it was tested on py3 build. Self reference is
>> > passed implicitly in python.
>>
>> Hi Andrew-
>>
>> Thanks for the clear details.
>>
>> I tested this (Linux/python3) but I'm getting the StopIteration raised
>> rather than handled by the standard python loops.  The example code I
>> used was
>>
>> import pcbnew
>> b = pcbnew.GetBoard()
>> d = b.GetDrawings()
>> for dwg in d:
>>      print(dwg)
>> m = b.GetModules()
>> for mod in m:
>>      print(mod)
>>
>> Here, the modules print normally and the StopIteration is handled
>> internally by python.  The drawings pass the StopIteration back up to
>> the UI.
>>
>> -Seth
>>
>
From 41b7aa07b2424f621c2ba6d62193e7be9b037642 Mon Sep 17 00:00:00 2001
From: qu1ck <anlutsenko@xxxxxxxxx>
Date: Mon, 3 Jun 2019 23:49:58 -0700
Subject: [PATCH] Pcbnew scripting fixes

* Remove infinite recursion calls in footprint.i
* Extend DRAWINGS deque iterator to auto cast contained BOARD_ITEMS,
similar to what dlist implementation did.
---
 pcbnew/swig/board.i     | 15 +++++++++++++++
 pcbnew/swig/footprint.i |  3 ---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/pcbnew/swig/board.i b/pcbnew/swig/board.i
index ca6d7b119..91c41cc90 100644
--- a/pcbnew/swig/board.i
+++ b/pcbnew/swig/board.i
@@ -106,6 +106,21 @@ HANDLE_EXCEPTIONS(BOARD::TracksInNetBetweenPoints)
 #include <class_board.h>
 %}
 
+%extend std::deque<BOARD_ITEM *>
+{
+    %pythoncode
+    %{
+        def __iter__(self):
+            it = self.iterator()
+            try:
+                while True:
+                    item = it.next()  # throws StopIteration when iterator reached the end.
+                    yield item.Cast()
+            except StopIteration:
+                return
+    %}
+}
+
 %extend BOARD
 {
     // BOARD_ITEM_CONTAINER's interface functions will be implemented by SWIG
diff --git a/pcbnew/swig/footprint.i b/pcbnew/swig/footprint.i
index 2c3108b17..15b5214aa 100644
--- a/pcbnew/swig/footprint.i
+++ b/pcbnew/swig/footprint.i
@@ -50,9 +50,6 @@
     %pythoncode
     %{
 
-    def Pads(self):            return self.Pads()
-    def GraphicalItems(self):  return self.GraphicalItems()
-
     #def SaveToLibrary(self,filename):
     #  return SaveModuleToLibrary(filename,self)
 
-- 
2.22.0.rc1.311.g5d7573a151-goog


Follow ups

References