Title: GH-76846, GH-85281: Call `__new__()` and `__init__()` on pathlib subclasses by barneygale · Pull Request #102789 · python/cpython · GitHub
Open Graph Title: GH-76846, GH-85281: Call `__new__()` and `__init__()` on pathlib subclasses by barneygale · Pull Request #102789 · python/cpython
X Title: GH-76846, GH-85281: Call `__new__()` and `__init__()` on pathlib subclasses by barneygale · Pull Request #102789 · python/cpython
Description: This PR fixes an issue where __new__() and __init__() were not called on subclasses of pathlib.PurePath and Path in some circumstances. Specifically, the _from_parsed_parts() constructor -- which is used when iterating directories, parents, and in with_name() and friends -- has been altered as follows: diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 55c44f12e5..c9b45ddbfa 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -310,7 +310,9 @@ def _from_parts(cls, args): @classmethod def _from_parsed_parts(cls, drv, root, parts): - self = object.__new__(cls) + path = cls._format_parsed_parts(drv, root, parts) + self = cls(path) + self._str = path or '.' self._drv = drv self._root = root self._parts = parts This change alone has an unfortunate effect: paths constructed this way are re-parsed and re-normalized even though we have the fully normalized path at hand. To fix this, we change the main constructor to not normalize paths. Instead, paths are normalized on-demand. This also speeds up path construction, p.joinpath(q), and p / q. Issue: gh-76846 Issue: gh-85281
Open Graph Description: This PR fixes an issue where __new__() and __init__() were not called on subclasses of pathlib.PurePath and Path in some circumstances. Specifically, the _from_parsed_parts() constructor -- which i...
X Description: This PR fixes an issue where __new__() and __init__() were not called on subclasses of pathlib.PurePath and Path in some circumstances. Specifically, the _from_parsed_parts() constructor -- which i...
Opengraph URL: https://github.com/python/cpython/pull/102789
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:5b239f9f-d363-71aa-9669-c5644366f0cb |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | E704:9F506:126975A:18991FC:6969AD78 |
| html-safe-nonce | 6daaffd8ca0c0c2dc41d784b781a256032d37896fd8415765190dfe534fc6b79 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzA0OjlGNTA2OjEyNjk3NUE6MTg5OTFGQzo2OTY5QUQ3OCIsInZpc2l0b3JfaWQiOiI4OTEwMDYwNjgxMjU1NTYyNjE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1d1e3ebcb2feac73b1ab1f44749e5cc8ace9b2b477308712b6034b7e1f62b8d7 |
| hovercard-subject-tag | pull_request:1280667275 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/102789/checks |
| twitter:image | https://avatars.githubusercontent.com/u/960340?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/960340?s=400&v=4 |
| og:image:alt | This PR fixes an issue where __new__() and __init__() were not called on subclasses of pathlib.PurePath and Path in some circumstances. Specifically, the _from_parsed_parts() constructor -- which i... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 24c4c97a2d520cb286b35e1a4c22d7a4df3c26a2fa28dd7cdf0e65db327b4de7 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 124667f43168afb6c9c03b7c02eb5b1d2e1be3d9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width