def testListEqualityRecursion(self): # Yes, this is very hacky. first = ConstList([IntObject(42)]) first.strategy.append(first, [first]) second = ConstList([IntObject(42)]) second.strategy.append(second, [second]) self.assertEqual(optSame(first, second), EQUAL)
def eq(self, other): if not isinstance(other, Proxy): return False if self.checkSlot() or other.checkSlot(): raise userError(u"equals comparison of resolved proxy is" u" impossible") return optSame(self.handler, other.handler) is EQUAL
def testRefEqualitySettled(self): with scopedVat(testingVat()): first, r = makePromise() r.resolve(IntObject(42)) second, r = makePromise() r.resolve(IntObject(42)) self.assertEqual(optSame(first, second), EQUAL)
def testListEqualityRecursion(self): first = wrapList([IntObject(42), NullObject]) # Hax. first.objs.append(first) second = wrapList([IntObject(42), NullObject]) # Hax. second.objs.append(second) self.assertEqual(optSame(first, second), EQUAL)
def startOf(self, needleList, start=0): # This is quadratic. It could be better. from typhon.objects.equality import EQUAL, optSame for index in range(start, self.strategy.size(self)): for needleIndex, needle in enumerate(needleList): offset = index + needleIndex if optSame(self.strategy.fetch(self, offset), needle) is not EQUAL: break return index return -1
def recv(self, atom, args): from typhon.nodes import Noun, Method, Obj from typhon.objects.equality import optSame, EQUAL from typhon.objects.user import Audition if atom is _UNCALL_0: from typhon.objects.collections.maps import EMPTY_MAP return ConstList([subrangeGuardMaker, StrObject(u"get"), ConstList([self.superGuard]), EMPTY_MAP]) if atom is AUDIT_1: audition = args[0] if not isinstance(audition, Audition): raise userError(u"not invoked with an Audition") ast = audition.ast if not isinstance(ast, Obj): raise userError(u"audition not created with an object expr") methods = ast._script._methods for m in methods: if isinstance(m, Method) and m._verb == u"coerce": mguard = m._g if isinstance(mguard, Noun): rGSG = audition.getGuard(mguard.name) if isinstance(rGSG, FinalSlotGuard): rGSG0 = rGSG.valueGuard if isinstance(rGSG0, SameGuard): resultGuard = rGSG0.value if optSame(resultGuard, self.superGuard) is EQUAL or ( SUPERSETOF_1 in self.superGuard.respondingAtoms() and self.superGuard.call(u"supersetOf", [resultGuard]) is wrapBool(True) ): return wrapBool(True) raise userError( u"%s does not have a result guard implying " u"%s, but %s" % (audition.fqn, self.superGuard.toQuote(), resultGuard.toQuote()) ) raise userError( u"%s does not have a determinable " u"result guard, but <& %s> :%s" % (audition.fqn, mguard.name, rGSG.toQuote()) ) break return self if atom is PASSES_1: return wrapBool(args[0].auditedBy(self)) if atom is COERCE_2: specimen, ej = args[0], args[1] if specimen.auditedBy(self): return specimen c = specimen.call(u"_conformTo", [self]) if c.auditedBy(self): return c throw(ej, StrObject(u"%s does not conform to %s" % (specimen.toQuote(), self.toQuote()))) raise Refused(self, atom, args)
def _startOf(self, needleCL, start): if start < 0: raise userError(u"startOf/2: Negative start %d not permitted" % start) # This is quadratic. It could be better. from typhon.objects.equality import EQUAL, optSame for index in range(start, len(self.objs)): for needleIndex, needle in enumerate(needleCL): offset = index + needleIndex if optSame(self.objs[offset], needle) is not EQUAL: break return index return -1
def recv(self, atom, args): if atom is _UNCALL_0: from typhon.objects.collections.maps import EMPTY_MAP return ConstList([sameGuardMaker, StrObject(u"get"), ConstList([self.value]), EMPTY_MAP]) if atom is COERCE_2: from typhon.objects.equality import optSame, EQUAL specimen, ej = args[0], args[1] if optSame(specimen, self.value) is EQUAL: return specimen ej.call(u"run", [ConstList([specimen, StrObject(u"is not"), self.value])]) if atom is GETVALUE_0: return self.value raise Refused(self, atom, args)
def recv(self, atom, args): if atom is _UNCALL_0: from typhon.objects.collections.maps import EMPTY_MAP return ConstList([ sameGuardMaker, StrObject(u"get"), ConstList([self.value]), EMPTY_MAP ]) if atom is COERCE_2: from typhon.objects.equality import optSame, EQUAL specimen, ej = args[0], args[1] if optSame(specimen, self.value) is EQUAL: return specimen ej.call(u"run", [ConstList([specimen, StrObject(u"is not"), self.value])]) if atom is GETVALUE_0: return self.value raise Refused(self, atom, args)
def auditedBy(self, prospect): """ Whether a prospective stamp has been left on this object. """ # The number of different objects that will pass through here is # surprisingly low; consider how Python would look if metaclasses were # promoted. prospect = promote(prospect) from typhon.objects.equality import optSame, EQUAL # Already audited with an identical stamp? for stamp in self.auditorStamps(): if optSame(prospect, stamp) is EQUAL: return True # Sorry, but nope. return False
def audit(self, audition): from typhon.nodes import Noun, Method, Obj from typhon.objects.equality import optSame, EQUAL from typhon.objects.user import Audition if not isinstance(audition, Audition): raise userError(u"not invoked with an Audition") ast = audition.ast if not isinstance(ast, Obj): raise userError(u"audition not created with an object expr") methods = ast._script._methods for m in methods: if isinstance(m, Method) and m._verb == u"coerce": mguard = m._g if isinstance(mguard, Noun): rGSG = audition.getGuard(mguard.name) if isinstance(rGSG, FinalSlotGuard): rGSG0 = rGSG.valueGuard if isinstance(rGSG0, SameGuard): resultGuard = rGSG0.value if (optSame(resultGuard, self.superGuard) is EQUAL or (self.superGuard.call(u"supersetOf", [resultGuard]) is wrapBool(True))): return True raise userError( u"%s does not have a result guard implying " u"%s, but %s" % (audition.fqn, self.superGuard.toQuote(), resultGuard.toQuote())) raise userError(u"%s does not have a determinable " u"result guard, but <& %s> :%s" % ( audition.fqn, mguard.name, rGSG.toQuote())) break return False
def testNaNFail(self): # Found by accident. first = DoubleObject(float("nan")) second = IntObject(42) self.assertEqual(optSame(first, second), INEQUAL)
def indexOf(self, needle): from typhon.objects.equality import EQUAL, optSame for index, specimen in enumerate(self.objs): if optSame(needle, specimen) is EQUAL: return index return -1
def testListInequalityLength(self): first = ConstList([IntObject(42)]) second = ConstList([IntObject(42), IntObject(5)]) self.assertEqual(optSame(first, second), INEQUAL)
def testListEquality(self): first = wrapList([IntObject(42)]) second = wrapList([IntObject(42)]) self.assertEqual(optSame(first, second), EQUAL)
def testStrInequality(self): first = StrObject(u"false") second = StrObject(u"true") self.assertEqual(optSame(first, second), INEQUAL)
def testDoubleEquality(self): first = DoubleObject(4.2) second = DoubleObject(4.2) self.assertEqual(optSame(first, second), EQUAL)
def testIntEquality(self): first = IntObject(42) second = IntObject(42) self.assertEqual(optSame(first, second), EQUAL)
def testListEquality(self): first = ConstList([IntObject(42)]) second = ConstList([IntObject(42)]) self.assertEqual(optSame(first, second), EQUAL)
def contains(self, needle): from typhon.objects.equality import EQUAL, optSame for specimen in self.strategy.fetch_all(self): if optSame(needle, specimen) is EQUAL: return True return False
def testBigIntAndIntEquality(self): first = BigInt(rbigint.fromint(42)) second = IntObject(42) self.assertEqual(optSame(first, second), EQUAL)
def testDoubleEqualityNaN(self): first = DoubleObject(float("nan")) second = DoubleObject(float("nan")) self.assertEqual(optSame(first, second), EQUAL)
def testCharEquality(self): first = CharObject(u'c') second = CharObject(u'c') self.assertEqual(optSame(first, second), EQUAL)
def eq(self, other): if not isinstance(other, FarRef): return False return (Proxy.eq(self, other) and optSame( self.resolutionIdentity, other.resolutionIdentity) is EQUAL)
def testRefEquality(self): with scopedVat(testingVat()): first, r = makePromise() second, r = makePromise() self.assertEqual(optSame(first, second), NOTYET)
def testListInequality(self): first = ConstList([IntObject(42)]) second = ConstList([IntObject(41)]) self.assertEqual(optSame(first, second), INEQUAL)
def testListEqualityRecursionReflexive(self): first = wrapList([IntObject(42), NullObject]) # Hax. first.objs.append(first) self.assertEqual(optSame(first, first), EQUAL)
def testRefEqualityReflexive(self): with scopedVat(testingVat()): p, r = makePromise() self.assertEqual(optSame(p, p), EQUAL)
def contains(self, needle): from typhon.objects.equality import EQUAL, optSame for specimen in self.objs: if optSame(needle, specimen) is EQUAL: return True return False
def testStrEquality(self): first = StrObject(u"cs") second = StrObject(u"cs") self.assertEqual(optSame(first, second), EQUAL)
def testListEqualityRecursionReflexive(self): first = ConstList([IntObject(42)]) first.strategy.append(first, [first]) self.assertEqual(optSame(first, first), EQUAL)
def recv(self, atom, args): from typhon.nodes import Noun, Method, Obj from typhon.objects.equality import optSame, EQUAL from typhon.objects.user import Audition if atom is _UNCALL_0: from typhon.objects.collections.maps import EMPTY_MAP return ConstList([ subrangeGuardMaker, StrObject(u"get"), ConstList([self.superGuard]), EMPTY_MAP ]) if atom is AUDIT_1: audition = args[0] if not isinstance(audition, Audition): raise userError(u"not invoked with an Audition") ast = audition.ast if not isinstance(ast, Obj): raise userError(u"audition not created with an object expr") methods = ast._script._methods for m in methods: if isinstance(m, Method) and m._verb == u"coerce": mguard = m._g if isinstance(mguard, Noun): rGSG = audition.getGuard(mguard.name) if isinstance(rGSG, FinalSlotGuard): rGSG0 = rGSG.valueGuard if isinstance(rGSG0, SameGuard): resultGuard = rGSG0.value if (optSame(resultGuard, self.superGuard) is EQUAL or (SUPERSETOF_1 in self.superGuard.respondingAtoms() and self.superGuard.call( u"supersetOf", [resultGuard]) is wrapBool(True))): return wrapBool(True) raise userError( u"%s does not have a result guard implying " u"%s, but %s" % (audition.fqn, self.superGuard.toQuote(), resultGuard.toQuote())) raise userError( u"%s does not have a determinable " u"result guard, but <& %s> :%s" % (audition.fqn, mguard.name, rGSG.toQuote())) break return self if atom is PASSES_1: return wrapBool(args[0].auditedBy(self)) if atom is COERCE_2: specimen, ej = args[0], args[1] if specimen.auditedBy(self): return specimen c = specimen.call(u"_conformTo", [self]) if c.auditedBy(self): return c throw( ej, StrObject(u"%s does not conform to %s" % (specimen.toQuote(), self.toQuote()))) raise Refused(self, atom, args)
def indexOf(self, needle): from typhon.objects.equality import EQUAL, optSame for index, specimen in enumerate(self.strategy.fetch_all(self)): if optSame(needle, specimen) is EQUAL: return index return -1
def eq(self, other): if not isinstance(other, RemotePromise): return False return (Proxy.eq(self, other) and optSame(self.resolutionBox, other.resolutionBox) is EQUAL)
def keyEq(first, second): from typhon.objects.equality import optSame, EQUAL first = resolveKey(first) second = resolveKey(second) return optSame(first, second) is EQUAL
def eq(self, other): if not isinstance(other, FarRef): return False return (Proxy.eq(self, other) and optSame(self.resolutionIdentity, other.resolutionIdentity) is EQUAL)
def testListInequalityLength(self): first = wrapList([IntObject(42)]) second = wrapList([IntObject(42), IntObject(5)]) self.assertEqual(optSame(first, second), INEQUAL)
def subCoerce(self, specimen): from typhon.objects.equality import optSame, EQUAL if optSame(specimen, self.value) is EQUAL: return specimen