Spell module: fix check of UTF8 strings in pyaspell

This commit is contained in:
Némunaire 2012-11-24 22:30:46 +01:00
parent 8aaf3bd6dc
commit 9495e43cc0
2 changed files with 19 additions and 17 deletions

View File

@ -45,6 +45,8 @@ def cmd_spell(msg):
def check_spell(word, lang='fr'): def check_spell(word, lang='fr'):
a = Aspell(("lang", lang)) a = Aspell(("lang", lang))
if a.check(word): if a.check(word):
return True ret = True
else: else:
return a.suggest(word) ret = a.suggest(word)
a.close()
return ret

View File

@ -17,7 +17,7 @@
# 2011-02-20 # 2011-02-20
# * python3 compatible # * python3 compatible
# * fixed docs # * fixed docs
# #
# 2008-09-xx: # 2008-09-xx:
# * fixed typo in save_all, thanks to Thomas Waldecker (thomas!yospot.de) # * fixed typo in save_all, thanks to Thomas Waldecker (thomas!yospot.de)
# #
@ -54,7 +54,7 @@ class AspellLinux(object):
""" """
Aspell speller object. Allows to check spelling, get suggested Aspell speller object. Allows to check spelling, get suggested
spelling list, manage user dictionaries, and other. spelling list, manage user dictionaries, and other.
Must be closed with 'close' method, or one may experience Must be closed with 'close' method, or one may experience
problems, like segfaults. problems, like segfaults.
""" """
@ -113,7 +113,7 @@ class AspellLinux(object):
return bool( return bool(
self.__lib.aspell_speller_check( self.__lib.aspell_speller_check(
self.__speller, self.__speller,
_to_bytes(word), word,
len(word) len(word)
)) ))
else: else:
@ -132,7 +132,7 @@ class AspellLinux(object):
return self._aspellwordlist( return self._aspellwordlist(
self.__lib.aspell_speller_suggest( self.__lib.aspell_speller_suggest(
self.__speller, self.__speller,
_to_bytes(word), word,
len(word) len(word)
)) ))
else: else:
@ -162,7 +162,7 @@ class AspellLinux(object):
return self._aspellwordlist( return self._aspellwordlist(
self.__lib.aspell_speller_personal_word_list(self.__speller) self.__lib.aspell_speller_personal_word_list(self.__speller)
) )
def session_dict(self, word=None, clear=False): def session_dict(self, word=None, clear=False):
""" """
@ -193,7 +193,7 @@ class AspellLinux(object):
return self._aspellwordlist( return self._aspellwordlist(
self.__lib.aspell_speller_session_word_list(self.__speller) self.__lib.aspell_speller_session_word_list(self.__speller)
) )
def add_replacement_pair(self, misspelled, correct): def add_replacement_pair(self, misspelled, correct):
""" """
@ -212,7 +212,7 @@ class AspellLinux(object):
len(correct) len(correct)
) )
self._aspell_check_error() self._aspell_check_error()
def save_all(self): def save_all(self):
""" """
@ -221,13 +221,13 @@ class AspellLinux(object):
""" """
self.__lib.aspell_speller_save_all_word_lists(self.__speller) self.__lib.aspell_speller_save_all_word_lists(self.__speller)
self._aspell_check_error() self._aspell_check_error()
def configkeys(self): def configkeys(self):
""" """
Returns list of all available config keys that can be passed Returns list of all available config keys that can be passed
to constructor. to constructor.
List contains a 3-tuples: List contains a 3-tuples:
1. key name 1. key name
2. default value of type: 2. default value of type:
@ -239,7 +239,7 @@ class AspellLinux(object):
if None, then this key is undocumented is should not if None, then this key is undocumented is should not
be used, unless one know what really do be used, unless one know what really do
""" """
config = self.__lib.aspell_speller_config(self.__speller) config = self.__lib.aspell_speller_config(self.__speller)
if config is None: if config is None:
raise AspellConfigError("Can't get speller's config") raise AspellConfigError("Can't get speller's config")
@ -315,7 +315,7 @@ class AspellLinux(object):
Close aspell speller object. Close aspell speller object.
""" """
self.__lib.delete_aspell_speller(self.__speller) self.__lib.delete_aspell_speller(self.__speller)
# XXX: internal function, do not call directly # XXX: internal function, do not call directly
def _aspellwordlist(self, wordlist_id): def _aspellwordlist(self, wordlist_id):
@ -336,7 +336,7 @@ class AspellLinux(object):
self.__lib.delete_aspell_string_enumeration(elements) self.__lib.delete_aspell_string_enumeration(elements)
return list return list
def _aspell_config_error(self, config): def _aspell_config_error(self, config):
""" """
@ -345,13 +345,13 @@ class AspellLinux(object):
Raise exception if operation of speller config Raise exception if operation of speller config
caused an error. Additionally destroy config object. caused an error. Additionally destroy config object.
""" """
# make exception object & copy error msg # make exception object & copy error msg
exc = AspellConfigError( exc = AspellConfigError(
_from_bytes(ctypes.c_char_p( _from_bytes(ctypes.c_char_p(
self.__lib.aspell_config_error_message(config) self.__lib.aspell_config_error_message(config)
).value) ).value)
) )
# then destroy config objcet # then destroy config objcet
self.__lib.delete_aspell_config(config) self.__lib.delete_aspell_config(config)
@ -393,7 +393,7 @@ if __name__ == '__main__':
for item in a.configkeys(): for item in a.configkeys():
print(item) print(item)
a.close() a.close()
# vim: ts=4 sw=4 # vim: ts=4 sw=4