Ironpython: function works in CPython, cryptic null pointer exception in IronPython

I am trying to do something that seems very simple, and falls into the range of standard python. The following function takes a set of sets and returns all elements that are contained in two or more sets.

To do this, while the set of sets is not empty, it simply pops one set from the collection, intersects it with the rest of the sets, and updates the set of elements that fall into one of these intersections.

def cross_intersections(sets): in_two = set() sets_copy = copy(sets) while sets_copy: comp = sets_copy.pop() for each in sets_copy: new = comp & each print new, # Print statements to show that these references exist print in_two in_two |= new #This is where the error occurs in IronPython return in_two 

Above is the function I use. In order to test it, the following steps are performed in CPython:

 >>> a = set([1,2,3,4]) >>> b = set([3,4,5,6]) >>> c = set([2,4,6,8]) >>> cross = cross_intersections([a,b,c]) set([2, 4]) set([]) set([4, 6]) set([2, 4]) set([3, 4]) set([2, 4, 6]) >>> cross set([2, 3, 4, 6]) 

However, when I try to use IronPython:

 >>> b = cross_intersections([a,b,c]) set([2, 4]) set([]) set([4, 6]) set([2, 4]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:/path/to/code.py", line 10, in cross_intersections SystemError: Object reference not set to an instance of an object. 

In the title, I said that it was a cryptic null pointer exception. I probably don't know how .NET handles null pointers (I have never worked with a C-like language and only used IronPython for a month or so), but if my understanding is correct, this happens when you try to access some property of an object that points to null .

In this case, an error occurs on line 10 of my function: in_two |= new . However, I put print statements right in front of this line, which (at least for me) indicate that none of these objects point to null .

Where am I going wrong?

+4
source share
2 answers

This is a mistake . It will be fixed in 2.7.1, but I don’t think the fix is ​​in version 2.7.1 Beta 1.

+3
source

This is a bug that is still present in version 2.7.1 Beta 1.

It has been fixed in master , and the fix will be included in the next version.

 IronPython 3.0 (3.0.0.0) on .NET 4.0.30319.235 Type "help", "copyright", "credits" or "license" for more information. >>> import copy >>> >>> def cross_intersections(sets): ... in_two = set() ... sets_copy = copy.copy(sets) ... while sets_copy: ... comp = sets_copy.pop() ... for each in sets_copy: ... new = comp & each ... print new, # Print statements to show that these references exist ... print in_two ... in_two |= new # This is where the error occurs in IronPython ... return in_two ... >>> >>> a = set([1,2,3,4]) >>> b = set([3,4,5,6]) >>> c = set([2,4,6,8]) >>> >>> cross = cross_intersections([a,b,c]) set([2, 4]) set([]) set([4, 6]) set([2, 4]) set([3, 4]) set([2, 4, 6]) 
+1
source

All Articles