Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: benjaminp/six
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.14.0
Choose a base ref
...
head repository: benjaminp/six
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.15.0
Choose a head ref
  • 4 commits
  • 3 files changed
  • 3 contributors

Commits on Apr 10, 2020

  1. Fix ensure_text docs. (#326)

    Co-authored-by: Benjamin Peterson <benjamin@python.org>
    Neob91 and benjaminp authored Apr 10, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a5bb7aa View commit details

Commits on May 20, 2020

  1. Optimize ensure_str and ensure_binary. (#331)

    We found that large applications that have undergone a 2 -> 3 migration
    and wound up with a lot of six.ensure_str and six.ensure_binary calls
    could save 1-2% CPU usage by optimizing these for the common case.
    
    Further optimization could be done by replacing them with extension
    module implementations - assumed out of scope for the pure Python six
    project itself.
    
    Ideally all of these calls and use of six in people's code would be
    removed after there all need for any Python 2 compatibility is gone.
    But completing that kind of type cleanup requires a lot of human
    engineering time.  This lowers the ongoing costs in the interim.
    
    Contributed by YouTube.
    gpshead authored May 20, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    05c4f51 View commit details
  2. Document #331.

    benjaminp committed May 20, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    714f40f View commit details

Commits on May 21, 2020

  1. Advance version to 1.15.0.

    benjaminp committed May 21, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c0be881 View commit details
Showing with 17 additions and 10 deletions.
  1. +5 −0 CHANGES
  2. +1 −1 documentation/index.rst
  3. +11 −9 six.py
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -3,6 +3,11 @@ Changelog for six

This file lists the changes in each six version.

1.15.0
------

- Pull request #331: Optimize `six.ensure_str` and `six.ensure_binary`.

1.14.0
------

2 changes: 1 addition & 1 deletion documentation/index.rst
Original file line number Diff line number Diff line change
@@ -452,7 +452,7 @@ string data in all Python versions.
.. function:: ensure_text(s, encoding='utf-8', errors='strict')

Coerce *s* to :data:`text_type`. *encoding*, *errors* are the same as
:meth:`py3:str.encode`
:meth:`py3:bytes.decode`


.. data:: StringIO
20 changes: 11 additions & 9 deletions six.py
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
import types

__author__ = "Benjamin Peterson <benjamin@python.org>"
__version__ = "1.14.0"
__version__ = "1.15.0"


# Useful for very coarse version differentiation.
@@ -890,12 +890,11 @@ def ensure_binary(s, encoding='utf-8', errors='strict'):
- `str` -> encoded to `bytes`
- `bytes` -> `bytes`
"""
if isinstance(s, binary_type):
return s
if isinstance(s, text_type):
return s.encode(encoding, errors)
elif isinstance(s, binary_type):
return s
else:
raise TypeError("not expecting type '%s'" % type(s))
raise TypeError("not expecting type '%s'" % type(s))


def ensure_str(s, encoding='utf-8', errors='strict'):
@@ -909,12 +908,15 @@ def ensure_str(s, encoding='utf-8', errors='strict'):
- `str` -> `str`
- `bytes` -> decoded to `str`
"""
if not isinstance(s, (text_type, binary_type)):
raise TypeError("not expecting type '%s'" % type(s))
# Optimization: Fast return for the common case.
if type(s) is str:
return s
if PY2 and isinstance(s, text_type):
s = s.encode(encoding, errors)
return s.encode(encoding, errors)
elif PY3 and isinstance(s, binary_type):
s = s.decode(encoding, errors)
return s.decode(encoding, errors)
elif not isinstance(s, (text_type, binary_type)):
raise TypeError("not expecting type '%s'" % type(s))
return s