author | Boris Feld <boris.feld@octobus.net> |
Fri, 13 Jul 2018 03:05:30 +0200 | |
changeset 41088 | 5608b5a6c323 |
parent 30656 | 16f4b341288d |
child 41365 | 876494fd967d |
permissions | -rw-r--r-- |
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 |
from __future__ import absolute_import |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
30 |
from __future__ import division |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
31 |
from __future__ import print_function |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
32 |
# no unicode literals |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
33 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
34 |
import binascii |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
35 |
import collections |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
36 |
import ctypes |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
37 |
import struct |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
38 |
import sys |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
39 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
40 |
from . import ( |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
41 |
compat, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
42 |
) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
43 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
44 |
BSER_ARRAY = b'\x00' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
45 |
BSER_OBJECT = b'\x01' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
46 |
BSER_BYTESTRING = b'\x02' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
47 |
BSER_INT8 = b'\x03' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
48 |
BSER_INT16 = b'\x04' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
49 |
BSER_INT32 = b'\x05' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
50 |
BSER_INT64 = b'\x06' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
51 |
BSER_REAL = b'\x07' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
52 |
BSER_TRUE = b'\x08' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
53 |
BSER_FALSE = b'\x09' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
54 |
BSER_NULL = b'\x0a' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
55 |
BSER_TEMPLATE = b'\x0b' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
56 |
BSER_SKIP = b'\x0c' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
57 |
BSER_UTF8STRING = b'\x0d' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
58 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
59 |
if compat.PYTHON3: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
60 |
STRING_TYPES = (str, bytes) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
61 |
unicode = str |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
62 |
def tobytes(i): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
63 |
return str(i).encode('ascii') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
64 |
long = int |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
65 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
66 |
STRING_TYPES = (unicode, str) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
67 |
tobytes = bytes |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
68 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
69 |
# Leave room for the serialization header, which includes |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
70 |
# 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
|
71 |
# int32 for the header |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
75 |
def _int_size(x): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
76 |
"""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
|
77 |
if -0x80 <= x <= 0x7F: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
78 |
return 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
79 |
elif -0x8000 <= x <= 0x7FFF: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
80 |
return 2 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
81 |
elif -0x80000000 <= x <= 0x7FFFFFFF: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
82 |
return 4 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
83 |
elif long(-0x8000000000000000) <= x <= long(0x7FFFFFFFFFFFFFFF): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
84 |
return 8 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
85 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
86 |
raise RuntimeError('Cannot represent value: ' + str(x)) |
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] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
90 |
# In Python 2, buf is a str array so buf[pos] is a string. In Python 3, buf |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
91 |
# is a bytes array and buf[pos] is an integer. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
92 |
if compat.PYTHON3: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
93 |
ret = bytes((ret,)) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
94 |
return ret |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
95 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
96 |
class _bser_buffer(object): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
97 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
98 |
def __init__(self, version): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
99 |
self.bser_version = version |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
100 |
self.buf = ctypes.create_string_buffer(8192) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
101 |
if self.bser_version == 1: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
102 |
struct.pack_into(tobytes(len(EMPTY_HEADER)) + b's', self.buf, 0, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
103 |
EMPTY_HEADER) |
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 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
107 |
struct.pack_into(tobytes(len(EMPTY_HEADER_V2)) + b's', self.buf, 0, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
108 |
EMPTY_HEADER_V2) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
109 |
self.wpos = len(EMPTY_HEADER_V2) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
110 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
111 |
def ensure_size(self, size): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
112 |
while ctypes.sizeof(self.buf) - self.wpos < size: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
113 |
ctypes.resize(self.buf, ctypes.sizeof(self.buf) * 2) |
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 append_long(self, val): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
116 |
size = _int_size(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
117 |
to_write = size + 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
118 |
self.ensure_size(to_write) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
119 |
if size == 1: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
120 |
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
|
121 |
elif size == 2: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
122 |
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
|
123 |
elif size == 4: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
124 |
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
|
125 |
elif size == 8: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
126 |
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
|
127 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
128 |
raise RuntimeError('Cannot represent this long value') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
129 |
self.wpos += to_write |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
130 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
131 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
132 |
def append_string(self, s): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
133 |
if isinstance(s, unicode): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
134 |
s = s.encode('utf-8') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
135 |
s_len = len(s) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
136 |
size = _int_size(s_len) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
137 |
to_write = 2 + size + s_len |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
138 |
self.ensure_size(to_write) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
139 |
if size == 1: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
140 |
struct.pack_into(b'=ccb' + tobytes(s_len) + b's', self.buf, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
141 |
self.wpos, BSER_BYTESTRING, BSER_INT8, s_len, s) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
142 |
elif size == 2: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
143 |
struct.pack_into(b'=cch' + tobytes(s_len) + b's', self.buf, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
144 |
self.wpos, BSER_BYTESTRING, BSER_INT16, s_len, s) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
145 |
elif size == 4: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
146 |
struct.pack_into(b'=cci' + tobytes(s_len) + b's', self.buf, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
147 |
self.wpos, BSER_BYTESTRING, BSER_INT32, s_len, s) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
148 |
elif size == 8: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
149 |
struct.pack_into(b'=ccq' + tobytes(s_len) + b's', self.buf, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
150 |
self.wpos, BSER_BYTESTRING, BSER_INT64, s_len, s) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
151 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
152 |
raise RuntimeError('Cannot represent this string value') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
153 |
self.wpos += to_write |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
154 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
155 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
156 |
def append_recursive(self, val): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
157 |
if isinstance(val, bool): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
158 |
needed = 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
159 |
self.ensure_size(needed) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
160 |
if val: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
161 |
to_encode = BSER_TRUE |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
162 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
163 |
to_encode = BSER_FALSE |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
164 |
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
|
165 |
self.wpos += needed |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
166 |
elif val is None: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
167 |
needed = 1 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
168 |
self.ensure_size(needed) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
169 |
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
|
170 |
self.wpos += needed |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
171 |
elif isinstance(val, (int, long)): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
172 |
self.append_long(val) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
173 |
elif isinstance(val, STRING_TYPES): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
174 |
self.append_string(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
175 |
elif isinstance(val, float): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
176 |
needed = 9 |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
177 |
self.ensure_size(needed) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
178 |
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
|
179 |
self.wpos += needed |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
180 |
elif isinstance(val, collections.Mapping) and \ |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
181 |
isinstance(val, collections.Sized): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
182 |
val_len = len(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
183 |
size = _int_size(val_len) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
184 |
needed = 2 + size |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
185 |
self.ensure_size(needed) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
186 |
if size == 1: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
187 |
struct.pack_into(b'=ccb', self.buf, self.wpos, BSER_OBJECT, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
188 |
BSER_INT8, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
189 |
elif size == 2: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
190 |
struct.pack_into(b'=cch', self.buf, self.wpos, BSER_OBJECT, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
191 |
BSER_INT16, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
192 |
elif size == 4: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
193 |
struct.pack_into(b'=cci', self.buf, self.wpos, BSER_OBJECT, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
194 |
BSER_INT32, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
195 |
elif size == 8: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
196 |
struct.pack_into(b'=ccq', self.buf, self.wpos, BSER_OBJECT, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
197 |
BSER_INT64, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
198 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
199 |
raise RuntimeError('Cannot represent this mapping value') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
200 |
self.wpos += needed |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
201 |
if compat.PYTHON3: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
202 |
iteritems = val.items() |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
203 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
204 |
iteritems = val.iteritems() |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
205 |
for k, v in iteritems: |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
206 |
self.append_string(k) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
207 |
self.append_recursive(v) |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
208 |
elif isinstance(val, collections.Iterable) and \ |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
209 |
isinstance(val, collections.Sized): |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
210 |
val_len = len(val) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
211 |
size = _int_size(val_len) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
212 |
needed = 2 + size |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
213 |
self.ensure_size(needed) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
214 |
if size == 1: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
215 |
struct.pack_into(b'=ccb', self.buf, self.wpos, BSER_ARRAY, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
216 |
BSER_INT8, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
217 |
elif size == 2: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
218 |
struct.pack_into(b'=cch', self.buf, self.wpos, BSER_ARRAY, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
219 |
BSER_INT16, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
220 |
elif size == 4: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
221 |
struct.pack_into(b'=cci', self.buf, self.wpos, BSER_ARRAY, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
222 |
BSER_INT32, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
223 |
elif size == 8: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
224 |
struct.pack_into(b'=ccq', self.buf, self.wpos, BSER_ARRAY, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
225 |
BSER_INT64, val_len) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
226 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
227 |
raise RuntimeError('Cannot represent this sequence value') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
228 |
self.wpos += needed |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
229 |
for v in val: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
230 |
self.append_recursive(v) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
231 |
else: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
232 |
raise RuntimeError('Cannot represent unknown value type') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
233 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
234 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
235 |
def dumps(obj, version=1, capabilities=0): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
236 |
bser_buf = _bser_buffer(version=version) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
237 |
bser_buf.append_recursive(obj) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
238 |
# Now fill in the overall length |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
239 |
if version == 1: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
240 |
obj_len = bser_buf.wpos - len(EMPTY_HEADER) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
241 |
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
|
242 |
else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
243 |
obj_len = bser_buf.wpos - len(EMPTY_HEADER_V2) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
244 |
struct.pack_into(b'=i', bser_buf.buf, 2, capabilities) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
245 |
struct.pack_into(b'=i', bser_buf.buf, 7, obj_len) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
246 |
return bser_buf.buf.raw[:bser_buf.wpos] |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
247 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
248 |
# 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
|
249 |
# 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
|
250 |
# and name. |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
251 |
class _BunserDict(object): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
252 |
__slots__ = ('_keys', '_values') |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
253 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
254 |
def __init__(self, keys, values): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
255 |
self._keys = keys |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
256 |
self._values = values |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
257 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
258 |
def __getattr__(self, name): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
259 |
return self.__getitem__(name) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
260 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
261 |
def __getitem__(self, key): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
262 |
if isinstance(key, (int, long)): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
263 |
return self._values[key] |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
264 |
elif key.startswith('st_'): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
265 |
# 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
|
266 |
# reference "size" |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
267 |
key = key[3:] |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
268 |
try: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
269 |
return self._values[self._keys.index(key)] |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
270 |
except ValueError as ex: |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
271 |
raise KeyError('_BunserDict has no key %s' % key) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
272 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
273 |
def __len__(self): |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
274 |
return len(self._keys) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
275 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
276 |
class Bunser(object): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
277 |
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
|
278 |
self.mutable = mutable |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
279 |
self.value_encoding = value_encoding |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
280 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
281 |
if value_encoding is None: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
282 |
self.value_errors = None |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
283 |
elif value_errors is None: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
284 |
self.value_errors = 'strict' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
285 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
286 |
self.value_errors = value_errors |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
287 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
288 |
@staticmethod |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
289 |
def unser_int(buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
290 |
try: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
291 |
int_type = _buf_pos(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
292 |
except IndexError: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
293 |
raise ValueError('Invalid bser int encoding, pos out of range') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
294 |
if int_type == BSER_INT8: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
295 |
needed = 2 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
296 |
fmt = b'=b' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
297 |
elif int_type == BSER_INT16: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
298 |
needed = 3 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
299 |
fmt = b'=h' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
300 |
elif int_type == BSER_INT32: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
301 |
needed = 5 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
302 |
fmt = b'=i' |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
303 |
elif int_type == BSER_INT64: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
304 |
needed = 9 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
305 |
fmt = b'=q' |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
306 |
else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
307 |
raise ValueError('Invalid bser int encoding 0x%s' % |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
308 |
binascii.hexlify(int_type).decode('ascii')) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
309 |
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
|
310 |
return (int_val, pos + needed) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
311 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
312 |
def unser_utf8_string(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
313 |
str_len, pos = self.unser_int(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
314 |
str_val = struct.unpack_from(tobytes(str_len) + b's', buf, pos)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
315 |
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
|
316 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
317 |
def unser_bytestring(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
318 |
str_len, pos = self.unser_int(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
319 |
str_val = struct.unpack_from(tobytes(str_len) + b's', buf, pos)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
320 |
if self.value_encoding is not None: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
321 |
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
|
322 |
# 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
|
323 |
return (str_val, pos + str_len) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
324 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
325 |
def unser_array(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
326 |
arr_len, pos = self.unser_int(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
327 |
arr = [] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
328 |
for i in range(arr_len): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
329 |
arr_item, pos = self.loads_recursive(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
330 |
arr.append(arr_item) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
331 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
332 |
if not self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
333 |
arr = tuple(arr) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
334 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
335 |
return arr, pos |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
336 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
337 |
def unser_object(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
338 |
obj_len, pos = self.unser_int(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
339 |
if self.mutable: |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
340 |
obj = {} |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
341 |
else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
342 |
keys = [] |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
343 |
vals = [] |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
344 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
345 |
for i in range(obj_len): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
346 |
key, pos = self.unser_utf8_string(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
347 |
val, pos = self.loads_recursive(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
348 |
if self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
349 |
obj[key] = val |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
350 |
else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
351 |
keys.append(key) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
352 |
vals.append(val) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
353 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
354 |
if not self.mutable: |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
355 |
obj = _BunserDict(keys, vals) |
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
356 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
357 |
return obj, pos |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
358 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
359 |
def unser_template(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
360 |
val_type = _buf_pos(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
361 |
if val_type != BSER_ARRAY: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
362 |
raise RuntimeError('Expect ARRAY to follow TEMPLATE') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
363 |
# force UTF-8 on keys |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
364 |
keys_bunser = Bunser(mutable=self.mutable, value_encoding='utf-8') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
365 |
keys, pos = keys_bunser.unser_array(buf, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
366 |
nitems, pos = self.unser_int(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
367 |
arr = [] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
368 |
for i in range(nitems): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
369 |
if self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
370 |
obj = {} |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
371 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
372 |
vals = [] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
373 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
374 |
for keyidx in range(len(keys)): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
375 |
if _buf_pos(buf, pos) == BSER_SKIP: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
376 |
pos += 1 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
377 |
ele = None |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
378 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
379 |
ele, pos = self.loads_recursive(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
380 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
381 |
if self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
382 |
key = keys[keyidx] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
383 |
obj[key] = ele |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
384 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
385 |
vals.append(ele) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
386 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
387 |
if not self.mutable: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
388 |
obj = _BunserDict(keys, vals) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
389 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
390 |
arr.append(obj) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
391 |
return arr, pos |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
392 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
393 |
def loads_recursive(self, buf, pos): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
394 |
val_type = _buf_pos(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
395 |
if (val_type == BSER_INT8 or val_type == BSER_INT16 or |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
396 |
val_type == BSER_INT32 or val_type == BSER_INT64): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
397 |
return self.unser_int(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
398 |
elif val_type == BSER_REAL: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
399 |
val = struct.unpack_from(b'=d', buf, pos + 1)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
400 |
return (val, pos + 9) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
401 |
elif val_type == BSER_TRUE: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
402 |
return (True, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
403 |
elif val_type == BSER_FALSE: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
404 |
return (False, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
405 |
elif val_type == BSER_NULL: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
406 |
return (None, pos + 1) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
407 |
elif val_type == BSER_BYTESTRING: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
408 |
return self.unser_bytestring(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
409 |
elif val_type == BSER_UTF8STRING: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
410 |
return self.unser_utf8_string(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
411 |
elif val_type == BSER_ARRAY: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
412 |
return self.unser_array(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
413 |
elif val_type == BSER_OBJECT: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
414 |
return self.unser_object(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
415 |
elif val_type == BSER_TEMPLATE: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
416 |
return self.unser_template(buf, pos) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
417 |
else: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
418 |
raise ValueError('unhandled bser opcode 0x%s' % |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
419 |
binascii.hexlify(val_type).decode('ascii')) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
420 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
421 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
422 |
def _pdu_info_helper(buf): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
423 |
bser_version = -1 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
424 |
if buf[0:2] == EMPTY_HEADER[0:2]: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
425 |
bser_version = 1 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
426 |
bser_capabilities = 0 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
427 |
expected_len, pos2 = Bunser.unser_int(buf, 2) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
428 |
elif buf[0:2] == EMPTY_HEADER_V2[0:2]: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
429 |
if len(buf) < 8: |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
430 |
raise ValueError('Invalid BSER header') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
431 |
bser_version = 2 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
432 |
bser_capabilities = struct.unpack_from("I", buf, 2)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
433 |
expected_len, pos2 = Bunser.unser_int(buf, 6) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
434 |
else: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
435 |
raise ValueError('Invalid BSER header') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
436 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
437 |
return bser_version, bser_capabilities, expected_len, pos2 |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
438 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
439 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
440 |
def pdu_info(buf): |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
441 |
info = _pdu_info_helper(buf) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
442 |
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
|
443 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
444 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
445 |
def pdu_len(buf): |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
446 |
info = _pdu_info_helper(buf) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
447 |
return info[2] + info[3] |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
448 |
|
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
449 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
450 |
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
|
451 |
"""Deserialize a BSER-encoded blob. |
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 |
@param buf: The buffer to deserialize. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
454 |
@type buf: bytes |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
455 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
456 |
@param mutable: Whether to return mutable results. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
457 |
@type mutable: bool |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
458 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
459 |
@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
|
460 |
unspecified or None, return values as bytestrings. |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
461 |
@type value_encoding: str |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
462 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
463 |
@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
|
464 |
The other most common argument is 'surrogateescape' on |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
465 |
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
|
466 |
@type value_errors: str |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
467 |
""" |
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 |
info = _pdu_info_helper(buf) |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
470 |
expected_len = info[2] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
471 |
pos = info[3] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
472 |
|
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
473 |
if len(buf) != expected_len + pos: |
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
474 |
raise ValueError('bser data len != header len') |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
475 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
476 |
bunser = Bunser(mutable=mutable, value_encoding=value_encoding, |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
477 |
value_errors=value_errors) |
28432
2377c4ac4eec
fsmonitor: dependencies for new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
478 |
|
30656
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
479 |
return bunser.loads_recursive(buf, pos)[0] |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
480 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
481 |
|
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
482 |
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
|
483 |
from . import load |
16f4b341288d
fsmonitor: refresh pywatchman to upstream
Zack Hricz <zphricz@fb.com>
parents:
28432
diff
changeset
|
484 |
return load.load(fp, mutable, value_encoding, value_errors) |