summaryrefslogtreecommitdiff
path: root/src/zope/interface/tests
Commit message (Collapse)AuthorAgeFilesLines
* Update the Adaptation docs to be more concrete.docs-updateJason Madden2021-03-171-0/+7
| | | | | | This should help provide better motivating use cases. Examples inspired by https://glyph.twistedmatrix.com/2021/03/interfaces-and-protocols.html Also some minor typo fixes and updates to comments.
* Let subclasses of BaseAdapterRegistry customize the data structures.Jason Madden2021-03-032-13/+624
| | | | | | | | | | | | Add extensive tests for this. Fixes #224. Also adds test for, and fixes #227 Add BAR.rebuild() to fix the refcount issue, and to change datatypes. It works by using the new methods allRegistrations() and allSubscriptions() to re-create the data in new data structures. This makes fresh calls to subscribe() and register(). I went this way instead of trying to manually walk the data structures and create them because the logic in those methods is fully tested.
* Merge branch 'master' of github.com:zopefoundation/zope.interface into masterJan-Jaap Driessen2020-10-011-0/+29
|\
| * C optimizations: Spec_clear and Spec_traverse need to include Spec->_basesJason Madden2020-09-281-0/+29
| | | | | | | | | | | | Previously they did not, leading to a reference leak of a tuple. Fixes #216
* | When an invariant is defined in an interface, it's found byJan-Jaap Driessen2020-09-251-0/+14
|/ | | | | `validateInvariants` in all interfaces inheriting from that interface. Make sure to call each invariant only once when validating invariants.
* Fix interface definitions of IAdapterRegistry.subscribe, subscribers and ↵Jason Madden2020-04-231-54/+72
| | | | | | | | subscriptions. They were defined to accept a name argument, but the actual implementation doesn't. Add tests for this. Fixes #208. Also in test_adapter.py modernize idioms from assertTrue(x in y) and assertTrue(x is y) to assertIn and assertIs.
* Merge pull request #203 from zopefoundation/issue199Jason Madden2020-04-082-106/+161
|\ | | | | Make @implementer and classImplements not add redundant interfaces
| * Fix implementerOnly when duplicating something inherited from the parent.issue199Jason Madden2020-04-071-79/+84
| | | | | | | | | | | | Seen in plone.app.testing as a problem in Products.GenericSetup. Added tests for that.
| * Special case the base Interface in @implementer/classImplements to skip the ↵Jason Madden2020-04-072-1/+20
| | | | | | | | redundancy detection for the sake of plone.app.caching.
| * Make @implementer and classImplements not re-declare redundant interfaces.Jason Madden2020-04-071-52/+83
| | | | | | | | classImplementsOnly and @implementer_only can still be used to do that.
* | The ImmutableDeclaration also has immutable _v_attrs.issue204Jason Madden2020-04-071-6/+27
|/ | | | Fixes #204
* Feedback from review: whitespace, doc clarification, and a unit test showing ↵Jason Madden2020-04-071-1/+38
| | | | the precedence of __conform__ vs __adapt__.
* Let interface 'subclasses' override __adapt__.issue3Jason Madden2020-04-063-52/+179
| | | | | | | | | | | | | | | | | | | | 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-024-144/+279
| | | | | | | | 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-11/+47
| | | | | | their iro and sro. Fixes #197.
* Make the RO for InterfaceClass consistent and fix handling of the STRICT_IRO ↵issue192-issue194Jason Madden2020-03-202-13/+52
| | | | | | | | env variable. Fixes #192 and fixes #194. Also fix the IRO for OrderedDict on CPython 2
* Update comments and add a test for more coverage.Jason Madden2020-03-191-0/+21
|
* Add additional tests for assigning to Interface.__module__.Jason Madden2020-03-181-0/+7
|
* Add tests for comparing InterfaceClass/Implements objects to things without ↵Jason Madden2020-03-182-5/+119
| | | | | | the required attributes. And fix the C handling of this case.
* Use a descriptor for __module__Jason Madden2020-03-181-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 tests when zope.component is also importable.Jason Madden2020-03-182-1/+24
|
* Clean up linter errors in test_interface.py so new/real problems are more ↵Jason Madden2020-03-181-41/+52
| | | | obvious.
* Move Interface hashing and comparison to C; 2.5 to 15x speedup in micro ↵Jason Madden2020-03-182-1/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-0/+21
| | | | | | | | __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-0/+42
| | | | | | 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-1/+93
| | | | | | | | 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-153-14/+315
| | | | | | | | | | | | | | | | | | | | | | | | 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.
* Add additional low-level tests for super() unwrapping.Jason Madden2020-03-101-1/+38
|
* Make provided/implementedBy and adapter registries respect super().issue11Jason Madden2020-03-092-2/+473
| | | | | | | | | | | | | | | | | | | | | | | | The query functions now start by looking at the next class in the MRO (interfaces directly provided by the underlying object are not found). Adapter registries automatically pick up providedBy change to start finding the correct implementations of adapters, but to make that really useful they needed to change to unpack super() arguments and pass __self__ to the factory. Fixes #11 Unfortunately, this makes PyPy unable to build the C extensions. Additional crash-safety for adapter lookup. Make the C functions get the cache only after resolving the ``required`` into a tuple, in case of side-effects like...clearing the cache. This could lead to the ``cache`` object being deallocated before we used it. Drop the ``tuplefy`` function in favor of a direct call to ``PySequence_Tuple``. It's what the ``tuple`` constructor would do anyway and saves a few steps. Make sure that getting ``providedBy(super())`` and ``implementedBy(super())`` have no side effects.
* Find more concrete classes that are registered.Jason Madden2020-02-171-5/+2
|
* Merge pull request #177 from zopefoundation/fix-signatureJens W. Klein2020-02-161-4/+4
|\ | | | | Fix repr of "Attribute" to look like before
| * Fix repr of "Attribute" to look like beforeJens W. Klein2020-02-161-4/+4
| | | | | | | | and as usual in Python.
* | Add test case for #6.issue6Jason Madden2020-02-141-1/+50
|/ | | | It seems to be working now, so this fixes #6
* Simplify the string formatting rules for the Invalid exceptions.Jason Madden2020-02-101-14/+54
| | | | | | As per review. Also include the actual failing implementation object in the BrokenMethodImplementation to make it easier to track down what's going on when inheritance is involved.
* Make verifyObject/Class collect and raise all errors instead of only the first.issue171Jason Madden2020-02-082-2/+85
| | | | Fixes #171.
* Make verification errors more readable and useful.Jason Madden2020-02-072-18/+92
| | | | | | | | | | | | | | 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-6/+50
| | | | | | | | | on Pypy On PyPy2, they are ignored (like on CPython), but on PyPy3 they can actually be validated. Fixes #118
* The _empty singleton has no-op subscribe/unsubscribe methods.issue162Jason Madden2020-01-281-0/+18
| | | | | | | | 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.
* Make the singleton _empty immutable.Jason Madden2020-01-271-0/+53
| | | | Fixes #158
* Remove support for hashing uninitialized interfaces.issue157Jason Madden2020-01-271-10/+2
| | | | Fixes #157
* Move Declaration, Specification and ClassProvides to __slots__.Jason Madden2020-01-272-8/+8
| | | | 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.
* Use tp_members in SpecificationBase and ClassProvidesBase.Jason Madden2020-01-231-20/+20
| | | | | | | | | | | | 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-214-164/+158
| | | | | | | 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.
* Remove unused test files.Jason Madden2020-01-213-67/+0
|
* key order doesn't mattermauro2019-10-091-1/+1
|
* taggedvalue inheritancemauro2019-10-091-2/+11
|
* Fix coverage.issue126Jason Madden2018-10-231-2/+2
|
* Fix 'verifyObject' for class objects with staticmethods on Python 3.Jason Madden2018-10-231-0/+21
| | | | Fixes #126
* Allow Registering and unregistering instance methods as listeners (#102)‮rekcäH nitraM‮2017-11-281-0/+11
| | | * Fixes #12 - registering instance methods as listeners doesn’t allow to easily unregister them as the registry tries to find the handler with 'is' but it should use '==' to allow the python BoundMethod wrapper (which is a new instance every time instance.$methodname is accessed).