kgashok / pymox

Automatically exported from code.google.com/p/pymox
Apache License 2.0
0 stars 0 forks source link

mox.Mox.CreateMock() chokes if dir() and getattr() do not agree #51

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Attempt to use mox.Mox.CreateMock() on an object where dir(obj) lists an 
attribute but getattr(obj, attr_name) raises AttributeError.

What is the expected output? What do you see instead?

For example, in Python 2.7 type lists __abstractmethods__ as an attribute while 
getattr(type, '__abstractmethods__') fails:

>>> mox.Mox().CreateMock(type)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mox.py", line 284, in CreateMock
    new_mock = MockObject(class_to_mock, attrs=attrs)
  File "mox.py", line 603, in __init__
    attr = getattr(class_to_mock, method)
AttributeError: __abstractmethods__
>>>

A similar situation can arise when using slots:

>>> class ac(object): __slots__ = 'a',
... 
>>> mox.Mox().CreateMock(ac())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mox.py", line 284, in CreateMock
    new_mock = MockObject(class_to_mock, attrs=attrs)
  File "mox.py", line 603, in __init__
    attr = getattr(class_to_mock, method)
AttributeError: a
>>>

What version of the product are you using? On what operating system?

The latest version of mox from SVN, with Python 2.7.3rc2, built for 32-bit 
Linux.

I have attached a patch to ignore such bogus attributes, with two test cases.

Original issue reported on code.google.com by m...@ihavethememo.net on 9 Nov 2012 at 1:20

Attachments:

GoogleCodeExporter commented 8 years ago
Fixed in http://code.google.com/p/pymox/source/detail?r=74

Original comment by stev...@google.com on 12 Nov 2012 at 7:00