Skip to content

Commit

Permalink
Remove imports from pip/requests's vendored packages (conda#13171)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Oct 29, 2023
1 parent 1a327e6 commit 7e47528
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 41 deletions.
18 changes: 11 additions & 7 deletions conda/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,14 +1046,18 @@ def _override(self, key, value):

@memoizedproperty
def requests_version(self):
# used in User-Agent as "requests/<version>"
# if unable to detect a version we expect "requests/unknown"
try:
from requests import __version__ as REQUESTS_VERSION
except ImportError: # pragma: no cover
try:
from pip._vendor.requests import __version__ as REQUESTS_VERSION
except ImportError:
REQUESTS_VERSION = "unknown"
return REQUESTS_VERSION
from requests import __version__ as requests_version
except ImportError as err:
# ImportError: requests is not installed
log.error("Unable to import requests: %s", err)
requests_version = "unknown"
except Exception as err:
log.error("Error importing requests: %s", err)
requests_version = "unknown"
return requests_version

@memoizedproperty
def python_implementation_name_version(self):
Expand Down
32 changes: 8 additions & 24 deletions conda/cli/main_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,14 @@ def get_info_dict(system=False):
from ..models.channel import all_channel_urls, offline_keep

try:
from requests import __version__ as requests_version

# These environment variables can influence requests' behavior, along with configuration
# in a .netrc file
# CURL_CA_BUNDLE
# REQUESTS_CA_BUNDLE
# HTTP_PROXY
# HTTPS_PROXY
except ImportError: # pragma: no cover
try:
from pip._vendor.requests import __version__ as requests_version
except Exception as e: # pragma: no cover
requests_version = "Error %r" % e
except Exception as e: # pragma: no cover
requests_version = "Error %r" % e

try:
import conda_build
except ImportError: # pragma: no cover
from conda_build import __version__ as conda_build_version
except ImportError as err:
# ImportError: conda-build is not installed
log.debug("Unable to import conda-build: %s", err)
conda_build_version = "not installed"
except Exception as e: # pragma: no cover
conda_build_version = "Error %s" % e
else: # pragma: no cover
conda_build_version = conda_build.__version__
except Exception as err:
log.error("Error importing conda-build: %s", err)
conda_build_version = "error"

virtual_pkg_index = {}
_supplement_index_with_system(virtual_pkg_index)
Expand Down Expand Up @@ -282,7 +266,7 @@ def get_info_dict(system=False):
offline=context.offline,
envs=[],
python_version=".".join(map(str, sys.version_info)),
requests_version=requests_version,
requests_version=context.requests_version,
user_agent=context.user_agent,
conda_location=CONDA_PACKAGE_ROOT,
config_files=context.config_files,
Expand Down
12 changes: 3 additions & 9 deletions conda/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,9 @@ def ensure_text_type(value) -> str:
# In this case assume already text_type and do nothing
return value
except UnicodeDecodeError: # pragma: no cover
try:
from chardet import detect
except ImportError:
try:
from requests.packages.chardet import detect
except ImportError: # pragma: no cover
from pip._vendor.requests.packages.chardet import detect
encoding = detect(value).get("encoding") or "utf-8"
return value.decode(encoding, errors="replace")
from charset_normalizer import from_bytes

return str(from_bytes(value).best())
except UnicodeEncodeError: # pragma: no cover
# it's already str, so ignore?
# not sure, surfaced with tests/models/test_match_spec.py test_tarball_match_specs
Expand Down
19 changes: 19 additions & 0 deletions news/13171-charset-normalizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* Stop using `requests`/`pip` vendored `chardet` package. Instead, explicitly depend on `charset_normalizer`. (#13171)

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ classifiers = [
]
dependencies = [
"boltons >=23.0.0",
"charset-normalizer",
"conda-package-handling >=1.3.0",
"jsonpatch >=1.32",
"menuinst >=1.4.11,<2 ; platform_system=='Windows'",
Expand Down
1 change: 1 addition & 0 deletions recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ requirements:
run:
- archspec
- boltons >=23.0.0
- charset-normalizer
- conda-package-handling >=2.2.0
- jsonpatch >=1.32
- menuinst >=1.4.11,<2 # [win]
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
archspec
boltons >=23.0.0
chardet # see fallbacks in conda.common.compat
charset-normalizer
conda-content-trust
conda-forge::anaconda-client
conda-forge::pytest-split
Expand Down

0 comments on commit 7e47528

Please sign in to comment.