def test_symbols(self): a = Symbol("test1") b = Symbol("test2") c = Symbol("test1") self.assertNotEqual(a, b) self.assertEqual(a, c) self.assertEqual(id(a), id(c))
def write_string(self, obj): obj = obj.encode("utf-8") self.fd.write(TYPE_IVAR) self.write_bytes(obj) self.write_long(1) self.write(Symbol("E")) self.write(True)
def write_ruby_object(self, obj): if self.must_write(obj): self.fd.write(TYPE_OBJECT) self.write(Symbol(obj.ruby_class_name)) if not isinstance(obj.attributes, dict): raise ValueError("%r values is not a dict" % obj) self.write_attributes(obj.attributes)
def test_reader_read_symbol(): fd = io.BytesIO(b"\x04\b:\x10test_symbol") assert fd.read(1) == b"\x04" assert fd.read(1) == b"\x08" loader = Reader(fd) assert loader.read_symbol() == Symbol("test_symbol")
def convert_string_to_symbols(msg): msg_dict = {} for key, value in msg.items(): if key.startswith(':'): key = Symbol(key[1:]) if isinstance(value, dict): value = convert_string_to_symbols(value) msg_dict[key] = value return msg_dict
def write_usr_marshal(self, obj): if self.must_write(obj): if obj.attributes: self.fd.write(TYPE_IVAR) self.fd.write(TYPE_USRMARSHAL) self.write(Symbol(obj.ruby_class_name)) private_data = obj.marshal_dump() self.write(private_data) if obj.attributes: self.write_attributes(obj.attributes)
def write_user_def(self, obj): if self.must_write(obj): if obj.attributes: self.fd.write(TYPE_IVAR) self.fd.write(TYPE_USERDEF) self.write(Symbol(obj.ruby_class_name)) # noinspection PyProtectedMember bdata = obj._dump() self.write_long(len(bdata)) self.fd.write(bdata) if obj.attributes: self.write_attributes(obj.attributes)
def write_regexp(self, obj): flags = 0 if obj.flags & re.IGNORECASE: flags += 1 if obj.flags & re.MULTILINE: flags += 4 self.fd.write(TYPE_IVAR) self.fd.write(TYPE_REGEXP) pattern = obj.pattern.encode("utf-8") self.write_long(len(pattern)) self.fd.write(pattern) write_ubyte(self.fd, flags) self.write_long(1) self.write(Symbol("E")) self.write(False)
def write_attributes(self, attributes): self.write_long(len(attributes)) for attr_name, attr_value in attributes.items(): self.write(Symbol(attr_name)) self.write(attr_value)
def test_symlinks(self): self.check([Symbol("hello"), Symbol("hello")], "5b07 3a0a 6865 6c6c 6f3b 00")
def test_symbol(self): self.check(Symbol("hello"), "3a0a 6865 6c6c 6f")
def test_symlink(self): a = Symbol("test_symbol") self.assertEqual(loads(b"\x04\b[\a:\x10test_symbol;\x00"), [a, a])
def test_symbol(self): self.assertEqual(loads(b"\x04\b:\x10test_symbol"), Symbol("test_symbol"))
def test_write_constant(self): dumped = writes([Constant("test")], cls=ConstantWriter) read_constant = loads(dumped) self.assertEqual([Symbol("test")], read_constant)
def write_python_object(self, obj): if isinstance(obj, Constant): return self.write(Symbol(obj.name)) super().write_python_object(obj)
def read_symreal(self): size = self.read_long() result = self.fd.read(size) result = Symbol(result.decode("utf-8")) self.symbols.append(result) return result