| 548 | return False |
| 549 | |
| 550 | def _getmembers(object, predicate, getter): |
| 551 | results = [] |
| 552 | processed = set() |
| 553 | names = dir(object) |
| 554 | if isclass(object): |
| 555 | mro = (object,) + getmro(object) |
| 556 | # add any DynamicClassAttributes to the list of names if object is a class; |
| 557 | # this may result in duplicate entries if, for example, a virtual |
| 558 | # attribute with the same name as a DynamicClassAttribute exists |
| 559 | try: |
| 560 | for base in object.__bases__: |
| 561 | for k, v in base.__dict__.items(): |
| 562 | if isinstance(v, types.DynamicClassAttribute): |
| 563 | names.append(k) |
| 564 | except AttributeError: |
| 565 | pass |
| 566 | else: |
| 567 | mro = () |
| 568 | for key in names: |
| 569 | # First try to get the value via getattr. Some descriptors don't |
| 570 | # like calling their __get__ (see bug #1785), so fall back to |
| 571 | # looking in the __dict__. |
| 572 | try: |
| 573 | value = getter(object, key) |
| 574 | # handle the duplicate key |
| 575 | if key in processed: |
| 576 | raise AttributeError |
| 577 | except AttributeError: |
| 578 | for base in mro: |
| 579 | if key in base.__dict__: |
| 580 | value = base.__dict__[key] |
| 581 | break |
| 582 | else: |
| 583 | # could be a (currently) missing slot member, or a buggy |
| 584 | # __dir__; discard and move on |
| 585 | continue |
| 586 | if not predicate or predicate(value): |
| 587 | results.append((key, value)) |
| 588 | processed.add(key) |
| 589 | results.sort(key=lambda pair: pair[0]) |
| 590 | return results |
| 591 | |
| 592 | def getmembers(object, predicate=None): |
| 593 | """Return all members of an object as (name, value) pairs sorted by name. |