def _printOn(self, printer): printer.call(u"print", [StrObject(u"[")]) for i, obj in enumerate(self.objs): printer.call(u"quote", [obj]) if i + 1 < len(self.objs): printer.call(u"print", [StrObject(u", ")]) printer.call(u"print", [StrObject(u"]")])
def printOn(self, printer): printer.call(u"print", [StrObject(u"[")]) for i, obj in enumerate(self.objectSet.keys()): printer.call(u"quote", [obj]) if i + 1 < len(self.objectSet): printer.call(u"print", [StrObject(u", ")]) printer.call(u"print", [StrObject(u"].asSet()")])
def printOn(self, out): out.call(u"print", [StrObject(u"Any[")]) for i, subguard in enumerate(self.subguards): out.call(u"print", [subguard]) if i < (len(self.subguards) - 1): out.call(u"print", [StrObject(u", ")]) out.call(u"print", [StrObject(u"]")])
def connectCB(connect, status): status = intmask(status) stream = connect.c_handle try: vat, resolvers = ruv.unstashStream(stream) fountResolver, drainResolver = unwrapList(resolvers) assert isinstance(fountResolver, LocalResolver) assert isinstance(drainResolver, LocalResolver) with scopedVat(vat): if status >= 0: debug_print("Made connection!") fountResolver.resolve(StreamFount(stream, vat)) drainResolver.resolve(StreamDrain(stream, vat)) else: error = "Connection failed: " + ruv.formatError(status) debug_print(error) fountResolver.smash(StrObject(error.decode("utf-8"))) drainResolver.smash(StrObject(error.decode("utf-8"))) # Done with stream. ruv.closeAndFree(stream) except: if not we_are_translated(): raise
def uncall(self): return wrapList([ StrObject(self._d if self._d else u""), StrObject(self._verb), wrapList(self._ps), wrapList(self._namedParams), self._g if self._g is not None else NullObject, self._b ])
def printOn(self, printer): printer.call(u"print", [StrObject(u"[")]) items = self.strategy.fetch_all(self) for i, obj in enumerate(items): printer.call(u"quote", [obj]) if i + 1 < len(items): printer.call(u"print", [StrObject(u", ")]) printer.call(u"print", [StrObject(u"].diverge()")])
def testContainsTrue(self): """ String containment tests have true positives. """ haystack = StrObject(u"needle in a haystack") needle = StrObject(u"needle") result = haystack.call(u"contains", [needle]) self.assertTrue(result.isTrue())
def testSplit(self): """ Strings can be split. """ s = StrObject(u"first second") result = s.call(u"split", [StrObject(u" ")]) pieces = [obj._s for obj in unwrapList(result)] self.assertEqual(pieces, [u"first", u"second"])
def unsealException(specimen, ej): """ Unseal a specimen. """ if isinstance(specimen, SealedException): trail = ConstList([StrObject(s) for s in specimen.trail]) return ConstList([specimen.value, trail]) throw(ej, StrObject(u"Cannot unseal non-thrown object"))
def checkDeepFrozen(specimen, seen, ej, root): from typhon.objects.collections.lists import ConstList from typhon.objects.collections.maps import ConstMap from typhon.objects.data import StrObject from typhon.objects.equality import TraversalKey from typhon.objects.refs import Promise, isBroken key = TraversalKey(specimen) if key in seen: return seen[key] = None if isinstance(specimen, Promise): specimen = specimen.resolution() if specimen.auditedBy(deepFrozenStamp): return elif isBroken(specimen): checkDeepFrozen(specimen.optProblem(), seen, ej, root) return elif (specimen.auditedBy(selfless) and specimen.auditedBy(transparentStamp)): portrayal = specimen.call(u"_uncall", []) if not (isinstance(portrayal, ConstList) and portrayal.size() == 4): ej.call(u"run", [ StrObject(u"can't happen: transparent object " "gave bad portrayal") ]) return checkDeepFrozen(portrayal.strategy.fetch(portrayal, 0), seen, ej, root) checkDeepFrozen(portrayal.strategy.fetch(portrayal, 1), seen, ej, root) args = portrayal.strategy.fetch(portrayal, 2) if not isinstance(args, ConstList): ej.call(u"run", [ StrObject(u"can't happen: transparent object " "gave bad portrayal") ]) return for item in args.strategy.fetch_all(args): checkDeepFrozen(item, seen, ej, root) namedArgs = portrayal.strategy.fetch(portrayal, 3) if not isinstance(namedArgs, ConstMap): ej.call(u"run", [ StrObject(u"can't happen: transparent object " "gave bad portrayal") ]) return for k, v in namedArgs.objectMap.iteritems(): checkDeepFrozen(k, seen, ej, root) checkDeepFrozen(v, seen, ej, root) else: if specimen is root: ej.call(u"run", [StrObject(root.toQuote() + u" is not DeepFrozen")]) else: ej.call(u"run", [ StrObject(root.toQuote() + u" is not DeepFrozen because " + specimen.toQuote() + u"is not") ])
def _uncall(self): from typhon.objects.collections.lists import wrapList from typhon.objects.collections.maps import EMPTY_MAP # [1,2,3].asSet().diverge() -> [[[1,2,3], "asSet"], "diverge"] rv = wrapList(self.objectSet.keys()) return [ wrapList([rv, StrObject(u"asSet"), wrapList([]), EMPTY_MAP]), StrObject(u"diverge"), wrapList([]), EMPTY_MAP ]
def recv(self, atom, args): if atom is GETADDRESS_0: return BytesObject(self.addr) if atom is GETFAMILY_0: return StrObject(self.family) if atom is GETSOCKETTYPE_0: return StrObject(self.socktype) raise Refused(self, atom, args)
def _uncall(self): from typhon.objects.collections.maps import EMPTY_MAP from typhon.scopes.safe import theSlotBinder return [ wrapList( [theSlotBinder, StrObject(u"run"), wrapList([self.guard])]), StrObject(u"run"), wrapList([self.slot, NullObject]), EMPTY_MAP ]
def recv(self, atom, args): if atom is GETARITY_0: return IntObject(self.arity) if atom is GETDOCSTRING_0: if self.docstring is not None: return StrObject(self.docstring) return NullObject if atom is GETVERB_0: return StrObject(self.verb) raise Refused(self, atom, args)
def _printOn(self, printer): printer.call(u"print", [StrObject(u"[")]) i = 0 for k, v in self.objectMap.iteritems(): printer.call(u"quote", [k]) printer.call(u"print", [StrObject(u" => ")]) printer.call(u"quote", [v]) if i + 1 < len(self.objectMap): printer.call(u"print", [StrObject(u", ")]) i += 1 printer.call(u"print", [StrObject(u"]")]) if len(self.objectMap) == 0: printer.call(u"print", [StrObject(u".asMap()")])
def _uncall(self): from typhon.objects.collections.maps import EMPTY_MAP return [ wrapList(self.snapshot()), StrObject(u"diverge"), wrapList([]), EMPTY_MAP ]
def _uncall(self): from typhon.objects.collections.lists import wrapList return [ ConstMap(self.objectMap.copy()), StrObject(u"diverge"), wrapList([]), EMPTY_MAP ]
def _uncall(self): span = wrapList([NullObject]) return [ self.nodeMaker, StrObject(u"run"), self.uncall().call(u"add", [span]), EMPTY_MAP ]
def __init__(self, ub=None, newline=StrObject(u"\n"), context=None): self.ub = ub or UnicodeBuilder() self.newline = newline if context is None: self.context = {} else: self.context = context
def wrapNameList(names): # unroll_safe justified by inputs always being immutable. ~ C. # XXX monteMap() d = monteMap() for name in names: d[StrObject(name)] = None return d
def uncall(self): return wrapList([ self._target, StrObject(self._verb), wrapList(self._args), wrapList(self._namedArgs) ])
def _uncall(self): from typhon.scopes.safe import theFinalSlotMaker from typhon.objects.collections.maps import EMPTY_MAP return [ theFinalSlotMaker, StrObject(u"run"), wrapList([self._obj, self._guard, NullObject]), EMPTY_MAP ]
def testMakeIterator(self): """ Strings are iterable. """ s = StrObject(u"cs") iterator = s.call(u"_makeIterator", []) with Ejector() as ej: result = iterator.call(u"next", [ej]) objs = unwrapList(result) self.assertEqual(objs[0].getInt(), 0) self.assertEqual(objs[1]._c, u'c') result = iterator.call(u"next", [ej]) objs = unwrapList(result) self.assertEqual(objs[0].getInt(), 1) self.assertEqual(objs[1]._c, u's') self.assertRaises(Ejecting, iterator.call, u"next", [ej])
def sendAll(self, atom, args, namedArgs): if self.checkSlot(): ref = self.resolutionBox.get() return send(ref, atom, args, namedArgs) else: return send(self.handler, HANDLESEND_3, [StrObject(atom.verb), ConstList(args), namedArgs], EMPTY_MAP)
def run(self, pname): for extension in [".ty", ".mast"]: path = pname.encode("utf-8") + extension for base in self.paths: fullpath = os.path.join(base, path) if os.path.exists(fullpath): return StrObject(fullpath.decode("utf-8")) return NullObject
def unwrapList(o, ej=None): from typhon.objects.refs import resolution l = resolution(o) if isinstance(l, ConstList): return l.strategy.fetch_all(l) if isinstance(l, FlexList): return l.strategy.fetch_all(l) throw(ej, StrObject(u"Not a list!"))
def withStringKey(self, k, v): """ Add a key-value pair to this map. Like Monte m`self.with(k :Str, v)`. """ return ConstMap(self._with(StrObject(k), v))
def sendAllOnly(self, atom, args, namedArgs): if self.checkSlot(): ref = self.resolutionBox.get() sendOnly(ref, atom, args, namedArgs) else: sendOnly(self.handler, HANDLESENDONLY_3, [StrObject(atom.verb), wrapList(args), namedArgs], EMPTY_MAP) return NullObject
def makeMirandaArgs(): from typhon.objects.collections.maps import monteMap from typhon.objects.data import StrObject from typhon.objects.ejectors import theThrower # XXX monteMap() mirandaArgs = monteMap() mirandaArgs[StrObject(u"FAIL")] = theThrower return mirandaArgs
def unsealException(specimen, ej): """ Unseal a specimen. """ if isinstance(specimen, SealedException): ue = specimen.ue trail = wrapList([StrObject(s) for s in ue.formatTrail()]) return wrapList([ue.getPayload(), trail]) throwStr(ej, u"Cannot unseal non-thrown object")
def testLastIndexOfFail(self): s = StrObject(u"needle") result = s.call(u"lastIndexOf", [StrObject(u"x")]) self.assertEqual(result.getInt(), -1)
def testHashInequal(self): a = StrObject(u"acerbic") b = StrObject(u"bitter") self.assertNotEqual(a.hash(), b.hash())
def testJoin(self): s = StrObject(u"|") result = s.call(u"join", [ConstList([StrObject(u"5"), StrObject(u"42")])]) self.assertEqual(result._s, u"5|42")
def testTrimSpaces(self): s = StrObject(u" ") result = s.call(u"trim", []) self.assertEqual(result._s, u"")
def testSliceStart(self): s = StrObject(u"slice of lemon") result = s.call(u"slice", [IntObject(9)]) self.assertEqual(result._s, u"lemon")
def testTrimWord(self): s = StrObject(u" testing ") result = s.call(u"trim", []) self.assertEqual(result._s, u"testing")
def testHashEqual(self): a = StrObject(u"acidic") b = StrObject(u"acidic") self.assertEqual(a.hash(), b.hash())
def testSliceStartStop(self): s = StrObject(u"the lime in the coconut") result = s.call(u"slice", [IntObject(4), IntObject(8)]) self.assertEqual(result._s, u"lime")
def testToLowerCaseUnicode(self): s = StrObject(u"Α And Ω") result = s.call(u"toLowerCase", []) self.assertEqual(result._s, u"α and ω")
def testToUpperCase(self): s = StrObject(u"lower") result = s.call(u"toUpperCase", []) self.assertEqual(result._s, u"LOWER")
def testIndexOf(self): s = StrObject(u"needle") result = s.call(u"indexOf", [StrObject(u"e")]) self.assertEqual(result.getInt(), 1)
def testGet(self): s = StrObject(u"index") result = s.call(u"get", [IntObject(2)]) self.assertEqual(result._c, u'd')
def testToUpperCaseUnicode(self): s = StrObject(u"¡Holá!") result = s.call(u"toUpperCase", []) self.assertEqual(result._s, u"¡HOLÁ!")