Changeset e38c6d8


Ignore:
Timestamp:
2013-03-17T16:49:05Z (8 years ago)
Author:
Nick Murdoch <nick@…>
Children:
93302ef
Parents:
0147ca9
Message:

Register an (incomplete) plugin per python file found, with name based on module's 'name' variable.

Location:
protocols/python
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/python/src/bpython.c

    r0147ca9 re38c6d8  
    2525#include <bitlbee.h>
    2626#include <Python.h>
     27#include "bytesobject.h"
     28
     29static void load_pyfile(char * path, PyObject * main_dict) {
     30    FILE * pyfile;
     31    PyObject * err;
     32    struct prpl *ret;
     33    ret = g_new0(struct prpl, 1);
     34   
     35    printf("Loading python file %s\n", path);
     36    pyfile = fopen(path, "r");
     37    /* Copy main dict to make sure that separate plugins
     38       run in separate environments */
     39    PyObject * main_dict_copy = PyDict_Copy(main_dict);
     40    PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
     41   
     42    PyObject * pluginname = PyDict_GetItemString(main_dict_copy, "name");
     43    if ((err = PyErr_Occurred()) || !pluginname) {
     44        printf("No plugin name\n");
     45        PyErr_Print();
     46        g_free(ret);
     47        return;
     48    }
     49    PyObject * pluginname_unicode = PyObject_Unicode(pluginname);
     50    PyObject * pluginname_encoded = PyUnicode_AsEncodedString(pluginname_unicode, "ascii", "ignore");
     51    if ((err = PyErr_Occurred())) {
     52        printf("Error encoding plugin name\n");
     53        PyErr_Print();
     54        g_free(ret);
     55        return;
     56    }
     57    char * pluginname_tmp; /* reference, do not modify */
     58    Py_ssize_t length;
     59    PyBytes_AsStringAndSize(pluginname_encoded, &pluginname_tmp, &length);
     60    if ((err = PyErr_Occurred())) {
     61        printf("Bad plugin name\n");
     62        PyErr_Print();
     63        g_free(ret);
     64        return;
     65    }
     66    ret->name = g_malloc0(length + 1);
     67    memmove(ret->name, pluginname_tmp, length);
     68
     69    Py_DECREF(pluginname_encoded);
     70    Py_DECREF(pluginname_unicode);
     71
     72    register_protocol(ret);
     73}
    2774
    2875void init_plugin() {
    29     struct prpl *ret = g_new0(struct prpl, 1);
    3076    GDir *dir;
    3177    GError *error = NULL;
     
    5399           
    54100            if (g_str_has_suffix(path, ".py")) {
    55                 printf("Loading python file %s\n", path);
    56                 FILE* pyfile = fopen(path, "r");
    57                 /* Copy main dict to make sure that separate plugins
    58                    run in separate environments */
    59                 PyObject* main_dict_copy = PyDict_Copy(main_dict);
    60                 PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
    61                
     101                load_pyfile(path, main_dict);
    62102            }
    63103
     
    65105        }
    66106    }
    67 
    68 
    69     ret->name = "bpython";
    70 
    71     register_protocol(ret);
    72107
    73108    // Py_Finalize(); // TODO - there's no plugin_unload for us to do this in.
Note: See TracChangeset for help on using the changeset viewer.