Skip to main content
 首页 » 编程设计

returned a result with an error set"" rel="bookmark">python之Python 中的 "SystemError: class ' int '> returned a result with an error set"

2025年05月04日19linjiqin

我想使用 scipy 中的 ndimage.generic_filter() 应用一个非常简单的函数。这是代码:

import numpy as np 
import scipy.ndimage as ndimage 
 
data = np.random.rand(400,128) 
dimx = int(np.sqrt(np.size(data,0))) 
dimy = dimx     
coord = np.random.randint(np.size(data,0), size=(dimx,dimy)) 
 
def test_func(values): 
    idx_center = int(values[4]) 
    weight_center = data[idx_center] 
    weights_around = data[values] 
    differences = weights_around - weight_center 
    distances = np.linalg.norm(differences, axis=1) 
    return np.max(distances) 
 
results = ndimage.generic_filter(coord, 
                                 test_func, 
                                 footprint = np.ones((3,3))) 

当我执行它时,出现以下错误:

SystemError: <class 'int'> returned a result with an error set 

当试图将 values[4] 强制转换为 int 时。如果我运行函数 test_func() 而不对随机数组 values 使用 ndimage.generic_filter(),函数工作正常。

为什么会出现这个错误?有没有办法让它发挥作用?

请您参考如下方法:

针对您的情况:

这一定是 Python 或 SciPy 中的错误。请在 https://bugs.python.org 提交错误和/或 https://www.scipy.org/bug-report.html .包括 Python 和 NumPy/SciPy 的版本号、您在此处拥有的完整代码以及整个回溯。

(另外,如果你能找到一种不需要使用随机性的方法来触发这个错误,他们可能会很感激。但如果你找不到这样的方法,那么请将其归档为-是。)

一般来说:

“[R]eturned a result with an error set”是只能在 C 级完成的事情。 通常,Python/C API 期望大多数 C 函数执行以下两项操作之一:

  1. 使用 these functions 之一设置异常并返回NULL(对应抛出异常)。
  2. 不要设置异常并返回一个“真实”值,通常是一个PyObject*(对应于返回一个值,包括returning None)。

这两种情况通常是不正确的:

  1. 设置一个异常(或未能清除已存在的异常),但随后返回一些 NULL 以外的值。
  2. 不设置异常,然后返回NULL。

Python 引发了 SystemError,因为 Python 标准库中 int 的实现尝试执行 (3),这可能是 SciPy 先执行的结果.这总是错误的,因此 Python 或它调用的 SciPy 代码中一定存在错误。