jdlorimer / chinese-support-redux

Anki add-on providing support for Chinese study
https://ankiweb.net/shared/info/1128979221
GNU General Public License v3.0
101 stars 50 forks source link

Zhuyin transcription crashes when Pinyin transcription process returns null #115

Open hans opened 4 years ago

hans commented 4 years ago

Describe the bug With notes where Hanzi->Pinyin transcription fails, Zhuyin field filling causes Anki to crash (see exception below).

To Reproduce Steps to reproduce the behavior:

  1. Set up the addon with a note type containing a Hanzi and Ruby (Zhuyin) field
  2. Make a card with the hanzi field "龴"
  3. Attempt to auto-fill the Ruby field
  4. See exception:
Caught exception:
  File "/home/jon/.local/share/Anki2/addons21/1128979221/fill.py", line 186, in bulk_fill_transcript
    fill_all_rubies(hanzi, copy)
  File "/home/jon/.local/share/Anki2/addons21/1128979221/behavior.py", line 243, in fill_all_rubies
    fill_ruby(hanzi, note, trans_group, ruby_group)
  File "/home/jon/.local/share/Anki2/addons21/1128979221/behavior.py", line 214, in fill_ruby
    split_hanzi(hanzi, grouped=True), 'bopomofo', 'trad'
  File "/home/jon/.local/share/Anki2/addons21/1128979221/transcribe.py", line 88, in transcribe
    transcribed.extend(bopomofo([s]))
  File "/home/jon/.local/share/Anki2/addons21/1128979221/bopomofo.py", line 30, in bopomofo
    print(replace_tone_marks(pinyin))
  File "/home/jon/.local/share/Anki2/addons21/1128979221/transcribe.py", line 157, in replace_tone_marks
    for bottom, top in separate_ruby(pinyin):
  File "/home/jon/.local/share/Anki2/addons21/1128979221/ruby.py", line 106, in separate_ruby
    for word in text
  File "/home/jon/.local/share/Anki2/addons21/1128979221/ruby.py", line 106, in <listcomp>
    for word in text
  File "/home/jon/.local/share/Anki2/addons21/1128979221/ruby.py", line 95, in ruby_bottom
    if not has_ruby(text):
  File "/home/jon/.local/share/Anki2/addons21/1128979221/ruby.py", line 72, in has_ruby
    return search(r'%s\[.+\]' % HANZI_REGEX, text)
  File "/usr/lib/python3.6/re.py", line 182, in search
    return _compile(pattern, flags).search(string)
<class 'TypeError'>: expected string or bytes-like object

Expected behavior Safely fail (don't put anything in the Ruby field?)

Specs (please complete the following information):

Anki 2.1.11 (3cf770c7) Python 3.6.7 Qt 5.12.1 PyQt 5.11.3 Platform: Linux Flags: frz=True ao=True sv=1 Add-ons possibly involved: Chinese Support Redux v0130

Additional context

I've debugged my end a little bit and found the exact culprit: Dictionary.get_pinyin returns None for this character, and this None input breaks the bopomofo transcription process. The code should safely fail instead.

Not sure how best to do that here. Happy to submit a PR if you let me know how this should be handled (error dialog? quiet failure? etc.).

Thanks for the great addon!

jdlorimer commented 3 years ago

You're absolutely correct. Returning None shouldn't really be an option - that's just a recipe for breaking calls from other functions.