Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python_2_bytes_compatible #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions six.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,23 @@ def python_2_unicode_compatible(klass):
return klass


def python_2_bytes_compatible(klass):
"""
A class decorator that defines __str__ and __bytes__ methods under Python 2.
Under Python 3 it does nothing.

To support Python 2 and 3 with a single code base, define a __bytes__ method
returning bytes and apply this decorator to the class.
"""
if PY2:
if '__bytes__' not in klass.__dict__:
raise ValueError("@python_2_bytes_compatible cannot be applied "
"to %s because it doesn't define __bytes__()." %
klass.__name__)
klass.__str__ = klass.__bytes__
return klass


# Complete the moves implementation.
# This code is at the end of this module to speed up module loading.
# Turn this module into a package.
Expand Down