Ticket #2842 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

Unexpected pyflakes termination following an unknown encoding type in a .py file

Reported by: chaica Assigned to: exarkun
Priority: normal Milestone:
Component: Pyflakes Severity: normal
Keywords: Cc:
Estimated Completion (YYYY/MM/DD): Branch: branches/pyflakes-encoding-problem-2842-2
Author: exarkun

Description

Pyflakes crashes if a .py file has a wrong encoding type. For example :

$ cat test.py 
# -*- coding: IBO-8859-1 -*-

print 'éééé'
$ python test.py 
  File "test.py", line 1
SyntaxError: encoding problem: with BOM
$ pyflakes test.py 
Traceback (most recent call last):
  File "/usr/bin/pyflakes", line 5, in <module>
    pkg_resources.run_script('pyflakes==0.3.0', 'pyflakes')
  File "/usr/lib/python2.5/site-packages/pkg_resources.py", line 448, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python2.5/site-packages/pkg_resources.py", line 1166, in run_script
    execfile(script_filename, namespace, namespace)
  File "/usr/lib/python2.5/site-packages/pyflakes-0.3.0-py2.5.egg/EGG-INFO/scripts/pyflakes", line 4, in <module>
    main()
  File "/usr/lib/python2.5/site-packages/pyflakes-0.3.0-py2.5.egg/pyflakes/scripts/pyflakes.py", line 68, in main
    warnings += checkPath(arg)
  File "/usr/lib/python2.5/site-packages/pyflakes-0.3.0-py2.5.egg/pyflakes/scripts/pyflakes.py", line 48, in checkPath
    return check(file(filename, 'U').read() + '\n', filename)
  File "/usr/lib/python2.5/site-packages/pyflakes-0.3.0-py2.5.egg/pyflakes/scripts/pyflakes.py", line 25, in check
    line = text.splitlines()[-1]
AttributeError: 'NoneType' object has no attribute 'splitlines'

This kind of file triggers a SyntaxError? in check method but this SyntaxError? exception has an unexpected value (value[1] == (None, 0, 0, None)), assigning None to text variable, so when you try to apply splitlines() on None value you got a oooooops.

Attached to this bugreport is a really simple patch in order to warn the user and to exit gracefully.

Best regards, Carl Chenet www.ohmytux.com

Attachments

pyflakes.py.diff (487 bytes) - added by chaica on 03/15/09 18:19:45.
solves a bug triggered by a unknown encoding type error in a .py file

Change History

03/15/09 18:19:45 changed by chaica

  • attachment pyflakes.py.diff added.

solves a bug triggered by a unknown encoding type error in a .py file

06/15/09 17:39:44 changed by exarkun

  • branch set to branches/pyflakes-encoding-problem-2842.
  • branch_author set to exarkun.

(In [17474]) Branching to 'pyflakes-encoding-problem-2842'

06/15/09 17:40:18 changed by exarkun

(In [17475]) Handle the decoding case of SyntaxError?

refs #2842

06/15/09 17:43:01 changed by exarkun

  • keywords set to review.
  • owner deleted.

Added a test for this case and fixed it (couldn't use supplied patch, trunk has diverged too much). Very simple, please review.

06/17/09 16:02:35 changed by radix

  • keywords deleted.
  • owner set to exarkun.

+1

06/17/09 16:56:19 changed by exarkun

  • branch changed from branches/pyflakes-encoding-problem-2842 to branches/pyflakes-encoding-problem-2842-2.

(In [17487]) Branching to 'pyflakes-encoding-problem-2842-2'

06/17/09 16:58:51 changed by exarkun

  • status changed from new to closed.
  • resolution set to fixed.

(In [17489]) Merge pyflakes-encoding-problem-2842-2

Author: chaica, exarkun Reviewer: radix Fixes: #2842

Handle errors in the encoding of a source file.

jethro@divmod.org