Python Forum
Am I running this correctly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Am I running this correctly?
#1
I have a few qeustions.

I am running Python3.6

Working on this exmaple:
https://docs.python.org/3.6/extending/em...ded-python

What exactly do they mean by extending the Python Interpreter?

Can I call the import statement anyother way besides using PyRun_SimpleString, as shown below in the int main() function?

import emb 

print('Number of arguments', emb.numargs())
#include </usr/include/python3.6m/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;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

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

int main(int argc, char *argv[]){
  
  numargs = argc;
  PyImport_AppendInittab("emb", &PyInit_emb);
  Py_Initialize();
  PyRun_SimpleString("import emb");
  PyRun_SimpleString("print('Number of arguments', emb.numargs())");
  Py_Finalize();

  return 0;

}
Please advise
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data manipulation code running but not functioning correctly homotextual 1 2,128 Dec-30-2018, 03:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020