comparison hglib/util.py @ 145:f3c430afa598

hglib: abstract out use of cStringIO.StringIO (issue4520) The cStringIO module does not exist in Python 3, but io.BytesIO does. This change prepares for the use of io.BytesIO when available by replacing all uses of cStringIO.StringIO with an object named BytesIO.
author Brett Cannon <brett@python.org>
date Fri, 13 Mar 2015 11:31:54 -0400
parents fe74d5599539
children 8d7bf729a4db
comparison
equal deleted inserted replaced
144:3c59643a2bc3 145:f3c430afa598
1 import itertools, cStringIO, error, os, subprocess, sys 1 import itertools, error, os, subprocess, sys
2 from cStringIO import StringIO as BytesIO
2 3
3 if sys.version_info[0] > 2: 4 if sys.version_info[0] > 2:
4 def b(s): 5 def b(s):
5 """Encode the string as bytes.""" 6 """Encode the string as bytes."""
6 return s.encode('latin-1') 7 return s.encode('latin-1')
23 >>> eatlines("1\\n2", 3) 24 >>> eatlines("1\\n2", 3)
24 '' 25 ''
25 >>> eatlines("1\\n2\\n3", 1) 26 >>> eatlines("1\\n2\\n3", 1)
26 '2\\n3' 27 '2\\n3'
27 """ 28 """
28 cs = cStringIO.StringIO(s) 29 cs = BytesIO(s)
29 30
30 for line in cs: 31 for line in cs:
31 n -= 1 32 n -= 1
32 if n == 0: 33 if n == 0:
33 return cs.read() 34 return cs.read()
44 >>> skiplines('', 'a') 45 >>> skiplines('', 'a')
45 '' 46 ''
46 >>> skiplines('a\\nb', 'b') 47 >>> skiplines('a\\nb', 'b')
47 'a\\nb' 48 'a\\nb'
48 """ 49 """
49 cs = cStringIO.StringIO(s) 50 cs = BytesIO(s)
50 51
51 for line in cs: 52 for line in cs:
52 if not line.startswith(prefix): 53 if not line.startswith(prefix):
53 return line + cs.read() 54 return line + cs.read()
54 55