简体   繁体   中英

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

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. I am using Ubuntu.

Thanks a lot.

That example is out of date, the Py namespace doesn't exist in recent versions.

Some distributions ship the examples (that should be kept up to date) with 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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