Ejemplo n.º 1
0
class W_StringObject(W_Root):
    _immutable_fields_ = ['stringval']

    def __init__(self, stringval):
        assert(isinstance(stringval, str))
        self.stringval = RStringIO()
        self.stringval.write(stringval)

    def append(self, stringval):
        self.stringval.write(stringval)

    def get(self, key):
        assert isinstance(key, W_IntObject)
        key = key.get_int()
        return W_StringObject(self.str()[key])

    def str(self):
        return self.stringval.getvalue()

    def hash(self):
        return compute_hash(self.str())

    def len(self):
        return len(self.str())

    def __deepcopy__(self):
        obj = instantiate(self.__class__)
        obj.stringval = self.stringval
        return obj

    def __repr__(self):
        return 'W_StringObject(%s)' % (self.str(),)
Ejemplo n.º 2
0
def test_write_many():
    f = RStringIO()
    for j in range(10):
        for i in range(253):
            f.write(chr(i))
    expected = ''.join([chr(i) for j in range(10) for i in range(253)])
    assert f.getvalue() == expected
Ejemplo n.º 3
0
def test_write_many():
    f = RStringIO()
    for j in range(10):
        for i in range(253):
            f.write(chr(i))
    expected = ''.join([chr(i) for j in range(10) for i in range(253)])
    assert f.getvalue() == expected
Ejemplo n.º 4
0
def test_bug():
    f = RStringIO()
    f.write('0')
    f.write('1')
    f.write('2')
    assert f.getvalue() == '012'
    f.write('3')
    assert f.getvalue() == '0123'
Ejemplo n.º 5
0
def test_readline():
    f = RStringIO()
    f.write('foo\nbar\nbaz')
    f.seek(0)
    assert f.readline() == 'foo\n'
    assert f.readline(2) == 'ba'
    assert f.readline() == 'r\n'
    assert f.readline() == 'baz'
    assert f.readline() == ''
Ejemplo n.º 6
0
def test_bug():
    f = RStringIO()
    f.write('0')
    f.write('1')
    f.write('2')
    assert f.getvalue() == '012'
    f.write('3')
    assert f.getvalue() == '0123'
Ejemplo n.º 7
0
def test_readline():
    f = RStringIO()
    f.write('foo\nbar\nbaz')
    f.seek(0)
    assert f.readline() == 'foo\n'
    assert f.readline(2) == 'ba'
    assert f.readline() == 'r\n'
    assert f.readline() == 'baz'
    assert f.readline() == ''
Ejemplo n.º 8
0
def setup_cgi(interp, params, argv, post_data=None):
    get = OrderedDict()
    post = OrderedDict()
    files = OrderedDict()
    space = interp.space
    query = get_param(params, 'QUERY_STRING')
    if query is not None:
        unpack_query(get, query, space)
    script_name = get_param(params, 'SCRIPT_NAME')
    initial_server_dict = OrderedDict()
    if script_name is not None:
        initial_server_dict['PHP_SELF'] = space.wrap(script_name)

    cookie = get_param(params, 'HTTP_COOKIE')
    content_length = get_param(params, 'CONTENT_LENGTH')
    content_type_set = get_param(params, 'CONTENT_TYPE')
    content_type = ""
    boundary = ""
    for k in all_keys_from(params):
        initial_server_dict[k] = space.wrap(get_param(params, k))
    if content_type_set is not None and content_length is not None:
        m = search("[; ,]", content_type_set)
        if m:
            start = m.start(0)
            assert start >= 0
            content_type = content_type_set[:start]
            content_type = content_type.lower()
        else:
            content_type = content_type_set.lower()

        if (content_type == 'x-www-form-urlencoded'
                or content_type == 'application/x-www-form-urlencoded'):
            content_length = int(content_length)
            if post_data is None:
                stdin = interp.open_stdin_stream()
                post_data = stdin.read(content_length)
            unpack_query(post, post_data, space)
        elif content_type == 'multipart/form-data':
            m = search("boundary", content_type_set)
            if m:
                end = m.end(0)
                assert end >= 0
                boundary = content_type_set[end + 1:]
            if not boundary:
                interp.warn("Missing boundary, ignoring post")
            fp = RStringIO()
            fp.write(post_data)
            fp.seek(0)
            post, files = parse_multipart(space, fp, boundary, OrderedDict(),
                                          OrderedDict())
            fp.close()
        else:
            interp.warn("Unknown content type: %s, ignoring post" %
                        content_type)
    return CGIConfig(space.new_array_from_rdict(get),
                     space.new_array_from_rdict(post),
                     space.new_array_from_rdict(files), initial_server_dict,
                     cookie)
Ejemplo n.º 9
0
def setup_cgi(interp, params, argv, post_data=None):
    get = OrderedDict()
    post = OrderedDict()
    files = OrderedDict()
    space = interp.space
    query = get_param(params, 'QUERY_STRING')
    if query is not None:
        unpack_query(get, query, space)
    script_name = get_param(params, 'SCRIPT_NAME')
    initial_server_dict = OrderedDict()
    if script_name is not None:
        initial_server_dict['PHP_SELF'] = space.wrap(script_name)

    cookie = get_param(params, 'HTTP_COOKIE')
    content_length = get_param(params, 'CONTENT_LENGTH')
    content_type_set = get_param(params, 'CONTENT_TYPE')
    content_type = ""
    boundary = ""
    for k in all_keys_from(params):
        initial_server_dict[k] = space.wrap(get_param(params, k))
    if content_type_set is not None and content_length is not None:
        m = search("[; ,]", content_type_set)
        if m:
            start = m.start(0)
            assert start >= 0
            content_type = content_type_set[:start]
            content_type = content_type.lower()
        else:
            content_type = content_type_set.lower()

        if (content_type == 'x-www-form-urlencoded' or
            content_type == 'application/x-www-form-urlencoded'):
            content_length = int(content_length)
            if post_data is None:
                stdin = interp.open_stdin_stream()
                post_data = stdin.read(content_length)
            unpack_query(post, post_data, space)
        elif content_type == 'multipart/form-data':
            m = search("boundary", content_type_set)
            if m:
                end = m.end(0)
                assert end >= 0
                boundary = content_type_set[end + 1:]
            if not boundary:
                interp.warn("Missing boundary, ignoring post")
            fp = RStringIO()
            fp.write(post_data)
            fp.seek(0)
            post, files = parse_multipart(space, fp, boundary,
                                          OrderedDict(), OrderedDict())
            fp.close()
        else:
            interp.warn("Unknown content type: %s, ignoring post" %
                        content_type)
    return CGIConfig(space.new_array_from_rdict(get),
                     space.new_array_from_rdict(post),
                     space.new_array_from_rdict(files),
                     initial_server_dict, cookie)
Ejemplo n.º 10
0
def test_stress():
    import cStringIO, random
    f = RStringIO()
    expected = cStringIO.StringIO()
    for i in range(2000):
        r = random.random()
        if r < 0.15:
            p = random.randrange(-5000, 10000)
            if r < 0.05:
                mode = 0
            elif r < 0.1:
                mode = 1
            else:
                mode = 2
            print 'seek', p, mode
            f.seek(p, mode)
            expected.seek(p, mode)
        elif r < 0.6:
            buf = str(random.random())
            print 'write %d bytes' % len(buf)
            f.write(buf)
            expected.write(buf)
        elif r < 0.92:
            n = random.randrange(0, 100)
            print 'read %d bytes' % n
            data1 = f.read(n)
            data2 = expected.read(n)
            assert data1 == data2
        elif r < 0.97:
            print 'check tell()'
            assert f.tell() == expected.tell()
        else:
            print 'check getvalue()'
            assert f.getvalue() == expected.getvalue()
    assert f.getvalue() == expected.getvalue()
    assert f.tell() == expected.tell()
Ejemplo n.º 11
0
	def parse(self, stream):
		self.len_uncompressed = self.readInt4(stream)

		odata = self.readAny(stream, self.size - 4)
		
		# ugly
		data = self.decompress(odata)
		substream = RStringIO()
		substream.write(data)
		substream.seek(0)
		readed = self.readlen

		self.count = self.readInt4(substream)
		self.term_list = []
		for i in range(0, self.count):
			size = self.readInt4(substream)
			magic = self.readUCInt(substream)
			assert(magic == 0x83)
			self.term_list.append(self.create_term(substream))
		self.readlen = readed
		self.discardRemain(stream)
Ejemplo n.º 12
0
def test_truncate_end():
    f = RStringIO()
    f.write("abc")
    f.seek(0)
    f.truncate(0)
    assert f.getvalue() == ""
Ejemplo n.º 13
0
def test_read():
    f = RStringIO()
    assert f.read() == ''
    f.write('0123')
    f.write('456')
    assert f.read() == ''
    assert f.read(5) == ''
    assert f.tell() == 7
    f.seek(1)
    assert f.read() == '123456'
    assert f.tell() == 7
    f.seek(1)
    assert f.read(12) == '123456'
    assert f.tell() == 7
    f.seek(1)
    assert f.read(2) == '12'
    assert f.read(1) == '3'
    assert f.tell() == 4
    f.seek(0)
    assert f.read() == '0123456'
    assert f.tell() == 7
    f.seek(0)
    assert f.read(7) == '0123456'
    assert f.tell() == 7
    f.seek(15)
    assert f.read(2) == ''
    assert f.tell() == 15
Ejemplo n.º 14
0
def test_tell():
    f = RStringIO()
    f.write('0123')
    f.write('456')
    assert f.tell() == 7
    f.seek(2)
    for i in range(3, 20):
        f.write('X')
        assert f.tell() == i
    assert f.getvalue() == '01XXXXXXXXXXXXXXXXX'

    f.seek(1)
    f.close()
    assert f.tell() == 0
Ejemplo n.º 15
0
def test_write_beyond_end():
    f = RStringIO()
    f.seek(20, 1)
    assert f.tell() == 20
    f.write('X')
    assert f.getvalue() == '\x00' * 20 + 'X'
Ejemplo n.º 16
0
def test_simple():
    f = RStringIO()
    f.write('hello')
    f.write(' world')
    assert f.getvalue() == 'hello world'
Ejemplo n.º 17
0
def test_tell():
    f = RStringIO()
    f.write('0123')
    f.write('456')
    assert f.tell() == 7
    f.seek(2)
    for i in range(3, 20):
        f.write('X')
        assert f.tell() == i
    assert f.getvalue() == '01XXXXXXXXXXXXXXXXX'

    f.seek(1)
    f.close()
    assert f.tell() == 0
Ejemplo n.º 18
0
def test_stress():
    import cStringIO, random
    f = RStringIO()
    expected = cStringIO.StringIO()
    for i in range(2000):
        r = random.random()
        if r < 0.15:
            p = random.randrange(-5000, 10000)
            if r < 0.05:
                mode = 0
            elif r < 0.1:
                mode = 1
            else:
                mode = 2
            print 'seek', p, mode
            f.seek(p, mode)
            expected.seek(p, mode)
        elif r < 0.6:
            buf = str(random.random())
            print 'write %d bytes' % len(buf)
            f.write(buf)
            expected.write(buf)
        elif r < 0.92:
            n = random.randrange(0, 100)
            print 'read %d bytes' % n
            data1 = f.read(n)
            data2 = expected.read(n)
            assert data1 == data2
        elif r < 0.97:
            print 'check tell()'
            assert f.tell() == expected.tell()
        else:
            print 'check getvalue()'
            assert f.getvalue() == expected.getvalue()
    assert f.getvalue() == expected.getvalue()
    assert f.tell() == expected.tell()
Ejemplo n.º 19
0
def test_truncate_end():
    f = RStringIO()
    f.write("abc")
    f.seek(0)
    f.truncate(0)
    assert f.getvalue() == ""
Ejemplo n.º 20
0
def test_seek():
    f = RStringIO()
    f.write('0123')
    f.write('456')
    f.write('789')
    f.seek(4)
    f.write('AB')
    assert f.getvalue() == '0123AB6789'
    f.seek(-2, 2)
    f.write('CDE')
    assert f.getvalue() == '0123AB67CDE'
    f.seek(2, 0)
    f.seek(5, 1)
    f.write('F')
    assert f.getvalue() == '0123AB6FCDE'
Ejemplo n.º 21
0
def test_truncate():
    f = RStringIO()
    f.truncate(20)
    assert f.getvalue() == ''
    assert f.tell() == 0
    f.write('\x00' * 25)
    f.seek(12)
    f.truncate(20)
    assert f.getvalue() == '\x00' * 20
    assert f.tell() == 20
    f.write('more')
    f.truncate(20)
    assert f.getvalue() == '\x00' * 20
    assert f.tell() == 20
    f.write('hello')
    f.write(' world')
    f.truncate(30)
    assert f.getvalue() == '\x00' * 20 + 'hello worl'
    f.truncate(25)
    assert f.getvalue() == '\x00' * 20 + 'hello'
    f.write('baz')
    f.write('egg')
    f.truncate(3)
    assert f.getvalue() == '\x00' * 3
    assert f.tell() == 3
Ejemplo n.º 22
0
def test_simple():
    f = RStringIO()
    f.write('hello')
    f.write(' world')
    assert f.getvalue() == 'hello world'
Ejemplo n.º 23
0
def test_read():
    f = RStringIO()
    assert f.read() == ''
    f.write('0123')
    f.write('456')
    assert f.read() == ''
    assert f.read(5) == ''
    assert f.tell() == 7
    f.seek(1)
    assert f.read() == '123456'
    assert f.tell() == 7
    f.seek(1)
    assert f.read(12) == '123456'
    assert f.tell() == 7
    f.seek(1)
    assert f.read(2) == '12'
    assert f.read(1) == '3'
    assert f.tell() == 4
    f.seek(0)
    assert f.read() == '0123456'
    assert f.tell() == 7
    f.seek(0)
    assert f.read(7) == '0123456'
    assert f.tell() == 7
    f.seek(15)
    assert f.read(2) == ''
    assert f.tell() == 15
Ejemplo n.º 24
0
 def __init__(self, stringval):
     assert(isinstance(stringval, str))
     self.stringval = RStringIO()
     self.stringval.write(stringval)
Ejemplo n.º 25
0
def test_truncate():
    f = RStringIO()
    f.truncate(20)
    assert f.getvalue() == ''
    assert f.tell() == 0
    f.write('\x00' * 25)
    f.seek(12)
    f.truncate(20)
    assert f.getvalue() == '\x00' * 20
    assert f.tell() == 20
    f.write('more')
    f.truncate(20)
    assert f.getvalue() == '\x00' * 20
    assert f.tell() == 20
    f.write('hello')
    f.write(' world')
    f.truncate(30)
    assert f.getvalue() == '\x00' * 20 + 'hello worl'
    f.truncate(25)
    assert f.getvalue() == '\x00' * 20 + 'hello'
    f.write('baz')
    f.write('egg')
    f.truncate(3)
    assert f.getvalue() == '\x00' * 3
    assert f.tell() == 3
Ejemplo n.º 26
0
def test_seek():
    f = RStringIO()
    f.write('0123')
    f.write('456')
    f.write('789')
    f.seek(4)
    f.write('AB')
    assert f.getvalue() == '0123AB6789'
    f.seek(-2, 2)
    f.write('CDE')
    assert f.getvalue() == '0123AB67CDE'
    f.seek(2, 0)
    f.seek(5, 1)
    f.write('F')
    assert f.getvalue() == '0123AB6FCDE'
Ejemplo n.º 27
0
	def __init__(self, fstream):
		# ugly!!!
		stream = RStringIO()
		stream.write(fstream.read())
		stream.seek(0)
		BaseNode.__init__(self, stream)
Ejemplo n.º 28
0
def test_write_beyond_end():
    f = RStringIO()
    f.seek(20, 1)
    assert f.tell() == 20
    f.write('X')
    assert f.getvalue() == '\x00' * 20 + 'X'