[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Benjamin Peterson benja...@python.org added the comment: Fixed in r77093. -- resolution: - accepted status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Trundle andy-pyt...@hammerhartes.de added the comment: What Joe Amenta stumbled across is that ABCMeta caches its subclass checks. If you call ``isinstance(spam, Callable)`` and after that delete `type(spam).__call__`, every other call of ``isinstance(spam, Callable)`` will still return True. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: Ah yes, I misread the example. Agreed that that is a change in behaviour then - it is possible to clear the caches if absolutely necessary, but doing so isn't particularly portable. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Joe Amenta ament...@msu.edu added the comment: I believe that this patch works like you described... Attached a patch with more test cases to show this. (the [1:] parts are to make the test cases readable; they will still pass if all the leading newlines are removed from the triple-quoted strings and all [1:] are removed :-) -- Added file: http://bugs.python.org/file15673/new_callable_semantics_moretests.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Joe Amenta ament...@msu.edu added the comment: To elaborate on my last comment: - touch_import looks for the required import binding in any scope, and it will add a global import if not found, otherwise it leaves it alone - the import added does not have a newline prefix, so if the newlines were left in, (without [1:]) the tests would fail. However, I found that the test cases are easier to read if they all start on the same indentation level, so adding a newline for the reader but telling the parser to ignore it makes readable, correct test cases. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Joe Amenta ament...@msu.edu added the comment: One such weird corner case: from collections import Callable class Spam(object): def __call__(self): return self can_of_spam = Spam() print callable(can_of_spam) == isinstance(can_of_spam, Callable) # True del Spam.__call__ can_of_spam.__call__ = lambda can: 'spam' print callable(can_of_spam) == isinstance(can_of_spam, Callable) # False Regardless, attached a patch for the new proposed semantics -- keywords: +patch nosy: +joe.amenta Added file: http://bugs.python.org/file15671/new_callable_semantics.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: That isn't a semantic change, it is exactly the same semantics as callable() in 2.x: class Spam(object): ... def __call__(self): pass ... callable(Spam()) True del Spam.__call__ callable(Spam()) False -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: Just confirming that 2.x also correctly ignores instance attributes (as it should, since it looks at the tp_call slot): odd = Spam() odd.__call__ = lambda: 'very odd' callable(odd) False -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: The patch however does not look correct - the import should be added as a global import at the beginning of affected files rather than inline with each callable check. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: Antoine Pitrou wrote: Antoine Pitrou pit...@free.fr added the comment: I also think isinstance(x, collections.Callable) is the correct replacement. Even though it might give a different answer on weird corner cases, it is semantically what you are looking for. (too bad it has a stupid module placement) Yes, I was very surprised not to find it as abc.Callable... Regards, Nick. -- title: The replacement suggested for callable(x) in py3k is not equivalent - The replacement suggested for callable(x) in py3k is not equivalent ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Jean-Paul Calderone exar...@divmod.com added the comment: How about bringing callable back since there is no obvious replacement for it? It's valuable as a LYBL check in circumstances where an object submitted far away from the place where it's invoked. Such uses can't easily be replaced with direct calls to follow the recommended EAFP style. -- nosy: +exarkun ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Antoine Pitrou pit...@free.fr added the comment: I also think isinstance(x, collections.Callable) is the correct replacement. Even though it might give a different answer on weird corner cases, it is semantically what you are looking for. (too bad it has a stupid module placement) -- nosy: +pitrou ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Georg Brandl ge...@python.org added the comment: Not really, that was the last thing to get this issue closed. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Georg Brandl ge...@python.org added the comment: 2to3 still uses hasattr(x, 'callable'). -- assignee: georg.brandl - benjamin.peterson ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Benjamin Peterson benja...@python.org added the comment: 2009/10/22 Georg Brandl rep...@bugs.python.org: Georg Brandl ge...@python.org added the comment: 2to3 still uses hasattr(x, 'callable') Do you have strong opinions about this? IMO, hasattr(x, '__call__') is compatible enough. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Ezio Melotti ezio.melo...@gmail.com added the comment: Benjamin already replaced hasattr(x, __call__) with hasattr(type(x), __call__) in the Python 3.0 What's New in r75090 and r75094, but this still doesn't match completely the behavior of callable(): class Foo(object): pass ... foo = Foo() callable(foo) False hasattr(type(foo), '__call__') True foo() Traceback (most recent call last): File stdin, line 1, in module TypeError: 'Foo' object is not callable There are also other places where hasattr(x, __call__) is still suggested/used (e.g. PEP3100). -- nosy: +benjamin.peterson ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Trundle andy-pyt...@hammerhartes.de added the comment: As every type is an instance of `type`, every type also has a `__call__` attribute which means ``hasattr(type(x), '__call__')`` is always true. `callable()` checks whether `tp_call` is set on the type, which cannot be done in Python directly. -- nosy: +Trundle ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: hasattr(type(x), __call__) is technically a more valid replacement due to the usual matter of metaclass confusion. (Although putting special methods on instance objects is a recipe for trouble in more ways than just this one). -- nosy: +ncoghlan ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7006] The replacement suggested for callable(x) in py3k is not equivalent
Nick Coghlan ncogh...@gmail.com added the comment: Although we should seriously consider properly exposing special method lookup at the Python level... -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7006 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com