• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python compat.text_函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: test_from_text_file

def test_from_text_file():
    res = Response("test")
    inp = io.StringIO(text_(str(res), "utf-8"))
    equal_resp(res, inp)
    res = Response(app_iter=iter([b"test ", b"body"]), content_type="text/plain")
    inp = io.StringIO(text_(str(res), "utf-8"))
    equal_resp(res, inp)
开发者ID:Pylons,项目名称:webob,代码行数:7,代码来源:test_response.py


示例2: test_from_text_file

def test_from_text_file():
    res = Response('test')
    inp = io.StringIO(text_(str(res), 'utf-8'))
    equal_resp(res, inp)
    res = Response(app_iter=iter([b'test ', b'body']),
                   content_type='text/plain')
    inp = io.StringIO(text_(str(res), 'utf-8'))
    equal_resp(res, inp)
开发者ID:doulbekill,项目名称:webob,代码行数:8,代码来源:test_response.py


示例3: test_header_getter_fset_text_control_chars

def test_header_getter_fset_text_control_chars():
    from webob.compat import text_
    from webob.descriptors import header_getter
    from webob import Response
    resp = Response('aresp')
    desc = header_getter('AHEADER', '14.3')
    desc.fset(resp, text_('pylons\n'))
    with pytest.raises(ValueError):
        desc.fset(resp, text_('pylons\npyramid'))
开发者ID:invisibleroads,项目名称:webob,代码行数:9,代码来源:test_descriptors.py


示例4: test_from_fieldstorage_with_quoted_printable_encoding

    def test_from_fieldstorage_with_quoted_printable_encoding(self):
        from cgi import FieldStorage
        from webob.request import BaseRequest
        from webob.multidict import MultiDict

        multipart_type = "multipart/form-data; boundary=foobar"
        from io import BytesIO

        body = (
            b"--foobar\r\n"
            b'Content-Disposition: form-data; name="title"\r\n'
            b'Content-type: text/plain; charset="ISO-2022-JP"\r\n'
            b"Content-Transfer-Encoding: quoted-printable\r\n"
            b"\r\n"
            b"=1B$B$3$s$K$A$O=1B(B"
            b"\r\n"
            b"--foobar--"
        )
        multipart_body = BytesIO(body)
        environ = BaseRequest.blank("/").environ
        environ.update(CONTENT_TYPE=multipart_type)
        environ.update(REQUEST_METHOD="POST")
        environ.update(CONTENT_LENGTH=len(body))
        fs = FieldStorage(multipart_body, environ=environ)
        vars = MultiDict.from_fieldstorage(fs)
        self.assertEqual(vars["title"].encode("utf8"), text_("こんにちは", "utf8").encode("utf8"))
开发者ID:B-Rich,项目名称:webob,代码行数:26,代码来源:test_multidict.py


示例5: test_unicode_cookies_error_raised

def test_unicode_cookies_error_raised():
    res = Response()
    with pytest.raises(ValueError):
        Response.set_cookie(
            res,
            'x',
            text_(b'\N{BLACK SQUARE}', 'unicode_escape'))
开发者ID:doulbekill,项目名称:webob,代码行数:7,代码来源:test_response.py


示例6: html_escape

def html_escape(s):
    """HTML-escape a string or object

    This converts any non-string objects passed into it to strings
    (actually, using ``unicode()``).  All values returned are
    non-unicode strings (using ``&#num;`` entities for all non-ASCII
    characters).

    None is treated specially, and returns the empty string.
    """
    if s is None:
        return ''
    __html__ = getattr(s, '__html__', None)
    if __html__ is not None and callable(__html__):
        return s.__html__()
    if not isinstance(s, string_types):
        __unicode__ = getattr(s, '__unicode__', None)
        if __unicode__ is not None and callable(__unicode__):
            s = s.__unicode__()
        else:
            s = str(s)
    s = escape(s, True)
    if isinstance(s, text_type):
        s = s.encode('ascii', 'xmlcharrefreplace')
    return text_(s)
开发者ID:koansys,项目名称:webob,代码行数:25,代码来源:util.py


示例7: test_response

def test_response():
    req = BaseRequest.blank("/")
    res = req.get_response(simple_app)
    assert res.status == "200 OK"
    assert res.status_code == 200
    assert res.body == "OK"
    assert res.charset == "UTF-8"
    assert res.content_type == "text/html"
    res.status = 404
    assert res.status == "404 Not Found"
    assert res.status_code == 404
    res.body = b"Not OK"
    assert b"".join(res.app_iter) == b"Not OK"
    res.charset = "iso8859-1"
    assert "text/html; charset=iso8859-1" == res.headers["content-type"]
    res.content_type = "text/xml"
    assert "text/xml; charset=UTF-8" == res.headers["content-type"]
    res.content_type = "text/xml; charset=UTF-8"
    assert "text/xml; charset=UTF-8" == res.headers["content-type"]
    res.headers = {"content-type": "text/html"}
    assert res.headers["content-type"] == "text/html"
    assert res.headerlist == [("content-type", "text/html")]
    res.set_cookie("x", "y")
    assert res.headers["set-cookie"].strip(";") == "x=y; Path=/"
    res.set_cookie(text_("x"), text_("y"))
    assert res.headers["set-cookie"].strip(";") == "x=y; Path=/"
    res = Response("a body", "200 OK", content_type="text/html")
    res.encode_content()
    assert res.content_encoding == "gzip"
    assert (
        res.body
        == b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00"
    )
    res.decode_content()
    assert res.content_encoding is None
    assert res.body == b"a body"
    res.set_cookie("x", text_(b"foo"))  # test unicode value
    with pytest.raises(TypeError):
        Response(app_iter=iter(["a"]), body="somebody")
    del req.environ
    with pytest.raises(TypeError):
        Response(charset=None, content_type="image/jpeg", body=text_(b"unicode body"))
    with pytest.raises(TypeError):
        Response(wrong_key="dummy")
    with pytest.raises(TypeError):
        resp = Response()
        resp.body = text_(b"unicode body")
开发者ID:Pylons,项目名称:webob,代码行数:47,代码来源:test_response.py


示例8: test_response

def test_response():
    req = BaseRequest.blank('/')
    res = req.get_response(simple_app)
    assert res.status == '200 OK'
    assert res.status_code == 200
    assert res.body == "OK"
    assert res.charset == "UTF-8"
    assert res.content_type == 'text/html'
    res.status = 404
    assert res.status == '404 Not Found'
    assert res.status_code == 404
    res.body = b'Not OK'
    assert b''.join(res.app_iter) == b'Not OK'
    res.charset = 'iso8859-1'
    assert 'text/html; charset=iso8859-1' == res.headers['content-type']
    res.content_type = 'text/xml'
    assert 'text/xml; charset=UTF-8' == res.headers['content-type']
    res.content_type = 'text/xml; charset=UTF-8'
    assert 'text/xml; charset=UTF-8' == res.headers['content-type']
    res.headers = {'content-type': 'text/html'}
    assert res.headers['content-type'] == 'text/html'
    assert res.headerlist == [('content-type', 'text/html')]
    res.set_cookie('x', 'y')
    assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/'
    res.set_cookie(text_('x'), text_('y'))
    assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/'
    res = Response('a body', '200 OK', content_type='text/html')
    res.encode_content()
    assert res.content_encoding == 'gzip'
    assert res.body == b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00'
    res.decode_content()
    assert res.content_encoding is None
    assert res.body == b'a body'
    res.set_cookie('x', text_(b'foo')) # test unicode value
    with pytest.raises(TypeError):
        Response(app_iter=iter(['a']),
                 body="somebody")
    del req.environ
    with pytest.raises(TypeError):
        Response(charset=None,
                 content_type='image/jpeg',
                 body=text_(b"unicode body"))
    with pytest.raises(TypeError):
        Response(wrong_key='dummy')
    with pytest.raises(TypeError):
        resp = Response()
        resp.body = text_(b"unicode body")
开发者ID:SmartTeleMax,项目名称:webob,代码行数:47,代码来源:test_response.py


示例9: test_unset_cookie_key_in_cookies

def test_unset_cookie_key_in_cookies():
    res = Response()
    res.headers.add('Set-Cookie', 'a=2; Path=/')
    res.headers.add('Set-Cookie', 'b=3; Path=/')
    res.unset_cookie('a')
    eq_(res.headers.getall('Set-Cookie'), ['b=3; Path=/'])
    res.unset_cookie(text_('b'))
    eq_(res.headers.getall('Set-Cookie'), [])
开发者ID:perey,项目名称:webob,代码行数:8,代码来源:test_response.py


示例10: test_fset_nonascii

 def test_fset_nonascii(self):
     desc = self._callFUT("HTTP_X_AKEY", encattr="url_encoding")
     req = self._makeRequest()
     desc.fset(req, text_(b"\xc3\xab", "utf-8"))
     if PY3:
         self.assertEqual(req.environ["HTTP_X_AKEY"], b"\xc3\xab".decode("latin-1"))
     else:
         self.assertEqual(req.environ["HTTP_X_AKEY"], b"\xc3\xab")
开发者ID:sigmavirus24,项目名称:webob,代码行数:8,代码来源:test_descriptors.py


示例11: test_header_getter_fset_text_control_chars

def test_header_getter_fset_text_control_chars():
    from webob.compat import text_
    from webob.descriptors import header_getter
    from webob import Response

    resp = Response("aresp")
    desc = header_getter("AHEADER", "14.3")
    assert_raises(ValueError, desc.fset, resp, text_("\n"))
开发者ID:sigmavirus24,项目名称:webob,代码行数:8,代码来源:test_descriptors.py


示例12: test_unset_cookie_key_in_cookies

def test_unset_cookie_key_in_cookies():
    res = Response()
    res.headers.add("Set-Cookie", "a=2; Path=/")
    res.headers.add("Set-Cookie", "b=3; Path=/")
    res.unset_cookie("a")
    assert res.headers.getall("Set-Cookie") == ["b=3; Path=/"]
    res.unset_cookie(text_("b"))
    assert res.headers.getall("Set-Cookie") == []
开发者ID:Pylons,项目名称:webob,代码行数:8,代码来源:test_response.py


示例13: test_header_getter_fset_text

def test_header_getter_fset_text():
    from webob.compat import text_
    from webob.descriptors import header_getter
    from webob import Response
    resp = Response('aresp')
    desc = header_getter('AHEADER', '14.3')
    desc.fset(resp, text_('avalue'))
    assert desc.fget(resp) == 'avalue'
开发者ID:invisibleroads,项目名称:webob,代码行数:8,代码来源:test_descriptors.py


示例14: test_fset_nonascii

 def test_fset_nonascii(self):
     desc = self._callFUT('HTTP_X_AKEY', encattr='url_encoding')
     req = self._makeRequest()
     desc.fset(req, text_(b'\xc3\xab', 'utf-8'))
     if PY3:
         assert req.environ['HTTP_X_AKEY'] == b'\xc3\xab'.decode('latin-1')
     else:
         assert req.environ['HTTP_X_AKEY'] == b'\xc3\xab'
开发者ID:invisibleroads,项目名称:webob,代码行数:8,代码来源:test_descriptors.py


示例15: test_cookies

def test_cookies():
    res = Response()
    # test unicode value
    res.set_cookie("x", text_(b"\N{BLACK SQUARE}", "unicode_escape"))
    # utf8 encoded
    eq_(res.headers.getall("set-cookie"), ['x="\\342\\226\\240"; Path=/'])
    r2 = res.merge_cookies(simple_app)
    r2 = BaseRequest.blank("/").get_response(r2)
    eq_(r2.headerlist, [("Content-Type", "text/html; charset=utf8"), ("Set-Cookie", 'x="\\342\\226\\240"; Path=/')])
开发者ID:xpahos,项目名称:webob,代码行数:9,代码来源:test_response.py


示例16: test_header_getter_fset_text

def test_header_getter_fset_text():
    from webob.compat import text_
    from webob.descriptors import header_getter
    from webob import Response

    resp = Response("aresp")
    desc = header_getter("AHEADER", "14.3")
    desc.fset(resp, text_("avalue"))
    eq_(desc.fget(resp), "avalue")
开发者ID:sigmavirus24,项目名称:webob,代码行数:9,代码来源:test_descriptors.py


示例17: test_fget_nonascii

 def test_fget_nonascii(self):
     desc = self._callFUT('HTTP_X_AKEY', encattr='url_encoding')
     req = self._makeRequest()
     if PY3:
         req.environ['HTTP_X_AKEY'] = b'\xc3\xab'.decode('latin-1')
     else:
         req.environ['HTTP_X_AKEY'] = b'\xc3\xab'
     result = desc.fget(req)
     self.assertEqual(result, text_(b'\xc3\xab', 'utf-8'))
开发者ID:AgentJay,项目名称:webapp-improved,代码行数:9,代码来源:test_descriptors.py


示例18: __setitem__

 def __setitem__(self, name, value):
     name = self._valid_cookie_name(name)
     if not isinstance(value, string_types):
         raise ValueError(value, "cookie value must be a string")
     if not isinstance(value, text_type):
         try:
             value = text_(value, "utf-8")
         except UnicodeDecodeError:
             raise ValueError(value, "cookie value must be utf-8 binary or unicode")
     self._mutate_header(name, value)
开发者ID:hzweveryday,项目名称:webob,代码行数:10,代码来源:cookies.py


示例19: test_unicode_body

def test_unicode_body():
    res = Response()
    res.charset = 'utf-8'
    bbody = b'La Pe\xc3\xb1a' # binary string
    ubody = text_(bbody, 'utf-8') # unicode string
    res.body = bbody
    assert res.unicode_body == ubody
    res.ubody = ubody
    assert res.body == bbody
    del res.ubody
    assert res.body == b''
开发者ID:doulbekill,项目名称:webob,代码行数:11,代码来源:test_response.py


示例20: test_unicode_body

def test_unicode_body():
    res = Response()
    res.charset = "utf-8"
    bbody = b"La Pe\xc3\xb1a"  # binary string
    ubody = text_(bbody, "utf-8")  # unicode string
    res.body = bbody
    assert res.unicode_body == ubody
    res.ubody = ubody
    assert res.body == bbody
    del res.ubody
    assert res.body == b""
开发者ID:Pylons,项目名称:webob,代码行数:11,代码来源:test_response.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python descriptors.converter函数代码示例发布时间:2022-05-26
下一篇:
Python compat.native_函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap