1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
|
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: tests.py 109298 2010-02-22 17:30:41Z hannosch $
"""
from ExtensionClass import *
import pickle
def print_dict(d):
d = d.items()
d.sort()
print '{%s}' % (', '.join(
[('%r: %r' % (k, v)) for (k, v) in d]
))
def test_mixing():
"""Test working with a classic class
>>> class Classic:
... def x(self):
... return 42
>>> class O(Base):
... def __of__(*a):
... return a
>>> class O2(Classic, O):
... def __of__(*a):
... return (O2.inheritedAttribute('__of__')(*a),
... O2.inheritedAttribute('x')(a[0]))
>>> class C(Base):
... def __class_init__(self):
... print 'class init called'
... print self.__name__
... def bar(self):
... return 'bar called'
class init called
C
>>> c = C()
>>> o2 = O2()
>>> c.o2 = o2
>>> int(c.o2 == ((o2, c), 42))
1
Test working with a new style
>>> class Modern(object):
... def x(self):
... return 42
>>> class O2(Modern, O):
... def __of__(*a):
... return (O2.inheritedAttribute('__of__')(*a),
... O2.inheritedAttribute('x')(a[0]))
>>> o2 = O2()
>>> c.o2 = o2
>>> int(c.o2 == ((o2, c), 42))
1
"""
def test_class_creation_under_stress():
"""
>>> for i in range(100):
... class B(Base):
... print i,
... if i and i%20 == 0:
... print
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
>>> import gc
>>> x = gc.collect()
"""
def old_test_add():
"""test_add.py from old EC
>>> class foo(Base):
... def __add__(self,other): print 'add called'
>>> foo()+foo()
add called
"""
def proper_error_on_deleattr():
"""
Florent Guillaume wrote:
...
Excellent.
Will it also fix this particularity of ExtensionClass:
>>> class A(Base):
... def foo(self):
... self.gee
... def bar(self):
... del self.gee
>>> a=A()
>>> a.foo()
Traceback (most recent call last):
...
AttributeError: gee
>>> a.bar()
Traceback (most recent call last):
...
AttributeError: 'A' object has no attribute 'gee'
I.e., the fact that KeyError is raised whereas a normal class would
raise AttributeError.
"""
def test_NoInstanceDictionaryBase():
"""
>>> class B(NoInstanceDictionaryBase): pass
...
>>> B().__dict__
Traceback (most recent call last):
...
AttributeError: This object has no __dict__
>>> class B(NoInstanceDictionaryBase):
... __slots__ = ('a', 'b')
...
>>> class BB(B): pass
...
>>> b = BB()
>>> b.__dict__
Traceback (most recent call last):
...
AttributeError: This object has no __dict__
>>> b.a = 1
>>> b.b = 2
>>> b.a
1
>>> b.b
2
"""
def test__basicnew__():
"""
>>> x = Simple.__basicnew__()
>>> x.__dict__
{}
"""
def cmpattrs(self, other, *attrs):
for attr in attrs:
if attr[:3] in ('_v_', '_p_'):
continue
c = cmp(getattr(self, attr, None), getattr(other, attr, None))
if c:
return c
return 0
class Simple(Base):
def __init__(self, name, **kw):
self.__name__ = name
self.__dict__.update(kw)
self._v_favorite_color = 'blue'
self._p_foo = 'bar'
def __cmp__(self, other):
return cmpattrs(self, other, '__class__', *(self.__dict__.keys()))
def test_basic_pickling():
"""
>>> x = Simple('x', aaa=1, bbb='foo')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> print_dict(x.__getstate__())
{'__name__': 'x', 'aaa': 1, 'bbb': 'foo'}
>>> f, (c,), state = x.__reduce__()
>>> f.__name__
'__newobj__'
>>> f.__module__
'copy_reg'
>>> c.__name__
'Simple'
>>> print_dict(state)
{'__name__': 'x', 'aaa': 1, 'bbb': 'foo'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.__setstate__({'z': 1})
>>> x.__dict__
{'z': 1}
"""
class Custom(Simple):
def __new__(cls, x, y):
r = Base.__new__(cls)
r.x, r.y = x, y
return r
def __init__(self, x, y):
self.a = 42
def __getnewargs__(self):
return self.x, self.y
def __getstate__(self):
return self.a
def __setstate__(self, a):
self.a = a
def test_pickling_w_overrides():
"""
>>> x = Custom('x', 'y')
>>> x.a = 99
>>> (f, (c, ax, ay), a) = x.__reduce__()
>>> f.__name__
'__newobj__'
>>> f.__module__
'copy_reg'
>>> c.__name__
'Custom'
>>> ax, ay, a
('x', 'y', 99)
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
class Slotted(Base):
__slots__ = 's1', 's2', '_p_splat', '_v_eek'
def __init__(self, s1, s2):
self.s1, self.s2 = s1, s2
self._v_eek = 1
self._p_splat = 2
class SubSlotted(Slotted):
__slots__ = 's3', 's4'
def __init__(self, s1, s2, s3):
Slotted.__init__(self, s1, s2)
self.s3 = s3
def __cmp__(self, other):
return cmpattrs(self, other, '__class__', 's1', 's2', 's3', 's4')
def test_pickling_w_slots_only():
"""
>>> x = SubSlotted('x', 'y', 'z')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> d, s = x.__getstate__()
>>> d
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.s4 = 'spam'
>>> d, s = x.__getstate__()
>>> d
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
class SubSubSlotted(SubSlotted):
def __init__(self, s1, s2, s3, **kw):
SubSlotted.__init__(self, s1, s2, s3)
self.__dict__.update(kw)
self._v_favorite_color = 'blue'
self._p_foo = 'bar'
def __cmp__(self, other):
return cmpattrs(self, other,
'__class__', 's1', 's2', 's3', 's4',
*(self.__dict__.keys()))
def test_pickling_w_slots():
"""
>>> x = SubSubSlotted('x', 'y', 'z', aaa=1, bbb='foo')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> d, s = x.__getstate__()
>>> print_dict(d)
{'aaa': 1, 'bbb': 'foo'}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.s4 = 'spam'
>>> d, s = x.__getstate__()
>>> print_dict(d)
{'aaa': 1, 'bbb': 'foo'}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
def test_pickling_w_slots_w_empty_dict():
"""
>>> x = SubSubSlotted('x', 'y', 'z')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> d, s = x.__getstate__()
>>> print_dict(d)
{}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.s4 = 'spam'
>>> d, s = x.__getstate__()
>>> print_dict(d)
{}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
def test_setattr_on_extension_type():
"""
>>> for name in 'x', '_x', 'x_', '__x_y__', '___x__', '__x___', '_x_':
... setattr(Base, name, 1)
... print getattr(Base, name)
... delattr(Base, name)
... print getattr(Base, name, 0)
1
0
1
0
1
0
1
0
1
0
1
0
1
0
>>> Base.__foo__ = 1
Traceback (most recent call last):
...
TypeError: can't set attributes of built-in/extension type """ \
"""'ExtensionClass.Base' if the attribute name begins """ \
"""and ends with __ and contains only 4 _ characters
>>> Base.__foo__
Traceback (most recent call last):
...
AttributeError: type object 'ExtensionClass.Base' """ \
"""has no attribute '__foo__'
>>> del Base.__foo__
Traceback (most recent call last):
...
TypeError: can't set attributes of built-in/extension type """ \
"""'ExtensionClass.Base' if the attribute name begins """ \
"""and ends with __ and contains only 4 _ characters
"""
def test_mro():
"""ExtensionClass method-resolution order
The EC MRO is chosen to maximize backward compatibility and
provide a model that is easy to reason about. The basic idea is:
I'll call this the "encapsulated base" scheme.
Consider:
>>> class X(Base):
... pass
>>> class Y(Base):
... pass
>>> class Z(Base):
... pass
>>> class C(X, Y, Z):
... def foo(self):
... return 42
When we look up an attribute, we do the following:
- Look in C's dictionary first.
- Look up the attribute in X. We don't care how we get the
attribute from X. If X is a new-style-class, we use the new
algorithm. If X is a classic class, we use left-to-right
depth-first. If X is an nsEC, use the "encapsulated base"
algorithm.
If we don't find the attribute in X, look in Y and then in Z,
using the same approach.
This algorithm will produce backward compatible results, providing
the equivalent of left-to-right depth-first for nsECs and classic
classes.
We'll actually do something less abstract. We'll use a simple
algorthm to merge the __mro__ of the base classes, computing an
__mro__ for classic classes using the left-to-right depth-first
algorithm. We'll basically lay the mros end-to-end left-to-right
and remove repeats, keeping the first occurence of each class.
>>> [c.__name__ for c in C.__mro__]
['C', 'X', 'Y', 'Z', 'Base', 'object']
For backward-compatability's sake, we actually depart from the
above description a bit. We always put Base and object last in the
mro, as shown in the example above. The primary reason for this is
that object provides a do-nothing __init__ method. It is common
practice to mix a C-implemented base class that implements a few
methods with a Python class that implements those methods and
others. The idea is that the C implementation overrides selected
methods in C, so the C subclass is listed first. Unfortunately,
because all extension classes are required to subclass Base, and
thus, object, the C subclass brings along the __init__ object
from objects, which would hide any __init__ method provided by the
Python mix-in.
Base and object are special in that they are implied by their meta
classes. For example, a new-style class always has object as an
ancestor, even if it isn't listed as a base:
>>> class O:
... __metaclass__ = type
>>> [c.__name__ for c in O.__bases__]
['object']
>>> [c.__name__ for c in O.__mro__]
['O', 'object']
Similarly, Base is always an ancestor of an extension class:
>>> class E:
... __metaclass__ = ExtensionClass
>>> [c.__name__ for c in E.__bases__]
['Base']
>>> [c.__name__ for c in E.__mro__]
['E', 'Base', 'object']
Base and object are generally added soley to get a particular meta
class. They aren't used to provide application functionality and
really shouldn't be considered when reasoning about where
attributes come from. They do provide some useful default
functionality and should be included at the end of the mro.
Here are more examples:
>>> from ExtensionClass import Base
>>> class NA(object):
... pass
>>> class NB(NA):
... pass
>>> class NC(NA):
... pass
>>> class ND(NB, NC):
... pass
>>> [c.__name__ for c in ND.__mro__]
['ND', 'NB', 'NC', 'NA', 'object']
>>> class EA(Base):
... pass
>>> class EB(EA):
... pass
>>> class EC(EA):
... pass
>>> class ED(EB, EC):
... pass
>>> [c.__name__ for c in ED.__mro__]
['ED', 'EB', 'EA', 'EC', 'Base', 'object']
>>> class EE(ED, ND):
... pass
>>> [c.__name__ for c in EE.__mro__]
['EE', 'ED', 'EB', 'EA', 'EC', 'ND', 'NB', 'NC', 'NA', 'Base', 'object']
>>> class EF(ND, ED):
... pass
>>> [c.__name__ for c in EF.__mro__]
['EF', 'ND', 'NB', 'NC', 'NA', 'ED', 'EB', 'EA', 'EC', 'Base', 'object']
>>> class CA:
... pass
>>> class CB(CA):
... pass
>>> class CC(CA):
... pass
>>> class CD(CB, CC):
... pass
>>> class ECD(Base, CD):
... pass
>>> [c.__name__ for c in ECD.__mro__]
['ECD', 'CD', 'CB', 'CA', 'CC', 'Base', 'object']
>>> class CDE(CD, Base):
... pass
>>> [c.__name__ for c in CDE.__mro__]
['CDE', 'CD', 'CB', 'CA', 'CC', 'Base', 'object']
>>> class CEND(CD, ED, ND):
... pass
>>> [c.__name__ for c in CEND.__mro__]
['CEND', 'CD', 'CB', 'CA', 'CC', """ \
"""'ED', 'EB', 'EA', 'EC', 'ND', 'NB', 'NC', 'NA', 'Base', 'object']
"""
def test_avoiding___init__decoy_w_inheritedAttribute():
"""
>>> class Decoy(Base):
... pass
>>> class B(Base):
... def __init__(self, a, b):
... print '__init__', a, b
>>> class C(Decoy, B):
... def __init__(self):
... print 'C init'
... C.inheritedAttribute('__init__')(self, 1, 2)
>>> x = C()
C init
__init__ 1 2
"""
def test_of_not_called_when_not_accessed_through_EC_instance():
"""
>>> class Eek(Base):
... def __of__(self, parent):
... return self, parent
If I define an EC instance as an attr of an ordinary class:
>>> class O(object):
... eek = Eek()
>>> class C:
... eek = Eek()
I get the instance, without calling __of__, when I get it from
either tha class:
>>> O.eek is O.__dict__['eek']
True
>>> C.eek is C.__dict__['eek']
True
or an instance of the class:
>>> O().eek is O.__dict__['eek']
True
>>> C().eek is C.__dict__['eek']
True
If I define an EC instance as an attr of an extension class:
>>> class E(Base):
... eek = Eek()
I get the instance, without calling __of__, when I get it from
tha class:
>>> E.eek is E.__dict__['eek']
True
But __of__ is called if I go through the instance:
>>> e = E()
>>> e.eek == (E.__dict__['eek'], e)
True
"""
def test_inheriting___doc__():
"""Old-style ExtensionClass inherited __doc__ from base classes.
>>> class E(Base):
... "eek"
>>> class EE(E):
... pass
>>> EE.__doc__
'eek'
>>> EE().__doc__
'eek'
"""
def test___of___w_metaclass_instance():
"""When looking for extension class instances, need to handle meta classes
>>> class C(Base):
... pass
>>> class O(Base):
... def __of__(self, parent):
... print '__of__ called on an O'
>>> class M(ExtensionClass):
... pass
>>> class X:
... __metaclass__ = M
...
>>> class S(X, O):
... pass
>>> c = C()
>>> c.s = S()
>>> c.s
__of__ called on an O
"""
def test___of__set_after_creation():
"""We may need to set __of__ after a class is created.
Normally, in a class's __init__, the initialization code checks for
an __of__ method and, if it isn't already set, sets __get__.
If a class is persistent and loaded from the database, we want
this to happen in __setstate__. The pmc_init_of function allws us
to do that.
We'll create an extension class without a __of__. We'll also give
it a special meta class, just to make sure that this works with
funny metaclasses too:
>>> import ExtensionClass
>>> class M(ExtensionClass.ExtensionClass):
... "A meta class"
>>> class B(ExtensionClass.Base):
... __metaclass__ = M
... def __init__(self, name):
... self.name = name
... def __repr__(self):
... return self.name
>>> B.__class__ is M
True
>>> x = B('x')
>>> x.y = B('y')
>>> x.y
y
We define a __of__ method for B after the fact:
>>> def __of__(self, other):
... print '__of__(%r, %r)' % (self, other)
... return self
>>> B.__of__ = __of__
We see that this has no effect:
>>> x.y
y
Until we use pmc_init_of:
>>> ExtensionClass.pmc_init_of(B)
>>> x.y
__of__(y, x)
y
Note that there is no harm in calling pmc_init_of multiple times:
>>> ExtensionClass.pmc_init_of(B)
>>> ExtensionClass.pmc_init_of(B)
>>> ExtensionClass.pmc_init_of(B)
>>> x.y
__of__(y, x)
y
If we remove __of__, we'll go back to the behavior we had before:
>>> del B.__of__
>>> ExtensionClass.pmc_init_of(B)
>>> x.y
y
"""
def test_Basic_gc():
"""Test to make sure that EC instances participate in GC
>>> from ExtensionClass import Base
>>> import gc
>>> class C1(Base):
... pass
...
>>> class C2(Base):
... def __del__(self):
... print 'removed'
...
>>> a=C1()
>>> a.b = C1()
>>> a.b.a = a
>>> a.b.c = C2()
>>> thresholds = gc.get_threshold()
>>> gc.set_threshold(0)
>>> ignore = gc.collect()
>>> del a
>>> ignored = gc.collect()
removed
>>> ignored > 0
True
>>> gc.set_threshold(*thresholds)
"""
from zope.testing.doctest import DocTestSuite
import unittest
def test_suite():
return unittest.TestSuite((
DocTestSuite('ExtensionClass'),
DocTestSuite(),
))
if __name__ == '__main__': unittest.main()
|