Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
4c69be22
Commit
4c69be22
authored
Aug 08, 2019
by
aiudirog
Committed by
Serhiy Storchaka
Aug 08, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-34775: Return NotImplemented in PurePath division. (GH-9509)
parent
0378d986
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
2 deletions
+52
-2
Lib/pathlib.py
Lib/pathlib.py
+8
-2
Lib/test/test_pathlib.py
Lib/test/test_pathlib.py
+41
-0
Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst
...S.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst
+3
-0
No files found.
Lib/pathlib.py
View file @
4c69be22
...
...
@@ -907,10 +907,16 @@ class PurePath(object):
return
self
.
_make_child
(
args
)
def
__truediv__
(
self
,
key
):
return
self
.
_make_child
((
key
,))
try
:
return
self
.
_make_child
((
key
,))
except
TypeError
:
return
NotImplemented
def
__rtruediv__
(
self
,
key
):
return
self
.
_from_parts
([
key
]
+
self
.
_parts
)
try
:
return
self
.
_from_parts
([
key
]
+
self
.
_parts
)
except
TypeError
:
return
NotImplemented
@
property
def
parent
(
self
):
...
...
Lib/test/test_pathlib.py
View file @
4c69be22
...
...
@@ -2329,5 +2329,46 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase):
check
()
class
CompatiblePathTest
(
unittest
.
TestCase
):
"""
Test that a type can be made compatible with PurePath
derivatives by implementing division operator overloads.
"""
class
CompatPath
:
"""
Minimum viable class to test PurePath compatibility.
Simply uses the division operator to join a given
string and the string value of another object with
a forward slash.
"""
def
__init__
(
self
,
string
):
self
.
string
=
string
def
__truediv__
(
self
,
other
):
return
type
(
self
)(
f"
{
self
.
string
}
/
{
other
}
"
)
def
__rtruediv__
(
self
,
other
):
return
type
(
self
)(
f"
{
other
}
/
{
self
.
string
}
"
)
def
test_truediv
(
self
):
result
=
pathlib
.
PurePath
(
"test"
)
/
self
.
CompatPath
(
"right"
)
self
.
assertIsInstance
(
result
,
self
.
CompatPath
)
self
.
assertEqual
(
result
.
string
,
"test/right"
)
with
self
.
assertRaises
(
TypeError
):
# Verify improper operations still raise a TypeError
pathlib
.
PurePath
(
"test"
)
/
10
def
test_rtruediv
(
self
):
result
=
self
.
CompatPath
(
"left"
)
/
pathlib
.
PurePath
(
"test"
)
self
.
assertIsInstance
(
result
,
self
.
CompatPath
)
self
.
assertEqual
(
result
.
string
,
"left/test"
)
with
self
.
assertRaises
(
TypeError
):
# Verify improper operations still raise a TypeError
10
/
pathlib
.
PurePath
(
"test"
)
if
__name__
==
"__main__"
:
unittest
.
main
()
Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst
0 → 100644
View file @
4c69be22
Division handling of PurePath now returns NotImplemented instead of raising
a TypeError when passed something other than an instance of str or PurePath.
Patch by Roger Aiudi.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment