# HG changeset patch # User Matt Mackall # Date 1420946311 21600 # Node ID 94951db84fc0a3c3099418e15785d4db21e53b7d # Parent 316ad725a1dd0c9210e8e64fe6c4905a9adb8bd4 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. diff -r 316ad725a1dd -r 94951db84fc0 mercurial/util.py --- a/mercurial/util.py Sat Jan 10 21:13:10 2015 -0600 +++ b/mercurial/util.py Sat Jan 10 21:18:31 2015 -0600 @@ -19,7 +19,7 @@ import errno, shutil, sys, tempfile, traceback import re as remod import os, time, datetime, calendar, textwrap, signal, collections -import imp, socket, urllib +import imp, socket, urllib, struct import gc if os.name == 'nt': @@ -229,6 +229,15 @@ import subprocess closefds = os.name == 'posix' +def unpacker(fmt): + """create a struct unpacker for the specified format""" + try: + # 2.5+ + return struct.Struct(fmt).unpack + except NameError: + # 2.4 + return lambda buf: struct.unpack(fmt) + def popen2(cmd, env=None, newlines=False): # Setting bufsize to -1 lets the system decide the buffer size. # The default for bufsize is 0, meaning unbuffered. This leads to