Skip to content

Commit

Permalink
Make initialization errors more orderly
Browse files Browse the repository at this point in the history
This is conceputally the same as the change in mecab-python3 as a result
of SamuraiT/mecab-python3#67. Instead of the
initialization error producing output directly, it throws an exception
so you can catch it. Potentially useful in downstream applications for
testing the environment configuration.
  • Loading branch information
polm committed Jul 24, 2021
1 parent 6fb19a6 commit b9f244c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions fugashi/fugashi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ cdef str get_error_details(int argc, char** argv):
model = mecab_model_new(argc, argv)
return mecab_strerror(NULL).decode('utf-8')

cdef void print_detailed_error(list args, int argc, char** argv):
"""Print guide to solving initialization errors."""
print(FAILMESSAGE, file=sys.stderr)
print('arguments:', args, file=sys.stderr)
cdef str get_detailed_error(list args, int argc, char** argv):
"""Generate guide to solving initialization errors."""
msg = FAILMESSAGE + "\n"
msg += "arguments: " + str(args) + "\n"
msg += get_error_details(argc, argv) + "\n"
msg += '----------------------------------------------------------\n'
return msg

message = get_error_details(argc, argv)
print('error message:', message, file=sys.stderr)
print('----------------------------------------------------------')

cdef class GenericTagger:
"""Generic Tagger, supports any dictionary.
Expand Down Expand Up @@ -224,10 +224,11 @@ cdef class GenericTagger:
# In theory mecab_strerror should return an error string from MeCab
# It doesn't seem to work and just returns b'' though, so this will
# have to do.
msg = "Failed initializing MeCab"
if not quiet:
print_detailed_error(args, argc, argv)
msg = get_detailed_error(args, argc, argv)
free(argv)
raise RuntimeError("Failed initializing MeCab")
raise RuntimeError(msg)
free(argv)
self.wrapper = wrapper
self._cache = {}
Expand Down

0 comments on commit b9f244c

Please sign in to comment.