Пример #1
0
def Uint8Array_init(obj):
    if isinstance(obj, space.Integer):
        return Uint8Array(
            lltype.malloc(rffi.UCHARP.TO, obj.value, flavor='raw'), obj.value)
    if isinstance(obj, space.List):
        length = len(obj.contents)
        array = Uint8Array(lltype.malloc(rffi.UCHARP.TO, length, flavor='raw'),
                           length)
        for i in range(0, length):
            x = obj.contents[i]
            if isinstance(x, space.Integer):
                array.uint8data[i] = rffi.r_uchar(x.value)
            else:
                raise space.OldError(u"Value of incorrect type: " + x.repr())
        return array
    it = obj.iter()
    out = []
    try:
        while True:
            x = it.callattr(u"next", [])
            if isinstance(x, space.Integer):
                out.append(rffi.r_uchar(x.value))
            else:
                raise space.OldError(u"Value of incorrect type: " + x.repr())
    except StopIteration as stop:
        pass
    length = len(out)
    uint8data = lltype.malloc(rffi.UCHARP.TO, length, flavor='raw')
    for i in range(0, length):
        uint8data[i] = out[i]
    return Uint8Array(uint8data, length)
Пример #2
0
def decode(stream):
    stream.skipspace()
    if stream.shift(u'{'):
        dct = space.Dict()
        if stream.shift(u'}'):
            return dct
        decode_pair(stream, dct)
        while stream.shift(u','):
            decode_pair(stream, dct)
            stream.skipspace()
        stream.skipspace()
        stream.expect(u'}')
        return dct
    if stream.shift(u'['):
        lst = []
        if stream.shift(u']'):
            return space.List(lst)
        lst.append(decode(stream))
        while stream.shift(u','):
            lst.append(decode(stream))
            stream.skipspace()
        stream.skipspace()
        stream.expect(u']')
        return space.List(lst)
    if stream.get() == u'"':
        return decode_string(stream)
    if stream.shift(u'f'):
        stream.expect(u'a')
        stream.expect(u'l')
        stream.expect(u's')
        stream.expect(u'e')
        return space.false
    if stream.shift(u't'):
        stream.expect(u'r')
        stream.expect(u'u')
        stream.expect(u'e')
        return space.true
    if stream.shift(u'n'):
        stream.expect(u'u')
        stream.expect(u'l')
        stream.expect(u'l')
        return space.null
    if stream.shift(u'-'):
        sign = -1
    else:
        sign = +1
    num = digits(stream)
    if stream.shift(u'.'):
        num += u'.' + digits(stream)
        if stream.get() in u'eE':
            raise space.OldError(u"XXX")
        return space.Float(float(num.encode('utf-8')))
    else:
        if stream.get() in u'eE':
            raise space.OldError(u"XXX")
        return space.Integer(sign*int(num.encode('utf-8')))
    raise space.OldError(u"JSON decode error at %s" % stream.get())
Пример #3
0
 def setitem(self, index, value):
     if not isinstance(index, numbers.Integer):
         raise space.OldError(u"index not an integer")
     if not 0 <= index.value < self.length:
         raise space.OldError(u"index out of range")
     if not isinstance(value, numbers.Integer):
         raise space.OldError(u"value of incorrect type")
     self.uint8data[index.value] = rffi.r_uchar(value.value)
     return value
Пример #4
0
def sleep(argv):
    if len(argv) == 1:
        return sleep_greenlet(argv)
    elif len(argv) == 2:
        return sleep_callback(argv)
    else:
        raise space.OldError(u"expected 1 or 2 arguments to sleep(), got %d" %
                             len(argv))
Пример #5
0
def Introspection_getupv(self, ix1, ix2):
    frame = self.closure.frame  # already the parent of 'current' function.
    for i in range(ix1.value):
        if frame:
            frame = frame.parent
    if frame and ix2.value < len(frame.local):
        return frame.local[rffi.r_ushort(ix2.value)]
    raise space.OldError(
        u"Introspection.getupv cannot succeed (opcode messup/corruption?)")
Пример #6
0
def String_rjust(self, width, fillchar):
    if fillchar:
        fill = fillchar.string
        if len(fill) != 1:
            raise space.OldError(u"fill character must be exactly one character long")
    else:
        fill = u" "
    c = max(0, width.value - len(self.string))
    return String(fill*c + self.string)
Пример #7
0
def String_center(self, width, fillchar):
    if fillchar:
        fill = fillchar.string
        if len(fill) != 1:
            raise space.OldError(u"fill character must be exactly one character long")
    else:
        fill = u" "
    c = max(0, width.value - len(self.string))
    lhs = (c&1)+c/2
    rhs = c/2
    return String(fill*lhs + self.string + fill*rhs)
Пример #8
0
def String_join(string, seq):
    strings = []
    it = seq.iter()
    while True:
        try:
            x = it.callattr(u"next", [])
            if not isinstance(x, String):
                raise space.OldError(u".join expects strings")
            strings.append(x.string)
        except StopIteration as _:
            break
    return String(string.string.join(strings))
Пример #9
0
def sleep_greenlet(duration):
    ec = core.get_ec()
    if ec.current == ec.eventloop:
        raise space.OldError(u"bad context for greenlet sleep")
    assert ec.current.is_exhausted() == False

    uv_sleeper = uv.malloc_bytes(uv.timer_ptr, uv.handle_size(uv.TIMER))
    ec.uv_sleepers[rffi.cast_ptr_to_adr(uv_sleeper)] = ec.current
    uv.timer_init(ec.uv_loop, uv_sleeper)
    uv.timer_start(uv_sleeper, wakeup_sleeper, int(duration.number * 1000), 0)

    return core.switch([ec.eventloop])
Пример #10
0
def escape_ch(ch):
    if ch == u'"':
        return u'"'
    if ch == u'\\':
        return u'\\'
    if ch == u'\/':
        return u'/'
    if ch == u'b':
        return u'\b'
    if ch == u'f':
        return u'\f'
    if ch == u'n':
        return u'\n'
    if ch == u'r':
        return u'\r'
    if ch == u't':
        return u'\t'
    raise space.OldError(u"JSON decode error")
Пример #11
0
 def expect(self, ch):
     if not self.shift(ch):
         raise space.OldError(u"JSON decode expected '%s', got '%s'" % (ch, self.get()))
Пример #12
0
def getitem_int(cell, index):
    item = cell.getitem(space.Integer(index))
    if isinstance(item, space.Integer):
        return item.value
    raise space.OldError(u"invalid sourcemap format")
Пример #13
0
 def getattr(self, name):
     try:
         return self.cells[name]
     except KeyError:
         raise space.OldError(u"object contains no field %s" % name)
Пример #14
0
 def getitem(self, index):
     if not isinstance(index, numbers.Integer):
         raise space.unwind(space.LKeyError(self, index))
     if not 0 <= index.value < len(self.string):
         raise space.OldError(u"index out of range")
     return String(self.string[index.value])
Пример #15
0
def Introspection_constant(self, ix1):
    unit = self.closure.function.unit
    if ix1.value < len(unit.constants):
        return unit.constants[ix1.value]
    raise space.OldError(
        u"Introspection.constant cannot succeed (opcode messup/corruption?)")
Пример #16
0
 def getitem(self, index):
     pc = space.cast(index, space.Integer, u"[index]").value
     if pc < len(self.closure.function.block):
         return space.Integer(rffi.r_long(self.closure.function.block[pc]))
     raise space.OldError(u"pc out of range")
Пример #17
0
 def getitem(self, index):
     if not isinstance(index, numbers.Integer):
         raise space.OldError(u"index not an integer")
     if not 0 <= index.value < self.length:
         raise space.OldError(u"index out of range")
     return numbers.Integer(rffi.r_long(self.uint8data[index.value]))