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

w3lib.utils: deprecate functions not needed for Python 3 #170

Merged
merged 5 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 26 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest import TestCase

from pytest import deprecated_call

from w3lib.util import str_to_unicode, to_native_str, unicode_to_str


class StrToUnicodeTestCase(TestCase):

def test_deprecation(self):
with deprecated_call():
str_to_unicode('')


class ToNativeStrTestCase(TestCase):

def test_deprecation(self):
with deprecated_call():
to_native_str('')


class UnicodeToStrTestCase(TestCase):

def test_deprecation(self):
with deprecated_call():
unicode_to_str('')
12 changes: 6 additions & 6 deletions w3lib/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
urlunsplit,
)
from urllib.request import pathname2url, url2pathname
from w3lib.util import to_bytes, to_native_str, to_unicode
from w3lib.util import to_bytes, to_unicode


# error handling function for bytes-to-Unicode decoding errors with URLs
Expand Down Expand Up @@ -85,13 +85,13 @@ def safe_url_string(url, encoding='utf8', path_encoding='utf8', quote_path=True)
if quote_path:
path = quote(to_bytes(parts.path, path_encoding), _path_safe_chars)
else:
path = to_native_str(parts.path)
path = to_unicode(parts.path)
Gallaecio marked this conversation as resolved.
Show resolved Hide resolved

# quote() in Python2 return type follows input type;
# quote() in Python3 always returns Unicode (native str)
return urlunsplit((
to_native_str(parts.scheme),
to_native_str(netloc).rstrip(':'),
to_unicode(parts.scheme),
to_unicode(netloc).rstrip(':'),
path,
# encoding of query and fragment follows page encoding
# or form-charset (if known and passed)
Expand Down Expand Up @@ -415,8 +415,8 @@ def _safe_ParseResult(parts, encoding='utf8', path_encoding='utf8'):
netloc = parts.netloc

return (
to_native_str(parts.scheme),
to_native_str(netloc),
to_unicode(parts.scheme),
to_unicode(netloc),

# default encoding for path component SHOULD be UTF-8
quote(to_bytes(parts.path, path_encoding), _path_safe_chars),
Expand Down
22 changes: 22 additions & 0 deletions w3lib/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from warnings import warn


def str_to_unicode(text, encoding=None, errors='strict'):
warn(
"The w3lib.utils.str_to_unicode function is deprecated and "
"will be removed in a future release.",
DeprecationWarning,
stacklevel=2,
)
if encoding is None:
encoding = 'utf-8'
if isinstance(text, bytes):
return text.decode(encoding, errors)
return text

def unicode_to_str(text, encoding=None, errors='strict'):
warn(
"The w3lib.utils.unicode_to_str function is deprecated and "
"will be removed in a future release.",
DeprecationWarning,
stacklevel=2,
)
if encoding is None:
encoding = 'utf-8'
if isinstance(text, str):
Expand Down Expand Up @@ -38,4 +53,11 @@ def to_bytes(text, encoding=None, errors='strict'):

def to_native_str(text, encoding=None, errors='strict'):
""" Return str representation of `text` """
warn(
"The w3lib.utils.to_native_str function is deprecated and "
"will be removed in a future release. Please use "
"w3lib.utils.to_unicode instead.",
DeprecationWarning,
stacklevel=2,
)
return to_unicode(text, encoding, errors)