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