summaryrefslogtreecommitdiff
path: root/src/zope/interface/interface.py
Commit message (Collapse)AuthorAgeFilesLines
* Feedback from review: whitespace, doc clarification, and a unit test showing ↵Jason Madden2020-04-071-1/+4
| | | | the precedence of __conform__ vs __adapt__.
* Let interface 'subclasses' override __adapt__.issue3Jason Madden2020-04-061-1/+76
| | | | | | | | | | | | | | | | | | | | Cooperate with InterfaceClass to ensure there is no performance penalty for this. Fixes #3 +-------------------------------------------------------------+----------------+------------------------------+------------------------------+ | Benchmark | bench_master38 | bench_issue3 | bench_issue3_opt | +=============================================================+================+==============================+==============================+ | call interface (provides; deep) | 369 ns | 454 ns: 1.23x slower (+23%) | not significant | +-------------------------------------------------------------+----------------+------------------------------+------------------------------+ | call interface (provides; wide) | 373 ns | 457 ns: 1.22x slower (+22%) | 365 ns: 1.02x faster (-2%) | +-------------------------------------------------------------+----------------+------------------------------+------------------------------+ | call interface (no alternate, no conform, not provided) | 671 ns | 760 ns: 1.13x slower (+13%) | 636 ns: 1.06x faster (-5%) | +-------------------------------------------------------------+----------------+------------------------------+------------------------------+ | call interface (alternate, no conform, not provided) | 395 ns | 494 ns: 1.25x slower (+25%) | not significant | +-------------------------------------------------------------+----------------+------------------------------+------------------------------+ | call interface (no alternate, valid conform, not provided) | 250 ns | not significant | 227 ns: 1.10x faster (-9%) | +-------------------------------------------------------------+----------------+------------------------------+------------------------------+ | call interface (alternate, invalid conform, not provided) | 348 ns | 424 ns: 1.22x slower (+22%) | not significant | +-------------------------------------------------------------+----------------+------------------------------+------------------------------+
* Remove the bare except: statements.Jason Madden2020-04-021-9/+3
| | | | | | | | Only catch AttributeError instead of everything. Fixes #200 Note that this does break a doctest in five.intid (it's expecting a TypeError but it now gets Acquisition's RuntimeError).
* Ensure that objects that implement no interfaces still have Interface in ↵Jason Madden2020-03-301-0/+6
| | | | | | their iro and sro. Fixes #197.
* Make the RO for InterfaceClass consistent and fix handling of the STRICT_IRO ↵issue192-issue194Jason Madden2020-03-201-4/+6
| | | | | | | | env variable. Fixes #192 and fixes #194. Also fix the IRO for OrderedDict on CPython 2
* Another comment update, referencing #163Jason Madden2020-03-191-2/+7
|
* Update comments and add a test for more coverage.Jason Madden2020-03-191-19/+35
|
* Remove untested except in the metaclass __new__.faster-eq-hash-comparisonJason Madden2020-03-181-7/+5
| | | | Reviewers weren't sure how it could be raised.
* Add tests for comparing InterfaceClass/Implements objects to things without ↵Jason Madden2020-03-181-0/+10
| | | | | | the required attributes. And fix the C handling of this case.
* Move to a metaclass for handling __module__.Jason Madden2020-03-181-62/+108
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This offers the absolute best performance at what seems like reasonable complexity. +-------------------------------------------------------------+----------------+-------------------------------+ | Benchmark | 38-master-full | 38-faster-meta | +=============================================================+================+===============================+ | read __module__ | 41.8 ns | 40.9 ns: 1.02x faster (-2%) | +-------------------------------------------------------------+----------------+-------------------------------+ | read __name__ | 41.8 ns | 39.9 ns: 1.05x faster (-5%) | +-------------------------------------------------------------+----------------+-------------------------------+ | read providedBy | 56.9 ns | 58.4 ns: 1.03x slower (+3%) | +-------------------------------------------------------------+----------------+-------------------------------+ | query adapter (no registrations) | 3.85 ms | 2.95 ms: 1.31x faster (-24%) | +-------------------------------------------------------------+----------------+-------------------------------+ | query adapter (all trivial registrations) | 4.62 ms | 3.63 ms: 1.27x faster (-21%) | +-------------------------------------------------------------+----------------+-------------------------------+ | query adapter (all trivial registrations, wide inheritance) | 51.8 us | 42.2 us: 1.23x faster (-19%) | +-------------------------------------------------------------+----------------+-------------------------------+ | query adapter (all trivial registrations, deep inheritance) | 52.0 us | 41.7 us: 1.25x faster (-20%) | +-------------------------------------------------------------+----------------+-------------------------------+ | sort interfaces | 234 us | 29.9 us: 7.84x faster (-87%) | +-------------------------------------------------------------+----------------+-------------------------------+ | sort mixed | 569 us | 340 us: 1.67x faster (-40%) | +-------------------------------------------------------------+----------------+-------------------------------+ | contains (empty dict) | 135 ns | 55.2 ns: 2.44x faster (-59%) | +-------------------------------------------------------------+----------------+-------------------------------+ | contains (populated dict: interfaces) | 137 ns | 56.1 ns: 2.45x faster (-59%) | +-------------------------------------------------------------+----------------+-------------------------------+ | contains (populated list: interfaces) | 39.7 us | 2.96 us: 13.42x faster (-93%) | +-------------------------------------------------------------+----------------+-------------------------------+ | contains (populated dict: implementedBy) | 137 ns | 55.2 ns: 2.48x faster (-60%) | +-------------------------------------------------------------+----------------+-------------------------------+ | contains (populated list: implementedBy) | 40.6 us | 24.1 us: 1.68x faster (-41%) | +-------------------------------------------------------------+----------------+-------------------------------+ Not significant (2): read __doc__; sort implementedBy
* Use a descriptor for __module__Jason Madden2020-03-181-24/+65
| | | | | | | | | | | | | | | | | | | | | | | | | | | This makes the rest of the attribute access fast again, but slows down __module__. +-------------------------------------------+------------+-------------------------------+ | Benchmark | 38-master3 | 38-faster-descr | +===========================================+============+===============================+ | read __module__ | 41.1 ns | 123 ns: 2.99x slower (+199%) | +-------------------------------------------+------------+-------------------------------+ | read __name__ | 41.3 ns | 39.9 ns: 1.04x faster (-3%) | +-------------------------------------------+------------+-------------------------------+ | read __doc__ | 41.8 ns | 42.4 ns: 1.01x slower (+1%) | +-------------------------------------------+------------+-------------------------------+ | query adapter (no registrations) | 3.85 ms | 2.95 ms: 1.30x faster (-23%) | +-------------------------------------------+------------+-------------------------------+ | query adapter (all trivial registrations) | 4.59 ms | 3.67 ms: 1.25x faster (-20%) | +-------------------------------------------+------------+-------------------------------+ | contains (empty dict) | 136 ns | 54.8 ns: 2.48x faster (-60%) | +-------------------------------------------+------------+-------------------------------+ | contains (populated dict) | 137 ns | 55.7 ns: 2.46x faster (-59%) | +-------------------------------------------+------------+-------------------------------+ | contains (populated list) | 40.2 us | 2.86 us: 14.03x faster (-93%) | +-------------------------------------------+------------+-------------------------------+ Not significant (1): read providedBy
* Avoid use of a metaclass by implementeng __getattribute__.Jason Madden2020-03-181-132/+162
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This is pretty, but it slows down all attribute access to interfaces. By up to 25%. I'm not sure that's acceptable for things like Interface.providedBy. +-------------------------------------------+------------+-------------------------------+ | Benchmark | 38-master3 | 38-faster3 | +===========================================+============+===============================+ | read __module__ | 41.1 ns | 44.3 ns: 1.08x slower (+8%) | +-------------------------------------------+------------+-------------------------------+ | read __name__ | 41.3 ns | 51.6 ns: 1.25x slower (+25%) | +-------------------------------------------+------------+-------------------------------+ | read __doc__ | 41.8 ns | 53.3 ns: 1.28x slower (+28%) | +-------------------------------------------+------------+-------------------------------+ | read providedBy | 56.7 ns | 71.6 ns: 1.26x slower (+26%) | +-------------------------------------------+------------+-------------------------------+ | query adapter (no registrations) | 3.85 ms | 2.95 ms: 1.31x faster (-23%) | +-------------------------------------------+------------+-------------------------------+ | query adapter (all trivial registrations) | 4.59 ms | 3.65 ms: 1.26x faster (-20%) | +-------------------------------------------+------------+-------------------------------+ | contains (empty dict) | 136 ns | 55.4 ns: 2.45x faster (-59%) | +-------------------------------------------+------------+-------------------------------+ | contains (populated dict) | 137 ns | 55.0 ns: 2.49x faster (-60%) | +-------------------------------------------+------------+-------------------------------+ | contains (populated list) | 40.2 us | 2.95 us: 13.62x faster (-93%) | +-------------------------------------------+------------+-------------------------------+
* Fix doctest by making sure the default type repr can be used.Jason Madden2020-03-181-2/+16
|
* Move Interface hashing and comparison to C; 2.5 to 15x speedup in micro ↵Jason Madden2020-03-181-66/+95
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | benchmarks Included benchmark numbers: Current master, Python 3.8: ..................... contains (empty dict): Mean +- std dev: 198 ns +- 5 ns ..................... contains (populated dict): Mean +- std dev: 197 ns +- 6 ns ..................... contains (populated list): Mean +- std dev: 53.1 us +- 1.2 us This code: ..................... contains (empty dict): Mean +- std dev: 77.9 ns +- 2.3 ns ..................... contains (populated dict): Mean +- std dev: 78.4 ns +- 3.1 ns ..................... contains (populated list): Mean +- std dev: 3.69 us +- 0.08 us So anywhere from 2.5 to 15x faster. Not sure how that will translate to larger benchmarks, but I'm hopeful. It turns out that messing with ``__module__`` is nasty, tricky business, especially when you do it from C. Everytime you define a new subclass, the descriptors that you set get overridden by the type machinery (PyType_Ready). I'm using a data descriptor and a meta class right now to avoid that but I'm not super happy with that and would like to find a better way. (At least, maybe the data part of the descriptor isn't necessary?) It may be needed to move more code into C, I don't want a slowdown accessing ``__module__`` either; copying around the standard PyGetSet or PyMember descriptors isn't enough because they don't work on the class object (so ``classImplements(InterfaceClass, IInterface)`` fails).
* Move the one-base optimization down a level, and enable using pre-calculated ↵Jason Madden2020-03-181-15/+16
| | | | | | | | __sro__ for caching. In my local 'load the world' test, this went from ~7800 full C3 merges to about ~1100. Also take steps to avoid triggering false positive warnings about changed ROs when it's *just* Interface that moved.
* Ensure Interface is the last item in the __sro__.Jason Madden2020-03-181-18/+76
| | | | | | None of the elegant solutions mentioned in the issue worked out, so I had to brute force it. Fixes #8
* Make Interface.getTaggedValue follow the __iro__.Jason Madden2020-03-181-12/+26
| | | | | | | | Previously it manually walked up __bases__, meaning the answers could be inconsistent. Fixes #190. Also fixes several minor issues in the documentation, mostly cross-reference related.
* Use C3 (mostly) to compute IRO.issue21Jason Madden2020-03-151-2/+11
| | | | | | | | | | | | | | | | | | | | | | | | Fixes #21 The 'mostly' is because interfaces are used in cases that C3 forbids; when there's a conflict, we fallback to the legacy algorithm. It turns out there are few conflicts (13K out of 149K total orderings in Plone). I hoped the fix for #8 might shake out automatically, but it didn't. Optimize the extremely common case of a __bases__ of length one. In the benchmark, 4/5 of the interfaces and related objects have a base of length one. Fix the bad IROs in the bundled ABC interfaces, and implement a way to get warnings or errors. In running plone/buildout.coredev and tracking the RO requests, the stats for equal, not equal, and inconsistent-so-fallback, I got {'ros': 148868, 'eq': 138461, 'ne': 10407, 'inconsistent': 12934} Add the interface module to the Attribute str. This was extremely helpful tracking down the Plone problem; IDate is defined in multiple modules.
* Fix repr of "Attribute" to look like beforeJens W. Klein2020-02-161-1/+1
| | | | and as usual in Python.
* Make verification errors more readable and useful.Jason Madden2020-02-071-0/+18
| | | | | | | | | | | | | | Eliminate the trailing newlines and blank spaces (the code called them "a stupid artifact"). Include the name of the defining interface (so the user can easily look up any requirements on the attribute) and, for methods, the expected signature (no more guessing about how many arguments are required!). This is implemented by giving Attribute and Method useful reprs and strs. Previously, they just had the defaults. Fixes #170
* Fix verification for methods of builtin types with pseudo-default arguments ↵issue118Jason Madden2020-02-061-2/+9
| | | | | | | | | on Pypy On PyPy2, they are ignored (like on CPython), but on PyPy3 they can actually be validated. Fixes #118
* Add __all__ to all modules.issue153Jason Madden2020-02-051-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #153 The items that went in each ``__all__`` are based on what is documented: $ rg --no-filename 'import' docs/ -trst | tr -s "[:blank:]" | sort | uniq | grep zope >>> from pprint import pprint >>> from zope.interface import * >>> from zope.interface import Interface >>> from zope.interface import Interface, Attribute, implementer >>> from zope.interface import alsoProvides >>> from zope.interface import classImplements >>> from zope.interface import classImplementsOnly >>> from zope.interface import directlyProvidedBy >>> from zope.interface import directlyProvides >>> from zope.interface import implementedBy >>> from zope.interface import implementer >>> from zope.interface import implementer_only >>> from zope.interface import noLongerProvides >>> from zope.interface import providedBy >>> from zope.interface import provider >>> from zope.interface.adapter import AdapterRegistry >>> from zope.interface.declarations import Declaration >>> from zope.interface.declarations import InstanceDeclarations >>> from zope.interface.declarations import ProvidesClass >>> from zope.interface.declarations import named >>> from zope.interface.exceptions import BrokenImplementation >>> from zope.interface.exceptions import Invalid >>> from zope.interface.interface import Specification >>> from zope.interface.interface import adapter_hooks >>> from zope.interface.verify import verifyObject >>> import gc >>> import zope.interface And also some personal judgement about what the public API is that I'm more than happy to have reviewed.
* The _empty singleton has no-op subscribe/unsubscribe methods.issue162Jason Madden2020-01-281-1/+4
| | | | | | | | Turns out they can be called in some very strange error cases. See #162 and #163 for details. This should fix #162 (at least the provided test case, five.intid, passes now). It also does enough work on #163 that (a) the test can be written and run in pure-python mode, which was needed to debug it and (b) five.intid runs in pure-python mode (well, with a bunch of other small hacks to Acquisition, ExtensionClass, DocumentTemplate and AccessControl), but I won't claim that it fully fixes #163. For one thing, there are no specific tests. For another, I see more such differences.
* Remove support for hashing uninitialized interfaces.issue157Jason Madden2020-01-271-7/+2
| | | | Fixes #157
* Avoid allocating space for tagged values unless they're used. This saves ↵Jason Madden2020-01-271-3/+9
| | | | another few percent.
* Specifications with no dependents are common (4700 out of 7000 in this ↵Jason Madden2020-01-271-9/+25
| | | | | | example), so avoid allocating a WeakKeyDictionary until needed. This saves another 2% or so.
* Move Declaration, Specification and ClassProvides to __slots__.Jason Madden2020-01-271-16/+42
| | | | In a test of 6000 modules that load 2245 InterfaceClass objects and produce 2233 ClassProvides instances, this saves about 1% total memory usage in Python 2.7.
* cache on volatile attribute in case class is persisted in ZODBhash_performanceJens W. Klein2020-01-251-3/+3
|
* Improve InterfaceClass __hash__ performanceJens W. Klein2020-01-241-5/+9
|
* Use tp_members in SpecificationBase and ClassProvidesBase.Jason Madden2020-01-231-1/+5
| | | | | | | | | | | | Instead of storing 1 and 2 attributes, respectively, in __dict__. This lets us eliminate inst_attr and its use of an undocumented API. It also helps reign in the size of these objects. Before, on CPython 8, a ClassProvides object that subclasses both SpecificationBase and ClassProvidesBase required 408 bytes: 48 for the object, 360 for the dict. Now it requires 72 + 232 = 304. With a bit of work on defining __slots__ in Python, that can be brought much lower still, I think. zope.security tests pass with this installed.
* Test PURE_PYTHON at runtime.Jason Madden2020-01-211-27/+18
| | | | | | | Turns out that the C extensions build and work fine with PyPy, but don't use them by default. Let them be forced, though. Tests needed some refactoring to account for the various permutations.
* fix 'dict_keys' object has no attribute 'extend'mauro2019-10-091-3/+5
|
* taggedvalue inheritancemauro2019-10-091-0/+25
|
* Formatting and cross refs for the 'Interface Specification' docJason Madden2018-07-191-2/+3
|
* Drop __annotations__ in interface declarationsMaxim Avanov2017-09-011-1/+2
|
* Enable combined coverage reports under tox and branch coveragecombined-coverageJason Madden2017-06-141-3/+3
| | | | | | | | Fixes #91 Remove many (or most) now-redundant 'pragma: no cover'. A few minor test changes to get full branch coverage.
* 100% coverage (#90)Jason Madden2017-06-121-7/+7
| | | | | | | | | | | | | | | | | | | | | * Get as close to 100% coverage as possible. Without conflicting with #86. This is almost entirely trivial changes: - Make dummy methods that should never be called either raise NotImplementedError or call self.fail() in tests. - Standardize the no-coverage pragma - Add a few pragmas where coverage varies across versions/implementations, mostly for the sake of tox (travis should hit them all). * Convert tox to run the coverage command, like travis. A follow up should have it produce coverage for all python versions/implementations. Fixes #87 * Adjust coverage config for py2 only tests; fix typo; finish replacing self with cls
* Fix C optimizations broken on Py3k.60-fix_c_optimizations-py3kTres Seaver2016-11-151-3/+3
| | | | | | | | | | - The bare import of '_zope_c_optimizations' prevented them from being used. - After enabling them via absolute imports, they would fail due to http://bugs.python.org/issue15657. Fixes #60.
* Make declarations.Implements sortable.comp-implementsJason Madden2016-08-031-1/+1
| | | | Fixes #42
* 100% branch coverage.Tres Seaver2014-12-091-1/+1
| | | | | | We exclude interfaces declared with the 'class' statement, because their methods typically have only a doctstring in the suite, which confuses the branch coverage machinery.
* Remove fossilized 'InterfaceClass.deferred()'.Tres Seaver2014-12-081-28/+1
|
* - Fixed a bug that was revealed by porting zope.traversing. During a loop, theStephan Richter2013-02-211-1/+1
| | | | loop body modified a weakref dict causing a ``RuntimeError`` error
* further simplify some attribute lookupsFred Drake2012-04-061-2/+2
|
* Drop unneeded compatibility shims for method / function attributes.Tres Seaver2012-04-061-6/+3
| | | | | '__code__', '__defaults__', '__func__', and '__self__' all work fine on Python 2.6+.
* Move commented-out doctest snippets into API docs as real snippets.Tres Seaver2012-04-061-140/+3
|
* Merge Py3k w/o 2to3 work.Tres Seaver2012-04-041-152/+153
|
* Merge from LP branch.Tres Seaver2012-03-261-24/+35
|
* Avoid exceptions due to tne new ``__qualname__`` attribute added in Python 3.3 Tres Seaver2012-02-161-3/+5
| | | | | | | See PEP 3155 for rationale. Thanks to Antoine Pitrou for the patch. Fixes LP #900906.
* Work around buggy behavior in some subclasses of `InterfaceClass``Tres Seaver2011-08-111-0/+5
| | | | | | | | | Some sublcasses invoke '__hash__' before initializing '__module__' and '__name__'. The workaround returns a fixed constant hash in such cases, and issues a UserWarning. Addresses LP #811792.
* Revert r122462 after updating zope.app.interface.Tres Seaver2011-08-111-2/+1
|