Menu

#321 Import block of `utils/error_reporting.py` might cause name error

closed-fixed
nobody
None
5
2020-03-03
2017-06-22
Sirui Tan
No

According to code here: https://sourceforge.net/p/docutils/code/HEAD/tree/trunk/docutils/docutils/utils/error_reporting.py#l55.
If a value error like 'unknown locale: en-us' is generated, variable locale_encoding will not be initialized.Causing line codecs.lookup(locale_encoding or '') # None -> '' to complain using undeclared locale_encoding. This bug is a blocker on our current work.

Discussion

  • Günter Milde

    Günter Milde - 2017-06-22

    On 2017-06-22, Sirui Tan via Docutils-develop wrote in gmane.text.docutils.devel:

    I cannot see a bug here, it is the expected and documented behaviour:

    # If no valid guess can be made, locale_encoding is set to None:

    --- error_reporting.py line 41

    Causing line codecs.lookup(locale_encoding or '') # None -> '' to
    complain using undeclared locale_encoding.

    However, this is is catched by exception handling in line 61.

    This bug is a blocker on our current work.

    I don't know what you are trying to achieve or what you would expect as
    reaction if no valid locale encoding can be determined.

    The correct usage would be to correctly handle the case that
    locale_encoding == None, e.g. like in frontend.py::

    default_error_encoding = getattr(sys.stderr, 'encoding',
                                     None) or locale_encoding or 'ascii'
    
     
    • Sirui Tan

      Sirui Tan - 2017-06-22

      Okay, here is the workflow:
      * On line 43 import locale, no ImportError is triggered so enter else block on line 46 with name locale_encoding undefined.
      * Run line 48 and ValueError is thrown.
      * error.args is inspected by if block on line 55. In our case the error message is not 'unknown locale: UTF-8' so the contents in if block will not be executed. locale_encoding is still undefined.
      * except block on line 57 is ignored since ValueError has already been captured. locale_encoding is still undefined.
      * Run line 60, codecs.lookup(locale_encoding or ''), since locale_encoding is undefined, exception (not LookupError) will be raised.
      * Exception handling on line 60 only capture LookupError, so exception raised on line 60 will be thrown and (porbably) terminate the process execution.

      Hope that helps!

       
  • Günter Milde

    Günter Milde - 2017-06-22
    • status: open --> closed-fixed
     
  • Günter Milde

    Günter Milde - 2017-06-22

    Yes, with this description I can spot the problem and solve it.
    Should be fixed with revision 8119.

     

Log in to post a comment.