CGAL / cgal-swig-bindings

CGAL bindings using SWIG
Other
351 stars 93 forks source link

Can not unpack a Triangulation_2::Edge => interpreted as infinite iterable #48

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. get `edge`, an instance of 
CGAL.CGAL_Mesh_2.Mesh_2_Constrained_Delaunay_triangulation_plus_2_Edge 
2. try to unpack `edge` : fh, i = edge

What is the expected output? What do you see instead?

One would expect the same thing as given by:

  fh = edge[0]
  i = edge[1]

Instead we get a exception : "ValueError: too many values to unpack"

What version of the product are you using? On what operating system?

I'm using commit 29ca15dbd820e190cc7e9736d468aa1fbe657f51 on linux debian 
wheezy with CGAL 4.4

Please provide any additional information below.

Some investigation with the python debugger leads to the swig generated python 
file `cgal-bindings/build-python/CGAL/CGAL_Mesh_2.py` :

* the __len__ method is defined to return 2
* the __getitem__ and __setitem__ methods are defined to accept any index and 
interpreting it modulo 2

The point is that an Edge instance, in the context of the iterator protocol is 
seen as an infinite sequence :

  t = tuple(edge) loops for ever

Cf. 
https://docs.python.org/2/reference/datamodel.html#emulating-container-types:

  Note for loops expect that an IndexError will be raised for illegal indexes to allow proper detection of the end of the sequence.

Please either implement a proper iterable protocol of fix the sequence protocol.

Original issue reported on code.google.com by anthony.truchet@gmail.com on 1 Jul 2014 at 3:18

GoogleCodeExporter commented 9 years ago
Here is a simple monkey-patch 

# Monkey patch Edge to work around issue
# http://code.google.com/p/cgal-bindings/issues/detail?id=48
Edge.__iter__= lambda this: iter((this[0], this[1]))

NB : This assumes you have done some import like

from CGAL.CGAL_Mesh_2 import 
Mesh_2_Constrained_Delaunay_triangulation_plus_2_Edge as Edge

Original comment by anthony.truchet@gmail.com on 1 Jul 2014 at 3:26