##// END OF EJS Templates
parsers: alias long to int on Python 3
Pulkit Goyal -
r31220:37596c98 default
parent child Browse files
Show More
@@ -1,178 +1,181 b''
1 1 # parsers.py - Python implementation of parsers.c
2 2 #
3 3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import struct
11 11 import zlib
12 12
13 13 from .node import nullid
14 14 from . import pycompat
15 15 stringio = pycompat.stringio
16 16
17 if pycompat.ispy3:
18 long = int
19
17 20 _pack = struct.pack
18 21 _unpack = struct.unpack
19 22 _compress = zlib.compress
20 23 _decompress = zlib.decompress
21 24
22 25 # Some code below makes tuples directly because it's more convenient. However,
23 26 # code outside this module should always use dirstatetuple.
24 27 def dirstatetuple(*x):
25 28 # x is a tuple
26 29 return x
27 30
28 31 indexformatng = ">Qiiiiii20s12x"
29 32 indexfirst = struct.calcsize('Q')
30 33 sizeint = struct.calcsize('i')
31 34 indexsize = struct.calcsize(indexformatng)
32 35
33 36 def gettype(q):
34 37 return int(q & 0xFFFF)
35 38
36 39 def offset_type(offset, type):
37 40 return long(long(offset) << 16 | type)
38 41
39 42 class BaseIndexObject(object):
40 43 def __len__(self):
41 44 return self._lgt + len(self._extra) + 1
42 45
43 46 def insert(self, i, tup):
44 47 assert i == -1
45 48 self._extra.append(tup)
46 49
47 50 def _fix_index(self, i):
48 51 if not isinstance(i, int):
49 52 raise TypeError("expecting int indexes")
50 53 if i < 0:
51 54 i = len(self) + i
52 55 if i < 0 or i >= len(self):
53 56 raise IndexError
54 57 return i
55 58
56 59 def __getitem__(self, i):
57 60 i = self._fix_index(i)
58 61 if i == len(self) - 1:
59 62 return (0, 0, 0, -1, -1, -1, -1, nullid)
60 63 if i >= self._lgt:
61 64 return self._extra[i - self._lgt]
62 65 index = self._calculate_index(i)
63 66 r = struct.unpack(indexformatng, self._data[index:index + indexsize])
64 67 if i == 0:
65 68 e = list(r)
66 69 type = gettype(e[0])
67 70 e[0] = offset_type(0, type)
68 71 return tuple(e)
69 72 return r
70 73
71 74 class IndexObject(BaseIndexObject):
72 75 def __init__(self, data):
73 76 assert len(data) % indexsize == 0
74 77 self._data = data
75 78 self._lgt = len(data) // indexsize
76 79 self._extra = []
77 80
78 81 def _calculate_index(self, i):
79 82 return i * indexsize
80 83
81 84 def __delitem__(self, i):
82 85 if not isinstance(i, slice) or not i.stop == -1 or not i.step is None:
83 86 raise ValueError("deleting slices only supports a:-1 with step 1")
84 87 i = self._fix_index(i.start)
85 88 if i < self._lgt:
86 89 self._data = self._data[:i * indexsize]
87 90 self._lgt = i
88 91 self._extra = []
89 92 else:
90 93 self._extra = self._extra[:i - self._lgt]
91 94
92 95 class InlinedIndexObject(BaseIndexObject):
93 96 def __init__(self, data, inline=0):
94 97 self._data = data
95 98 self._lgt = self._inline_scan(None)
96 99 self._inline_scan(self._lgt)
97 100 self._extra = []
98 101
99 102 def _inline_scan(self, lgt):
100 103 off = 0
101 104 if lgt is not None:
102 105 self._offsets = [0] * lgt
103 106 count = 0
104 107 while off <= len(self._data) - indexsize:
105 108 s, = struct.unpack('>i',
106 109 self._data[off + indexfirst:off + sizeint + indexfirst])
107 110 if lgt is not None:
108 111 self._offsets[count] = off
109 112 count += 1
110 113 off += indexsize + s
111 114 if off != len(self._data):
112 115 raise ValueError("corrupted data")
113 116 return count
114 117
115 118 def __delitem__(self, i):
116 119 if not isinstance(i, slice) or not i.stop == -1 or not i.step is None:
117 120 raise ValueError("deleting slices only supports a:-1 with step 1")
118 121 i = self._fix_index(i.start)
119 122 if i < self._lgt:
120 123 self._offsets = self._offsets[:i]
121 124 self._lgt = i
122 125 self._extra = []
123 126 else:
124 127 self._extra = self._extra[:i - self._lgt]
125 128
126 129 def _calculate_index(self, i):
127 130 return self._offsets[i]
128 131
129 132 def parse_index2(data, inline):
130 133 if not inline:
131 134 return IndexObject(data), None
132 135 return InlinedIndexObject(data, inline), (0, data)
133 136
134 137 def parse_dirstate(dmap, copymap, st):
135 138 parents = [st[:20], st[20: 40]]
136 139 # dereference fields so they will be local in loop
137 140 format = ">cllll"
138 141 e_size = struct.calcsize(format)
139 142 pos1 = 40
140 143 l = len(st)
141 144
142 145 # the inner loop
143 146 while pos1 < l:
144 147 pos2 = pos1 + e_size
145 148 e = _unpack(">cllll", st[pos1:pos2]) # a literal here is faster
146 149 pos1 = pos2 + e[4]
147 150 f = st[pos2:pos1]
148 151 if '\0' in f:
149 152 f, c = f.split('\0')
150 153 copymap[f] = c
151 154 dmap[f] = e[:4]
152 155 return parents
153 156
154 157 def pack_dirstate(dmap, copymap, pl, now):
155 158 now = int(now)
156 159 cs = stringio()
157 160 write = cs.write
158 161 write("".join(pl))
159 162 for f, e in dmap.iteritems():
160 163 if e[0] == 'n' and e[3] == now:
161 164 # The file was last modified "simultaneously" with the current
162 165 # write to dirstate (i.e. within the same second for file-
163 166 # systems with a granularity of 1 sec). This commonly happens
164 167 # for at least a couple of files on 'update'.
165 168 # The user could change the file without changing its size
166 169 # within the same second. Invalidate the file's mtime in
167 170 # dirstate, forcing future 'status' calls to compare the
168 171 # contents of the file if the size is the same. This prevents
169 172 # mistakenly treating such files as clean.
170 173 e = dirstatetuple(e[0], e[1], e[2], -1)
171 174 dmap[f] = e
172 175
173 176 if f in copymap:
174 177 f = "%s\0%s" % (f, copymap[f])
175 178 e = _pack(">cllll", e[0], e[1], e[2], e[3], len(f))
176 179 write(e)
177 180 write(f)
178 181 return cs.getvalue()
General Comments 0
You need to be logged in to leave comments. Login now