| 1 |
/* -*- Mode: c; c-basic-offset: 2 -*- |
| 2 |
* |
| 3 |
* redland-post.i - Python SWIG helper routines |
| 4 |
* |
| 5 |
* Copyright (C) 2000-2006, David Beckett http://www.dajobe.org/ |
| 6 |
* Copyright (C) 2000-2005, University of Bristol, UK http://www.bristol.ac.uk/ |
| 7 |
* |
| 8 |
* This package is Free Software and part of Redland http://librdf.org/ |
| 9 |
* |
| 10 |
* It is licensed under the following three licenses as alternatives: |
| 11 |
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version |
| 12 |
* 2. GNU General Public License (GPL) V2 or any newer version |
| 13 |
* 3. Apache License, V2.0 or any newer version |
| 14 |
* |
| 15 |
* You may not use this file except in compliance with at least one of |
| 16 |
* the above three licenses. |
| 17 |
* |
| 18 |
* See LICENSE.html or LICENSE.txt at the top of this package for the |
| 19 |
* full license terms. |
| 20 |
* |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
void librdf_python_world_init(librdf_world *world); |
| 25 |
|
| 26 |
#if PY_MAJOR_VERSION >= 3 |
| 27 |
PyObject* |
| 28 |
#else |
| 29 |
void |
| 30 |
#endif |
| 31 |
SWIG_init(void); |
| 32 |
|
| 33 |
#if PY_MAJOR_VERSION >= 3 |
| 34 |
#define PyInt_AS_LONG PyLong_AsLong |
| 35 |
#define PyString_FromStringAndSize PyBytes_FromStringAndSize |
| 36 |
#endif |
| 37 |
|
| 38 |
static PyObject *librdf_python_callback = NULL; |
| 39 |
|
| 40 |
static PyObject * librdf_python_set_callback(PyObject *dummy, PyObject *args); |
| 41 |
static PyObject * librdf_python_reset_callback(PyObject *dummy, PyObject *args); |
| 42 |
static PyObject * librdf_python_set_parser_uri_filter(PyObject *dummy, PyObject *args); |
| 43 |
|
| 44 |
|
| 45 |
/* |
| 46 |
* set the Python function object callback |
| 47 |
*/ |
| 48 |
static PyObject * |
| 49 |
librdf_python_set_callback(PyObject *dummy, PyObject *args) |
| 50 |
{ |
| 51 |
PyObject *result = NULL; |
| 52 |
PyObject *temp; |
| 53 |
|
| 54 |
if (PyArg_ParseTuple(args, "O:set_callback", &temp)) { |
| 55 |
if (!PyCallable_Check(temp)) { |
| 56 |
PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
Py_XINCREF(temp); /* Add a reference to new callback */ |
| 60 |
Py_XDECREF(librdf_python_callback); /* Dispose of previous callback */ |
| 61 |
librdf_python_callback = temp; /* Remember new callback */ |
| 62 |
|
| 63 |
/* Boilerplate to return "None" */ |
| 64 |
Py_INCREF(Py_None); |
| 65 |
result = Py_None; |
| 66 |
} |
| 67 |
return result; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/* |
| 72 |
* set the Python function object callback |
| 73 |
*/ |
| 74 |
static PyObject * |
| 75 |
librdf_python_reset_callback(PyObject *dummy, PyObject *args) |
| 76 |
{ |
| 77 |
if(librdf_python_callback) { |
| 78 |
Py_XDECREF(librdf_python_callback); /* Dispose of previous callback */ |
| 79 |
librdf_python_callback = NULL; |
| 80 |
} |
| 81 |
|
| 82 |
/* Boilerplate to return "None" */ |
| 83 |
Py_INCREF(Py_None); |
| 84 |
return Py_None; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/* |
| 89 |
* calls a python function defined as: |
| 90 |
* RDF.message(level, message, line, file, uri) |
| 91 |
* where first argument is the log leve, second is a (scalar) string |
| 92 |
* message, line number, file (or None), uri (or None) |
| 93 |
* with an integer return value indicating if the message was handled. |
| 94 |
*/ |
| 95 |
static int |
| 96 |
librdf_call_python_message(int code, int level, int facility, |
| 97 |
const char *message, |
| 98 |
int line, int column, int byte, |
| 99 |
const char *file, const char *uri) |
| 100 |
{ |
| 101 |
PyObject *arglist; |
| 102 |
PyObject *result; |
| 103 |
int rc=0; |
| 104 |
|
| 105 |
if(!librdf_python_callback) |
| 106 |
return 0; |
| 107 |
|
| 108 |
/* call the callback */ |
| 109 |
arglist = Py_BuildValue("(iiisiiiss)", code, level, facility, message, line, column, byte, file, uri); |
| 110 |
if(!arglist) { |
| 111 |
fprintf(stderr, "librdf_call_python_message: Out of memory\n"); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
result = PyEval_CallObject(librdf_python_callback, arglist); |
| 115 |
Py_DECREF(arglist); |
| 116 |
if(result) { |
| 117 |
if(PyInt_Check(result)) |
| 118 |
rc=(int)PyInt_AS_LONG(result); |
| 119 |
|
| 120 |
Py_DECREF(result); |
| 121 |
} |
| 122 |
|
| 123 |
rc=1; |
| 124 |
|
| 125 |
return rc; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
static char* librdf_python_error_message = NULL; |
| 130 |
static char* librdf_python_warning_message = NULL; |
| 131 |
|
| 132 |
static PyObject *PyRedland_Warning; |
| 133 |
static PyObject *PyRedland_Error; |
| 134 |
|
| 135 |
/* |
| 136 |
* helper function for turning a python Unicode string into the UTF-8 |
| 137 |
* set of bytes suitable for calling Redland string functions |
| 138 |
*/ |
| 139 |
static PyObject * |
| 140 |
librdf_python_unicode_to_bytes(PyObject *dummy, PyObject *args) |
| 141 |
{ |
| 142 |
PyObject *result = NULL; |
| 143 |
PyObject *unicod; |
| 144 |
char *output = NULL; |
| 145 |
|
| 146 |
if (PyArg_ParseTuple(args, "U:unicode_to_bytes", &unicod)) { |
| 147 |
Py_UNICODE* input=(Py_UNICODE*)PyUnicode_AS_DATA(unicod); |
| 148 |
/* turn size of Python Unicode string - UCS2 encoded into size |
| 149 |
* needed for UTF-8 encoded C-string |
| 150 |
*/ |
| 151 |
size_t input_len = PyUnicode_GET_SIZE(unicod); |
| 152 |
size_t output_len; |
| 153 |
int i, j; |
| 154 |
|
| 155 |
/* Turn Unicode string length into max possible UTF-8 length (1-3 |
| 156 |
* bytes/char) |
| 157 |
*/ |
| 158 |
output_len = (3 * input_len); |
| 159 |
|
| 160 |
output = (char*)malloc(output_len + 1); |
| 161 |
if(!output) { |
| 162 |
PyErr_SetString(PyExc_MemoryError, "Out of memory"); |
| 163 |
goto failure; |
| 164 |
} |
| 165 |
|
| 166 |
j = 0; |
| 167 |
for(i=0; i < input_len; i++) { |
| 168 |
int size = raptor_unicode_utf8_string_put_char((unsigned long)input[i], |
| 169 |
(unsigned char*)&output[j], |
| 170 |
output_len - j); |
| 171 |
if(size <= 0) { |
| 172 |
PyErr_SetString(PyExc_ValueError, "Invalid input Unicode"); |
| 173 |
goto failure; |
| 174 |
} |
| 175 |
|
| 176 |
j += size; |
| 177 |
} |
| 178 |
output[j] = '\0'; |
| 179 |
|
| 180 |
result = PyString_FromStringAndSize(output, j + 1); |
| 181 |
} |
| 182 |
|
| 183 |
failure: |
| 184 |
if(output) |
| 185 |
free(output); |
| 186 |
return result; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
static int |
| 191 |
librdf_call_python_uri_filter(void* user_data, librdf_uri* uri) |
| 192 |
{ |
| 193 |
PyObject *arglist; |
| 194 |
PyObject *result; |
| 195 |
int rc=0; |
| 196 |
PyObject *callback=(PyObject*)user_data; |
| 197 |
|
| 198 |
/* call the callback */ |
| 199 |
arglist = Py_BuildValue("(s)", librdf_uri_as_string(uri)); |
| 200 |
if(!arglist) { |
| 201 |
fprintf(stderr, "librdf_call_python_uri_filter: Out of memory\n"); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
result = PyEval_CallObject(callback, arglist); |
| 205 |
Py_DECREF(arglist); |
| 206 |
if(result) { |
| 207 |
if(PyInt_Check(result)) |
| 208 |
rc=(int)PyInt_AS_LONG(result); |
| 209 |
|
| 210 |
Py_DECREF(result); |
| 211 |
} else |
| 212 |
rc=1; |
| 213 |
|
| 214 |
return rc; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/* |
| 219 |
* set the parser URI filter callback |
| 220 |
*/ |
| 221 |
static PyObject * |
| 222 |
librdf_python_set_parser_uri_filter(PyObject *dummy, PyObject *args) |
| 223 |
{ |
| 224 |
PyObject *result = NULL; |
| 225 |
PyObject *temp; |
| 226 |
PyObject * obj0 = 0 ; |
| 227 |
void *argp1 = 0 ; |
| 228 |
int res1 = 0 ; |
| 229 |
librdf_parser* parser=NULL; |
| 230 |
librdf_uri_filter_func current_filter; |
| 231 |
void* current_filter_user_data; |
| 232 |
|
| 233 |
if (PyArg_ParseTuple(args, "OO:set_parser_uri_filter", &obj0, &temp)) { |
| 234 |
/* first argument: SWIG wrapped librdf_parser* */ |
| 235 |
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_librdf_parser_s, 0 | 0 ); |
| 236 |
if (!SWIG_IsOK(res1)) { |
| 237 |
SWIG_exception_fail(SWIG_ArgError(res1), "in 'librdf_python_set_parser_uri_filter', argument 1 of type 'librdf_parser *'"); |
| 238 |
} |
| 239 |
parser = (librdf_parser *)(argp1); |
| 240 |
|
| 241 |
/* second argument: PyObject* callback function */ |
| 242 |
if (!PyCallable_Check(temp)) { |
| 243 |
PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
/* Add a reference to new callback */ |
| 248 |
Py_XINCREF(temp); |
| 249 |
|
| 250 |
/* FIXME: The above object reference should be remembered and |
| 251 |
* Py_XDECREFed when the parser is destroyed |
| 252 |
*/ |
| 253 |
|
| 254 |
/* Check for any existing callback to discard */ |
| 255 |
current_filter=librdf_parser_get_uri_filter(parser, |
| 256 |
¤t_filter_user_data); |
| 257 |
if(current_filter) { |
| 258 |
Py_XDECREF((PyObject*)current_filter_user_data); |
| 259 |
} |
| 260 |
|
| 261 |
/* Set new callback */ |
| 262 |
librdf_parser_set_uri_filter(parser, librdf_call_python_uri_filter, temp); |
| 263 |
|
| 264 |
/* Boilerplate to return "None" */ |
| 265 |
Py_INCREF(Py_None); |
| 266 |
result = Py_None; |
| 267 |
} |
| 268 |
return result; |
| 269 |
fail: |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/* Declare a table of methods that python can call */ |
| 275 |
static PyMethodDef librdf_python_methods [] = { |
| 276 |
{"set_callback", librdf_python_set_callback, METH_VARARGS, |
| 277 |
"Set python message callback."}, |
| 278 |
{"reset_callback", librdf_python_reset_callback, METH_VARARGS, |
| 279 |
"Set python message callback."}, |
| 280 |
{"unicode_to_bytes", librdf_python_unicode_to_bytes, METH_VARARGS, |
| 281 |
"Turn a python Unicode string into the UTF-8 bytes."}, |
| 282 |
{"set_parser_uri_filter", librdf_python_set_parser_uri_filter, METH_VARARGS, |
| 283 |
"Set a parser's URI filter function"}, |
| 284 |
{NULL, NULL, 0, NULL} /* Sentinel */ |
| 285 |
}; |
| 286 |
|
| 287 |
#if PY_MAJOR_VERSION >= 3 |
| 288 |
static struct PyModuleDef librdf_python_moduledef = { |
| 289 |
PyModuleDef_HEAD_INIT, |
| 290 |
"Redland_python", |
| 291 |
NULL, /* doc string */ |
| 292 |
-1, /* per module memory */ |
| 293 |
librdf_python_methods, |
| 294 |
NULL, /* Unused, reload */ |
| 295 |
NULL, /* GC traverse */ |
| 296 |
NULL, /* GC clearing*/ |
| 297 |
NULL /* free during deallocation */ |
| 298 |
}; |
| 299 |
#endif |
| 300 |
|
| 301 |
/* |
| 302 |
* stores a redland error message for later |
| 303 |
*/ |
| 304 |
static int |
| 305 |
librdf_python_message_handler(int is_warning, const char *message) |
| 306 |
{ |
| 307 |
int len; |
| 308 |
char **buffer; |
| 309 |
|
| 310 |
if(is_warning) |
| 311 |
buffer=&librdf_python_warning_message; |
| 312 |
else |
| 313 |
buffer=&librdf_python_error_message; |
| 314 |
|
| 315 |
if(*buffer) { |
| 316 |
/* There is already a pending warning or error, return not handled */ |
| 317 |
return 0; |
| 318 |
/* alternative: discard the older one with free(*buffer); */ |
| 319 |
} |
| 320 |
|
| 321 |
len=strlen(message); |
| 322 |
*buffer=(char*)malloc(len+1); |
| 323 |
if(!*buffer) { |
| 324 |
fprintf(stderr, "librdf_python_message_handler: Out of memory\n"); |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
strncpy(*buffer, message, len+1); |
| 329 |
|
| 330 |
/* |
| 331 |
* Emit warnings right away |
| 332 |
* Note: this makes the %exception code for warning never run |
| 333 |
*/ |
| 334 |
if(*buffer == librdf_python_warning_message) { |
| 335 |
PyErr_Warn(PyRedland_Warning, librdf_python_warning_message); |
| 336 |
free(librdf_python_warning_message); |
| 337 |
librdf_python_warning_message=NULL; |
| 338 |
} |
| 339 |
|
| 340 |
return 1; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
static int |
| 345 |
librdf_python_logger_handler(void *user_data, librdf_log_message *log_msg) |
| 346 |
{ |
| 347 |
raptor_locator* locator = log_msg->locator; |
| 348 |
int line= -1; |
| 349 |
int column= -1; |
| 350 |
int byte= -1; |
| 351 |
const char *uri=NULL; |
| 352 |
const char *file=NULL; |
| 353 |
|
| 354 |
if(locator) { |
| 355 |
line = raptor_locator_line(locator); |
| 356 |
column = raptor_locator_column(locator); |
| 357 |
byte = raptor_locator_byte(locator); |
| 358 |
file = raptor_locator_file(locator); |
| 359 |
uri = raptor_locator_uri(locator); |
| 360 |
} |
| 361 |
|
| 362 |
if(librdf_python_callback) |
| 363 |
return librdf_call_python_message(log_msg->code, log_msg->level, |
| 364 |
log_msg->facility, log_msg->message, |
| 365 |
line, column, byte, file, uri); |
| 366 |
|
| 367 |
|
| 368 |
if(log_msg->level < LIBRDF_LOG_WARN) |
| 369 |
return 1; |
| 370 |
|
| 371 |
return librdf_python_message_handler((log_msg->level < LIBRDF_LOG_ERROR), |
| 372 |
log_msg->message); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
void |
| 377 |
librdf_python_world_init(librdf_world *world) |
| 378 |
{ |
| 379 |
PyObject *module; |
| 380 |
PyObject *dict; |
| 381 |
PyObject *tuple; |
| 382 |
PyObject* rdf_module; |
| 383 |
const char *module_name="RDF"; |
| 384 |
|
| 385 |
#if PY_MAJOR_VERSION >= 3 |
| 386 |
module = PyModule_Create(&librdf_python_moduledef); |
| 387 |
dict = PyImport_GetModuleDict(); |
| 388 |
PyDict_SetItemString(dict, "Redland_python", module); |
| 389 |
#else |
| 390 |
module = Py_InitModule("Redland_python", librdf_python_methods); |
| 391 |
#endif |
| 392 |
dict = PyModule_GetDict(module); /* borrowed reference */ |
| 393 |
|
| 394 |
tuple = Py_BuildValue ("(iii)", librdf_version_major, librdf_version_minor, |
| 395 |
librdf_version_release); |
| 396 |
PyDict_SetItemString(dict, "version", tuple); |
| 397 |
Py_DECREF(tuple); |
| 398 |
|
| 399 |
rdf_module=PyImport_ImportModule((char*)module_name); |
| 400 |
if(rdf_module != NULL) { |
| 401 |
PyObject* rdf_module_dict; |
| 402 |
|
| 403 |
rdf_module_dict = PyModule_GetDict(rdf_module); /* borrowed reference */ |
| 404 |
|
| 405 |
PyRedland_Warning = PyDict_GetItemString(rdf_module_dict, |
| 406 |
"RedlandWarning"); |
| 407 |
|
| 408 |
PyRedland_Error = PyDict_GetItemString(rdf_module_dict, |
| 409 |
"RedlandError"); |
| 410 |
|
| 411 |
Py_DECREF(rdf_module); |
| 412 |
} else { |
| 413 |
PyErr_Print(); |
| 414 |
fprintf(stderr, "Failed to import module \"%s\"\n", module_name); |
| 415 |
} |
| 416 |
|
| 417 |
librdf_world_set_logger(world, NULL, librdf_python_logger_handler); |
| 418 |
} |