def test_encode_long_integer(self): self.assertEqual(b"\x83n\4\0\0\0\0\x80", encode(2147483648)) self.assertEqual(b"\x83n\4\1\1\0\0\x80", encode(-2147483649)) self.assertEqual(b"\x83o\0\0\1\0\0" + b"\0" * 255 + b"\1", encode(2 ** 2040)) self.assertEqual(b"\x83o\0\0\1\0\1" + b"\0" * 255 + b"\1", encode(-2 ** 2040))
def test_encode_compressed_term(self): self.assertEqual(b"\x83l\x00\x00\x00\x05jjjjjj", encode([[]] * 5, True)) self.assertEqual(b"\x83P\x00\x00\x00\x15" b"x\x9c\xcba``\xe0\xcfB\x03\x00B@\x07\x1c", encode([[]] * 15, True)) self.assertEqual(b"\x83P\x00\x00\x00\x15" b"x\x9c\xcba``\xe0\xcfB\x03\x00B@\x07\x1c", encode([[]] * 15, 6)) self.assertEqual(b"\x83P\x00\x00\x00\x15" b"x\xda\xcba``\xe0\xcfB\x03\x00B@\x07\x1c", encode([[]] * 15, 9)) self.assertEqual(b"\x83l\0\0\0\x0f" + b"j" * 15 + b"j", encode([[]] * 15, 0)) self.assertEqual(b"\x83P\x00\x00\x00\x15" b"x\x01\xcba``\xe0\xcfB\x03\x00B@\x07\x1c", encode([[]] * 15, 1))
def test_encode_list(self): self.assertEqual("\x83l\0\0\0\1jj", encode([[]])) self.assertEqual("\x83l\0\0\0\5jjjjjj", encode([[], [], [], [], []])) self.assertEqual( "\x83l\0\0\0\5jjjjjj", encode(List([List([]), List([]), List([]), List([]), List([])])))
def test_encode_unicode(self): self.assertEqual(b"\x83j", encode("")) self.assertEqual(b"\x83k\0\4test", encode("test")) self.assertEqual(b"\x83k\0\2\0\xff", encode("\0\xff")) self.assertEqual(b"\x83l\0\0\0\1b\0\0\1\0j", encode("\u0100")) self.assertEqual(b"\x83l\0\0\0\4b\0\0\4Bb\0\0\x045b\0\0\4Ab\0\0\4Bj", encode("\u0442\u0435\u0441\u0442")) self.assertEqual(b"\x83l\0\1\0\0" + b"aX" * 65536 + b"j", encode("X" * 65536)) self.assertEqual(b"\x83l\0\1\0\0" + b"b\0\0\4\x10" * 65536 + b"j", encode("\u0410" * 65536))
def write_result(self, response): """Write outgoing result.""" data = encode(response) data = pack(self._format, len(data)) + data try: self._fout.write(data) self._fout.flush() except IOError, why: if why.errno == errno.EPIPE: raise EOFError() raise
def write(self, message): """Write outgoing message.""" data = encode(message, compressed=self.compressed) data = pack(self._format, len(data)) + data while len(data) != 0: try: n = os.write(self.out_d, data) except IOError, why: if why.errno == errno.EPIPE: raise EOFError() raise if n == 0: raise EOFError() data = data[n:]
def write(self, message): """Write outgoing message.""" data = encode(message) data = pack(self._format, len(data)) + data length = len(data) if length != 0: try: n = os.write(self.out_d, data) except IOError, why: if why.errno == errno.EPIPE: raise EOFError() raise if n == 0: raise EOFError() length -= n
def write(self, message): """Write outgoing message.""" data = encode(message, compressed=self.compressed) length = len(data) data = self.__pack(length) + data with self.__write_lock: while data: try: n = os.write(self.out_d, data) except OSError, why: if why.errno in (errno.EPIPE, errno.EINVAL): raise EOFError() raise if not n: raise EOFError() data = data[n:]
def test_encode_improper_list(self): self.assertEqual(b"\x83l\0\0\0\1h\0h\0", encode(ImproperList([()], ()))) self.assertEqual(b"\x83l\0\0\0\1a\0a\1", encode(ImproperList([0], 1)))
def test_encode_atom(self): self.assertEqual(b"\x83d\0\0", encode(Atom(b""))) self.assertEqual(b"\x83d\0\4test", encode(Atom(b"test")))
def test_encode_empty_list(self): self.assertEqual(b"\x83j", encode([]))
def test_encode_none(self): self.assertEqual(b"\x83d\0\11undefined", encode(None))
def test_encode_string_list(self): self.assertEqual("\x83k\0\1\0", encode([0])) r = range(0, 256) self.assertEqual("\x83k\1\0" + "".join(map(chr, r)), encode(r))
def test_simple_list(self): input = List([1, 2, 3]) self.assertEquals((input, b""), decode(encode(input)))
def test_encode_float(self): self.assertEqual(b"\x83F\0\0\0\0\0\0\0\0", encode(0.0)) self.assertEqual(b"\x83F?\xe0\0\0\0\0\0\0", encode(0.5)) self.assertEqual(b"\x83F\xbf\xe0\0\0\0\0\0\0", encode(-0.5)) self.assertEqual(b"\x83F@\t!\xfbM\x12\xd8J", encode(3.1415926)) self.assertEqual(b"\x83F\xc0\t!\xfbM\x12\xd8J", encode(-3.1415926))
def test_encode_opaque_object(self): self.assertEqual( b"\x83h\3d\0\x0f$erlport.opaqued\0\10language" b"m\0\0\0\4data", encode(OpaqueObject(b"data", Atom(b"language")))) self.assertEqual(b"\x83data", encode(OpaqueObject(b"data", Atom(b"erlang"))))
def test_empty_list(self): input = List() self.assertEquals((input, b""), decode(encode(input)))
def test_encode_integer(self): self.assertEqual(b"\x83b\xff\xff\xff\xff", encode(-1)) self.assertEqual(b"\x83b\x80\0\0\0", encode(-2147483648)) self.assertEqual(b"\x83b\0\0\1\0", encode(256)) self.assertEqual(b"\x83b\x7f\xff\xff\xff", encode(2147483647))
def test_encode_short_integer(self): self.assertEqual(b"\x83a\0", encode(0)) self.assertEqual(b"\x83a\xff", encode(255))
def test_encode_boolean(self): self.assertEqual(b"\x83d\0\4true", encode(True)) self.assertEqual(b"\x83d\0\5false", encode(False))
def test_nested_list(self): input = List([[[[[[]]]]]]) self.assertEquals((input, b""), decode(encode(input)))
def test_empty_map(self): input = Map() self.assertEquals((input, b""), decode(encode(input))) input = {} self.assertEquals((input, b""), decode(encode(input)))
def test_encode_python_opaque_object(self): self.assertEqual( b"\x83h\x03d\x00\x0f$erlport.opaqued\x00\x06python" b"m\x00\x00\x00\x06\x80\x02}q\x00.", encode(dict()) ) self.assertRaises(ValueError, encode, compile(b"0", b"<string>", "eval"))
def test_simple_map(self): input = Map({b"hello": b"world"}) self.assertEquals((input, b""), decode(encode(input)))
def test_encode_empty_map(self): self.assertEqual(b"\x83t\x00\x00\x00\x00", encode(Map()))
def test_big_map(self): input = {b"hello_" + str(i): b"world" + str(i) for i in range(10000)} self.assertEquals((input, b""), decode(encode(input)))
def test_encode_list(self): self.assertEqual(b"\x83l\0\0\0\1jj", encode([[]])) self.assertEqual(b"\x83l\0\0\0\5jjjjjj", encode([[], [], [], [], []])) self.assertEqual(b"\x83l\0\0\0\5jjjjjj", encode(List([List([]), List([]), List([]), List([]), List([])])))
def test_hash_list(self): input = Map({List([1, 2, 3]): [4, 5, 6]}) self.assertEquals(input[List([1, 2, 3])], List([4, 5, 6])) self.assertEquals((input, b""), decode(encode(input)))
def test_encode_tuple(self): self.assertEqual(b"\x83h\0", encode(())) self.assertEqual(b"\x83h\2h\0h\0", encode(((), ()))) self.assertEqual(b"\x83h\xff" + b"h\0" * 255, encode(tuple([()] * 255))) self.assertEqual(b"\x83i\0\0\1\0" + b"h\0" * 256, encode(tuple([()] * 256)))
def test_hash_improper_list(self): input = Map({ImproperList([1, 2, 3], 1000): b"xxx"}) self.assertEquals((input, ""), decode(encode(input))) self.assertEquals(input[ImproperList([1, 2, 3], 1000)], b"xxx")
def test_encode_string_list(self): self.assertEqual(b"\x83k\0\1\0", encode([0])) r = list(range(0, 256)) self.assertEqual(b"\x83k\1\0" + bytes(r), encode(r))
def test_opaque(self): input = _TestObj(100) output, _ = decode(encode(input)) self.assertEquals(input.v, input.v)
def test_encode_string(self): self.assertEqual(b"\x83m\0\0\0\0", encode(b"")) self.assertEqual(b"\x83m\0\0\0\4test", encode(b"test"))
def test_encode_opaque_object(self): self.assertEqual( b"\x83h\3d\0\x0f$erlport.opaqued\0\10language" b"m\0\0\0\4data", encode(OpaqueObject(b"data", Atom(b"language"))), ) self.assertEqual(b"\x83data", encode(OpaqueObject(b"data", Atom(b"erlang"))))
def test_encode_python_opaque_object(self): self.assertEqual(b"\x83h\x03d\x00\x0f$erlport.opaqued\x00\x06python" b"m\x00\x00\x00\x06\x80\x02}q\x00.", encode(dict())) self.assertRaises(ValueError, encode, compile(b"0", b"<string>", "eval"))