~ubuntu-branches/ubuntu/trusty/pylint/trusty

« back to all changes in this revision

Viewing changes to checkers/misc.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2009-03-27 09:45:39 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090327094539-xe2s86jkbvdv3xhb
Tags: 0.18.0-1
* New upstream release
* debian/copyright
  - added packaging copyright for the work I do
  - clearly separated copyright and license notices, indenting by 4 spaces
  - link to GPL-2 file, not to the generic GPL
* debian/control
  - updated Homepage field
  - bump versions for python-logilab-common and python-logilab-astng depends
  - bump Standard-Versions to 3.8.1 (no changes needed)
* debian/{control, rules}
  - switch from python-central to python-support
* debian/rules
  - 'build' is a dir, we need to clean with 'rm'

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
# You should have received a copy of the GNU General Public License along with
12
12
# this program; if not, write to the Free Software Foundation, Inc.,
13
13
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
14
 
""" Copyright (c) 2000-2003 LOGILAB S.A. (Paris, FRANCE).
 
14
""" Copyright (c) 2000-2009 LOGILAB S.A. (Paris, FRANCE).
15
15
 http://www.logilab.fr/ -- mailto:contact@logilab.fr
16
16
 
17
17
Check source code is ascii only or has an encoding declaration (PEP 263)
18
18
"""
19
19
 
20
 
__revision__ = '$Id: misc.py,v 1.19 2005-11-02 09:21:47 syt Exp $'
21
 
 
22
20
import re
23
21
 
24
22
from pylint.interfaces import IRawChecker
26
24
 
27
25
def is_ascii(string):
28
26
    """return true if non ascii characters are detected in the given string
 
27
    and line number where non-ascii has been encountered.
29
28
    """
30
 
    if string:
31
 
        return max([ord(char) for char in string]) < 128
32
 
    return True
 
29
    for i, line in enumerate(string.splitlines()):
 
30
        if line and max([ord(char) for char in line]) >= 128:
 
31
            return False, i + 1
 
32
    return True, 0
33
33
    
34
34
# regexp matching both emacs and vim declaration
35
35
ENCODING_RGX = re.compile("[^#]*#*.*coding[:=]\s*([^\s]+)")
93
93
        """
94
94
        # source encoding
95
95
        data = stream.read()
96
 
        if not is_ascii(data):
 
96
        ascii, lineno = is_ascii(data)
 
97
        if not ascii:
97
98
            encoding = guess_encoding(data)
98
99
            if encoding is None:
99
 
                self.add_message('E0501', line=1)
 
100
                self.add_message('E0501', line=lineno)
100
101
            else:
101
102
                try:
102
103
                    unicode(data, encoding)