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
e63e617e
Commit
e63e617e
authored
Dec 04, 2018
by
Andrew Dunai
Committed by
Chris Withers
Dec 04, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-35357: Add _mock_ prefix to name/parent/from_kall attributes of _Call/_MagicProxy. (#10873)
Fix minor typo in test function name.
parent
eeb719ea
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
16 deletions
+34
-16
Lib/unittest/mock.py
Lib/unittest/mock.py
+14
-14
Lib/unittest/test/testmock/testcallable.py
Lib/unittest/test/testmock/testcallable.py
+1
-1
Lib/unittest/test/testmock/testmock.py
Lib/unittest/test/testmock/testmock.py
+15
-1
Misc/NEWS.d/next/Core and Builtins/2018-12-03-21-20-24.bpo-35357.rhhoiC.rst
...ore and Builtins/2018-12-03-21-20-24.bpo-35357.rhhoiC.rst
+4
-0
No files found.
Lib/unittest/mock.py
View file @
e63e617e
...
@@ -2040,9 +2040,9 @@ class _Call(tuple):
...
@@ -2040,9 +2040,9 @@ class _Call(tuple):
def
__init__
(
self
,
value
=
(),
name
=
None
,
parent
=
None
,
two
=
False
,
def
__init__
(
self
,
value
=
(),
name
=
None
,
parent
=
None
,
two
=
False
,
from_kall
=
True
):
from_kall
=
True
):
self
.
name
=
name
self
.
_mock_
name
=
name
self
.
parent
=
parent
self
.
_mock_
parent
=
parent
self
.
from_kall
=
from_kall
self
.
_mock_
from_kall
=
from_kall
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
...
@@ -2059,8 +2059,8 @@ class _Call(tuple):
...
@@ -2059,8 +2059,8 @@ class _Call(tuple):
else
:
else
:
self_name
,
self_args
,
self_kwargs
=
self
self_name
,
self_args
,
self_kwargs
=
self
if
(
getattr
(
self
,
'
parent'
,
None
)
and
getattr
(
other
,
'
parent'
,
None
)
if
(
getattr
(
self
,
'
_mock_parent'
,
None
)
and
getattr
(
other
,
'_mock_
parent'
,
None
)
and
self
.
parent
!=
other
.
parent
):
and
self
.
_mock_parent
!=
other
.
_mock_
parent
):
return
False
return
False
other_name
=
''
other_name
=
''
...
@@ -2104,17 +2104,17 @@ class _Call(tuple):
...
@@ -2104,17 +2104,17 @@ class _Call(tuple):
def
__call__
(
self
,
*
args
,
**
kwargs
):
def
__call__
(
self
,
*
args
,
**
kwargs
):
if
self
.
name
is
None
:
if
self
.
_mock_
name
is
None
:
return
_Call
((
''
,
args
,
kwargs
),
name
=
'()'
)
return
_Call
((
''
,
args
,
kwargs
),
name
=
'()'
)
name
=
self
.
name
+
'()'
name
=
self
.
_mock_
name
+
'()'
return
_Call
((
self
.
name
,
args
,
kwargs
),
name
=
name
,
parent
=
self
)
return
_Call
((
self
.
_mock_
name
,
args
,
kwargs
),
name
=
name
,
parent
=
self
)
def
__getattr__
(
self
,
attr
):
def
__getattr__
(
self
,
attr
):
if
self
.
name
is
None
:
if
self
.
_mock_
name
is
None
:
return
_Call
(
name
=
attr
,
from_kall
=
False
)
return
_Call
(
name
=
attr
,
from_kall
=
False
)
name
=
'%s.%s'
%
(
self
.
name
,
attr
)
name
=
'%s.%s'
%
(
self
.
_mock_
name
,
attr
)
return
_Call
(
name
=
name
,
parent
=
self
,
from_kall
=
False
)
return
_Call
(
name
=
name
,
parent
=
self
,
from_kall
=
False
)
...
@@ -2125,8 +2125,8 @@ class _Call(tuple):
...
@@ -2125,8 +2125,8 @@ class _Call(tuple):
return
self
.
__getattr__
(
'index'
)(
*
args
,
**
kwargs
)
return
self
.
__getattr__
(
'index'
)(
*
args
,
**
kwargs
)
def
__repr__
(
self
):
def
__repr__
(
self
):
if
not
self
.
from_kall
:
if
not
self
.
_mock_
from_kall
:
name
=
self
.
name
or
'call'
name
=
self
.
_mock_
name
or
'call'
if
name
.
startswith
(
'()'
):
if
name
.
startswith
(
'()'
):
name
=
'call%s'
%
name
name
=
'call%s'
%
name
return
name
return
name
...
@@ -2152,9 +2152,9 @@ class _Call(tuple):
...
@@ -2152,9 +2152,9 @@ class _Call(tuple):
vals
=
[]
vals
=
[]
thing
=
self
thing
=
self
while
thing
is
not
None
:
while
thing
is
not
None
:
if
thing
.
from_kall
:
if
thing
.
_mock_
from_kall
:
vals
.
append
(
thing
)
vals
.
append
(
thing
)
thing
=
thing
.
parent
thing
=
thing
.
_mock_
parent
return
_CallList
(
reversed
(
vals
))
return
_CallList
(
reversed
(
vals
))
...
...
Lib/unittest/test/testmock/testcallable.py
View file @
e63e617e
...
@@ -128,7 +128,7 @@ class TestCallable(unittest.TestCase):
...
@@ -128,7 +128,7 @@ class TestCallable(unittest.TestCase):
result
.
foo
.
assert_called_once_with
(
3
,
2
,
1
)
result
.
foo
.
assert_called_once_with
(
3
,
2
,
1
)
def
test_create_auto
ps
ec
(
self
):
def
test_create_auto
sp
ec
(
self
):
mock
=
create_autospec
(
X
)
mock
=
create_autospec
(
X
)
instance
=
mock
()
instance
=
mock
()
self
.
assertRaises
(
TypeError
,
instance
)
self
.
assertRaises
(
TypeError
,
instance
)
...
...
Lib/unittest/test/testmock/testmock.py
View file @
e63e617e
...
@@ -9,7 +9,7 @@ from unittest import mock
...
@@ -9,7 +9,7 @@ from unittest import mock
from
unittest.mock
import
(
from
unittest.mock
import
(
call
,
DEFAULT
,
patch
,
sentinel
,
call
,
DEFAULT
,
patch
,
sentinel
,
MagicMock
,
Mock
,
NonCallableMock
,
MagicMock
,
Mock
,
NonCallableMock
,
NonCallableMagicMock
,
_CallList
,
NonCallableMagicMock
,
_Call
,
_Call
List
,
create_autospec
create_autospec
)
)
...
@@ -1665,6 +1665,20 @@ class MockTest(unittest.TestCase):
...
@@ -1665,6 +1665,20 @@ class MockTest(unittest.TestCase):
self.assertIsInstance(mock, int)
self.assertIsInstance(mock, int)
mock.foo
mock.foo
def test_name_attribute_of_call(self):
# bpo-35357: _Call should not disclose any attributes whose names
# may clash with popular ones (such as "
.
name
")
self.assertIsNotNone(call.name)
self.assertEqual(type(call.name), _Call)
self.assertEqual(type(call.name().name), _Call)
def test_parent_attribute_of_call(self):
# bpo-35357: _Call should not disclose any attributes whose names
# may clash with popular ones (such as "
.
parent
")
self.assertIsNotNone(call.parent)
self.assertEqual(type(call.parent), _Call)
self.assertEqual(type(call.parent().parent), _Call)
if __name__ == '__main__':
if __name__ == '__main__':
unittest.main()
unittest.main()
Misc/NEWS.d/next/Core and Builtins/2018-12-03-21-20-24.bpo-35357.rhhoiC.rst
0 → 100644
View file @
e63e617e
Internal attributes' names of unittest.mock._Call and
unittest.mock.MagicProxy (name, parent & from_kall) are now prefixed with
_mock_ in order to prevent clashes with widely used object attributes.
Fixed minor typo in test function name.
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