Changeset 76ca5cb


Ignore:
Timestamp:
2013-03-27T20:31:46Z (8 years ago)
Author:
Nick Murdoch <nick@…>
Parents:
1dcfd1f
Message:

bpython.register_protocl() actually takes meaningful args and does something.

Location:
protocols/python
Files:
2 edited

Legend:

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

    r1dcfd1f r76ca5cb  
    77import sys
    88import bpython
    9 print "hello, I'm a test plugin running on Python", sys.version_info
     9print ("hello, I'm a test plugin running on Python {}.{}.{}".format(
     10    sys.version_info.major, sys.version_info.minor, sys.version_info.micro))
    1011
    11 print bpython.Protocol
    12 print bpython.Protocol()
    13 print bpython.register_protocol
    14 
    15 try:
    16     class MyProtocol(bpython.Protocol):
    17         pass
    18 except Exception, e:
    19     print "error:", e
     12class MyProtocol(bpython.Protocol):
     13    pass
    2014
    2115myproto = MyProtocol()
    22 print "myproto: ", myproto
    2316
    24 bpython.register_protocol('echo "hello"')
     17bpython.register_protocol('myproto', myproto)
     18
  • protocols/python/src/bpython.c

    r1dcfd1f r76ca5cb  
    138138static PyObject * bpython_register_protocol(PyObject *self, PyObject *args)
    139139{
    140     const char *command;
    141     int sts;
    142 
    143     if (!PyArg_ParseTuple(args, "s", &command))
     140    PyObject *protocol;
     141    char * name;
     142    struct prpl * ret;
     143
     144    if (!PyArg_ParseTuple(args, "sO", &name, &protocol))
    144145        return NULL;
    145     sts = system(command);
    146     return Py_BuildValue("i", sts);
     146   
     147    /* Increase the reference count to stop garbage collection */
     148    Py_INCREF(protocol);
     149
     150    ret = g_new0(struct prpl, 1);
     151    ret->name = name;
     152    ret->data = ret;
     153
     154    register_protocol(ret);
     155
     156    Py_RETURN_NONE;
    147157}
    148158
     
    168178}
    169179
    170 
    171 static void load_pyfile(char * path, PyObject * main_dict) {
    172     PyObject * err;
    173     struct prpl *ret;
    174     ret = g_new0(struct prpl, 1);
    175    
    176     PyObject * pluginname = PyDict_GetItemString(main_dict, "name");
    177     if ((err = PyErr_Occurred()) || !pluginname) {
    178         printf("No plugin name\n");
    179         PyErr_Print();
    180         g_free(ret);
    181         return;
    182     }
    183     PyObject * pluginname_unicode = PyObject_Unicode(pluginname);
    184     PyObject * pluginname_encoded = PyUnicode_AsEncodedString(pluginname_unicode, "ascii", "ignore");
    185     if ((err = PyErr_Occurred())) {
    186         printf("Error encoding plugin name\n");
    187         PyErr_Print();
    188         g_free(ret);
    189         return;
    190     }
    191     char * pluginname_tmp; /* reference, do not modify */
    192     Py_ssize_t length;
    193     PyBytes_AsStringAndSize(pluginname_encoded, &pluginname_tmp, &length);
    194     if ((err = PyErr_Occurred())) {
    195         printf("Bad plugin name\n");
    196         PyErr_Print();
    197         g_free(ret);
    198         return;
    199     }
    200     ret->name = g_malloc0(length + 1);
    201     memmove(ret->name, pluginname_tmp, length);
    202 
    203     Py_DECREF(pluginname_encoded);
    204     Py_DECREF(pluginname_unicode);
    205 
    206     register_protocol(ret);
    207 }
    208180
    209181/* Bitlbee plugin functions: */
     
    239211            if (g_str_has_suffix(path, ".py")) {
    240212                FILE * pyfile;
    241                 printf("Loading python file %s\n", path);
     213                PyObject * result;
     214                printf("Loading python file %s\n", path);
    242215                pyfile = fopen(path, "r");
    243216               
     
    247220
    248221                /* Run the python file */
    249                 PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
    250 
     222                result = PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
     223                if (result == NULL) {
     224                    printf("Error loading module %s\n", path);
     225                    PyErr_PrintEx(0);
     226                }
    251227            }
    252228
Note: See TracChangeset for help on using the changeset viewer.