Skip to content
Myr

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

5 files+22 added−28 removed

modified:httpx/config.py+8 added-8 removed
1import asyncio2import os2import ssl3import typing4from pathlib import Path56import certifi78from .__version__ import __version__
84 unmodified lines
93 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 lines
119 context.hostname_checks_common_name = False # type: ignore120 except AttributeError: # pragma: nocover121 pass122123 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))127128 if self.cert is not None:129 if isinstance(self.cert, str):130 context.load_cert_chain(certfile=self.cert)
116 unmodified lines
247248DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)249DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)250DEFAULT_POOL_LIMITS = PoolLimits(soft_limit=10, hard_limit=100, pool_timeout=5.0)251DEFAULT_CA_BUNDLE_PATH = certifi.where()251DEFAULT_CA_BUNDLE_PATH = Path(certifi.where())252DEFAULT_MAX_REDIRECTS = 20
modified:httpx/multipart.py+3 added-2 removed
2 unmodified lines
3import os4import re5import typing6from io import BytesIO7from pathlib import Path89_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 lines
47 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 lines
113114def _format_param(name: str, value: typing.Union[str, bytes]) -> bytes:115 if isinstance(value, bytes):116 value = value.decode()116 117118 def replacer(match: typing.Match[str]) -> str:119 return _HTML5_FORM_ENCODING_REPLACEMENTS[match.group(0)]120121 value = _HTML5_FORM_ENCODING_RE.sub(replacer, value)
1 unmodified line
modified:httpx/utils.py+6 added-8 removed
1import codecs2import netrc3import os4import typing5from pathlib import Path678def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:9 """
73 unmodified lines
83 # Did not detect a valid UTF-32 ascii-range character84 return None858686NETRC_STATIC_FILES = tuple(87 os.path.expanduser(path) for path in ("~/.netrc", "~/_netrc")88)87NETRC_STATIC_FILES = (Path("~/.netrc"), Path("~/_netrc"))888990def 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 = None9394 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 break99100 if netrc_path is None:101 return None
3 unmodified lines
modified:tests/test_config.py+1 added-2 removed
1import os1import ssl23import pytest4
24 unmodified lines
293031@pytest.mark.asyncio32async 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 True
96 unmodified lines
modified:setup.py+4 added-8 removed
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34import os4import re5from pathlib import Path67from setuptools import setup8910def 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)161718def get_long_description():19 """
6 unmodified lines
26def 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")]313233setup(34 name="httpx",
33 unmodified lines