comparison mercurial/util.py @ 23789:94951db84fc0

util: introduce unpacker This allows taking advantage of Python 2.5+'s struct.Struct, which provides a slightly faster unpack due to reusing formats. Sadly, .unpack_from is significantly slower.
author Matt Mackall <mpm@selenic.com>
date Sat, 10 Jan 2015 21:18:31 -0600
parents 4dd8a6a1240d
children 62f41f251e52
comparison
equal deleted inserted replaced
23788:316ad725a1dd 23789:94951db84fc0
17 _ = i18n._ 17 _ = i18n._
18 import error, osutil, encoding 18 import error, osutil, encoding
19 import errno, shutil, sys, tempfile, traceback 19 import errno, shutil, sys, tempfile, traceback
20 import re as remod 20 import re as remod
21 import os, time, datetime, calendar, textwrap, signal, collections 21 import os, time, datetime, calendar, textwrap, signal, collections
22 import imp, socket, urllib 22 import imp, socket, urllib, struct
23 import gc 23 import gc
24 24
25 if os.name == 'nt': 25 if os.name == 'nt':
26 import windows as platform 26 import windows as platform
27 else: 27 else:
227 return memoryview(sliceable)[offset:] 227 return memoryview(sliceable)[offset:]
228 228
229 import subprocess 229 import subprocess
230 closefds = os.name == 'posix' 230 closefds = os.name == 'posix'
231 231
232 def unpacker(fmt):
233 """create a struct unpacker for the specified format"""
234 try:
235 # 2.5+
236 return struct.Struct(fmt).unpack
237 except NameError:
238 # 2.4
239 return lambda buf: struct.unpack(fmt)
240
232 def popen2(cmd, env=None, newlines=False): 241 def popen2(cmd, env=None, newlines=False):
233 # Setting bufsize to -1 lets the system decide the buffer size. 242 # Setting bufsize to -1 lets the system decide the buffer size.
234 # The default for bufsize is 0, meaning unbuffered. This leads to 243 # The default for bufsize is 0, meaning unbuffered. This leads to
235 # poor performance on Mac OS X: http://bugs.python.org/issue4194 244 # poor performance on Mac OS X: http://bugs.python.org/issue4194
236 p = subprocess.Popen(cmd, shell=True, bufsize=-1, 245 p = subprocess.Popen(cmd, shell=True, bufsize=-1,