Ironpython:函数在CPython中起作用,IronPython中的神秘空指针异常 [英] Ironpython: Function works in CPython, mysterious null pointer exception in IronPython

查看:131
本文介绍了Ironpython:函数在CPython中起作用,IronPython中的神秘空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做看起来很简单的事情,并且属于标准python范围之内。以下函数获取集合的集合,并返回两个或多个集合中包含的所有项目。

I'm trying to do something that seems very simple, and falls within the range of standard python. The following function takes a collection of sets, and returns all of the items that are contained in two or more sets.

要执行此操作,而集合的集合不是为空,它只是从集合中弹出一个集合,与其余集合相交,并更新属于这些交集之一的一组项目。

To do this, while the collection of sets is not empty, it simply pops one set out of the collection, intersects it with the remaining sets, and updates a set of items that fall in 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

以上是我正在使用的功能。为了对其进行测试,在CPython中,可以使用以下功能:

Above is the function I'm using. To test it, in CPython, the following works:

>>> 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])

但是,当我尝试使用IronPython:

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.

标题中,我说这是一个神秘的空指针异常。我可能不知道.NET如何处理空指针(我从未使用过类似C的语言,并且仅使用IronPython一个月左右),但是如果我的理解是正确的,那么当您尝试访问指向 null 的对象的某些属性。

In the title I said this was a mysterious null pointer exception. I probably have no idea how .NET handles null pointers (I've never worked with a C-like language, and have only been using IronPython for a month or so), but if my understanding is correct, it occurs when you attempt to access some property of an object that points to null.

在这种情况下,错误发生在第10行我的功能: in_two | = new 。但是,我已经在这行之前放置了 print 语句,(至少对我来说)表明这两个对象都不指向 null

In this case, the error occurs at line 10 of my function: in_two |= new. However, I've put print statements right before this line that (at least to me) indicate that neither of these objects point to null.

我要去哪里了?

推荐答案

这是一个错误。该问题将在2.7.1中修复,但我认为该修复不存在于2.7.1 Beta 1版本中。

It's a bug. It will be fixed in 2.7.1, but I don't think the fix is in the 2.7.1 Beta 1 release.

这篇关于Ironpython:函数在CPython中起作用,IronPython中的神秘空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆