Mercurial > hg
annotate hgext/fsmonitor/pywatchman/pybser.py @ 48946:642e31cb55f0
py3: use class X: instead of class X(object):
The inheritance from object is implied in Python 3. So this should
be equivalent.
This change was generated via an automated search and replace. So there
may have been some accidental changes.
Differential Revision: https://phab.mercurial-scm.org/D12352
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 21 Feb 2022 13:08:28 -0700 |
parents | 6000f5b25c9b |
children | 59a72267f5ce |
rev | line source |
---|---|
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
1 # Copyright 2015 Facebook, Inc. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
2 # All rights reserved. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
3 # |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
4 # Redistribution and use in source and binary forms, with or without |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
5 # modification, are permitted provided that the following conditions are met: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
6 # |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
7 # * Redistributions of source code must retain the above copyright notice, |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
8 # this list of conditions and the following disclaimer. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
9 # |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
10 # * Redistributions in binary form must reproduce the above copyright notice, |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
11 # this list of conditions and the following disclaimer in the documentation |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
12 # and/or other materials provided with the distribution. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
13 # |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
14 # * Neither the name Facebook nor the names of its contributors may be used to |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
15 # endorse or promote products derived from this software without specific |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
16 # prior written permission. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
17 # |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
24 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
28 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
29 # no unicode literals |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
30 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
31 import binascii |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
32 import collections |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
33 import ctypes |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
34 import struct |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
35 import sys |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
36 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
37 from . import compat |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
38 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
39 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
40 BSER_ARRAY = b"\x00" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
41 BSER_OBJECT = b"\x01" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
42 BSER_BYTESTRING = b"\x02" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
43 BSER_INT8 = b"\x03" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
44 BSER_INT16 = b"\x04" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
45 BSER_INT32 = b"\x05" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
46 BSER_INT64 = b"\x06" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
47 BSER_REAL = b"\x07" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
48 BSER_TRUE = b"\x08" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
49 BSER_FALSE = b"\x09" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
50 BSER_NULL = b"\x0a" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
51 BSER_TEMPLATE = b"\x0b" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
52 BSER_SKIP = b"\x0c" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
53 BSER_UTF8STRING = b"\x0d" |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
54 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
55 if compat.PYTHON3: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
56 STRING_TYPES = (str, bytes) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
57 unicode = str |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
58 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
59 def tobytes(i): |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
60 return str(i).encode("ascii") |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
61 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
62 long = int |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
63 else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
64 STRING_TYPES = (unicode, str) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
65 tobytes = bytes |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
66 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
67 # Leave room for the serialization header, which includes |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
68 # our overall length. To make things simpler, we'll use an |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
69 # int32 for the header |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
70 EMPTY_HEADER = b"\x00\x01\x05\x00\x00\x00\x00" |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
71 EMPTY_HEADER_V2 = b"\x00\x02\x00\x00\x00\x00\x05\x00\x00\x00\x00" |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
72 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
73 |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
74 def _int_size(x): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
75 """Return the smallest size int that can store the value""" |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
76 if -0x80 <= x <= 0x7F: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
77 return 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
78 elif -0x8000 <= x <= 0x7FFF: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
79 return 2 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
80 elif -0x80000000 <= x <= 0x7FFFFFFF: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
81 return 4 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
82 elif long(-0x8000000000000000) <= x <= long(0x7FFFFFFFFFFFFFFF): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
83 return 8 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
84 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
85 raise RuntimeError("Cannot represent value: " + str(x)) |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
86 |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
87 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
88 def _buf_pos(buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
89 ret = buf[pos] |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
90 # Normalize the return type to bytes |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
91 if compat.PYTHON3 and not isinstance(ret, bytes): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
92 ret = bytes((ret,)) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
93 return ret |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
94 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
95 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
96 class _bser_buffer: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
97 def __init__(self, version): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
98 self.bser_version = version |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
99 self.buf = ctypes.create_string_buffer(8192) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
100 if self.bser_version == 1: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
101 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
102 tobytes(len(EMPTY_HEADER)) + b"s", self.buf, 0, EMPTY_HEADER |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
103 ) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
104 self.wpos = len(EMPTY_HEADER) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
105 else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
106 assert self.bser_version == 2 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
107 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
108 tobytes(len(EMPTY_HEADER_V2)) + b"s", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
109 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
110 0, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
111 EMPTY_HEADER_V2, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
112 ) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
113 self.wpos = len(EMPTY_HEADER_V2) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
114 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
115 def ensure_size(self, size): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
116 while ctypes.sizeof(self.buf) - self.wpos < size: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
117 ctypes.resize(self.buf, ctypes.sizeof(self.buf) * 2) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
118 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
119 def append_long(self, val): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
120 size = _int_size(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
121 to_write = size + 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
122 self.ensure_size(to_write) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
123 if size == 1: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
124 struct.pack_into(b"=cb", self.buf, self.wpos, BSER_INT8, val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
125 elif size == 2: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
126 struct.pack_into(b"=ch", self.buf, self.wpos, BSER_INT16, val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
127 elif size == 4: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
128 struct.pack_into(b"=ci", self.buf, self.wpos, BSER_INT32, val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
129 elif size == 8: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
130 struct.pack_into(b"=cq", self.buf, self.wpos, BSER_INT64, val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
131 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
132 raise RuntimeError("Cannot represent this long value") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
133 self.wpos += to_write |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
134 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
135 def append_string(self, s): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
136 if isinstance(s, unicode): |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
137 s = s.encode("utf-8") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
138 s_len = len(s) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
139 size = _int_size(s_len) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
140 to_write = 2 + size + s_len |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
141 self.ensure_size(to_write) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
142 if size == 1: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
143 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
144 b"=ccb" + tobytes(s_len) + b"s", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
145 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
146 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
147 BSER_BYTESTRING, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
148 BSER_INT8, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
149 s_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
150 s, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
151 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
152 elif size == 2: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
153 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
154 b"=cch" + tobytes(s_len) + b"s", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
155 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
156 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
157 BSER_BYTESTRING, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
158 BSER_INT16, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
159 s_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
160 s, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
161 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
162 elif size == 4: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
163 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
164 b"=cci" + tobytes(s_len) + b"s", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
165 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
166 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
167 BSER_BYTESTRING, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
168 BSER_INT32, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
169 s_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
170 s, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
171 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
172 elif size == 8: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
173 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
174 b"=ccq" + tobytes(s_len) + b"s", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
175 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
176 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
177 BSER_BYTESTRING, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
178 BSER_INT64, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
179 s_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
180 s, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
181 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
182 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
183 raise RuntimeError("Cannot represent this string value") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
184 self.wpos += to_write |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
185 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
186 def append_recursive(self, val): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
187 if isinstance(val, bool): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
188 needed = 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
189 self.ensure_size(needed) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
190 if val: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
191 to_encode = BSER_TRUE |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
192 else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
193 to_encode = BSER_FALSE |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
194 struct.pack_into(b"=c", self.buf, self.wpos, to_encode) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
195 self.wpos += needed |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
196 elif val is None: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
197 needed = 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
198 self.ensure_size(needed) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
199 struct.pack_into(b"=c", self.buf, self.wpos, BSER_NULL) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
200 self.wpos += needed |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
201 elif isinstance(val, (int, long)): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
202 self.append_long(val) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
203 elif isinstance(val, STRING_TYPES): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
204 self.append_string(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
205 elif isinstance(val, float): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
206 needed = 9 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
207 self.ensure_size(needed) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
208 struct.pack_into(b"=cd", self.buf, self.wpos, BSER_REAL, val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
209 self.wpos += needed |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
210 elif isinstance(val, collections.Mapping) and isinstance( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
211 val, collections.Sized |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
212 ): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
213 val_len = len(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
214 size = _int_size(val_len) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
215 needed = 2 + size |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
216 self.ensure_size(needed) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
217 if size == 1: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
218 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
219 b"=ccb", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
220 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
221 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
222 BSER_OBJECT, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
223 BSER_INT8, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
224 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
225 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
226 elif size == 2: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
227 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
228 b"=cch", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
229 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
230 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
231 BSER_OBJECT, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
232 BSER_INT16, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
233 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
234 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
235 elif size == 4: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
236 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
237 b"=cci", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
238 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
239 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
240 BSER_OBJECT, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
241 BSER_INT32, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
242 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
243 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
244 elif size == 8: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
245 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
246 b"=ccq", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
247 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
248 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
249 BSER_OBJECT, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
250 BSER_INT64, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
251 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
252 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
253 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
254 raise RuntimeError("Cannot represent this mapping value") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
255 self.wpos += needed |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
256 if compat.PYTHON3: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
257 iteritems = val.items() |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
258 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
259 iteritems = val.iteritems() # noqa: B301 Checked version above |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
260 for k, v in iteritems: |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
261 self.append_string(k) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
262 self.append_recursive(v) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
263 elif isinstance(val, collections.Iterable) and isinstance( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
264 val, collections.Sized |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
265 ): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
266 val_len = len(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
267 size = _int_size(val_len) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
268 needed = 2 + size |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
269 self.ensure_size(needed) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
270 if size == 1: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
271 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
272 b"=ccb", self.buf, self.wpos, BSER_ARRAY, BSER_INT8, val_len |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
273 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
274 elif size == 2: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
275 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
276 b"=cch", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
277 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
278 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
279 BSER_ARRAY, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
280 BSER_INT16, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
281 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
282 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
283 elif size == 4: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
284 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
285 b"=cci", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
286 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
287 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
288 BSER_ARRAY, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
289 BSER_INT32, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
290 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
291 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
292 elif size == 8: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
293 struct.pack_into( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
294 b"=ccq", |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
295 self.buf, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
296 self.wpos, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
297 BSER_ARRAY, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
298 BSER_INT64, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
299 val_len, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
300 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
301 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
302 raise RuntimeError("Cannot represent this sequence value") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
303 self.wpos += needed |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
304 for v in val: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
305 self.append_recursive(v) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
306 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
307 raise RuntimeError("Cannot represent unknown value type") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
308 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
309 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
310 def dumps(obj, version=1, capabilities=0): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
311 bser_buf = _bser_buffer(version=version) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
312 bser_buf.append_recursive(obj) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
313 # Now fill in the overall length |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
314 if version == 1: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
315 obj_len = bser_buf.wpos - len(EMPTY_HEADER) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
316 struct.pack_into(b"=i", bser_buf.buf, 3, obj_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
317 else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
318 obj_len = bser_buf.wpos - len(EMPTY_HEADER_V2) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
319 struct.pack_into(b"=i", bser_buf.buf, 2, capabilities) |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
320 struct.pack_into(b"=i", bser_buf.buf, 7, obj_len) |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
321 return bser_buf.buf.raw[: bser_buf.wpos] |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
322 |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
323 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
324 # This is a quack-alike with the bserObjectType in bser.c |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
325 # It provides by getattr accessors and getitem for both index |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
326 # and name. |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
327 class _BunserDict: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
328 __slots__ = ("_keys", "_values") |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
329 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
330 def __init__(self, keys, values): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
331 self._keys = keys |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
332 self._values = values |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
333 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
334 def __getattr__(self, name): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
335 return self.__getitem__(name) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
336 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
337 def __getitem__(self, key): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
338 if isinstance(key, (int, long)): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
339 return self._values[key] |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
340 elif key.startswith("st_"): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
341 # hack^Wfeature to allow mercurial to use "st_size" to |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
342 # reference "size" |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
343 key = key[3:] |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
344 try: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
345 return self._values[self._keys.index(key)] |
41365
876494fd967d
cleanup: delete lots of unused local variables
Martin von Zweigbergk <martinvonz@google.com>
parents:
30656
diff
changeset
|
346 except ValueError: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
347 raise KeyError("_BunserDict has no key %s" % key) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
348 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
349 def __len__(self): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
350 return len(self._keys) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
351 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
352 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
353 class Bunser: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
354 def __init__(self, mutable=True, value_encoding=None, value_errors=None): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
355 self.mutable = mutable |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
356 self.value_encoding = value_encoding |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
357 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
358 if value_encoding is None: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
359 self.value_errors = None |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
360 elif value_errors is None: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
361 self.value_errors = "strict" |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
362 else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
363 self.value_errors = value_errors |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
364 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
365 @staticmethod |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
366 def unser_int(buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
367 try: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
368 int_type = _buf_pos(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
369 except IndexError: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
370 raise ValueError("Invalid bser int encoding, pos out of range") |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
371 if int_type == BSER_INT8: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
372 needed = 2 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
373 fmt = b"=b" |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
374 elif int_type == BSER_INT16: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
375 needed = 3 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
376 fmt = b"=h" |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
377 elif int_type == BSER_INT32: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
378 needed = 5 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
379 fmt = b"=i" |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
380 elif int_type == BSER_INT64: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
381 needed = 9 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
382 fmt = b"=q" |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
383 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
384 raise ValueError( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
385 "Invalid bser int encoding 0x%s at position %s" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
386 % (binascii.hexlify(int_type).decode("ascii"), pos) |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
387 ) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
388 int_val = struct.unpack_from(fmt, buf, pos + 1)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
389 return (int_val, pos + needed) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
390 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
391 def unser_utf8_string(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
392 str_len, pos = self.unser_int(buf, pos + 1) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
393 str_val = struct.unpack_from(tobytes(str_len) + b"s", buf, pos)[0] |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
394 return (str_val.decode("utf-8"), pos + str_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
395 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
396 def unser_bytestring(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
397 str_len, pos = self.unser_int(buf, pos + 1) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
398 str_val = struct.unpack_from(tobytes(str_len) + b"s", buf, pos)[0] |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
399 if self.value_encoding is not None: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
400 str_val = str_val.decode(self.value_encoding, self.value_errors) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
401 # str_len stays the same because that's the length in bytes |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
402 return (str_val, pos + str_len) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
403 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
404 def unser_array(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
405 arr_len, pos = self.unser_int(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
406 arr = [] |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
407 for _ in range(arr_len): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
408 arr_item, pos = self.loads_recursive(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
409 arr.append(arr_item) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
410 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
411 if not self.mutable: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
412 arr = tuple(arr) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
413 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
414 return arr, pos |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
415 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
416 def unser_object(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
417 obj_len, pos = self.unser_int(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
418 if self.mutable: |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
419 obj = {} |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
420 else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
421 keys = [] |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
422 vals = [] |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
423 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
424 for _ in range(obj_len): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
425 key, pos = self.unser_utf8_string(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
426 val, pos = self.loads_recursive(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
427 if self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
428 obj[key] = val |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
429 else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
430 keys.append(key) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
431 vals.append(val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
432 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
433 if not self.mutable: |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
434 obj = _BunserDict(keys, vals) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
435 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
436 return obj, pos |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
437 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
438 def unser_template(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
439 val_type = _buf_pos(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
440 if val_type != BSER_ARRAY: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
441 raise RuntimeError("Expect ARRAY to follow TEMPLATE") |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
442 # force UTF-8 on keys |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
443 keys_bunser = Bunser(mutable=self.mutable, value_encoding="utf-8") |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
444 keys, pos = keys_bunser.unser_array(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
445 nitems, pos = self.unser_int(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
446 arr = [] |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
447 for _ in range(nitems): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
448 if self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
449 obj = {} |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
450 else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
451 vals = [] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
452 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
453 for keyidx in range(len(keys)): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
454 if _buf_pos(buf, pos) == BSER_SKIP: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
455 pos += 1 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
456 ele = None |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
457 else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
458 ele, pos = self.loads_recursive(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
459 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
460 if self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
461 key = keys[keyidx] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
462 obj[key] = ele |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
463 else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
464 vals.append(ele) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
465 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
466 if not self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
467 obj = _BunserDict(keys, vals) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
468 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
469 arr.append(obj) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
470 return arr, pos |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
471 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
472 def loads_recursive(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
473 val_type = _buf_pos(buf, pos) |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
474 if ( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
475 val_type == BSER_INT8 |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
476 or val_type == BSER_INT16 |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
477 or val_type == BSER_INT32 |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
478 or val_type == BSER_INT64 |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
479 ): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
480 return self.unser_int(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
481 elif val_type == BSER_REAL: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
482 val = struct.unpack_from(b"=d", buf, pos + 1)[0] |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
483 return (val, pos + 9) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
484 elif val_type == BSER_TRUE: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
485 return (True, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
486 elif val_type == BSER_FALSE: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
487 return (False, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
488 elif val_type == BSER_NULL: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
489 return (None, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
490 elif val_type == BSER_BYTESTRING: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
491 return self.unser_bytestring(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
492 elif val_type == BSER_UTF8STRING: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
493 return self.unser_utf8_string(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
494 elif val_type == BSER_ARRAY: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
495 return self.unser_array(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
496 elif val_type == BSER_OBJECT: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
497 return self.unser_object(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
498 elif val_type == BSER_TEMPLATE: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
499 return self.unser_template(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
500 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
501 raise ValueError( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
502 "unhandled bser opcode 0x%s" |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
503 % binascii.hexlify(val_type).decode("ascii") |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
504 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
505 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
506 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
507 def _pdu_info_helper(buf): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
508 if buf[0:2] == EMPTY_HEADER[0:2]: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
509 bser_version = 1 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
510 bser_capabilities = 0 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
511 expected_len, pos2 = Bunser.unser_int(buf, 2) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
512 elif buf[0:2] == EMPTY_HEADER_V2[0:2]: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
513 if len(buf) < 8: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
514 raise ValueError("Invalid BSER header") |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
515 bser_version = 2 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
516 bser_capabilities = struct.unpack_from("I", buf, 2)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
517 expected_len, pos2 = Bunser.unser_int(buf, 6) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
518 else: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
519 raise ValueError("Invalid BSER header") |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
520 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
521 return bser_version, bser_capabilities, expected_len, pos2 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
522 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
523 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
524 def pdu_info(buf): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
525 info = _pdu_info_helper(buf) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
526 return info[0], info[1], info[2] + info[3] |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
527 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
528 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
529 def pdu_len(buf): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
530 info = _pdu_info_helper(buf) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
531 return info[2] + info[3] |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
532 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
533 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
534 def loads(buf, mutable=True, value_encoding=None, value_errors=None): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
535 """Deserialize a BSER-encoded blob. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
536 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
537 @param buf: The buffer to deserialize. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
538 @type buf: bytes |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
539 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
540 @param mutable: Whether to return mutable results. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
541 @type mutable: bool |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
542 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
543 @param value_encoding: Optional codec to use to decode values. If |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
544 unspecified or None, return values as bytestrings. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
545 @type value_encoding: str |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
546 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
547 @param value_errors: Optional error handler for codec. 'strict' by default. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
548 The other most common argument is 'surrogateescape' on |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
549 Python 3. If value_encoding is None, this is ignored. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
550 @type value_errors: str |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
551 """ |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
552 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
553 info = _pdu_info_helper(buf) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
554 expected_len = info[2] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
555 pos = info[3] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
556 |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
557 if len(buf) != expected_len + pos: |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
558 raise ValueError( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
559 "bser data len %d != header len %d" % (expected_len + pos, len(buf)) |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
560 ) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
561 |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
562 bunser = Bunser( |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
563 mutable=mutable, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
564 value_encoding=value_encoding, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
565 value_errors=value_errors, |
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
566 ) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
567 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
568 return bunser.loads_recursive(buf, pos)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
569 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
570 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
571 def load(fp, mutable=True, value_encoding=None, value_errors=None): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
572 from . import load |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
573 |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
574 return load.load(fp, mutable, value_encoding, value_errors) |