Browse Source

repl: import python module

master
Aqua-sama 5 months ago
parent
commit
078349c43f
Signed by: Aqua-sama <aqua@iserlohn-fortress.net> GPG Key ID: 5378B8349C1D5ADA
1 changed files with 43 additions and 2 deletions
  1. 43
    2
      src/cli/cli.cpp

+ 43
- 2
src/cli/cli.cpp View File

@@ -1,9 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <linenoise.h>
#include <Python.h>

static int numargs=0;

/* Return the number of arguments of the application command line */
static PyObject* emb_numargs(PyObject *self, PyObject *args)
{
if(!PyArg_ParseTuple(args, ":numargs"))
return NULL;

numargs = 19;
return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
/* ml_name Name of the method
* ml_meth pointer to C implementation
* ml_flags flag bits indicating how it should be called
* ml_doc docstring
*/
{"numargs", emb_numargs, METH_VARARGS, "Return the number of arguments received by the process."},
{NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
/* m_base */ PyModuleDef_HEAD_INIT, // base module, always HEAD_INIT
/* m_name */ "emb", // module name
/* m_doc */ NULL, // Docstring for the module; usually a docstring variable created with PyDoc_STRVAR()
/* m_size */ -1, //
/* m_methods */ EmbMethods, // A pointer to a table of module-level functions
/* m_slots */ NULL, // An array of slot definitions for multi-phase initialization
/* traverse */ NULL, // A traversal function to call during GC traversal of the module object
/* clear */ NULL, // A clear function to call during GC clearing of the module object
/* free */ NULL // A function to call during deallocation of the module object
};

static PyObject* PyInit_emb(void)
{
return PyModule_Create(&EmbModule);
}

int main(int argc, char** argv)
{
printf("cli test application\n");
@@ -17,6 +55,7 @@ int main(int argc, char** argv)
// inform the interpreter about paths to run-time libraries
Py_SetProgramName(program); /* optional but recommended */

printf("import emb: %i\n", PyImport_AppendInittab("emb", &PyInit_emb));

// Initialize the python interpreter
Py_Initialize();
@@ -29,13 +68,14 @@ int main(int argc, char** argv)
char *cmd = linenoise(prompt);

if(cmd == nullptr || *cmd == '\0') {
printf("break\n");
printf("breaking out of repl\n");
free(cmd);
break;
}

//printf("echo(%i):'%s'\n", strlen(cmd), cmd);
PyRun_SimpleString(cmd);
linenoiseHistoryAdd(cmd);
free(cmd);
}

@@ -49,3 +89,4 @@ int main(int argc, char** argv)

return 0;
}


Loading…
Cancel
Save