[MERGE]Change "print >> outfile" to "outfile.write".

John Arbash Meinel john at arbash-meinel.com
Tue Oct 16 17:48:50 BST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Blake Winton wrote:
> Martin Pool wrote:
>> On 10/16/07, John Arbash Meinel <john at arbash-meinel.com> wrote:
>>> I've always felt like "print >> outfile, 'string'" was worse than
>>> "outfile.write('string\n')".
>> I tend to use write in new code, and I believe that in Python 3000
>> print is changing away from being a statement with special syntax.  It
>> might be good to change that now, and maybe someone would also like to
>> do a semi-mechanical patch to update all existing uses.
> 
> Ta-da!  Well, actually, ta-da-ish...
> 
> I'm getting a bunch of (what I believe to be new) errors of the form:
> ERROR: test_bundle.V4WeaveBundleTester.test_alt_timezone_bundle
>     Weave invariant violated: unexpected line ' w\n'
> 
> But I can't see where my semi-mechanical translation went wrong, and
> debugging the tests over my SSH link is just too painful.  If someone
> has a few minutes to look through the changes, I would love the help,
> otherwise I'll try to figure out what went wrong when I get home tonight.
> 
> Thanks,
> Blake.
> 

I believe the fault lines somewhere in here:
- -    print >>f, FORMAT_1,
+    f.write(FORMAT_1 + ' ')

     for version, included in enumerate(weave._parents):
         if included:
             # mininc = weave.minimal_parents(version)
             mininc = included
- -            print >>f, 'i',
+            f.write('i ')
             for i in mininc:
- -                print >>f, i,
- -            print >>f
+                f.write(i + ' ')
+            f.write('\n')
         else:
- -            print >>f, 'i'
- -        print >>f, '1', weave._sha1s[version]
- -        print >>f, 'n', weave._names[version]
- -        print >>f
+            f.write('i\n')
+        f.write('1 ' + weave._sha1s[version] + '\n')
+        f.write('n ' + weave._names[version] + '\n')
+        f.write('\n')

- -    print >>f, 'w'
+    f.write('w\n')

Specifically, when you do:

  print 'i',
  print 'i'

It isn't doing:

  write('i ')
  write('i\n')

It is actually doing
  write('i')
  write(' i\n')

print 'x',

Is actually queuing up that the next print will add a space if there wasn't one
from the previous entry. (As I believe "print 'x '," will not add an extra
space on the next print.)

At least, from the error you are getting, you are getting a line with:

' w\n'

Rather than a plain
'w\n'

I think the problem is just:

f.write(FORMAT_1) # Format_1 ends in a \n so it will not create an extra ' '

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHFOtxJdeBCYSNAAMRApqvAJ0bpJEUZQ1Eir7oIPGa4nd2cDhOOQCfSyUb
eiYAscLlwbAfG+wISSQPdwQ=
=03jP
-----END PGP SIGNATURE-----



More information about the bazaar mailing list