Last active
February 5, 2020 13:08
-
-
Save Foadsf/ac170ada8aeaec4a73842a5eba6e1179 to your computer and use it in GitHub Desktop.
testing command line arguments double pointer type casting for pybind11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8.12) | |
project (example) | |
find_package(pybind11 REQUIRED) | |
pybind11_add_module(${PROJECT_NAME} example.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pybind11/pybind11.h> | |
namespace py = pybind11; | |
#if PY_VERSION_HEX < 0x03000000 | |
#define MyPyText_AsString PyString_AsString | |
#else | |
#define MyPyText_AsString PyUnicode_AsUTF8 | |
#endif | |
void myFunc(int argc, char* argv[]) { | |
for (int i = 0; i < argc; ++i) { | |
printf("%s\n", argv[i]); | |
} | |
} | |
// int run(int argc, long* argv_) { | |
int run(py::object pyargs_) { | |
PyObject* pyargs = pyargs_.ptr(); | |
int argc = (int)PyList_Size(pyargs); | |
char** argv = (char**)malloc(argc * sizeof(char*)); | |
for (int i = 0; i < argc; ++i) { | |
//argv[i] = (char*)(argv_[i]); | |
argv[i] = (char*)MyPyText_AsString(PyList_GetItem(pyargs, (Py_ssize_t)i)); | |
} | |
myFunc(argc, argv); | |
free(argv); | |
return 0; | |
} | |
PYBIND11_MODULE(example, m) { | |
m.def("run", &run, "runs the example"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import example | |
import sys | |
# from typing import List, Tuple | |
# def tmpConvert(strList: List[str]) -> Tuple[int, List[int]]: | |
# return len(strList), [id(item) for item in strList] | |
# print(example.run(tmpConvert(sys.argv))) | |
print(example.run(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment