Use pathlib.Path instead of os.path functions (#188)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Stephen Brown II · committed by Seth Michael Larson · parent de91fdf · Browse files · 5 files·+22 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+22 added−28 removed
- tests:+1 added−2 removed
- other:+21 added−26 removed
Provenance
- Commit
fde30b8- Tree
950c643- Author
Stephen Brown II <stephen.brownii@rackspace.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
de91fdf
5 files changed+22 added−28 removed
modified:httpx/config.py+8 added-8 removed
1 import asyncio2-import os2 import ssl3 import typing4+from pathlib import Path5 6 import certifi7 8 from .__version__ import __version__84 unmodified lines93 Return an SSL context for verified connections.94 """95 if isinstance(self.verify, bool):96 ca_bundle_path = DEFAULT_CA_BUNDLE_PATH97- elif os.path.exists(self.verify):98- ca_bundle_path = self.verify97+ elif Path(self.verify).exists():98+ ca_bundle_path = Path(self.verify)99 else:100 raise IOError(101 "Could not find a suitable TLS CA certificate bundle, "102 "invalid path: {}".format(self.verify)16 unmodified lines119 context.hostname_checks_common_name = False # type: ignore120 except AttributeError: # pragma: nocover121 pass122 123- if os.path.isfile(ca_bundle_path):124- context.load_verify_locations(cafile=ca_bundle_path)125- elif os.path.isdir(ca_bundle_path):126- context.load_verify_locations(capath=ca_bundle_path)123+ if ca_bundle_path.is_file():124+ context.load_verify_locations(cafile=str(ca_bundle_path))125+ elif ca_bundle_path.is_dir():126+ context.load_verify_locations(capath=str(ca_bundle_path))127 128 if self.cert is not None:129 if isinstance(self.cert, str):130 context.load_cert_chain(certfile=self.cert)116 unmodified lines247 248 DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)249 DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)250 DEFAULT_POOL_LIMITS = PoolLimits(soft_limit=10, hard_limit=100, pool_timeout=5.0)251-DEFAULT_CA_BUNDLE_PATH = certifi.where()251+DEFAULT_CA_BUNDLE_PATH = Path(certifi.where())252 DEFAULT_MAX_REDIRECTS = 20modified:httpx/multipart.py+3 added-2 removed
2 unmodified lines3 import os4 import re5 import typing6 from io import BytesIO7+from pathlib import Path8 9 _HTML5_FORM_ENCODING_REPLACEMENTS = {'"': "%22", "\\": "\\\\"}10 _HTML5_FORM_ENCODING_REPLACEMENTS.update(11 {chr(c): "%{:02X}".format(c) for c in range(0x00, 0x1F + 1) if c != 0x1B}35 unmodified lines47 self, name: str, value: typing.Union[typing.IO[typing.AnyStr], tuple]48 ) -> None:49 self.name = name50 if not isinstance(value, tuple):50- self.filename = os.path.basename(getattr(value, "name", "upload"))51+ self.filename = Path(getattr(value, "name", "upload")).name52 self.file = value # type: typing.Union[typing.IO[str], typing.IO[bytes]]53 self.content_type = self.guess_content_type()54 else:55 self.filename = value[0]57 unmodified lines113 114 def _format_param(name: str, value: typing.Union[str, bytes]) -> bytes:115 if isinstance(value, bytes):116 value = value.decode()116- 117+118 def replacer(match: typing.Match[str]) -> str:119 return _HTML5_FORM_ENCODING_REPLACEMENTS[match.group(0)]120 121 value = _HTML5_FORM_ENCODING_RE.sub(replacer, value)1 unmodified linemodified:httpx/utils.py+6 added-8 removed
1 import codecs2 import netrc3 import os4 import typing5+from pathlib import Path6 7 8 def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:9 """73 unmodified lines83 # Did not detect a valid UTF-32 ascii-range character84 return None85 86 86-NETRC_STATIC_FILES = tuple(87- os.path.expanduser(path) for path in ("~/.netrc", "~/_netrc")88-)87+NETRC_STATIC_FILES = (Path("~/.netrc"), Path("~/_netrc"))88 89 90 def get_netrc_login(host: str) -> typing.Optional[typing.Tuple[str, str, str]]:92- NETRC_FILES = (93- (os.environ["NETRC"],) if "NETRC" in os.environ else NETRC_STATIC_FILES94- )91+ NETRC_FILES = (Path(os.getenv("NETRC", "")),) + NETRC_STATIC_FILES92 netrc_path = None93 94 for file_path in NETRC_FILES:98- if os.path.isfile(file_path):99- netrc_path = file_path95+ expanded_path = file_path.expanduser()96+ if expanded_path.is_file():97+ netrc_path = expanded_path98 break99 100 if netrc_path is None:101 return None3 unmodified linesmodified:tests/test_config.py+1 added-2 removed
1-import os1 import ssl2 3 import pytest4 24 unmodified lines29 30 31 @pytest.mark.asyncio32 async def test_load_ssl_config_verify_directory():34- path = os.path.dirname(httpx.config.DEFAULT_CA_BUNDLE_PATH)33+ path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent34 ssl_config = httpx.SSLConfig(verify=path)35 context = await ssl_config.load_ssl_context()36 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED37 assert context.check_hostname is True96 unmodified linesmodified:setup.py+4 added-8 removed
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4-import os4 import re5+from pathlib import Path6 7 from setuptools import setup8 9 10 def get_version(package):11 """12 Return package version as listed in `__version__` in `init.py`.13 """14- with open(os.path.join(package, "__version__.py")) as f:15- return re.search("__version__ = ['\"]([^'\"]+)['\"]", f.read()).group(1)14+ version = Path(package, "__version__.py").read_text()15+ return re.search("__version__ = ['\"]([^'\"]+)['\"]", version).group(1)16 17 18 def get_long_description():19 """6 unmodified lines26 def get_packages(package):27 """28 Return root package and all sub-packages.29 """30- return [31- dirpath32- for dirpath, dirnames, filenames in os.walk(package)33- if os.path.exists(os.path.join(dirpath, "__init__.py"))34- ]30+ return [str(path.parent) for path in Path(package).glob("**/__init__.py")]31 32 33 setup(34 name="httpx",33 unmodified lines