diff options
| author | Michael Merickel <michael@merickel.org> | 2018-11-18 17:24:54 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2018-11-18 17:24:54 -0600 |
| commit | 80cce1874f90a73f74d7c9ef8156df3637694387 (patch) | |
| tree | 4c619c46724386c13ce7267dcd5d76416ec0b699 /src | |
| parent | 880fcbdf366e231a746dc3ab694b1b88771d23c2 (diff) | |
| download | pyramid-80cce1874f90a73f74d7c9ef8156df3637694387.tar.gz pyramid-80cce1874f90a73f74d7c9ef8156df3637694387.tar.bz2 pyramid-80cce1874f90a73f74d7c9ef8156df3637694387.zip | |
clean up docstrings referencing unicode data
Diffstat (limited to 'src')
| -rw-r--r-- | src/pyramid/encode.py | 4 | ||||
| -rw-r--r-- | src/pyramid/i18n.py | 15 | ||||
| -rw-r--r-- | src/pyramid/response.py | 4 | ||||
| -rw-r--r-- | src/pyramid/security.py | 4 | ||||
| -rw-r--r-- | src/pyramid/traversal.py | 132 |
5 files changed, 68 insertions, 91 deletions
diff --git a/src/pyramid/encode.py b/src/pyramid/encode.py index 11d3a7787..ed8e177b8 100644 --- a/src/pyramid/encode.py +++ b/src/pyramid/encode.py @@ -26,8 +26,8 @@ def quote_plus(val, safe=''): def urlencode(query, doseq=True, quote_via=quote_plus): """ An alternate implementation of Python's stdlib - :func:`urllib.parse.urlencode` function which accepts unicode keys and - values within the ``query`` dict/sequence; all Unicode keys and values are + :func:`urllib.parse.urlencode` function which accepts string keys and + values within the ``query`` dict/sequence; all string keys and values are first converted to UTF-8 before being used to compose the query string. The value of ``query`` must be a sequence of two-tuples diff --git a/src/pyramid/i18n.py b/src/pyramid/i18n.py index 45f528852..112044e47 100644 --- a/src/pyramid/i18n.py +++ b/src/pyramid/i18n.py @@ -45,12 +45,11 @@ class Localizer(object): ``translate`` method accepts three arguments: ``tstring`` (required), ``domain`` (optional) and ``mapping`` (optional). When called, it will translate the ``tstring`` translation - string to a ``unicode`` object using the current locale. If - the current locale could not be determined, the result of - interpolation of the default value is returned. The optional - ``domain`` argument can be used to specify or override the - domain of the ``tstring`` (useful when ``tstring`` is a normal - string rather than a translation string). The optional + string the current locale. If the current locale could not be + determined, the result of interpolation of the default value is + returned. The optional ``domain`` argument can be used to specify + or override the domain of the ``tstring`` (useful when ``tstring`` + is a normal string rather than a translation string). The optional ``mapping`` argument can specify or override the ``tstring`` interpolation mapping, useful when the ``tstring`` argument is a simple string instead of a translation string. @@ -74,11 +73,11 @@ class Localizer(object): def pluralize(self, singular, plural, n, domain=None, mapping=None): """ - Return a Unicode string translation by using two + Return a string translation by using two :term:`message identifier` objects as a singular/plural pair and an ``n`` value representing the number that appears in the message using gettext plural forms support. The ``singular`` - and ``plural`` objects should be unicode strings. There is no + and ``plural`` objects should be strings. There is no reason to use translation string objects as arguments as all metadata is ignored. diff --git a/src/pyramid/response.py b/src/pyramid/response.py index 8a2ba8929..ea4677226 100644 --- a/src/pyramid/response.py +++ b/src/pyramid/response.py @@ -212,8 +212,4 @@ def _guess_type(path): content_type, content_encoding = mimetypes.guess_type(path, strict=False) if content_type is None: content_type = 'application/octet-stream' - # str-ifying content_type is a workaround for a bug in Python 2.7.7 - # on Windows where mimetypes.guess_type returns unicode for the - # content_type. - content_type = str(content_type) return content_type, content_encoding diff --git a/src/pyramid/security.py b/src/pyramid/security.py index f8743e9a7..61819588b 100644 --- a/src/pyramid/security.py +++ b/src/pyramid/security.py @@ -112,7 +112,7 @@ def forget(request): def principals_allowed_by_permission(context, permission): """ Provided a ``context`` (a resource object), and a ``permission`` - (a string or unicode object), if an :term:`authorization policy` is + string, if an :term:`authorization policy` is in effect, return a sequence of :term:`principal` ids that possess the permission in the ``context``. If no authorization policy is in effect, this will return a sequence with the single value @@ -340,7 +340,7 @@ class AuthorizationAPIMixin(object): ``request.context`` attribute. :param permission: Does this request have the given permission? - :type permission: unicode, str + :type permission: str :param context: A resource object or ``None`` :type context: object :returns: Either :class:`pyramid.security.Allowed` or diff --git a/src/pyramid/traversal.py b/src/pyramid/traversal.py index 7de4c3f91..72176fc4b 100644 --- a/src/pyramid/traversal.py +++ b/src/pyramid/traversal.py @@ -57,16 +57,12 @@ def find_resource(resource, path): object supplied to the function as the ``resource`` argument. If an empty string is passed as ``path``, the ``resource`` passed in will be returned. Resource path strings must be escaped in the following - manner: each Unicode path segment must be encoded as UTF-8 and as - each path segment must escaped via Python's :mod:`urllib.quote`. - For example, ``/path/to%20the/La%20Pe%C3%B1a`` (absolute) or - ``to%20the/La%20Pe%C3%B1a`` (relative). The + manner: each path segment must be UTF-8 encoded and escaped via Python's + :mod:`urllib.quote`. For example, ``/path/to%20the/La%20Pe%C3%B1a`` + (absolute) or ``to%20the/La%20Pe%C3%B1a`` (relative). The :func:`pyramid.traversal.resource_path` function generates strings - which follow these rules (albeit only absolute ones). - - Rules for passing *text* (Unicode) as the ``path`` argument are the same - as those for a string. In particular, the text may not have any nonascii - characters in it. + which follow these rules (albeit only absolute ones). The text may not + have any nonascii characters in it. Rules for passing a *tuple* as the ``path`` argument: if the first element in the path tuple is the empty string (for example ``('', @@ -77,11 +73,10 @@ def find_resource(resource, path): traversal will begin at the resource object supplied to the function as the ``resource`` argument. If an empty sequence is passed as ``path``, the ``resource`` passed in itself will be returned. No - URL-quoting or UTF-8-encoding of individual path segments within - the tuple is required (each segment may be any string or unicode - object representing a resource name). Resource path tuples generated by - :func:`pyramid.traversal.resource_path_tuple` can always be - resolved by ``find_resource``. + URL-quoting of individual path segments within the tuple is required + (each segment may be any string representing a resource name). Resource + path tuples generated by :func:`pyramid.traversal.resource_path_tuple` can + always be resolved by ``find_resource``. """ if isinstance(path, str): path = ascii_(path) @@ -126,8 +121,8 @@ def resource_path(resource, *elements): ``/`` character in a path string represents that the path is absolute). Resource path strings returned will be escaped in the following - manner: each unicode path segment will be encoded as UTF-8 and - each path segment will be escaped via Python's :mod:`urllib.quote`. + manner: each path segment will be encoded as UTF-8 and escaped via + Python's :mod:`urllib.quote`. For example, ``/path/to%20the/La%20Pe%C3%B1a``. This function is a logical inverse of @@ -140,7 +135,7 @@ def resource_path(resource, *elements): Each segment in the path string returned will use the ``__name__`` attribute of the resource it represents within the resource tree. Each - of these segments *should* be a unicode or string object (as per the + of these segments *should* be a string (as per the contract of :term:`location`-awareness). However, no conversion or safety checking of resource names is performed. For instance, if one of the resources in your tree has a ``__name__`` which (by error) is a @@ -189,30 +184,30 @@ def traverse(resource, path): :term:`traversal` or :term:`url dispatch`; if the ``resource`` was found via traversal, this is usually a representation of the path segment which directly follows the path to the ``context`` - in the ``path``. The ``view_name`` will be a Unicode object or - the empty string. The ``view_name`` will be the empty string if + in the ``path``. The ``view_name`` will be a string. The + ``view_name`` will be the empty string if there is no element which follows the ``context`` path. An example: if the path passed is ``/foo/bar``, and a resource object is found at ``/foo`` (but not at ``/foo/bar``), the 'view - name' will be ``u'bar'``. If the ``resource`` was found via + name' will be ``'bar'``. If the ``resource`` was found via urldispatch, the view_name will be the name the route found was registered with. - ``subpath``: For a ``resource`` found via :term:`traversal`, this is a sequence of path segments found in the ``path`` that follow - the ``view_name`` (if any). Each of these items is a Unicode - object. If no path segments follow the ``view_name``, the + the ``view_name`` (if any). Each of these items is a string. + If no path segments follow the ``view_name``, the subpath will be the empty sequence. An example: if the path passed is ``/foo/bar/baz/buz``, and a resource object is found at ``/foo`` (but not ``/foo/bar``), the 'view name' will be - ``u'bar'`` and the :term:`subpath` will be ``[u'baz', u'buz']``. + ``'bar'`` and the :term:`subpath` will be ``['baz', 'buz']``. For a ``resource`` found via url dispatch, the subpath will be a sequence of values discerned from ``*subpath`` in the route pattern matched or the empty sequence. - ``traversed``: The sequence of path elements traversed from the root to find the ``context`` object during :term:`traversal`. - Each of these items is a Unicode object. If no path segments + Each of these items is a string. If no path segments were traversed to find the ``context`` object (e.g. if the ``path`` provided is the empty string), the ``traversed`` value will be the empty sequence. If the ``resource`` is a resource found @@ -232,7 +227,7 @@ def traverse(resource, path): - ``virtual_root_path`` -- If :term:`traversal` was used to find the ``resource``, this will be the sequence of path elements traversed to find the ``virtual_root`` resource. Each of these - items is a Unicode object. If no path segments were traversed + items is a string. If no path segments were traversed to find the ``virtual_root`` resource (e.g. if virtual hosting is not in effect), the ``traversed`` value will be the empty list. If url dispatch was used to find the ``resource``, this will be @@ -249,9 +244,9 @@ def traverse(resource, path): object supplied to the function as the ``resource`` argument. If an empty string is passed as ``path``, the ``resource`` passed in will be returned. Resource path strings must be escaped in the following - manner: each Unicode path segment must be encoded as UTF-8 and - each path segment must escaped via Python's :mod:`urllib.quote`. - For example, ``/path/to%20the/La%20Pe%C3%B1a`` (absolute) or + manner: each path segment must be encoded as UTF-8 and escaped via + Python's :mod:`urllib.quote`. For example, + ``/path/to%20the/La%20Pe%C3%B1a`` (absolute) or ``to%20the/La%20Pe%C3%B1a`` (relative). The :func:`pyramid.traversal.resource_path` function generates strings which follow these rules (albeit only absolute ones). @@ -266,26 +261,24 @@ def traverse(resource, path): as the ``resource`` argument. If an empty sequence is passed as ``path``, the ``resource`` passed in itself will be returned. No URL-quoting or UTF-8-encoding of individual path segments within - the tuple is required (each segment may be any string or unicode - object representing a resource name). + the tuple is required (each segment may be any string representing + a resource name). - Explanation of the conversion of ``path`` segment values to - Unicode during traversal: Each segment is URL-unquoted, and - decoded into Unicode. Each segment is assumed to be encoded using - the UTF-8 encoding (or a subset, such as ASCII); a + Explanation of the decoding of ``path`` segment values during traversal: + Each segment is URL-unquoted, and UTF-8 decoded. Each segment is assumed + to be encoded using the UTF-8 encoding (or a subset, such as ASCII); a :exc:`pyramid.exceptions.URLDecodeError` is raised if a segment cannot be decoded. If a segment name is empty or if it is ``.``, it is ignored. If a segment name is ``..``, the previous segment is deleted, and the ``..`` is ignored. As a result of this process, the return values ``view_name``, each element in the ``subpath``, each element in ``traversed``, and each element in - the ``virtual_root_path`` will be Unicode as opposed to a string, - and will be URL-decoded. + the ``virtual_root_path`` will be decoded strings. """ if is_nonstr_iter(path): - # the traverser factory expects PATH_INFO to be a string, not - # unicode and it expects path segments to be utf-8 and + # the traverser factory expects PATH_INFO to be a string and it + # expects path segments to be utf-8 and # urlencoded (it's the same traverser which accepts PATH_INFO # from user agents; user agents always send strings). if path: @@ -346,7 +339,7 @@ def resource_path_tuple(resource, *elements): Each segment in the path tuple returned will equal the ``__name__`` attribute of the resource it represents within the resource tree. Each - of these segments *should* be a unicode or string object (as per the + of these segments *should* be a string (as per the contract of :term:`location`-awareness). However, no conversion or safety checking of resource names is performed. For instance, if one of the resources in your tree has a ``__name__`` which (by error) is a @@ -428,11 +421,10 @@ def traversal_path(path): """ Variant of :func:`pyramid.traversal.traversal_path_info` suitable for decoding paths that are URL-encoded. - If this function is passed a Unicode object instead of a sequence of - bytes as ``path``, that Unicode object *must* directly encodeable to - ASCII. For example, u'/foo' will work but u'/<unprintable unicode>' (a - Unicode object with characters that cannot be encoded to ascii) will - not. A :exc:`UnicodeEncodeError` will be raised if the Unicode cannot be + If this function is passed a string, it *must* directly encodeable to + ASCII. For example, '/foo' will work but '/<unprintable unicode>' (a + string object with characters that cannot be encoded to ascii) will + not. A :exc:`UnicodeEncodeError` will be raised if the string cannot be encoded directly to ASCII. """ if isinstance(path, str): @@ -450,18 +442,11 @@ def traversal_path_info(path): already-URL-decoded ``str`` type as if it had come to us from an upstream WSGI server as the ``PATH_INFO`` environ variable. - The ``path`` is first decoded to from its WSGI representation to Unicode; - it is decoded differently depending on platform: - - - On Python 2, ``path`` is decoded to Unicode from bytes using the UTF-8 - decoding directly; a :exc:`pyramid.exc.URLDecodeError` is raised if a the - URL cannot be decoded. - - - On Python 3, as per the PEP 3333 spec, ``path`` is first encoded to - bytes using the Latin-1 encoding; the resulting set of bytes is - subsequently decoded to text using the UTF-8 encoding; a - :exc:`pyramid.exc.URLDecodeError` is raised if a the URL cannot be - decoded. + The ``path`` is first decoded to from its WSGI representation to text. + Per the PEP 3333 spec, ``path`` is first encoded to bytes using the + Latin-1 encoding; the resulting set of bytes is subsequently decoded to + text using the UTF-8 encoding; a :exc:`pyramid.exc.URLDecodeError` is + raised if a the URL cannot be decoded. The ``path`` is split on slashes, creating a list of segments. If a segment name is empty or if it is ``.``, it is ignored. If a segment @@ -476,31 +461,31 @@ def traversal_path_info(path): ``/foo/bar/baz`` - (u'foo', u'bar', u'baz') + ('foo', 'bar', 'baz') ``foo/bar/baz`` - (u'foo', u'bar', u'baz') + ('foo', 'bar', 'baz') ``/foo/bar/baz/`` - (u'foo', u'bar', u'baz') + ('foo', 'bar', 'baz') ``/foo//bar//baz/`` - (u'foo', u'bar', u'baz') + ('foo', 'bar', 'baz') ``/foo/bar/baz/..`` - (u'foo', u'bar') + ('foo', 'bar') ``/my%20archives/hello`` - (u'my archives', u'hello') + ('my archives', 'hello') ``/archives/La%20Pe%C3%B1a`` - (u'archives', u'<unprintable unicode>') + ('archives', '<unprintable unicode>') .. note:: @@ -555,13 +540,10 @@ def quote_path_segment(segment, safe=PATH_SEGMENT_SAFE): """ Return a quoted representation of a 'path segment' (such as the string ``__name__`` attribute of a resource) as a string. If the - ``segment`` passed in is a unicode object, it is converted to a - UTF-8 string, then it is URL-quoted using Python's - ``urllib.quote``. If the ``segment`` passed in is a string, it is - URL-quoted using Python's :mod:`urllib.quote`. If the segment - passed in is not a string or unicode object, an error will be - raised. The return value of ``quote_path_segment`` is always a - string, never Unicode. + ``segment`` passed in is a bytes object, it is decoded as a UTF-8 string. + The result is then URL-quoted using Python's ``urllib.quote``. + If the segment passed in is not bytes or a string, an error will be + raised. The return value of ``quote_path_segment`` is always a string. You may pass a string of characters that need not be encoded as the ``safe`` argument to this function. This corresponds to the @@ -582,14 +564,14 @@ def quote_path_segment(segment, safe=PATH_SEGMENT_SAFE): """ # The bit of this code that deals with ``_segment_cache`` is an # optimization: we cache all the computation of URL path segments - # in this module-scope dictionary with the original string (or - # unicode value) as the key, so we can look it up later without - # needing to reencode or re-url-quote it + # in this module-scope dictionary with the original string as the + # key, so we can look it up later without needing to reencode + # or re-url-quote it try: - return _segment_cache[(segment, safe)] - except KeyError: if segment.__class__ not in (str, bytes): segment = str(segment) + return _segment_cache[(segment, safe)] + except KeyError: result = url_quote(text_(segment, 'utf-8'), safe) # we don't need a lock to mutate _segment_cache, as the below # will generate exactly one Python bytecode (STORE_SUBSCR) |
