FAQ

On 2015-05-26 12:26 AM, Terry Reedy wrote:
+ try:
+ encoding, lines = detect_encoding(buffer.readline)
+ buffer.seek(0)
+ text = TextIOWrapper(buffer, encoding, line_buffering=True)
+ text.mode = 'r'
+ return text
+ except:
+ buffer.close()
+ raise
Please do not add bare 'except:'. If you mean 'except
BaseException:', say so.

try..finally would be even better.


Yury

Search Discussions

  • Victor Stinner at May 26, 2015 at 6:20 am
    What is wrong with "except:" in this specific case?


    Victor


    Le mardi 26 mai 2015, Yury Selivanov <yselivanov.ml@gmail.com> a ?crit :

    On 2015-05-26 12:26 AM, Terry Reedy wrote:

    + try:
    + encoding, lines = detect_encoding(buffer.readline)
    + buffer.seek(0)
    + text = TextIOWrapper(buffer, encoding, line_buffering=True)
    + text.mode = 'r'
    + return text
    + except:
    + buffer.close()
    + raise
    Please do not add bare 'except:'. If you mean 'except BaseException:',
    say so.
    try..finally would be even better.

    Yury
    _______________________________________________
    Python-Dev mailing list
    Python-Dev at python.org
    https://mail.python.org/mailman/listinfo/python-dev
    Unsubscribe:
    https://mail.python.org/mailman/options/python-dev/victor.stinner%40gmail.com
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-dev/attachments/20150526/ee476903/attachment.html>
  • R. David Murray at May 26, 2015 at 9:12 pm

    On Tue, 26 May 2015 08:20:01 +0200, Victor Stinner wrote:
    What is wrong with "except:" in this specific case?

    Nothing is wrong with it from a technical standpoint. However, if we
    use 'except BaseException' that makes it clear that someone has thought
    about it and decided that all exceptions should be caught, as opposed to
    it being legacy code or a programming mistake.

    On 2015-05-26 12:26 AM, Terry Reedy wrote:

    + try:
    + encoding, lines = detect_encoding(buffer.readline)
    + buffer.seek(0)
    + text = TextIOWrapper(buffer, encoding, line_buffering=True)
    + text.mode = 'r'
    + return text
    + except:
    + buffer.close()
    + raise
    Please do not add bare 'except:'. If you mean 'except BaseException:',
    say so.
  • Serhiy Storchaka at May 26, 2015 at 6:44 am

    On 26.05.15 08:33, Yury Selivanov wrote:
    On 2015-05-26 12:26 AM, Terry Reedy wrote:
    + try:
    + encoding, lines = detect_encoding(buffer.readline)
    + buffer.seek(0)
    + text = TextIOWrapper(buffer, encoding, line_buffering=True)
    + text.mode = 'r'
    + return text
    + except:
    + buffer.close()
    + raise
    Please do not add bare 'except:'. If you mean 'except
    BaseException:', say so.
    try..finally would be even better.

    No, finally is not correct there. buffer shouldn't be closed if no
    exception is raised.


    And this is one of the cases in which bare 'except:' is absolutely legal.
  • Victor Stinner at May 26, 2015 at 7:54 am

    2015-05-26 8:44 GMT+02:00 Serhiy Storchaka <storchaka@gmail.com>:
    No, finally is not correct there. buffer shouldn't be closed if no exception
    is raised.

    Yep. The binary file must only be closed in case of error, as written
    in the commit message.

    And this is one of the cases in which bare 'except:' is absolutely legal.

    The "except: <cleanup code>; raise" is common in the Python stdlib.


    Victor
  • Yury Selivanov at May 26, 2015 at 1:30 pm

    On 2015-05-26 3:54 AM, Victor Stinner wrote:
    2015-05-26 8:44 GMT+02:00 Serhiy Storchaka <storchaka@gmail.com>:
    No, finally is not correct there. buffer shouldn't be closed if no exception
    is raised.
    Yep. The binary file must only be closed in case of error, as written
    in the commit message.



    Right. My bad, sorry Victor ;)


    Yury

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-dev @
categoriespython
postedMay 26, '15 at 5:33a
activeMay 26, '15 at 9:12p
posts6
users4
websitepython.org

People

Translate

site design / logo © 2017 Grokbase