Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2dbb2e0

Browse files
authored
GH-110109: Churnpathlib.PurePath methods (#112012)
Re-arrange `pathlib.PurePath` methods in source code. No other changes.The `PurePath` implementations of certain special methods, such as`__eq__()` and `__hash__()`, are not usually applicable to user subclassesof `_PathBase`. To facilitate their removal, another patch will split the`PurePath` class into `_PurePathBase` and `PurePath`, with the latterproviding these special methods.This patch prepares the ground for splitting `PurePath`. It's similar toe8d77b0, which preceded splitting `Path`. By churning the methods here,subsequent patches will be easier to review and less likely to breakthings.
1 parent7c50800 commit2dbb2e0

File tree

2 files changed

+204
-204
lines changed

2 files changed

+204
-204
lines changed

‎Lib/pathlib.py‎

Lines changed: 120 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -246,44 +246,6 @@ class PurePath:
246246
)
247247
pathmod=os.path
248248

249-
def__new__(cls,*args,**kwargs):
250-
"""Construct a PurePath from one or several strings and or existing
251-
PurePath objects. The strings and path objects are combined so as
252-
to yield a canonicalized path, which is incorporated into the
253-
new PurePath object.
254-
"""
255-
ifclsisPurePath:
256-
cls=PureWindowsPathifos.name=='nt'elsePurePosixPath
257-
returnobject.__new__(cls)
258-
259-
def__reduce__(self):
260-
# Using the parts tuple helps share interned path parts
261-
# when pickling related paths.
262-
return (self.__class__,self.parts)
263-
264-
def__init__(self,*args):
265-
paths= []
266-
forarginargs:
267-
ifisinstance(arg,PurePath):
268-
ifarg.pathmodisntpathandself.pathmodisposixpath:
269-
# GH-103631: Convert separators for backwards compatibility.
270-
paths.extend(path.replace('\\','/')forpathinarg._raw_paths)
271-
else:
272-
paths.extend(arg._raw_paths)
273-
else:
274-
try:
275-
path=os.fspath(arg)
276-
exceptTypeError:
277-
path=arg
278-
ifnotisinstance(path,str):
279-
raiseTypeError(
280-
"argument should be a str or an os.PathLike "
281-
"object where __fspath__ returns a str, "
282-
f"not{type(path).__name__!r}")
283-
paths.append(path)
284-
self._raw_paths=paths
285-
self._resolving=False
286-
287249
defwith_segments(self,*pathsegments):
288250
"""Construct a new path object from any number of path-like objects.
289251
Subclasses may override this method to customize how new path objects
@@ -351,96 +313,14 @@ def __str__(self):
351313
self._tail)or'.'
352314
returnself._str
353315

354-
def__fspath__(self):
355-
returnstr(self)
356-
357316
defas_posix(self):
358317
"""Return the string representation of the path with forward (/)
359318
slashes."""
360319
returnstr(self).replace(self.pathmod.sep,'/')
361320

362-
def__bytes__(self):
363-
"""Return the bytes representation of the path. This is only
364-
recommended to use under Unix."""
365-
returnos.fsencode(self)
366-
367321
def__repr__(self):
368322
return"{}({!r})".format(self.__class__.__name__,self.as_posix())
369323

370-
defas_uri(self):
371-
"""Return the path as a URI."""
372-
ifnotself.is_absolute():
373-
raiseValueError("relative path can't be expressed as a file URI")
374-
375-
drive=self.drive
376-
iflen(drive)==2anddrive[1]==':':
377-
# It's a path on a local drive => 'file:///c:/a/b'
378-
prefix='file:///'+drive
379-
path=self.as_posix()[2:]
380-
elifdrive:
381-
# It's a path on a network drive => 'file://host/share/a/b'
382-
prefix='file:'
383-
path=self.as_posix()
384-
else:
385-
# It's a posix path => 'file:///etc/hosts'
386-
prefix='file://'
387-
path=str(self)
388-
fromurllib.parseimportquote_from_bytes
389-
returnprefix+quote_from_bytes(os.fsencode(path))
390-
391-
@property
392-
def_str_normcase(self):
393-
# String with normalized case, for hashing and equality checks
394-
try:
395-
returnself._str_normcase_cached
396-
exceptAttributeError:
397-
if_is_case_sensitive(self.pathmod):
398-
self._str_normcase_cached=str(self)
399-
else:
400-
self._str_normcase_cached=str(self).lower()
401-
returnself._str_normcase_cached
402-
403-
@property
404-
def_parts_normcase(self):
405-
# Cached parts with normalized case, for comparisons.
406-
try:
407-
returnself._parts_normcase_cached
408-
exceptAttributeError:
409-
self._parts_normcase_cached=self._str_normcase.split(self.pathmod.sep)
410-
returnself._parts_normcase_cached
411-
412-
def__eq__(self,other):
413-
ifnotisinstance(other,PurePath):
414-
returnNotImplemented
415-
returnself._str_normcase==other._str_normcaseandself.pathmodisother.pathmod
416-
417-
def__hash__(self):
418-
try:
419-
returnself._hash
420-
exceptAttributeError:
421-
self._hash=hash(self._str_normcase)
422-
returnself._hash
423-
424-
def__lt__(self,other):
425-
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
426-
returnNotImplemented
427-
returnself._parts_normcase<other._parts_normcase
428-
429-
def__le__(self,other):
430-
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
431-
returnNotImplemented
432-
returnself._parts_normcase<=other._parts_normcase
433-
434-
def__gt__(self,other):
435-
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
436-
returnNotImplemented
437-
returnself._parts_normcase>other._parts_normcase
438-
439-
def__ge__(self,other):
440-
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
441-
returnNotImplemented
442-
returnself._parts_normcase>=other._parts_normcase
443-
444324
@property
445325
defdrive(self):
446326
"""The drive prefix (letter or UNC path), if any."""
@@ -694,6 +574,126 @@ def match(self, path_pattern, *, case_sensitive=None):
694574
match=_compile_pattern(pattern_str,sep,case_sensitive)
695575
returnmatch(str(self))isnotNone
696576

577+
def__new__(cls,*args,**kwargs):
578+
"""Construct a PurePath from one or several strings and or existing
579+
PurePath objects. The strings and path objects are combined so as
580+
to yield a canonicalized path, which is incorporated into the
581+
new PurePath object.
582+
"""
583+
ifclsisPurePath:
584+
cls=PureWindowsPathifos.name=='nt'elsePurePosixPath
585+
returnobject.__new__(cls)
586+
587+
def__init__(self,*args):
588+
paths= []
589+
forarginargs:
590+
ifisinstance(arg,PurePath):
591+
ifarg.pathmodisntpathandself.pathmodisposixpath:
592+
# GH-103631: Convert separators for backwards compatibility.
593+
paths.extend(path.replace('\\','/')forpathinarg._raw_paths)
594+
else:
595+
paths.extend(arg._raw_paths)
596+
else:
597+
try:
598+
path=os.fspath(arg)
599+
exceptTypeError:
600+
path=arg
601+
ifnotisinstance(path,str):
602+
raiseTypeError(
603+
"argument should be a str or an os.PathLike "
604+
"object where __fspath__ returns a str, "
605+
f"not{type(path).__name__!r}")
606+
paths.append(path)
607+
self._raw_paths=paths
608+
self._resolving=False
609+
610+
def__reduce__(self):
611+
# Using the parts tuple helps share interned path parts
612+
# when pickling related paths.
613+
return (self.__class__,self.parts)
614+
615+
def__fspath__(self):
616+
returnstr(self)
617+
618+
def__bytes__(self):
619+
"""Return the bytes representation of the path. This is only
620+
recommended to use under Unix."""
621+
returnos.fsencode(self)
622+
623+
@property
624+
def_str_normcase(self):
625+
# String with normalized case, for hashing and equality checks
626+
try:
627+
returnself._str_normcase_cached
628+
exceptAttributeError:
629+
if_is_case_sensitive(self.pathmod):
630+
self._str_normcase_cached=str(self)
631+
else:
632+
self._str_normcase_cached=str(self).lower()
633+
returnself._str_normcase_cached
634+
635+
def__hash__(self):
636+
try:
637+
returnself._hash
638+
exceptAttributeError:
639+
self._hash=hash(self._str_normcase)
640+
returnself._hash
641+
642+
def__eq__(self,other):
643+
ifnotisinstance(other,PurePath):
644+
returnNotImplemented
645+
returnself._str_normcase==other._str_normcaseandself.pathmodisother.pathmod
646+
647+
@property
648+
def_parts_normcase(self):
649+
# Cached parts with normalized case, for comparisons.
650+
try:
651+
returnself._parts_normcase_cached
652+
exceptAttributeError:
653+
self._parts_normcase_cached=self._str_normcase.split(self.pathmod.sep)
654+
returnself._parts_normcase_cached
655+
656+
def__lt__(self,other):
657+
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
658+
returnNotImplemented
659+
returnself._parts_normcase<other._parts_normcase
660+
661+
def__le__(self,other):
662+
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
663+
returnNotImplemented
664+
returnself._parts_normcase<=other._parts_normcase
665+
666+
def__gt__(self,other):
667+
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
668+
returnNotImplemented
669+
returnself._parts_normcase>other._parts_normcase
670+
671+
def__ge__(self,other):
672+
ifnotisinstance(other,PurePath)orself.pathmodisnotother.pathmod:
673+
returnNotImplemented
674+
returnself._parts_normcase>=other._parts_normcase
675+
676+
defas_uri(self):
677+
"""Return the path as a URI."""
678+
ifnotself.is_absolute():
679+
raiseValueError("relative path can't be expressed as a file URI")
680+
681+
drive=self.drive
682+
iflen(drive)==2anddrive[1]==':':
683+
# It's a path on a local drive => 'file:///c:/a/b'
684+
prefix='file:///'+drive
685+
path=self.as_posix()[2:]
686+
elifdrive:
687+
# It's a path on a network drive => 'file://host/share/a/b'
688+
prefix='file:'
689+
path=self.as_posix()
690+
else:
691+
# It's a posix path => 'file:///etc/hosts'
692+
prefix='file://'
693+
path=str(self)
694+
fromurllib.parseimportquote_from_bytes
695+
returnprefix+quote_from_bytes(os.fsencode(path))
696+
697697

698698
# Subclassing os.PathLike makes isinstance() checks slower,
699699
# which in turn makes Path construction slower. Register instead!

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp