pocketpy/3rd/numpy/tests/test_numpy.cpp
Anurag Bhat 86b4fc623c
Merge numpy to pocketpy (#303)
* Merge numpy to pocketpy

* Add CI

* Fix CI
2024-09-02 16:22:41 +08:00

44 lines
1.2 KiB
C++

#include <pybind11/embed.h>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
namespace py = pybind11;
int main() {
try {
// Initialize the Python interpreter
py::scoped_interpreter guard{};
// Path to the script
const std::string script_path = "./test_numpy.py";
// Open the script file
std::ifstream file(script_path);
if (!file.is_open()) {
std::cerr << "Could not open " << script_path << " script file." << std::endl;
return 1;
}
// Read and execute the script into a string
std::stringstream buffer;
buffer << file.rdbuf();
std::string script = buffer.str();
py::exec(script);
std::cout << "Numpy script executed successfully." << std::endl;
}
catch (const py::error_already_set& e) {
// Catch and print Python exceptions
std::cerr << "Python error: " << e.what() << std::endl;
return 1;
}
catch (const std::exception& e) {
// Catch and print other C++ exceptions
std::cerr << "C++ exception: " << e.what() << std::endl;
return 1;
}
return 0;
}