Python's 8-bit cleanness deprecated?

Jp Calderone exarkun at intarweb.us
Mon Feb 3 16:36:08 EST 2003


On Mon, Feb 03, 2003 at 10:29:51PM +0300, Roman Suzi wrote:
> On Mon, 3 Feb 2003, Brian Quinlan wrote:
> 
> >> I think it's madness... There must be other ways to deal with it. I
> >could
> >> agree that for correct operation IDLE is demanding correct encoding
> >> setting (and nonetheless workis incorrectly!), but plain scripts
> >should 
> >> be 8-bit clean, without any conditions! (Luckily, it's alpha version, 
> >> so nothing really changed yet.)
> >
> >Just add:
> ># -*- coding: Latin-1 -*-
> >
> >to the top of your source files and you will be fine.
> 
> It's no problem with new scripts. But is there any reason to introduce this
> useful feature by force? 

> Requiring everyone to add one line to every script they wrote?
> 
> It's not very pleasant...
> 

  It's not, true, but that's a bit of an exageration.  For example, I won't
have to add anything to any of my source files.  :)

  For people who do, a simple script should do the job (somewhat tested code):

#!/usr/bin/python

import os
CODING_DECL = '# -*- coding: Latin-1 -*-' + os.linesep

def main():
    processDirectory('.')

def processDirectory(directory):
    for f in os.listdir(directory):
        if f.endswith('.py'):
            processFile(os.path.join(directory, f))   
        elif os.path.isdir(f):
            processDirectory(os.path.join(directory, f))

def processFile(f):
    contents = file(f).read()
    for c in contents:
        if c > '\x7f':
            break
    else:
        return
    
    print 'Adding coding declaration to', f
    output = file(f, 'w')
    contents = contents.splitlines()
    if contents[0].startswith('#!'):
        output.write(contents[0] + os.linesep)   
        del contents[0]
        
    output.write(CODING_DECL)
    output.write(os.linesep.join(contents))
    output.close()

if __name__ == '__main__':
    main()


  Customize to taste.

  Jp

-- 
 up 50 days, 1:50, 4 users, load average: 0.03, 0.07, 0.04
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030203/7d688d63/attachment.sig>


More information about the Python-list mailing list