简体   繁体   English

在 python 中使用 c 代码时出错

[英]Error while using the c code in python

I am using the following code which I have found online我正在使用我在网上找到的以下代码

def c_int_binary_search(seq,t):
    # do a little type checking in Python
    assert(type(t) == type(1))
    assert(type(seq) == type([]))

    # now the C code
    code = """
       #line 29 "binary_search.py"
       int val, m, min = 0;
       int max = seq.length() - 1;
       PyObject *py_val;
       for(;;)
       {
           if (max < min  )
           {
               return_val =  Py::new_reference_to(Py::Int(-1));
               break;
           }
           m =  (min + max) /2;
           val = py_to_int(PyList_GetItem(seq.ptr(),m),"val");
           if (val  < t)
               min = m  + 1;
           else if (val >  t)
               max = m - 1;
           else
           {
               return_val = Py::new_reference_to(Py::Int(m));
               break;
           }
       }
       """
    return inline(code,['seq','t'])

from the documentation of scipy来自scipy 的文档

When I try to execute this script then i have the following errors当我尝试执行此脚本时,出现以下错误

  binary_search.py: In function ‘PyObject* compiled_func(PyObject*, PyObject*)’:
  binary_search.py:36:38: error: ‘Py’ has not been declared

I am wondering if someone can guide me in this.我想知道是否有人可以指导我。 I have already installed PyCXX.我已经安装了 PyCXX。 I am using Ubuntu.我正在使用 Ubuntu。

Thanks a lot.非常感谢。

That example is out of date, the Py namespace doesn't exist in recent versions.该示例已过时,最新版本中不存在Py命名空间。

Some distributions ship the examples (that should be kept up to date) with scipy .某些发行版随scipy一起提供示例(应保持最新)。 On my machine, there's this:在我的机器上,有这样的:

/usr/lib64/python2.7/site-packages/scipy/weave/examples/binary_search.py

If you don't have something like that, you can download it from SciPy repository .如果您没有类似的东西,您可以从SciPy 存储库下载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM