[Python-Dev] [Python-checkins] cpython: PEP 417: Adding unittest.mock
Michael Foord
fuzzyman at voidspace.org.uk
Wed Mar 14 22:41:52 CET 2012
On 14 Mar 2012, at 13:46, Terry Reedy wrote:
> On 3/14/2012 4:22 PM, Michael Foord wrote:
>>
>> On 14 Mar 2012, at 13:08, Terry Reedy wrote:
>>
>>> On 3/14/2012 3:25 PM, michael.foord wrote:
>>>> +# mock.py +# Test tools for mocking and patching.
>
>>> Should there be a note here about restrictions on editing this
>>> file? I notice that there are things like
>>>
>>>> +class OldStyleClass: + pass +ClassType = type(OldStyleClass)
>>>
>>> which are only present for running under Py2 and which would
>>> normally be removed for Py3.
>>
>>
>> Yeah, I removed as much of the Python 2 compatibility code and
>> thought I'd got it all. Thanks for pointing it out.
>
> 2000 lines is a lot to check through.
>>
>> I'm maintaining a "clean" (no Python 2 compatibility code) version in
>> the standard library.
>
> Great. Here is something else, which is why I thought otherwise ;-).
>
> +def _instance_callable(obj):
> + """Given an object, return True if the object is callable.
> + For classes, return True if instances would be callable."""
> + if not isinstance(obj, type):
> + # already an instance
> + return getattr(obj, '__call__', None) is not None
> +
> + klass = obj
> + # uses __bases__ instead of __mro__ so that we work with
> >>> old style classes
> + if klass.__dict__.get('__call__') is not None:
> + return True
> +
> + for base in klass.__bases__:
> + if _instance_callable(base):
> + return True
> + return False
>
> If you want to leave the code as is, remove or revise the comment.
Thanks very much for finding these, I'm pretty sure I've fixed all the ones you reported - and one more case where try...except...finally can now be used.
All the best,
Michael Foord
>
>> I'll be maintaining mock, so I'd like to be
>> assigned any issues on it and at least talked to before changes are
>> made. I am maintaining a backport still, but the Python standard
>> library version is the canonical version.
>
> Add unittest.mock to devguide/experts.rst and yourself with * appended.
>
> ---
> Searching for 'old', I also found
>
> +def _must_skip(spec, entry, is_type):
> + if not isinstance(spec, type):
> + if entry in getattr(spec, '__dict__', {}):
> + # instance attribute - shouldn't skip
> + return False
> >>>+ # can't use type because of old style classes
> + spec = spec.__class__
> + if not hasattr(spec, '__mro__'):
> >>>+ # old style class: can't have descriptors anyway
> + return is_type
>
> In testcallable.py
> + def test_patch_spec_callable_class(self):
> + class CallableX(X):
> + def __call__(self):
> + pass
> +
> + class Sub(CallableX):
> + pass
> +
> + class Multi(SomeClass, Sub):
> + pass
> +
> >>>+ class OldStyle:
> + def __call__(self):
> + pass
> +
> >>>+ class OldStyleSub(OldStyle):
> + pass
> +
> + for arg in 'spec', 'spec_set':
> >>>+ for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:
>
> This is the last.
>
> --
> Terry Jan Reedy
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
>
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing
http://www.sqlite.org/different.html
More information about the Python-Dev
mailing list