Beispiel #1
0
def load_str(fp, delim):
    oldpos = fp.pos
    length = fp.read_int_until(':', can_be_negative=False)
    if length < 0:
        raise SerializerError("integer overflow")
    try:
        i = fp.consume(length + 3)      # quote, string, quote, semicolon
    except SerializerError:
        if len(fp.s) > fp.pos:
            extra = len(fp.s) - fp.pos - length
            if extra > 0:
                fp.error_pos = fp.pos + length + 1
                if extra > 1:
                    fp.error_pos += 2
            else:
                fp.error_pos = oldpos
        raise
    check_nonneg(i)
    s = fp.s
    if s[i] != '"':
        raise SerializerError("'\"' expected")
    i += 1
    data = s[i: i + length]
    i += length
    if s[i] != '"':
        raise SerializerError("'\"' expected")
    i += 1
    if s[i] != delim:
        raise SerializerError("delim expected")
    return data
Beispiel #2
0
    def run_instr(self, space, name, num_args, bytecode, frame, pc):
        args = ()
        # Do not change these from * 256 to << 8, lshift has defined overflow
        # semantics which cause it to not propogate the nonnegative-ness.
        if num_args >= 1:
            v = ord(bytecode.code[pc]) | (ord(bytecode.code[pc + 1]) * 256)
            check_nonneg(v)
            args += (v,)
            pc += 2
        if num_args >= 2:
            v = ord(bytecode.code[pc]) | (ord(bytecode.code[pc + 1]) * 256)
            check_nonneg(v)
            args += (v,)
            pc += 2
        if num_args >= 3:
            raise NotImplementedError

        method = getattr(self, name)
        try:
            res = method(space, bytecode, frame, pc, *args)
        except RaiseBreak as e:
            if e.parent_interp is not self:
                raise
            frame.push(e.w_value)
            res = None
        if res is not None:
            pc = res
        return pc
Beispiel #3
0
 def __init__(self, space, code, w_globals, outer_func):
     if not we_are_translated():
         assert type(self) == space.FrameClass, (
             "use space.FrameClass(), not directly PyFrame()")
     self = hint(self, access_directly=True, fresh_virtualizable=True)
     assert isinstance(code, pycode.PyCode)
     self.space = space
     self.pycode = code
     if code.frame_stores_global(w_globals):
         self.getorcreatedebug().w_globals = w_globals
     ncellvars = len(code.co_cellvars)
     nfreevars = len(code.co_freevars)
     size = code.co_nlocals + ncellvars + nfreevars + code.co_stacksize
     # the layout of this list is as follows:
     # | local vars | cells | stack |
     self.locals_cells_stack_w = [None] * size
     self.valuestackdepth = code.co_nlocals + ncellvars + nfreevars
     make_sure_not_resized(self.locals_cells_stack_w)
     check_nonneg(self.valuestackdepth)
     #
     if space.config.objspace.honor__builtins__:
         self.builtin = space.builtin.pick_builtin(w_globals)
     # regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
     # class bodies only have CO_NEWLOCALS.
     self.initialize_frame_scopes(outer_func, code)
Beispiel #4
0
 def __init__(self, space, s):
     self.space = space
     self.s = s
     self.pos = 0
     self.error_pos = 0
     self.next_reference_obj = -1
     self._ref_numbers = []
     check_nonneg(self.pos)
Beispiel #5
0
 def __init__(self, deque):
     self.space = deque.space
     self.deque = deque
     self.block = deque.rightblock
     self.index = deque.rightindex
     self.counter = deque.len
     self.lock = deque.getlock()
     check_nonneg(self.index)
Beispiel #6
0
 def pat(self, index):
     check_nonneg(index)
     result = self.pattern[index]
     # Check that we only return non-negative integers from this helper.
     # It is possible that self.pattern contains negative integers
     # (see set_charset() and set_bigcharset() in rsre_char.py)
     # but they should not be fetched via this helper here.
     assert result >= 0
     return result
Beispiel #7
0
 def __init__(self, pattern, match_start, end, flags):
     # 'match_start' and 'end' must be known to be non-negative
     # and they must not be more than len(string).
     check_nonneg(match_start)
     check_nonneg(end)
     self.pattern = pattern
     self.match_start = match_start
     self.end = end
     self.flags = flags
Beispiel #8
0
def get_subclass_of_correct_size(space, cls, w_type):
    assert space.config.objspace.std.withmapdict
    map = w_type.terminator
    classes = memo_get_subclass_of_correct_size(space, cls)
    if SUBCLASSES_MIN_FIELDS == SUBCLASSES_MAX_FIELDS:
        return classes[0]
    size = map.size_estimate()
    debug.check_nonneg(size)
    if size < len(classes):
        return classes[size]
    else:
        return classes[len(classes)-1]
Beispiel #9
0
 def __init__(self, space):
     self.space = space
     self.maxlen = sys.maxint
     self.clear()
     check_nonneg(self.leftindex)
     check_nonneg(self.rightindex)
     #
     # lightweight locking: any modification to the content of the deque
     # sets the lock to None.  Taking an iterator sets it to a non-None
     # value.  The iterator can check if further modifications occurred
     # by checking if the lock still has the same non-None value.
     # (In CPython, this is implemented using d->state.)
     self.lock = None
Beispiel #10
0
 def __init__(self, space):
     self.space = space
     self.maxlen = sys.maxint
     self.clear()
     check_nonneg(self.leftindex)
     check_nonneg(self.rightindex)
     #
     # lightweight locking: any modification to the content of the deque
     # sets the lock to None.  Taking an iterator sets it to a non-None
     # value.  The iterator can check if further modifications occurred
     # by checking if the lock still has the same non-None value.
     # (In CPython, this is implemented using d->state.)
     self.lock = None
Beispiel #11
0
 def __init__(self, pattern, match_start, end, flags):
     # 'match_start' and 'end' must be known to be non-negative
     # and they must not be more than len(string).
     check_nonneg(match_start)
     check_nonneg(end)
     self.pattern = pattern
     self.match_start = match_start
     self.end = end
     self.flags = flags
     # check we don't get the old value of MAXREPEAT
     # during the untranslated tests
     if not we_are_translated():
         assert 65535 not in pattern
Beispiel #12
0
 def pp_expect_nonneg_int(self, startpos):
     check_nonneg(startpos)
     s = self.s
     i = startpos
     limit = len(s)
     result = 0
     while i < limit and s[i].isdigit():
         result = result * 10 + (ord(s[i]) - 48)
         i += 1
     self.pos = i
     result = intmask(result)
     if result < 0:
         result = 0   # integer overflow!
     return result
Beispiel #13
0
 def pp_expect_nonneg_int(self, startpos):
     check_nonneg(startpos)
     s = self.s
     i = startpos
     limit = len(s)
     result = 0
     while i < limit and s[i].isdigit():
         result = result * 10 + (ord(s[i]) - 48)
         i += 1
     self.pos = i
     result = intmask(result)
     if result < 0:
         result = 0   # integer overflow!
     return result
Beispiel #14
0
 def _preprocess(self, i=0):
     check_nonneg(i)
     s = self.s
     nesting = 0
     assert i >= 0
     while True:
         if i >= len(s):
             return -1
         tp = s[i]
         if tp == 'b':
             i += 4          # 'b:0;' or 'b:1;'
         elif tp == 'i' or tp == 'd':
             i = s.find(';', i + 1)
             if i < 0:
                 return -1
             i += 1
         elif tp == 's':     # 's:LEN:"....";'
             length = self.pp_expect_nonneg_int(i + 2)
             i = self.pos + length + 4
         elif tp == 'N':
             i += 2          # 'N;'
         elif tp == 'a':     # 'a:LEN:{....}'
             i = s.find('{', i + 1)
             if i < 0:
                 return -1
             i += 1
             nesting += 1
         elif tp == 'O':     # 'O:LEN:"....":LEN:{....}'
             length = self.pp_expect_nonneg_int(i + 2)
             i = self.pos + length + 4
             i = s.find('{', i + 1)
             if i < 0:
                 return -1
             i += 1
             nesting += 1
         elif tp == '}':
             nesting -= 1
             i += 1
         elif tp == 'R' or tp == 'r':     # 'R:BACKREF;'
             backref = self.pp_expect_nonneg_int(i + 2)
             self._ref_numbers.append(backref)
             i = self.pos + 1
         else:
             return -1
         if nesting <= 0:
             return i
Beispiel #15
0
 def _preprocess(self, i=0):
     check_nonneg(i)
     s = self.s
     nesting = 0
     assert i >= 0
     while True:
         if i >= len(s):
             return -1
         tp = s[i]
         if tp == 'b':
             i += 4          # 'b:0;' or 'b:1;'
         elif tp == 'i' or tp == 'd':
             i = s.find(';', i + 1)
             if i < 0:
                 return -1
             i += 1
         elif tp == 's':     # 's:LEN:"....";'
             length = self.pp_expect_nonneg_int(i + 2)
             i = self.pos + length + 4
         elif tp == 'N':
             i += 2          # 'N;'
         elif tp == 'a':     # 'a:LEN:{....}'
             i = s.find('{', i + 1)
             if i < 0:
                 return -1
             i += 1
             nesting += 1
         elif tp == 'O':     # 'O:LEN:"....":LEN:{....}'
             length = self.pp_expect_nonneg_int(i + 2)
             i = self.pos + length + 4
             i = s.find('{', i + 1)
             if i < 0:
                 return -1
             i += 1
             nesting += 1
         elif tp == '}':
             nesting -= 1
             i += 1
         elif tp == 'R' or tp == 'r':     # 'R:BACKREF;'
             backref = self.pp_expect_nonneg_int(i + 2)
             self._ref_numbers.append(backref)
             i = self.pos + 1
         else:
             return -1
         if nesting <= 0:
             return i
Beispiel #16
0
 def __init__(self, space, code, w_globals, outer_func):
     if not we_are_translated():
         assert type(self) == space.FrameClass, (
             "use space.FrameClass(), not directly PyFrame()")
     self = hint(self, access_directly=True, fresh_virtualizable=True)
     assert isinstance(code, pycode.PyCode)
     self.space = space
     self.w_globals = w_globals
     self.pycode = code
     self.locals_stack_w = [None] * (code.co_nlocals + code.co_stacksize)
     self.valuestackdepth = code.co_nlocals
     make_sure_not_resized(self.locals_stack_w)
     check_nonneg(self.valuestackdepth)
     #
     if space.config.objspace.honor__builtins__:
         self.builtin = space.builtin.pick_builtin(w_globals)
     # regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
     # class bodies only have CO_NEWLOCALS.
     self.initialize_frame_scopes(outer_func, code)
Beispiel #17
0
 def __init__(self, space, code, w_globals, outer_func):
     if not we_are_translated():
         assert type(self) in (space.FrameClass, CPythonFrame), (
             "use space.FrameClass(), not directly PyFrame()")
     self = hint(self, access_directly=True, fresh_virtualizable=True)
     assert isinstance(code, pycode.PyCode)
     self.pycode = code
     eval.Frame.__init__(self, space, w_globals)
     self.locals_stack_w = [None] * (code.co_nlocals + code.co_stacksize)
     self.valuestackdepth = code.co_nlocals
     self.lastblock = None
     make_sure_not_resized(self.locals_stack_w)
     check_nonneg(self.valuestackdepth)
     #
     if space.config.objspace.honor__builtins__:
         self.builtin = space.builtin.pick_builtin(w_globals)
     # regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
     # class bodies only have CO_NEWLOCALS.
     self.initialize_frame_scopes(outer_func, code)
     self.f_lineno = code.co_firstlineno
Beispiel #18
0
 def str(self, index):
     check_nonneg(index)
     return ord(self._string[index])
Beispiel #19
0
 def str(self, index):
     check_nonneg(index)
     return ord(self._unicodestr[index])
Beispiel #20
0
 def g(x):
     check_nonneg(x-1)
Beispiel #21
0
 def str(self, index):
     check_nonneg(index)
     return ord(self._buffer.getitem(index))
Beispiel #22
0
def load_object(fp):
    fp.next_reference_obj -= 1
    needs_ref = (fp.next_reference_obj == 0)
    #
    fp.error_pos = fp.pos
    try:
        i = fp.consume(2)
    except SerializerError:
        i = fp.consume(1)
        if fp.s[i] == '}':
            fp.space.ec.notice("unserialize(): "
                               "Unexpected end of serialized data")
        raise
    s = fp.s
    tp = s[i]
    check_nonneg(i)
    check_nonneg(fp.pos)
    #
    if tp == 'i':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        i = fp.read_int_until(';')
        w_result = fp.space.wrap(i)
    #
    elif tp == 's':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        w_result = fp.space.newstr(load_str(fp, ';'))
    #
    elif tp == 'd':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        data = fp.read_substring_until(';')
        if data == 'INF':
            w_result = fp.space.wrap(rfloat.INFINITY)
        elif data == '-INF':
            w_result = fp.space.wrap(-rfloat.INFINITY)
        elif data == 'NAN':
            w_result = fp.space.wrap(rfloat.NAN)
        else:
            w_number, valid = convert_string_to_number(data)
            if not valid:
                raise SerializerError('bad double')
            w_result = fp.space.newfloat(w_number.float_w(fp.space))
    #
    elif tp == 'b':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        i = fp.consume(2)
        digit = s[i]
        if digit == '0':
            w_result = fp.space.w_False
        elif digit == '1':
            w_result = fp.space.w_True
        else:
            raise SerializerError('bad bool')
        if s[i + 1] != ';':
            raise SerializerError("';' expected")
    #
    elif tp == 'N':
        if s[i + 1] != ';':
            raise SerializerError("';' expected")
        w_result = fp.space.w_Null
    #
    elif tp == 'a':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        length = fp.read_int_until(':', can_be_negative=False)
        if length < 0:
            raise SerializerError("integer overflow")
        i = fp.consume(1)
        if s[i] != '{':
            raise SerializerError("'{' expected")
        w_result = None
        if needs_ref:
            w_result = fp.space.empty_ref()
            fp.save_reference(w_result)
        # first try to load the array as a direct list
        lst_w = []
        expected = ['i', ':', '0', ';']
        for n in range(length):
            i = fp.pos
            if i + len(expected) >= len(s):
                break
            for j in range(len(expected)):
                if s[i] != expected[j]:
                    break
                i += 1
            else:
                # ok, we got exactly 'i:N;' where N is the expected index
                fp.pos = i
                lst_w.append(load_object(fp))
                # increment the expected counter
                j = len(expected) - 2
                while j >= 2:
                    if expected[j] != '9':
                        expected[j] = chr(ord(expected[j]) + 1)
                        break
                    expected[j] = '0'
                    j -= 1
                else:
                    expected = ['i', ':', '1'] + expected[2:]
                continue
            break
        else:
            # we succeeded in loading the complete array as a list
            n = length
            _succeeded_as_a_list()   # for tests
        # fill in the remaining entries, if any, the slow way
        w_array = fp.space.new_array_from_list(lst_w)
        for n in range(n, length):
            w_key = load_array_key(fp)
            w_value = load_object(fp)
            w_array = fp.space.setitem_maybe_inplace(w_array, w_key, w_value)
        fp.expect_closing_brace()
        if w_result is not None:    # needs_ref
            w_result.store(w_array, unique=True)
            return w_result
        else:
            return w_array
    #
    elif tp == 'R':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        return load_reference(fp)
    #
    elif tp == 'r':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        return load_reference(fp).deref()
    #
    elif tp == 'O':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        klass_name = load_str(fp, ':')
        space = fp.space
        interp = space.ec.interpreter
        klass = interp.lookup_class_or_intf(klass_name)
        if klass is None:
            klass = k_incomplete
            w_instance = klass.get_empty_instance(space)
            w_instance.setattr(interp, '__PHP_Incomplete_Class_Name',
                               space.wrap(klass_name), None)
        else:
            w_instance = klass.get_empty_instance(space)
        w_result = w_instance
        if needs_ref:
            w_result = W_Reference(w_instance)
            fp.save_reference(w_result)
        count_attrs = fp.read_int_until(':')    # negative value accepted :-(
        i = fp.consume(1)
        if s[i] != '{':
            raise SerializerError("'{' expected")
        attrs = {}
        for i in xrange(count_attrs):
            w_attr = load_array_key(fp)
            w_value = load_object(fp)
            attr_name = space.str_w(w_attr)
            attrs[attr_name] = w_value
            w_instance.setattr(interp, attr_name, w_value, None)
        fp.expect_closing_brace()

        w_instance.unserialize(space, attrs)

        if '__wakeup' in klass.methods:
            klass.methods['__wakeup'].method_func.call_args(space.ec.interpreter,
                                                            [], w_this=w_instance,
                                                            thisclass=klass)
        return w_result
    #
    else:
        if tp == '}':
            fp.space.ec.notice("unserialize(): "
                               "Unexpected end of serialized data")
        raise SerializerError('malformed input')

    # this is for primitive types only; complex types 'return' above
    if needs_ref:
        w_result = W_Reference(w_result)
        fp.save_reference(w_result)
    return w_result
Beispiel #23
0
 def f(x):
     assert x >= 5
     check_nonneg(x)
Beispiel #24
0
 def str(self, index):
     check_nonneg(index)
     return ord(self._string[index])
Beispiel #25
0
 def str(self, index):
     check_nonneg(index)
     return ord(self._unicodestr[index])
Beispiel #26
0
def load_object(fp):
    fp.next_reference_obj -= 1
    needs_ref = (fp.next_reference_obj == 0)
    #
    fp.error_pos = fp.pos
    try:
        i = fp.consume(2)
    except SerializerError:
        i = fp.consume(1)
        if fp.s[i] == '}':
            fp.space.ec.notice("unserialize(): "
                               "Unexpected end of serialized data")
        raise
    s = fp.s
    tp = s[i]
    check_nonneg(i)
    check_nonneg(fp.pos)
    #
    if tp == 'i':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        i = fp.read_int_until(';')
        w_result = fp.space.wrap(i)
    #
    elif tp == 's':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        w_result = fp.space.newstr(load_str(fp, ';'))
    #
    elif tp == 'd':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        data = fp.read_substring_until(';')
        w_number, valid = convert_string_to_number(data)
        if not valid:
            raise SerializerError('bad double')
        w_result = fp.space.newfloat(w_number.float_w(fp.space))
    #
    elif tp == 'b':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        i = fp.consume(2)
        digit = s[i]
        if digit == '0':
            w_result = fp.space.w_False
        elif digit == '1':
            w_result = fp.space.w_True
        else:
            raise SerializerError('bad bool')
        if s[i + 1] != ';':
            raise SerializerError("';' expected")
    #
    elif tp == 'N':
        if s[i + 1] != ';':
            raise SerializerError("';' expected")
        w_result = fp.space.w_Null
    #
    elif tp == 'a':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        length = fp.read_int_until(':', can_be_negative=False)
        if length < 0:
            raise SerializerError("integer overflow")
        i = fp.consume(1)
        if s[i] != '{':
            raise SerializerError("'{' expected")
        w_result = None
        if needs_ref:
            w_result = fp.space.empty_ref()
            fp.save_reference(w_result)
        # first try to load the array as a direct list
        lst_w = []
        expected = ['i', ':', '0', ';']
        for n in range(length):
            i = fp.pos
            if i + len(expected) >= len(s):
                break
            for j in range(len(expected)):
                if s[i] != expected[j]:
                    break
                i += 1
            else:
                # ok, we got exactly 'i:N;' where N is the expected index
                fp.pos = i
                lst_w.append(load_object(fp))
                # increment the expected counter
                j = len(expected) - 2
                while j >= 2:
                    if expected[j] != '9':
                        expected[j] = chr(ord(expected[j]) + 1)
                        break
                    expected[j] = '0'
                    j -= 1
                else:
                    expected = ['i', ':', '1'] + expected[2:]
                continue
            break
        else:
            # we succeeded in loading the complete array as a list
            n = length
            _succeeded_as_a_list()   # for tests
        # fill in the remaining entries, if any, the slow way
        w_array = fp.space.new_array_from_list(lst_w)
        for n in range(n, length):
            w_key = load_array_key(fp)
            w_value = load_object(fp)
            w_array = fp.space.setitem_maybe_inplace(w_array, w_key, w_value)
        fp.expect_closing_brace()
        if w_result is not None:    # needs_ref
            w_result.store(w_array, unique=True)
            return w_result
        else:
            return w_array
    #
    elif tp == 'R':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        return load_reference(fp)
    #
    elif tp == 'r':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        return load_reference(fp).deref()
    #
    elif tp == 'O':
        if s[i + 1] != ':':
            raise SerializerError("':' expected")
        klass_name = load_str(fp, ':')
        space = fp.space
        interp = space.ec.interpreter
        klass = interp.lookup_class_or_intf(klass_name)
        if klass is None:
            klass = k_incomplete
            w_instance = klass.get_empty_instance(space)
            w_instance.setattr(interp, '__PHP_Incomplete_Class_Name',
                               space.wrap(klass_name), None)
        else:
            w_instance = klass.get_empty_instance(space)
        w_result = w_instance
        if needs_ref:
            w_result = W_Reference(w_instance)
            fp.save_reference(w_result)
        count_attrs = fp.read_int_until(':')    # negative value accepted :-(
        i = fp.consume(1)
        if s[i] != '{':
            raise SerializerError("'{' expected")
        attrs = {}
        for i in xrange(count_attrs):
            w_attr = load_array_key(fp)
            w_value = load_object(fp)
            attr_name = space.str_w(w_attr)
            attrs[attr_name] = w_value
            w_instance.setattr(interp, attr_name, w_value, None)
        fp.expect_closing_brace()

        w_instance.unserialize(space, attrs)

        if '__wakeup' in klass.methods:
            klass.methods['__wakeup'].method_func.call_args(space.ec.interpreter,
                                                            [], w_this=w_instance,
                                                            thisclass=klass)
        return w_result
    #
    else:
        if tp == '}':
            fp.space.ec.notice("unserialize(): "
                               "Unexpected end of serialized data")
        raise SerializerError('malformed input')

    # this is for primitive types only; complex types 'return' above
    if needs_ref:
        w_result = W_Reference(w_result)
        fp.save_reference(w_result)
    return w_result
Beispiel #27
0
 def str(self, index):
     check_nonneg(index)
     return rutf8.codepoint_at_pos(self._utf8, index)
Beispiel #28
0
 def g(x):
     check_nonneg(x - 1)
Beispiel #29
0
 def f(x):
     assert x >= 5
     check_nonneg(x)
Beispiel #30
0
 def str(self, index):
     check_nonneg(index)
     return ord(self._buffer.getitem(index))