def test_oracle_monthdelta(oracle): if oracle: expected = misc.monthdelta(42) got = oracle(f"{oracle.pkg}.monthdelta(c_out, 42);") assert expected == got expected = misc.monthdelta(-42) got = oracle(f"{oracle.pkg}.monthdelta(c_out, -42);") assert expected == got
def test_nested(t): d = { "nix": None, "int": 42, "float": math.pi, "foo": "bar", "baz": datetime.datetime(2012, 10, 29, 16, 44, 55, 987000), "td": datetime.timedelta(-1, 1, 1), "md": misc.monthdelta(-1), "gurk": ["hurz"], } assert d == t(d)
def load(self): """ Deserialize the next object in the stream and return it. """ from ll import misc typecode = self._nextchar() if typecode == "^": position = self._readint() return self._objects[position] elif typecode in "nN": if typecode == "N": self._loading(None) return None elif typecode in "bB": value = self.stream.read(1) if value == "T": value = True elif value == "F": value = False else: raise ValueError(f"broken UL4ON stream at position {self.stream.tell():,}: expected 'T' or 'F' for bool; got {value!r}") if typecode == "B": self._loading(value) return value elif typecode in "iI": value = self._readint() if typecode == "I": self._loading(value) return value elif typecode in "fF": chars = [] while True: c = self.stream.read(1) if c and not c.isspace(): chars.append(c) else: value = float("".join(chars)) break if typecode == "F": self._loading(value) return value elif typecode in "sS": delimiter = self.stream.read(1) if not delimiter: raise EOFError() buffer = [] while True: c = self.stream.read(1) if not c: raise EOFError() if c == delimiter: value = "".join(buffer).encode("ascii", "backslashreplace").decode("unicode_escape") break buffer.append(c) if c == "\\": c2 = self.stream.read(1) if not c2: raise EOFError() buffer.append(c2) if typecode == "S": self._loading(value) return value elif typecode in "cC": from ll import color if typecode == "C": oldpos = self._beginfakeloading() r = self.load() g = self.load() b = self.load() a = self.load() value = color.Color(r, g, b, a) if typecode == "C": self._endfakeloading(oldpos, value) return value elif typecode in "zZ": if typecode == "Z": oldpos = self._beginfakeloading() year = self.load() month = self.load() day = self.load() hour = self.load() minute = self.load() second = self.load() microsecond = self.load() value = datetime.datetime(year, month, day, hour, minute, second, microsecond) if typecode == "Z": self._endfakeloading(oldpos, value) return value elif typecode in "xX": if typecode == "X": oldpos = self._beginfakeloading() year = self.load() month = self.load() day = self.load() value = datetime.date(year, month, day) if typecode == "X": self._endfakeloading(oldpos, value) return value elif typecode in "rR": if typecode == "R": oldpos = self._beginfakeloading() start = self.load() stop = self.load() value = slice(start, stop) if typecode == "R": self._endfakeloading(oldpos, value) return value elif typecode in "tT": if typecode == "T": oldpos = self._beginfakeloading() days = self.load() seconds = self.load() microseconds = self.load() value = datetime.timedelta(days, seconds, microseconds) if typecode == "T": self._endfakeloading(oldpos, value) return value elif typecode in "mM": from ll import misc if typecode == "M": oldpos = self._beginfakeloading() months = self.load() value = misc.monthdelta(months) if typecode == "M": self._endfakeloading(oldpos, value) return value elif typecode in "lL": self._stack.append("list") value = [] if typecode == "L": self._loading(value) while True: typecode = self._nextchar() if typecode == "]": self._stack.pop() return value else: self._bufferedchar = typecode item = self.load() value.append(item) elif typecode in "dDeE": self._stack.append("dict" if typecode in "dD" else "odict") value = {} # Load all dicts as a standard Python 3.6 ordered dict if typecode in "DE": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": self._stack.pop() return value else: self._bufferedchar = typecode key = self.load() if isinstance(key, str): if key in self._keycache: key = self._keycache[key] else: self._keycache[key] = key item = self.load() value[key] = item elif typecode in "yY": self._stack.append("set") value = set() if typecode == "Y": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": self._stack.pop() return value else: self._bufferedchar = typecode item = self.load() value.add(item) elif typecode in "oO": if typecode == "O": oldpos = self._beginfakeloading() name = self.load() self._stack.append(name) cls = None if self.registry is not None: cls = self.registry.get(name) if cls is None: cls = _registry.get(name) if cls is None: raise TypeError(f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): can't decode object of type {name!r}") value = cls() if typecode == "O": self._endfakeloading(oldpos, value) value.ul4onload(self) typecode = self._nextchar() if typecode != ")": raise ValueError(f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): object terminator ')' expected, got {typecode!r}") self._stack.pop() return value else: raise ValueError(f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): unknown typecode {typecode!r}")
def _load(self, typecode): from ll import misc if typecode is None: typecode = self._nextchar() if typecode == "^": position = self._readint() return self._objects[position] elif typecode in "nN": if typecode == "N": self._loading(None) return None elif typecode in "bB": value = self.stream.read(1) if value == "T": value = True elif value == "F": value = False else: raise ValueError( "broken UL4ON stream at position {}: expected 'T' or 'F' for bool; got {!r}" .format(self.stream.tell(), value)) if typecode == "B": self._loading(value) return value elif typecode in "iI": value = self._readint() if typecode == "I": self._loading(value) return value elif typecode in "fF": chars = [] while True: c = self.stream.read(1) if c and not c.isspace(): chars.append(c) else: value = float("".join(chars)) break if typecode == "F": self._loading(value) return value elif typecode in "sS": delimiter = self.stream.read(1) if not delimiter: raise EOFError() buffer = [delimiter] while True: c = self.stream.read(1) if not c: raise EOFError() buffer.append(c) if c == delimiter: value = ast.literal_eval("".join(buffer)) break elif c == "\\": c2 = self.stream.read(1) if not c2: raise EOFError() buffer.append(c2) if typecode == "S": self._loading(value) return value elif typecode in "cC": from ll import color if typecode == "C": oldpos = self._beginfakeloading() r = self._load(None) g = self._load(None) b = self._load(None) a = self._load(None) value = color.Color(r, g, b, a) if typecode == "C": self._endfakeloading(oldpos, value) return value elif typecode in "zZ": if typecode == "Z": oldpos = self._beginfakeloading() year = self._load(None) month = self._load(None) day = self._load(None) hour = self._load(None) minute = self._load(None) second = self._load(None) microsecond = self._load(None) value = datetime.datetime(year, month, day, hour, minute, second, microsecond) if typecode == "Z": self._endfakeloading(oldpos, value) return value elif typecode in "rR": if typecode == "R": oldpos = self._beginfakeloading() start = self._load(None) stop = self._load(None) value = slice(start, stop) if typecode == "R": self._endfakeloading(oldpos, value) return value elif typecode in "tT": if typecode == "T": oldpos = self._beginfakeloading() days = self._load(None) seconds = self._load(None) microseconds = self._load(None) value = datetime.timedelta(days, seconds, microseconds) if typecode == "T": self._endfakeloading(oldpos, value) return value elif typecode in "mM": from ll import misc if typecode == "M": oldpos = self._beginfakeloading() months = self._load(None) value = misc.monthdelta(months) if typecode == "M": self._endfakeloading(oldpos, value) return value elif typecode in "lL": value = [] if typecode == "L": self._loading(value) while True: typecode = self._nextchar() if typecode == "]": return value else: item = self._load(typecode) value.append(item) elif typecode in "dDeE": value = {} if typecode in "dD" else ordereddict() if typecode in "DE": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": return value else: key = self._load(typecode) if isinstance(key, str): if key in self._keycache: key = self._keycache[key] else: self._keycache[key] = key item = self._load(None) value[key] = item elif typecode in "yY": value = set() if typecode == "Y": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": return value else: item = self._load(typecode) value.add(item) elif typecode in "oO": if typecode == "O": oldpos = self._beginfakeloading() name = self._load(None) cls = None if self.registry is not None: cls = self.registry.get(name) if cls is None: cls = _registry.get(name) if cls is None: raise TypeError("can't decode object of type {}".format(name)) value = cls() if typecode == "O": self._endfakeloading(oldpos, value) value.ul4onload(self) typecode = self._nextchar() if typecode != ")": raise ValueError( "broken UL4ON stream at position {}: object terminator ')' expected, got {!r}" .format(self.stream.tell(), typecode)) return value else: raise ValueError( "broken UL4ON stream at position {}: unknown typecode {!r}". format(self.stream.tell(), typecode))
def test_monthdelta(t): d = misc.monthdelta(1) assert d == t(d)
def load(self): """ Deserialize the next object in the stream and return it. """ from ll import misc typecode = self._nextchar() if typecode == "^": position = self._readint() return self._objects[position] elif typecode in "nN": if typecode == "N": self._loading(None) return None elif typecode in "bB": value = self.stream.read(1) if value == "T": value = True elif value == "F": value = False else: raise ValueError( f"broken UL4ON stream at position {self.stream.tell():,}: expected 'T' or 'F' for bool; got {value!r}" ) if typecode == "B": self._loading(value) return value elif typecode in "iI": value = self._readint() if typecode == "I": self._loading(value) return value elif typecode in "fF": chars = [] while True: c = self.stream.read(1) if c and not c.isspace(): chars.append(c) else: value = float("".join(chars)) break if typecode == "F": self._loading(value) return value elif typecode in "sS": delimiter = self.stream.read(1) if not delimiter: raise EOFError() buffer = [] while True: c = self.stream.read(1) if not c: raise EOFError() if c == delimiter: value = "".join(buffer).encode( "ascii", "backslashreplace").decode("unicode_escape") break buffer.append(c) if c == "\\": c2 = self.stream.read(1) if not c2: raise EOFError() buffer.append(c2) if typecode == "S": self._loading(value) return value elif typecode in "cC": from ll import color if typecode == "C": oldpos = self._beginfakeloading() r = self.load() g = self.load() b = self.load() a = self.load() value = color.Color(r, g, b, a) if typecode == "C": self._endfakeloading(oldpos, value) return value elif typecode in "zZ": if typecode == "Z": oldpos = self._beginfakeloading() year = self.load() month = self.load() day = self.load() hour = self.load() minute = self.load() second = self.load() microsecond = self.load() value = datetime.datetime(year, month, day, hour, minute, second, microsecond) if typecode == "Z": self._endfakeloading(oldpos, value) return value elif typecode in "xX": if typecode == "X": oldpos = self._beginfakeloading() year = self.load() month = self.load() day = self.load() value = datetime.date(year, month, day) if typecode == "X": self._endfakeloading(oldpos, value) return value elif typecode in "rR": if typecode == "R": oldpos = self._beginfakeloading() start = self.load() stop = self.load() value = slice(start, stop) if typecode == "R": self._endfakeloading(oldpos, value) return value elif typecode in "tT": if typecode == "T": oldpos = self._beginfakeloading() days = self.load() seconds = self.load() microseconds = self.load() value = datetime.timedelta(days, seconds, microseconds) if typecode == "T": self._endfakeloading(oldpos, value) return value elif typecode in "mM": from ll import misc if typecode == "M": oldpos = self._beginfakeloading() months = self.load() value = misc.monthdelta(months) if typecode == "M": self._endfakeloading(oldpos, value) return value elif typecode in "lL": self._stack.append("list") value = [] if typecode == "L": self._loading(value) while True: typecode = self._nextchar() if typecode == "]": self._stack.pop() return value else: self._bufferedchar = typecode item = self.load() value.append(item) elif typecode in "dDeE": self._stack.append("dict" if typecode in "dD" else "odict") value = {} # Load all dicts as a standard Python 3.6 ordered dict if typecode in "DE": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": self._stack.pop() return value else: self._bufferedchar = typecode key = self.load() if isinstance(key, str): if key in self._keycache: key = self._keycache[key] else: self._keycache[key] = key item = self.load() value[key] = item elif typecode in "yY": self._stack.append("set") value = set() if typecode == "Y": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": self._stack.pop() return value else: self._bufferedchar = typecode item = self.load() value.add(item) elif typecode in "oO": if typecode == "O": oldpos = self._beginfakeloading() name = self.load() self._stack.append(name) cls = None if self.registry is not None: cls = self.registry.get(name) if cls is None: cls = _registry.get(name) if cls is None: raise TypeError( f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): can't decode object of type {name!r}" ) value = cls() if typecode == "O": self._endfakeloading(oldpos, value) value.ul4onload(self) typecode = self._nextchar() if typecode != ")": raise ValueError( f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): object terminator ')' expected, got {typecode!r}" ) self._stack.pop() return value else: raise ValueError( f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): unknown typecode {typecode!r}" )
def test_oracle_monthdelta(oracle): if oracle: assert misc.monthdelta(42) == oracle( "ul4on_pkg.monthdelta(c_out, 42);") assert misc.monthdelta(-42) == oracle( "ul4on_pkg.monthdelta(c_out, -42);")