Switch to unified view

a/src/python/recoll/pyrclextract.cpp b/src/python/recoll/pyrclextract.cpp
...
...
49
static void 
49
static void 
50
Extractor_dealloc(rclx_ExtractorObject *self)
50
Extractor_dealloc(rclx_ExtractorObject *self)
51
{
51
{
52
    LOGDEB(("Extractor_dealloc\n"));
52
    LOGDEB(("Extractor_dealloc\n"));
53
    delete self->xtr;
53
    delete self->xtr;
54
    self->ob_type->tp_free((PyObject*)self);
54
    Py_TYPE(self)->tp_free((PyObject*)self);
55
}
55
}
56
56
57
static PyObject *
57
static PyObject *
58
Extractor_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
58
Extractor_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
59
{
59
{
...
...
189
        PyErr_SetString(PyExc_AttributeError, "interntofile failure");
189
        PyErr_SetString(PyExc_AttributeError, "interntofile failure");
190
    return 0;
190
    return 0;
191
    }
191
    }
192
    if (outfile.empty())
192
    if (outfile.empty())
193
    temp->setnoremove(1);
193
    temp->setnoremove(1);
194
    PyObject *result = outfile.empty() ? PyString_FromString(temp->filename()) :
194
    PyObject *result = outfile.empty() ? PyBytes_FromString(temp->filename()) :
195
    PyString_FromString(outfile.c_str());
195
    PyBytes_FromString(outfile.c_str());
196
    return (PyObject *)result;
196
    return (PyObject *)result;
197
}
197
}
198
198
199
static PyMethodDef Extractor_methods[] = {
199
static PyMethodDef Extractor_methods[] = {
200
    {"textextract", (PyCFunction)Extractor_textextract, 
200
    {"textextract", (PyCFunction)Extractor_textextract, 
...
...
209
"\n"
209
"\n"
210
"An Extractor object can extract data from a native simple or compound\n"
210
"An Extractor object can extract data from a native simple or compound\n"
211
"object.\n"
211
"object.\n"
212
);
212
);
213
static PyTypeObject rclx_ExtractorType = {
213
static PyTypeObject rclx_ExtractorType = {
214
    PyObject_HEAD_INIT(NULL)
214
    PyVarObject_HEAD_INIT(NULL, 0)
215
    0,                         /*ob_size*/
216
    "rclextract.Extractor",             /*tp_name*/
215
    "rclextract.Extractor",             /*tp_name*/
217
    sizeof(rclx_ExtractorObject), /*tp_basicsize*/
216
    sizeof(rclx_ExtractorObject), /*tp_basicsize*/
218
    0,                         /*tp_itemsize*/
217
    0,                         /*tp_itemsize*/
219
    (destructor)Extractor_dealloc,    /*tp_dealloc*/
218
    (destructor)Extractor_dealloc,    /*tp_dealloc*/
220
    0,                         /*tp_print*/
219
    0,                         /*tp_print*/
...
...
251
    0,                         /* tp_alloc */
250
    0,                         /* tp_alloc */
252
    Extractor_new,            /* tp_new */
251
    Extractor_new,            /* tp_new */
253
};
252
};
254
253
255
///////////////////////////////////// Module-level stuff
254
///////////////////////////////////// Module-level stuff
256
static PyMethodDef rclxMethods[] = {
255
static PyMethodDef rclextract_methods[] = {
257
    {NULL, NULL, 0, NULL}        /* Sentinel */
256
    {NULL, NULL, 0, NULL}        /* Sentinel */
258
};
257
};
259
PyDoc_STRVAR(rclx_doc_string,
258
PyDoc_STRVAR(rclx_doc_string,
260
         "This is an interface to the Recoll text extraction features.");
259
         "This is an interface to the Recoll text extraction features.");
261
260
262
#ifndef PyMODINIT_FUNC    /* declarations for DLL import/export */
261
struct module_state {
263
#define PyMODINIT_FUNC void
262
    PyObject *error;
263
};
264
265
#if PY_MAJOR_VERSION >= 3
266
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
267
#else
268
#define GETSTATE(m) (&_state)
269
static struct module_state _state;
264
#endif
270
#endif
271
272
#if PY_MAJOR_VERSION >= 3
273
static int rclextract_traverse(PyObject *m, visitproc visit, void *arg) {
274
    Py_VISIT(GETSTATE(m)->error);
275
    return 0;
276
}
277
278
static int rclextract_clear(PyObject *m) {
279
    Py_CLEAR(GETSTATE(m)->error);
280
    return 0;
281
}
282
283
static struct PyModuleDef moduledef = {
284
        PyModuleDef_HEAD_INIT,
285
        "rclextract",
286
        NULL,
287
        sizeof(struct module_state),
288
        rclextract_methods,
289
        NULL,
290
        rclextract_traverse,
291
        rclextract_clear,
292
        NULL
293
};
294
295
#define INITERROR return NULL
296
297
extern "C" PyObject *
298
PyInit_rclextract(void)
299
300
#else
301
#define INITERROR return
265
PyMODINIT_FUNC
302
PyMODINIT_FUNC
266
initrclextract(void)
303
initrclextract(void)
304
#endif
267
{
305
{
268
    // We run recollinit. It's responsible for initializing some static data
306
    // We run recollinit. It's responsible for initializing some static data
269
    // which is distinct from pyrecoll's as we're separately dlopened
307
    // which is distinct from pyrecoll's as we're separately dlopened
270
    string reason;
308
    string reason;
271
    rclconfig = recollinit(0, 0, reason, 0);
309
    rclconfig = recollinit(0, 0, reason, 0);
272
    if (rclconfig == 0) {
310
    if (rclconfig == 0) {
273
    PyErr_SetString(PyExc_EnvironmentError, reason.c_str());
311
    PyErr_SetString(PyExc_EnvironmentError, reason.c_str());
274
  return;
312
  INITERROR;
275
    }
313
    }
276
    if (!rclconfig->ok()) {
314
    if (!rclconfig->ok()) {
277
    PyErr_SetString(PyExc_EnvironmentError, 
315
    PyErr_SetString(PyExc_EnvironmentError, 
278
            "Recoll init error: bad environment ?");
316
            "Recoll init error: bad environment ?");
279
  return;
317
  INITERROR;
280
    }
318
    }
281
319
320
#if PY_MAJOR_VERSION >= 3
321
    PyObject *module = PyModule_Create(&moduledef);
322
#else
282
    PyObject* m = Py_InitModule("rclextract", rclxMethods);
323
    PyObject *module = Py_InitModule("rclextract", rclextract_methods);
324
#endif
325
    if (module == NULL)
326
        INITERROR;
327
328
    struct module_state *st = GETSTATE(module);
329
    // The first parameter is a char *. Hopefully we don't initialize
330
    // modules too often...
331
    st->error = PyErr_NewException(strdup("rclextract.Error"), NULL, NULL);
332
    if (st->error == NULL) {
333
        Py_DECREF(module);
334
        INITERROR;
335
    }
336
283
    PyModule_AddStringConstant(m, "__doc__", rclx_doc_string);
337
    PyModule_AddStringConstant(module, "__doc__", rclx_doc_string);
284
338
285
    if (PyType_Ready(&rclx_ExtractorType) < 0)
339
    if (PyType_Ready(&rclx_ExtractorType) < 0)
286
        return;
340
        INITERROR;
287
    Py_INCREF(&rclx_ExtractorType);
341
    Py_INCREF(&rclx_ExtractorType);
288
    PyModule_AddObject(m, "Extractor", (PyObject *)&rclx_ExtractorType);
342
    PyModule_AddObject(module, "Extractor", (PyObject *)&rclx_ExtractorType);
289
343
290
#if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 7
344
#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 7)
291
    recoll_DocType = (PyObject*)PyCapsule_Import(PYRECOLL_PACKAGE "recoll.doctypeptr", 0);
345
    recoll_DocType = (PyObject*)PyCapsule_Import(PYRECOLL_PACKAGE "recoll.doctypeptr", 0);
292
#else
346
#else
293
    PyObject *module = PyImport_ImportModule(PYRECOLL_PACKAGE "recoll");
347
    PyObject *module = PyImport_ImportModule(PYRECOLL_PACKAGE "recoll");
294
    if (module != NULL) {
348
    if (module != NULL) {
295
        PyObject *cobject = PyObject_GetAttrString(module, "_C_API");
349
        PyObject *cobject = PyObject_GetAttrString(module, "_C_API");
296
        if (cobject == NULL)
350
        if (cobject == NULL)
297
            return;
351
            INITERROR;
298
        if (PyCObject_Check(cobject))
352
        if (PyCObject_Check(cobject))
299
            recoll_DocType = (PyObject*)PyCObject_AsVoidPtr(cobject);
353
            recoll_DocType = (PyObject*)PyCObject_AsVoidPtr(cobject);
300
        Py_DECREF(cobject);
354
        Py_DECREF(cobject);
301
    }
355
    }
302
#endif
356
#endif
357
358
#if PY_MAJOR_VERSION >= 3
359
    return module;
360
#endif
303
}
361
}