当前位置: 首页>>代码示例>>Python>>正文


Python _compat.StringIO类代码示例

本文整理汇总了Python中flask._compat.StringIO的典型用法代码示例。如果您正苦于以下问题:Python StringIO类的具体用法?Python StringIO怎么用?Python StringIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了StringIO类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_send_file_object

    def test_send_file_object(self):
        app = flask.Flask(__name__)

        with app.test_request_context():
            with open(os.path.join(app.root_path, 'static/index.html'), mode='rb') as f:
                rv = flask.send_file(f)
                rv.direct_passthrough = False
                with app.open_resource('static/index.html') as f:
                    assert rv.data == f.read()
                assert rv.mimetype == 'text/html'
                rv.close()

        app.use_x_sendfile = True

        with app.test_request_context():
            with open(os.path.join(app.root_path, 'static/index.html')) as f:
                rv = flask.send_file(f)
                assert rv.mimetype == 'text/html'
                assert 'x-sendfile' in rv.headers
                assert rv.headers['x-sendfile'] == \
                    os.path.join(app.root_path, 'static/index.html')
                rv.close()

        app.use_x_sendfile = False
        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f)
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'application/octet-stream'
            rv.close()

            class PyStringIO(object):
                def __init__(self, *args, **kwargs):
                    self._io = StringIO(*args, **kwargs)
                def __getattr__(self, name):
                    return getattr(self._io, name)
            f = PyStringIO('Test')
            f.name = 'test.txt'
            rv = flask.send_file(f)
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'text/plain'
            rv.close()

            f = StringIO('Test')
            rv = flask.send_file(f, mimetype='text/plain')
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'text/plain'
            rv.close()

        app.use_x_sendfile = True

        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f)
            assert 'x-sendfile' not in rv.headers
            rv.close()
开发者ID:000fan000,项目名称:flask,代码行数:59,代码来源:test_helpers.py

示例2: test_json_dump_to_file

    def test_json_dump_to_file(self, app, app_ctx):
        test_data = {'name': 'Flask'}
        out = StringIO()

        flask.json.dump(test_data, out)
        out.seek(0)
        rv = flask.json.load(out)
        assert rv == test_data
开发者ID:s-block,项目名称:flask,代码行数:8,代码来源:test_helpers.py

示例3: test_json_dump_to_file

    def test_json_dump_to_file(self):
        app = flask.Flask(__name__)
        test_data = {'name': 'Flask'}
        out = StringIO()

        with app.app_context():
            flask.json.dump(test_data, out)
            out.seek(0)
            rv = flask.json.load(out)
            assert rv == test_data
开发者ID:VishvajitP,项目名称:flask,代码行数:10,代码来源:test_helpers.py

示例4: test_send_file_object

    def test_send_file_object(self, app, req_ctx):
        with open(os.path.join(app.root_path, "static/index.html"), mode="rb") as f:
            rv = flask.send_file(f, mimetype="text/html")
            rv.direct_passthrough = False
            with app.open_resource("static/index.html") as f:
                assert rv.data == f.read()
            assert rv.mimetype == "text/html"
            rv.close()

        app.use_x_sendfile = True

        with open(os.path.join(app.root_path, "static/index.html")) as f:
            rv = flask.send_file(f, mimetype="text/html")
            assert rv.mimetype == "text/html"
            assert "x-sendfile" not in rv.headers
            rv.close()

        app.use_x_sendfile = False
        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="application/octet-stream")
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "application/octet-stream"
        rv.close()

        class PyStringIO(object):
            def __init__(self, *args, **kwargs):
                self._io = StringIO(*args, **kwargs)

            def __getattr__(self, name):
                return getattr(self._io, name)

        f = PyStringIO("Test")
        f.name = "test.txt"
        rv = flask.send_file(f, attachment_filename=f.name)
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "text/plain"
        rv.close()

        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="text/plain")
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "text/plain"
        rv.close()

        app.use_x_sendfile = True

        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="text/html")
        assert "x-sendfile" not in rv.headers
        rv.close()
开发者ID:Warkanlock,项目名称:flask,代码行数:53,代码来源:test_helpers.py

示例5: test_log_view_exception

def test_log_view_exception(app, client):
    @app.route('/')
    def index():
        raise Exception('test')

    app.testing = False
    stream = StringIO()
    rv = client.get('/', errors_stream=stream)
    assert rv.status_code == 500
    assert rv.data
    err = stream.getvalue()
    assert 'Exception on / [GET]' in err
    assert 'Exception: test' in err
开发者ID:AEliu,项目名称:flask,代码行数:13,代码来源:test_logging.py

示例6: test_wsgi_errors_stream

def test_wsgi_errors_stream(app, client):
    @app.route('/')
    def index():
        app.logger.error('test')
        return ''

    stream = StringIO()
    client.get('/', errors_stream=stream)
    assert 'ERROR in test_logging: test' in stream.getvalue()

    assert wsgi_errors_stream._get_current_object() is sys.stderr

    with app.test_request_context(errors_stream=stream):
        assert wsgi_errors_stream._get_current_object() is stream
开发者ID:AEliu,项目名称:flask,代码行数:14,代码来源:test_logging.py

示例7: test_exception_logging

    def test_exception_logging(self):
        out = StringIO()
        app = flask.Flask(__name__)
        app.logger_name = 'flask_tests/test_exception_logging'
        app.logger.addHandler(StreamHandler(out))

        @app.route('/')
        def index():
            1 // 0

        rv = app.test_client().get('/')
        self.assert_equal(rv.status_code, 500)
        self.assert_in(b'Internal Server Error', rv.data)

        err = out.getvalue()
        self.assert_in('Exception on / [GET]', err)
        self.assert_in('Traceback (most recent call last):', err)
        self.assert_in('1 // 0', err)
        self.assert_in('ZeroDivisionError:', err)
开发者ID:lebseu,项目名称:VAMK-HELP,代码行数:19,代码来源:helpers.py

示例8: test_suppressed_exception_logging

def test_suppressed_exception_logging():
    class SuppressedFlask(flask.Flask):
        def log_exception(self, exc_info):
            pass

    out = StringIO()
    app = SuppressedFlask(__name__)
    app.logger_name = 'flask_tests/test_suppressed_exception_logging'
    app.logger.addHandler(StreamHandler(out))

    @app.route('/')
    def index():
        1 // 0

    rv = app.test_client().get('/')
    assert rv.status_code == 500
    assert b'Internal Server Error' in rv.data

    err = out.getvalue()
    assert err == ''
开发者ID:0x78kiki,项目名称:flask,代码行数:20,代码来源:test_subclassing.py

示例9: test_exception_logging

    def test_exception_logging(self, app, client):
        out = StringIO()
        app.config['LOGGER_HANDLER_POLICY'] = 'never'
        app.logger_name = 'flask_tests/test_exception_logging'
        app.logger.addHandler(StreamHandler(out))
        app.testing = False

        @app.route('/')
        def index():
            1 // 0

        rv = client.get('/')
        assert rv.status_code == 500
        assert b'Internal Server Error' in rv.data

        err = out.getvalue()
        assert 'Exception on / [GET]' in err
        assert 'Traceback (most recent call last):' in err
        assert '1 // 0' in err
        assert 'ZeroDivisionError:' in err
开发者ID:s-block,项目名称:flask,代码行数:20,代码来源:test_helpers.py

示例10: test_send_file_object

    def test_send_file_object(self):
        app = flask.Flask(__name__)
        with catch_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'))
                rv = flask.send_file(f)
                rv.direct_passthrough = False
                with app.open_resource('static/index.html') as f:
                    self.assert_equal(rv.data, f.read())
                self.assert_equal(rv.mimetype, 'text/html')
                rv.close()
            # mimetypes + etag
            self.assert_equal(len(captured), 2)

        app.use_x_sendfile = True
        with catch_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'))
                rv = flask.send_file(f)
                self.assert_equal(rv.mimetype, 'text/html')
                self.assert_in('x-sendfile', rv.headers)
                self.assert_equal(rv.headers['x-sendfile'],
                    os.path.join(app.root_path, 'static/index.html'))
                rv.close()
            # mimetypes + etag
            self.assert_equal(len(captured), 2)

        app.use_x_sendfile = False
        with app.test_request_context():
            with catch_warnings() as captured:
                f = StringIO('Test')
                rv = flask.send_file(f)
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'application/octet-stream')
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)
            with catch_warnings() as captured:
                class PyStringIO(object):
                    def __init__(self, *args, **kwargs):
                        self._io = StringIO(*args, **kwargs)
                    def __getattr__(self, name):
                        return getattr(self._io, name)
                f = PyStringIO('Test')
                f.name = 'test.txt'
                rv = flask.send_file(f)
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'text/plain')
                rv.close()
            # attachment_filename and etags
            self.assert_equal(len(captured), 3)
            with catch_warnings() as captured:
                f = StringIO('Test')
                rv = flask.send_file(f, mimetype='text/plain')
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'text/plain')
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)

        app.use_x_sendfile = True
        with catch_warnings() as captured:
            with app.test_request_context():
                f = StringIO('Test')
                rv = flask.send_file(f)
                self.assert_not_in('x-sendfile', rv.headers)
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)
开发者ID:BGNepal,项目名称:flask,代码行数:72,代码来源:helpers.py


注:本文中的flask._compat.StringIO类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。