简体   繁体   中英

What wrong for I failing call Visual c++ class's method from python

My environment and Version:

    Windows 10,
    Microsoft Visual Studio Professional 2017:
            Visual Project: 'hello2' / Debug / x86,
    Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:04:37) [MSC v.1929 32 bit (Intel)],
    Boost 1.77.0:
            Boost Builds command:
            > b2 --build-type=complete address-model=32 --toolset=msvc-14.1  stage >build.log 2>&1
                    Remarks:
                    I also download and try Boost prebuild (binary) from:
                    https://sourceforge.net/projects/boost/files/boost-binaries/1.77.0/
                    but having same problem (as describe below) while I includes and link with this prebuild Boost.

The project's code copied from Boost Example/document: https://www.boost.org/doc/libs/1_77_0/libs/python/doc/html/tutorial/tutorial/exposing.html

Steps summary:

  1. Build Visual DLL project hello2 DLL includes: C++ class, Boost.Python wrapper

  2. Copied 'hello2.dll' to hello2.pyd

  3. python interpreter: import hello2 (actual hello2.pyd) as a python module

    (try to access the class method - but faile - see below) … ….

Detailing:

Visual Project: 'hello2', File: hello2.cpp:

#include "pch.h"
    
#include <boost/python.hpp>
using namespace boost::python;

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }

    std::string msg;
};
    
BOOST_PYTHON_MODULE(hello2)
{
    class_<World>("World")
    .def("set", &World::set)
    .def("greet", &World::greet)
    ;
}

On Windows Commands line:

C:\Users\haimh\AppData\Local\Programs\Python\OnGointHH\SlnForTestProjects\Debug> copy hello2.dll hello2.pyd 1 file(s) copied.

Try-1 (On Windows Commands line):

C:\Users\haimh\AppData\Local\Programs\Python\OnGointHH\SlnForTestProjects\Debug> py

Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:04:37) [MSC v.1929 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

>>>
>>> import hello2
>>> planet = hello2.World()

[Error message thrown in popup window: Mrcrosoft Visual C++ Runtime Library Assertion failed] [1]: https://i.stack.imgur.com/Fc1wo.jpg

Try-2 (On Windows Commands line):

C:\Users\haimh\AppData\Local\Programs\Python\OnGointHH\SlnForTestProjects\Debug> py

Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:04:37) [MSC v.1929 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

>>>
>>> import hello2
>>> planet = hello2.World
>>> planet.set('howdy')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    World.set(str)
did not match C++ signature:
    set(struct World {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
>>>

I'm not sure why your first try ( pl.net = hello2.World() ) didn't work. My best (not-so-good) guess is a version mismatch between the python headers and the python interpreter you're using. You can check this using the python version macros .

As for the second version - this doesn't work because set is not a static method. Only static methods can be called directly on the class World . Regular methods need to be called on instances of the class. Trying to do that in pure Python will get you a similar error:

>>> class Klass:
...     def f(self):
...         pass
...
>>> Klass.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'self'

In the C++ case, you're missing the argument struct World {lvalue} which is the equivalent of self.

I found - at least how to make settings create a working code/project...

The answer is: Change Visual C++ settings from 'Debug' to 'Release'... Then the program run fine without any problem.

Thanks to everyone who tried to help.

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