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
656319e5
Commit
656319e5
authored
Apr 13, 2012
by
Michael Foord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make unittest.mock.create_autospec resilient against AttributeError on original object
parent
899ee613
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
2 deletions
+29
-2
Lib/unittest/mock.py
Lib/unittest/mock.py
+6
-2
Lib/unittest/test/testmock/testhelpers.py
Lib/unittest/test/testmock/testhelpers.py
+23
-0
No files found.
Lib/unittest/mock.py
View file @
656319e5
...
@@ -2044,10 +2044,14 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
...
@@ -2044,10 +2044,14 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
# object to mock it so we would rather trigger a property than mock
# object to mock it so we would rather trigger a property than mock
# the property descriptor. Likewise we want to mock out dynamically
# the property descriptor. Likewise we want to mock out dynamically
# provided attributes.
# provided attributes.
# XXXX what about attributes that raise exceptions on being fetched
# XXXX what about attributes that raise exceptions other than
# AttributeError on being fetched?
# we could be resilient against it, or catch and propagate the
# we could be resilient against it, or catch and propagate the
# exception when the attribute is fetched from the mock
# exception when the attribute is fetched from the mock
original
=
getattr
(
spec
,
entry
)
try
:
original
=
getattr
(
spec
,
entry
)
except
AttributeError
:
continue
kwargs
=
{
'spec'
:
original
}
kwargs
=
{
'spec'
:
original
}
if
spec_set
:
if
spec_set
:
...
...
Lib/unittest/test/testmock/testhelpers.py
View file @
656319e5
...
@@ -651,6 +651,29 @@ class SpecSignatureTest(unittest.TestCase):
...
@@ -651,6 +651,29 @@ class SpecSignatureTest(unittest.TestCase):
mock
.
f
.
assert_called_with
(
3
,
4
)
mock
.
f
.
assert_called_with
(
3
,
4
)
def
test_skip_attributeerrors
(
self
):
class
Raiser
(
object
):
def
__get__
(
self
,
obj
,
type
=
None
):
if
obj
is
None
:
raise
AttributeError
(
'Can only be accessed via an instance'
)
class
RaiserClass
(
object
):
raiser
=
Raiser
()
@
staticmethod
def
existing
(
a
,
b
):
return
a
+
b
s
=
create_autospec
(
RaiserClass
)
self
.
assertRaises
(
TypeError
,
lambda
x
:
s
.
existing
(
1
,
2
,
3
))
s
.
existing
(
1
,
2
)
self
.
assertRaises
(
AttributeError
,
lambda
:
s
.
nonexisting
)
# check we can fetch the raiser attribute and it has no spec
obj
=
s
.
raiser
obj
.
foo
,
obj
.
bar
def
test_signature_class
(
self
):
def
test_signature_class
(
self
):
class
Foo
(
object
):
class
Foo
(
object
):
def
__init__
(
self
,
a
,
b
=
3
):
def
__init__
(
self
,
a
,
b
=
3
):
...
...
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