Пример #1
0
    def recv(self, atom, args):
        if atom is RUN_1:
            return IntObject(int(unwrapStr(args[0]).encode('utf-8')))

        if atom is RUN_2:
            inp = unwrapStr(args[0])
            radix = unwrapInt(args[1])
            try:
                v = int(inp.encode("utf-8"), radix)
            except ValueError:
                raise userError(u"Invalid literal for base %d: %s" %
                                (radix, inp))
            return IntObject(v)

        if atom is FROMBYTES_1:
            return IntObject(int(unwrapBytes(args[0])))

        if atom is FROMBYTES_2:
            bs = unwrapBytes(args[0])
            radix = unwrapInt(args[1])
            try:
                v = int(bs, radix)
            except ValueError:
                raise userError(u"Invalid literal for base %d: %s" %
                                (radix, bytesToString(bs)))
            return IntObject(v)

        raise Refused(self, atom, args)
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
 def testSurprisingMapCorruption(self):
     d = monteMap()
     d[IntObject(1)] = IntObject(2)
     m = ConstMap(d)
     f = m.call(u"diverge", [])
     f.call(u"removeKey", [IntObject(1)])
     result = m.call(u"get", [IntObject(1)])
     self.assertEqual(result.getInt(), 2)
Пример #5
0
    def recv(self, atom, args):
        if atom is EXITSTATUS_0:
            return IntObject(self.exitStatus)

        if atom is TERMINATIONSIGNAL_0:
            return IntObject(self.terminationSignal)

        raise Refused(self, atom, args)
Пример #6
0
def bench(obj, name):
    name = unwrapStr(name).encode("utf-8")

    if not benchmarkSettings.enabled:
        debug_print("Not running benchmark", name,
                    "since benchmarking is disabled")
        return ConstList([IntObject(0), DoubleObject(0.0)])

    debug_print("Benchmarking", name)

    # Step 1: Calibrate timing loop.
    debug_print("Calibrating timing loop...")
    # Unroll do-while iteration.
    loops = 1
    debug_print("Trying 1 loop...")
    taken = time.time()
    obj.call(u"run", [])
    taken = time.time() - taken
    while taken < 1.0 and loops < 100000000:
        debug_print("Took", taken, "seconds to run", loops, "loops")
        loops *= 10
        debug_print("Trying", loops, "loops...")
        acc = 0
        taken = time.time()
        while acc < loops:
            acc += 1
            obj.call(u"run", [])
        taken = time.time() - taken
    debug_print("Okay! Will take", loops, "loops at", taken, "seconds")

    # Step 2: Take trials.
    debug_print("Taking trials...")
    trialCount = 3 - 1
    # Unroll first iteration to get maximum.
    acc = 0
    taken = time.time()
    while acc < loops:
        acc += 1
        obj.call(u"run", [])
    taken = time.time() - taken
    result = taken
    while trialCount:
        trialCount -= 1
        acc = 0
        taken = time.time()
        while acc < loops:
            acc += 1
            obj.call(u"run", [])
        taken = time.time() - taken
        if taken < result:
            result = taken

    # All done!
    return ConstList([IntObject(loops), DoubleObject(taken)])
Пример #7
0
    def recv(self, atom, args):
        if atom is GETALGORITHM_0:
            return StrObject(u"CSPRNG (libsodium)")

        if atom is GETENTROPY_0:
            # uint32_t in the FFI, so exactly 32 bits every time.
            return ConstList([
                IntObject(32),
                IntObject(intmask(rsodium.randombytesRandom()))
            ])

        raise Refused(self, atom, args)
Пример #8
0
    def testSwitchableBecomesResolved(self):
        with scopedVat(testingVat()):
            p, r = makePromise()
            self.assertFalse(isResolved(p))

            r.resolve(IntObject(42))
            self.assertTrue(isResolved(p))
Пример #9
0
 def next(self, ej):
     if self._index < self.size:
         rv = [IntObject(self._index), self.objects[self._index]]
         self._index += 1
         return rv
     else:
         throwStr(ej, u"Iterator exhausted")
Пример #10
0
 def testHashEqual(self):
     d = monteSet()
     d[IntObject(42)] = None
     d[CharObject(u'¡')] = None
     a = ConstSet(d)
     b = ConstSet(d)
     self.assertEqual(a.hash(), b.hash())
Пример #11
0
    def recv(self, atom, args):
        if atom is GETBUCKETS_0:
            d = monteMap()
            for name, count in self.buckets.items():
                size = self.sizes.get(name, -1)
                d[StrObject(name)] = ConstList(
                    [IntObject(size), IntObject(count)])
            return ConstMap(d)

        if atom is GETMEMORYUSAGE_0:
            return IntObject(self.memoryUsage)

        if atom is GETOBJECTCOUNT_0:
            return IntObject(self.objectCount)

        raise Refused(self, atom, args)
Пример #12
0
    def recv(self, atom, args):

        # and/1
        if atom is AND_1:
            return wrapBool(self._b and unwrapBool(args[0]))

        # butNot/1
        if atom is BUTNOT_1:
            return wrapBool(self._b and not unwrapBool(args[0]))

        # not/0
        if atom is NOT_0:
            return wrapBool(not self._b)

        # op__cmp/1
        if atom is OP__CMP_1:
            from typhon.objects.data import IntObject
            return IntObject(self._b - unwrapBool(args[0]))

        # or/1
        if atom is OR_1:
            return wrapBool(self._b or unwrapBool(args[0]))

        # pick/2
        if atom is PICK_2:
            return args[0] if self._b else args[1]

        # xor/1
        if atom is XOR_1:
            return wrapBool(self._b ^ unwrapBool(args[0]))

        raise Refused(self, atom, args)
Пример #13
0
    def testMulDouble(self):
        """
        Ints are promoted by doubles during multiplication.
        """

        i = IntObject(4)
        result = i.call(u"multiply", [DoubleObject(2.1)])
        self.assertTrue(isinstance(result, DoubleObject))
        self.assertEqual(result.getDouble(), 8.4)
Пример #14
0
    def testSwitchableChains(self):
        with scopedVat(testingVat()):
            p, r = makePromise()
            p2, r2 = makePromise()

            r.resolve(p2)
            self.assertFalse(isResolved(p))

            r2.resolve(IntObject(42))
            self.assertTrue(isResolved(p))
Пример #15
0
    def recv(self, atom, args):
        if atom is NEXT_1:
            if self._index < self.size:
                rv = [IntObject(self._index), self.objects[self._index]]
                self._index += 1
                return ConstList(rv)
            else:
                ej = args[0]
                ej.call(u"run", [StrObject(u"Iterator exhausted")])

        raise Refused(self, atom, args)
Пример #16
0
    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)
Пример #17
0
    def op__cmp(self, other):
        """
        Perform a subset comparison.
        """

        if len(self.objectSet) < len(other):
            smaller = self.objectSet
            larger = other
        else:
            smaller = other
            larger = self.objectSet

        for item in smaller.keys():
            if item not in larger:
                return Incomparable

        # smaller is a subset of larger.
        if len(self.objectSet) == len(other):
            return IntObject(0)
        elif len(self.objectSet) < len(other):
            return IntObject(-1)
        else:
            return IntObject(1)
Пример #18
0
    def recv(self, atom, args):
        # _makeIterator/0: Create an iterator for this collection's contents.
        if atom is _MAKEITERATOR_0:
            return self._makeIterator()

        if atom is _PRINTON_1:
            printer = args[0]
            self.printOn(printer)
            return NullObject

        # contains/1: Determine whether an element is in this collection.
        if atom is CONTAINS_1:
            return wrapBool(self.contains(args[0]))

        # size/0: Get the number of elements in the collection.
        if atom is SIZE_0:
            return IntObject(self.size())

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_1:
            start = unwrapInt(args[0])
            try:
                return self.slice(start)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_2:
            start = unwrapInt(args[0])
            stop = unwrapInt(args[1])
            try:
                return self.slice(start, stop)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # snapshot/0: Create a new constant collection with a copy of the
        # current collection's contents.
        if atom is SNAPSHOT_0:
            return self.snapshot()

        if atom is JOIN_1:
            l = unwrapList(args[0])
            return self.join(l)

        return self._recv(atom, args)
Пример #19
0
    def recv(self, atom, args):
        if atom is GETARGUMENTS_0:
            return ConstList(
                [StrObject(arg.decode("utf-8")) for arg in self.config.argv])

        if atom is GETENVIRONMENT_0:
            # XXX monteMap()
            d = monteMap()
            for key, value in os.environ.items():
                k = StrObject(key.decode("utf-8"))
                v = StrObject(value.decode("utf-8"))
                d[k] = v
            return ConstMap(d)

        if atom is GETPID_0:
            return IntObject(os.getpid())

        if atom is INTERRUPT_0:
            os.kill(os.getpid(), signal.SIGINT)
            return NullObject

        raise Refused(self, atom, args)
Пример #20
0
    def recv(self, atom, args):
        if atom is GETARGUMENTS_0:
            return ConstList([BytesObject(arg) for arg in self.argv])

        if atom is GETENVIRONMENT_0:
            # XXX monteMap()
            d = monteMap()
            for key, value in self.env.items():
                k = BytesObject(key)
                v = BytesObject(value)
                d[k] = v
            return ConstMap(d)

        if atom is GETPID_0:
            return IntObject(self.pid)

        if atom is INTERRUPT_0:
            os.kill(self.pid, signal.SIGINT)
            return NullObject

        if atom is WAIT_0:
            return self.makeWaiter()

        raise Refused(self, atom, args)
Пример #21
0
 def testSubtract(self):
     i = IntObject(5)
     result = i.call(u"subtract", [IntObject(15)])
     self.assertAlmostEqual(result.getInt(), -10)
Пример #22
0
 def testGetNegative(self):
     s = StrObject(u"index")
     self.assertRaises(UserException, s.call, u"get", [IntObject(-1)])
Пример #23
0
 def testGet(self):
     s = StrObject(u"index")
     result = s.call(u"get", [IntObject(2)])
     self.assertEqual(result._c, u'd')
Пример #24
0
 def testOpCmpInt(self):
     bi = BigInt(rbigint.fromint(6))
     i = IntObject(2)
     result = bi.call(u"op__cmp", [i])
     self.assertEqual(result.getInt(), 1)
Пример #25
0
 def testAndInt(self):
     bi = BigInt(rbigint.fromint(0x3fffffffffffffff).int_mul(3))
     result = bi.call(u"and", [IntObject(0xffff)])
     self.assertTrue(result.bi.int_eq(0xfffd))
Пример #26
0
 def testXorInt(self):
     bi = BigInt(rbigint.fromint(0xcccc))
     result = bi.call(u"xor", [IntObject(0xaaaa)])
     self.assertTrue(result.bi.int_eq(0x6666))
Пример #27
0
 def testShiftRight(self):
     bi = BigInt(rbigint.fromint(42))
     result = bi.call(u"shiftRight", [IntObject(2)])
     self.assertTrue(result.bi.int_eq(10))
Пример #28
0
 def testBitLength(self):
     i = IntObject(42)
     result = i.call(u"bitLength", [])
     self.assertEqual(result.getInt(), 6)
Пример #29
0
 def testSubtractDouble(self):
     i = IntObject(5)
     result = i.call(u"subtract", [DoubleObject(1.5)])
     self.assertAlmostEqual(result.getDouble(), 3.5)
Пример #30
0
 def testShiftRightLarge(self):
     i = IntObject(0x7fffffffffffffff)
     result = i.call(u"shiftRight", [IntObject(64)])
     self.assertEqual(result.getInt(), 0x0)