/usr/share/polymake/resources/JuPyMake/JuPyMake.cpp is in polymake-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #include <Python.h>
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
#include <polymake/Main.h>
#include <polymake/Vector.h>
/*
* Python different version stuff
*/
#if PY_MAJOR_VERSION >= 3
#define to_python_string(o) PyUnicode_FromString(o)
#else
#define to_python_string(o) PyString_FromString(const_cast<char*>(o))
#endif
#if PY_MAJOR_VERSION >= 3
#define char_to_python_string(o) PyUnicode_FromString(std::string(1,o).c_str())
#else
#define char_to_python_string(o) PyString_FromString(std::string(1,o).c_str())
#endif
polymake::Main* main_polymake_session;
PyObject* JuPyMakeError;
static PyObject * ToPyBool( bool input )
{
if(input){
return Py_True;
}
return Py_False;
}
/*
* Python functions
*/
static PyObject * ExecuteCommand( PyObject* self, PyObject* args )
{
const char * input_string;
if (! PyArg_ParseTuple(args, "s", &input_string) )
return NULL;
std::string polymake_input(input_string);
bool parsed;
std::string stdout;
std::string stderr;
std::string error;
try{
std::tie(parsed,stdout,stderr,error) = main_polymake_session->shell_execute(polymake_input);
}catch(const std::exception& e ){
PyErr_SetString( JuPyMakeError, e.what() );
return NULL;
}
return PyTuple_Pack( 4, ToPyBool( parsed ), to_python_string( stdout.c_str() ), to_python_string( stderr.c_str() ), to_python_string( error.c_str() ) );
}
static PyObject * GetCompletion( PyObject* self, PyObject* args )
{
const char* input_string;
if (! PyArg_ParseTuple(args, "s", &input_string) )
return NULL;
std::string polymake_input(input_string);
std::vector<std::string> completions;
int completion_offset;
char additional_character;
try{
std::tie(completion_offset,additional_character, completions) = main_polymake_session->shell_complete(polymake_input);
}catch(const std::exception& e ){
PyErr_SetString( JuPyMakeError, e.what() );
return NULL;
}
int completions_length = completions.size();
PyObject* return_list = PyList_New( completions_length );
for(int i=0;i<completions_length;i++){
PyList_SetItem( return_list, i, to_python_string( completions[ i ].c_str() ) );
}
return PyTuple_Pack( 3, PyLong_FromLong( completion_offset ), char_to_python_string(additional_character), return_list );
}
static PyObject * GetContextHelp( PyObject* self, PyObject* args, PyObject* kwargs )
{
const char* input_string;
int position = -1;
int full=false;
int html=false;
static char* kwlist[] = { "input", "position", "full", "html", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist, &input_string, &position, &full, &html ) )
return NULL;
std::string polymake_input(input_string);
if(position == -1){
position = static_cast<int>(std::string::npos);
}
std::vector<std::string> results;
try{
results = main_polymake_session->shell_context_help(polymake_input,position,static_cast<bool>(full),static_cast<bool>(html));
}catch(const std::exception& e ){
PyErr_SetString( JuPyMakeError, e.what() );
return NULL;
}
int results_length = results.size();
PyObject* return_list = PyList_New( results_length );
for(int i=0;i<results_length;i++){
PyList_SetItem( return_list, i, to_python_string( results[ i ].c_str() ) );
}
return return_list;
}
static PyObject * InitializePolymake( PyObject* self )
{
try{
main_polymake_session = new polymake::Main;
main_polymake_session->shell_enable();
main_polymake_session->set_application("polytope");
}catch(const std::exception& e){
PyErr_SetString( JuPyMakeError, e.what() );
return NULL;
}
return Py_True;
}
/*
* Python mixed init stuff
*/
struct module_state {
PyObject *error;
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
static PyObject * error_out(PyObject *m) {
struct module_state *st = GETSTATE(m);
PyErr_SetString(st->error, "something bad happened");
return NULL;
}
static PyMethodDef JuPyMakeMethods[] = {
{"ExecuteCommand",(PyCFunction)ExecuteCommand, METH_VARARGS,
"Runs a polymake command"},
{"GetCompletion",(PyCFunction)GetCompletion, METH_VARARGS,
"Get tab completions of string"},
{"GetContextHelp",(PyCFunction)GetContextHelp,METH_VARARGS | METH_KEYWORDS,
"Get context help of string"},
{"InitializePolymake",(PyCFunction)InitializePolymake,METH_NOARGS,
"Initialize the polymake session"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static int JuPyMake_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int JuPyMake_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"JuPyMake",
NULL,
sizeof(struct module_state),
JuPyMakeMethods,
NULL,
JuPyMake_traverse,
JuPyMake_clear,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC PyInit_JuPyMake(void)
#else
#define INITERROR return
extern "C" void initJuPyMake(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("JuPyMake", JuPyMakeMethods);
#endif
if (module == NULL)
INITERROR;
struct module_state *st = GETSTATE(module);
JuPyMakeError = PyErr_NewException(const_cast<char*>("JuPyMake.PolymakeError"), NULL, NULL );
Py_INCREF( JuPyMakeError );
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
|