简体   繁体   English

为什么此外部“ C”功能在使用python ctypes时不起作用?

[英]Why does this extern “C” function not work using python ctypes?

I have the following c++ function that sets an integer using a string. 我有以下使用字符串设置整数的c ++函数。

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

extern "C" {
  int a() {
    int number;
    string value("100");
    std::istringstream strm(value);
    strm >> number;
    if (strm.fail()) {
      cout << "Ouch!" << endl;
    }
    else {
      cout << "Number set to:" << number << endl;
    };
    return (int)strm.bad();
  }
}

int main(int argc, char **argv)
{
  a();
}

If I compile this as a program it works. 如果我将其作为程序进行编译,它将可以正常工作。

$ g++ ./streamtest.cc -o streamtest;./streamtest
Number set to:100

But if I call the same function from ctypes it does not set the integer and the "strm" is left in a "bad" state. 但是,如果我从ctypes调用相同的函数,它不会设置整数,并且“ strm”处于“坏”状态。

$ g++ -shared streamtest.cc  -o libstreamtest.so
$ python -c "import ctypes;a = ctypes.CDLL('libstreamtest.so').a();print 'Got [%s] from a()' %a"
Ouch!
Got [1] from a()

This got me puzzled. 这让我感到困惑。 How can I make this function work under ctypes? 如何使此函数在ctypes下工作?

It works for me on Windows 7 (x64) using x86 build. 它在Windows 7(x64)上使用x86构建对我有效。 Have you tried wrapping the code with C for use as module with Python? 您是否尝试过用C封装代码以用作Python模块? Maybe that does work for you.. 也许对您有用。

C:\Users\niklas\Desktop>g++ -o streamtest.pyd -shared -I"C:\Python27\include" -L"C:\Python27\libs" streamtestmodule.cpp -lpython27


C:\Users\niklas\Desktop>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import streamtest
>>> streamtest.a()
Number set to:100
0

#include <Python.h>
#include "streamtest.cpp"

extern "C" {

static PyObject* streamtest_a(PyObject* self) {
    PyObject* re = Py_BuildValue("i", a());
    return re;
}

static PyMethodDef StreamtestMethods[] = {
    {"a", (PyCFunction) streamtest_a, METH_NOARGS, NULL},
    {NULL, NULL, 0, NULL}
};


void initstreamtest(void) {
    PyObject* module = Py_InitModule("streamtest", StreamtestMethods);
    if (module == NULL) {
        cout << "Module initialization failed.";
    }
}

} // extern "C"

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

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