Changeset 93302ef


Ignore:
Timestamp:
2013-03-18T21:36:08Z (12 years ago)
Author:
Nick Murdoch <nick@…>
Children:
26f3b5f
Parents:
e38c6d8
Message:

Turn bpython.c into an extension module with a dummy method from the Extending Python guide.

Location:
protocols/python
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • protocols/python/examples/sampleplugin.py

    re38c6d8 r93302ef  
    66
    77import sys
     8import bpython
    89print "hello, I'm a test plugin running on Python", sys.version_info
    910
    1011# plugin name:
    1112name = 'moose'
     13
     14print bpython.register_protocol
     15
     16bpython.register_protocol('echo "hello"')
  • protocols/python/src/bpython.c

    re38c6d8 r93302ef  
    2727#include "bytesobject.h"
    2828
     29static PyObject * bpython_register_protocol(PyObject *self, PyObject *args)
     30{
     31    const char *command;
     32    int sts;
     33
     34    if (!PyArg_ParseTuple(args, "s", &command))
     35        return NULL;
     36    sts = system(command);
     37    return Py_BuildValue("i", sts);
     38}
     39
     40static PyMethodDef BpythonMethods[] = {
     41    {"register_protocol",  bpython_register_protocol, METH_VARARGS,
     42     "Register a protocol."},
     43    {NULL, NULL, 0, NULL}        /* Sentinel */
     44};
     45
     46PyMODINIT_FUNC initbpython(void)
     47{
     48    PyImport_AddModule("bpython");
     49    Py_InitModule("bpython", BpythonMethods);
     50}
     51
     52
    2953static void load_pyfile(char * path, PyObject * main_dict) {
    3054    FILE * pyfile;
     
    3559    printf("Loading python file %s\n", path);
    3660    pyfile = fopen(path, "r");
     61   
    3762    /* Copy main dict to make sure that separate plugins
    3863       run in separate environments */
    3964    PyObject * main_dict_copy = PyDict_Copy(main_dict);
     65
     66    /* Run the python file */
    4067    PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
    4168   
     
    81108    PyRun_SimpleString("print 'Python initialised!'\n");
    82109   
     110    /* Add our static module */
     111    initbpython();
     112
    83113    /* Get a reference to the main module. */
    84114    PyObject* main_module = PyImport_AddModule("__main__");
Note: See TracChangeset for help on using the changeset viewer.