Changeset 76ca5cb
- Timestamp:
- 2013-03-27T20:31:46Z (12 years ago)
- Parents:
- 1dcfd1f
- Location:
- protocols/python
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/python/examples/sampleplugin.py
r1dcfd1f r76ca5cb 7 7 import sys 8 8 import bpython 9 print "hello, I'm a test plugin running on Python", sys.version_info 9 print ("hello, I'm a test plugin running on Python {}.{}.{}".format( 10 sys.version_info.major, sys.version_info.minor, sys.version_info.micro)) 10 11 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 12 class MyProtocol(bpython.Protocol): 13 pass 20 14 21 15 myproto = MyProtocol() 22 print "myproto: ", myproto23 16 24 bpython.register_protocol('echo "hello"') 17 bpython.register_protocol('myproto', myproto) 18 -
protocols/python/src/bpython.c
r1dcfd1f r76ca5cb 138 138 static PyObject * bpython_register_protocol(PyObject *self, PyObject *args) 139 139 { 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)) 144 145 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; 147 157 } 148 158 … … 168 178 } 169 179 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 }208 180 209 181 /* Bitlbee plugin functions: */ … … 239 211 if (g_str_has_suffix(path, ".py")) { 240 212 FILE * pyfile; 241 printf("Loading python file %s\n", path); 213 PyObject * result; 214 printf("Loading python file %s\n", path); 242 215 pyfile = fopen(path, "r"); 243 216 … … 247 220 248 221 /* 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 } 251 227 } 252 228
Note: See TracChangeset
for help on using the changeset viewer.