This commit is contained in:
Kovid Goyal 2018-09-13 08:27:03 +05:30
commit 8785619938
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 91 additions and 63 deletions

View File

@ -38,12 +38,16 @@ class Extension(object):
if iswindows: if iswindows:
self.cflags.append('/DCALIBRE_MODINIT_FUNC=PyMODINIT_FUNC') self.cflags.append('/DCALIBRE_MODINIT_FUNC=PyMODINIT_FUNC')
else: else:
if self.needs_cxx: return_type = 'PyObject*' if sys.version_info >= (3,) else 'void'
self.cflags.append('-DCALIBRE_MODINIT_FUNC=extern "C" __attribute__ ((visibility ("default"))) void') extern_decl = 'extern "C"' if self.needs_cxx else ''
else:
self.cflags.append('-DCALIBRE_MODINIT_FUNC=__attribute__ ((visibility ("default"))) void') self.cflags.append(
if kwargs.get('needs_c99'): '-DCALIBRE_MODINIT_FUNC='
'{} __attribute__ ((visibility ("default"))) {}'.format(extern_decl, return_type))
if not self.needs_cxx and kwargs.get('needs_c99'):
self.cflags.insert(0, '-std=c99') self.cflags.insert(0, '-std=c99')
self.ldflags = d['ldflags'] = kwargs.get('ldflags', []) self.ldflags = d['ldflags'] = kwargs.get('ldflags', [])
self.optional = d['options'] = kwargs.get('optional', False) self.optional = d['options'] = kwargs.get('optional', False)
of = kwargs.get('optimize_level', None) of = kwargs.get('optimize_level', None)
@ -162,20 +166,21 @@ def init_env():
if islinux: if islinux:
cflags.append('-pthread') cflags.append('-pthread')
ldflags.append('-shared') ldflags.append('-shared')
cflags.append('-I'+sysconfig.get_python_inc())
ldflags.append('-lpython'+sysconfig.get_python_version())
if isbsd: if isbsd:
cflags.append('-pthread') cflags.append('-pthread')
ldflags.append('-shared') ldflags.append('-shared')
cflags.append('-I'+sysconfig.get_python_inc())
ldflags.append('-lpython'+sysconfig.get_python_version())
if ishaiku: if ishaiku:
cflags.append('-lpthread') cflags.append('-lpthread')
ldflags.append('-shared') ldflags.append('-shared')
if islinux or isbsd or ishaiku:
cflags.append('-I'+sysconfig.get_python_inc()) cflags.append('-I'+sysconfig.get_python_inc())
ldflags.append('-lpython'+sysconfig.get_python_version()) # getattr(..., 'abiflags') is for PY2 compat, since PY2 has no abiflags
# member
ldflags.append('-lpython{}{}'.format(
sysconfig.get_config_var('VERSION'), getattr(sys, 'abiflags', '')))
if isosx: if isosx:
cflags.append('-D_OSX') cflags.append('-D_OSX')

View File

@ -53,7 +53,7 @@ dealloc(Dictionary *self) {
if (self->handle != NULL) delete self->handle; if (self->handle != NULL) delete self->handle;
/* We do not free encoding, since it is managed by hunspell */ /* We do not free encoding, since it is managed by hunspell */
self->encoding = NULL; self->handle = NULL; self->encoding = NULL; self->handle = NULL;
self->ob_type->tp_free((PyObject *)self); Py_TYPE(self)->tp_free((PyObject *)self);
} }
static PyObject * static PyObject *
@ -112,9 +112,15 @@ remove_word(Dictionary *self, PyObject *args) {
static PyMethodDef HunSpell_methods[] = { static PyMethodDef HunSpell_methods[] = {
{"recognized", (PyCFunction)recognized, METH_VARARGS, {"recognized", (PyCFunction)recognized, METH_VARARGS,
"Checks the spelling of the given word. The word must be a unicode object. If encoding of the word to the encoding of the dictionary fails, a UnicodeEncodeError is raised. Returns False if the input word is not recognized."}, "Checks the spelling of the given word. The word must be a unicode "
"object. If encoding of the word to the encoding of the dictionary fails, "
"a UnicodeEncodeError is raised. Returns False if the input word is not "
"recognized."},
{"suggest", (PyCFunction)suggest, METH_VARARGS, {"suggest", (PyCFunction)suggest, METH_VARARGS,
"Provide suggestions for the given word. The input word must be a unicode object. If encoding of the word to the encoding of the dictionary fails, a UnicodeEncodeError is raised. Returns the list of suggested words as unicode objects."}, "Provide suggestions for the given word. The input word must be a unicode "
"object. If encoding of the word to the encoding of the dictionary fails, "
"a UnicodeEncodeError is raised. Returns the list of suggested words as "
"unicode objects."},
{"add", (PyCFunction)add, METH_VARARGS, {"add", (PyCFunction)add, METH_VARARGS,
"Adds the given word into the runtime dictionary"}, "Adds the given word into the runtime dictionary"},
{"remove", (PyCFunction)remove_word, METH_VARARGS, {"remove", (PyCFunction)remove_word, METH_VARARGS,
@ -123,65 +129,82 @@ static PyMethodDef HunSpell_methods[] = {
}; };
static PyTypeObject DictionaryType = { static PyTypeObject DictionaryType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /* ob_size */ /* tp_name */ "Dictionary",
"Dictionary", /* tp_name */ /* tp_basicsize */ sizeof(Dictionary),
sizeof(Dictionary), /* tp_basicsize */ /* tp_itemsize */ 0,
0, /* tp_itemsize */ /* tp_dealloc */ (destructor) dealloc,
(destructor) dealloc, /* tp_dealloc */ /* tp_print */ 0,
0, /* tp_print */ /* tp_getattr */ 0,
0, /* tp_getattr */ /* tp_setattr */ 0,
0, /* tp_setattr */ /* tp_compare */ 0,
0, /* tp_compare */ /* tp_repr */ 0,
0, /* tp_repr */ /* tp_as_number */ 0,
0, /* tp_as_number */ /* tp_as_sequence */ 0,
0, /* tp_as_sequence */ /* tp_as_mapping */ 0,
0, /* tp_as_mapping */ /* tp_hash */ 0,
0, /* tp_hash */ /* tp_call */ 0,
0, /* tp_call */ /* tp_str */ 0,
0, /* tp_str */ /* tp_getattro */ 0,
0, /* tp_getattro */ /* tp_setattro */ 0,
0, /* tp_setattro */ /* tp_as_buffer */ 0,
0, /* tp_as_buffer */ /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ /* tp_doc */ "Dictionary object",
"Dictionary object", /* tp_doc */ /* tp_traverse */ 0,
0, /* tp_traverse */ /* tp_clear */ 0,
0, /* tp_clear */ /* tp_richcompare */ 0,
0, /* tp_richcompare */ /* tp_weaklistoffset */ 0,
0, /* tp_weaklistoffset */ /* tp_iter */ 0,
0, /* tp_iter */ /* tp_iternext */ 0,
0, /* tp_iternext */ /* tp_methods */ HunSpell_methods,
HunSpell_methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ 0,
0, /* tp_getset */ /* tp_base */ 0,
0, /* tp_base */ /* tp_dict */ 0,
0, /* tp_dict */ /* tp_descr_get */ 0,
0, /* tp_descr_get */ /* tp_descr_set */ 0,
0, /* tp_descr_set */ /* tp_dictoffset */ 0,
0, /* tp_dictoffset */ /* tp_init */ (initproc) init_type,
(initproc) init_type, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ 0,
0, /* tp_new */
}; };
#if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
static struct PyModuleDef hunspell_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "hunspell",
/* m_doc */ "A wrapper for the hunspell spell checking library",
/* m_size */ -1,
/* m_methods */ 0,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC CALIBRE_MODINIT_FUNC PyInit_hunspell(void) {
inithunspell(void) { PyObject *mod = PyModule_Create(&hunspell_module);
PyObject *mod; #else
#define INITERROR return
// Create the module CALIBRE_MODINIT_FUNC inithunspell(void) {
mod = Py_InitModule3("hunspell", NULL, PyObject *mod = Py_InitModule3("hunspell", NULL,
"A wrapper for the hunspell spell checking library"); "A wrapper for the hunspell spell checking library");
if (mod == NULL) return; #endif
if (mod == NULL) INITERROR;
HunspellError = PyErr_NewException((char*)"hunspell.HunspellError", NULL, NULL); HunspellError = PyErr_NewException((char*)"hunspell.HunspellError", NULL, NULL);
if (HunspellError == NULL) return; if (HunspellError == NULL) INITERROR;
PyModule_AddObject(mod, "HunspellError", HunspellError); PyModule_AddObject(mod, "HunspellError", HunspellError);
// Fill in some slots in the type, and make it ready // Fill in some slots in the type, and make it ready
DictionaryType.tp_new = PyType_GenericNew; DictionaryType.tp_new = PyType_GenericNew;
if (PyType_Ready(&DictionaryType) < 0) return; if (PyType_Ready(&DictionaryType) < 0) INITERROR;
// Add the type to the module. // Add the type to the module.
Py_INCREF(&DictionaryType); Py_INCREF(&DictionaryType);
PyModule_AddObject(mod, "Dictionary", (PyObject *)&DictionaryType); PyModule_AddObject(mod, "Dictionary", (PyObject *)&DictionaryType);
#if PY_MAJOR_VERSION >= 3
return mod;
#endif
} }