尝试在 Python 内使用 C++ class 时出错 3
Error trying to use a C++ class inside Python 3
我正在尝试使用我在 Python.
中在 C++ 上声明和实现的 class
即使我在 Python 包装器中成功声明了我的 class,当我尝试使用 class 的函数时,甚至当我尝试创建此 class 的实例时我得到:
[andre@atlantis mcasta]$ python TimedInputWrapper.py
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
这是我的 C++ 文件 (input_timeout.cpp):
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <string>
class TimedInput {
std::condition_variable cv;
std::string prompt, input_string;
long int li_time_limit;
std::chrono::seconds time_limit;
public:
TimedInput(std::string prompt_str, long int time) : prompt{prompt_str}, li_time_limit{time} { }
void read_string() {
std::cin >> input_string;
cv.notify_one();
}
std::string return_input() {
std::cout << "Time limit for input = " << li_time_limit << " seconds!\n" << prompt << "\n";
std::thread th(&TimedInput::read_string, this);
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
time_limit = (std::chrono::seconds) li_time_limit;
while ((cv.wait_for(lck, time_limit) != std::cv_status::timeout) and (input_string.empty())) { }
th.detach();
return input_string;
}
};
extern "C" {
TimedInput* TimedInput_new(std::string prompt, long int time_limit) { return new TimedInput(prompt, time_limit); }
void TimedInput_read_string(TimedInput* timed_input) { timed_input->read_string(); }
std::string TimedInput_return_input(TimedInput* timed_input) { timed_input->return_input(); }
}
我使用这些命令创建了一个共享库:
g++ -c -fPIC -pthread input_timeout.cpp -o input_timeout.o
g++ -shared -Wl,-soname,libTimedInput.so -o libTimedInput.so input_timeout.o
这是我的 Python Wrapper/test(TimedInputWrapper.py):
import ctypes
lib = ctypes.cdll.LoadLibrary('./libTimedInput.so')
class TimedInput(object):
def __init__(self, prompt, time_limit):
self.prompt = prompt
self.time_limit = time_limit
self.obj = lib.TimedInput_new(self.prompt, self.time_limit)
def read_string(self):
lib.TimedInput_read_string(self.obj)
def return_input(self):
lib.TimedInput_return_input(self.obj)
prompt = ctypes.c_wchar_p("What's your name?")
time_limit = ctypes.c_long(10)
TimedInput(prompt, time_limit).return_input()
Python ctypes
不适用于 C++ 类型,例如 std::string
。但是,ctypes
确实适用于 C 语言类型,例如 const char*
.
请参阅 Python ctypes
文档中的 Fundamental Data Types。
我正在尝试使用我在 Python.
中在 C++ 上声明和实现的 class
即使我在 Python 包装器中成功声明了我的 class,当我尝试使用 class 的函数时,甚至当我尝试创建此 class 的实例时我得到:
[andre@atlantis mcasta]$ python TimedInputWrapper.py
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
这是我的 C++ 文件 (input_timeout.cpp):
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <string>
class TimedInput {
std::condition_variable cv;
std::string prompt, input_string;
long int li_time_limit;
std::chrono::seconds time_limit;
public:
TimedInput(std::string prompt_str, long int time) : prompt{prompt_str}, li_time_limit{time} { }
void read_string() {
std::cin >> input_string;
cv.notify_one();
}
std::string return_input() {
std::cout << "Time limit for input = " << li_time_limit << " seconds!\n" << prompt << "\n";
std::thread th(&TimedInput::read_string, this);
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
time_limit = (std::chrono::seconds) li_time_limit;
while ((cv.wait_for(lck, time_limit) != std::cv_status::timeout) and (input_string.empty())) { }
th.detach();
return input_string;
}
};
extern "C" {
TimedInput* TimedInput_new(std::string prompt, long int time_limit) { return new TimedInput(prompt, time_limit); }
void TimedInput_read_string(TimedInput* timed_input) { timed_input->read_string(); }
std::string TimedInput_return_input(TimedInput* timed_input) { timed_input->return_input(); }
}
我使用这些命令创建了一个共享库:
g++ -c -fPIC -pthread input_timeout.cpp -o input_timeout.o
g++ -shared -Wl,-soname,libTimedInput.so -o libTimedInput.so input_timeout.o
这是我的 Python Wrapper/test(TimedInputWrapper.py):
import ctypes
lib = ctypes.cdll.LoadLibrary('./libTimedInput.so')
class TimedInput(object):
def __init__(self, prompt, time_limit):
self.prompt = prompt
self.time_limit = time_limit
self.obj = lib.TimedInput_new(self.prompt, self.time_limit)
def read_string(self):
lib.TimedInput_read_string(self.obj)
def return_input(self):
lib.TimedInput_return_input(self.obj)
prompt = ctypes.c_wchar_p("What's your name?")
time_limit = ctypes.c_long(10)
TimedInput(prompt, time_limit).return_input()
Python ctypes
不适用于 C++ 类型,例如 std::string
。但是,ctypes
确实适用于 C 语言类型,例如 const char*
.
请参阅 Python ctypes
文档中的 Fundamental Data Types。