|
7 | 7 | from ._compat import exec_, iteritems, isclass, iterkeys |
8 | 8 | from .exceptions import FrozenInstanceError |
9 | 9 |
|
| 10 | +# This is used at least twice, so cache it here. |
| 11 | +_obj_setattr = object.__setattr__ |
| 12 | + |
10 | 13 |
|
11 | 14 | class _Nothing(object): |
12 | 15 | """ |
@@ -414,7 +417,7 @@ def _add_init(cls, frozen): |
414 | 417 | if frozen is True: |
415 | 418 | # Save the lookup overhead in __init__ if we need to circumvent |
416 | 419 | # immutability. |
417 | | - globs["_cached_setattr"] = object.__setattr__ |
| 420 | + globs["_cached_setattr"] = _obj_setattr |
418 | 421 | exec_(bytecode, globs, locs) |
419 | 422 | init = locs["__init__"] |
420 | 423 |
|
@@ -596,17 +599,19 @@ class Attribute(object): |
596 | 599 |
|
597 | 600 | _optional = {"convert": None} |
598 | 601 |
|
599 | | - def __init__(self, **kw): |
600 | | - if len(kw) > len(Attribute.__slots__): |
601 | | - raise TypeError("Too many arguments.") |
602 | | - for a in Attribute.__slots__: |
603 | | - try: |
604 | | - object.__setattr__(self, a, kw[a]) |
605 | | - except KeyError: |
606 | | - if a in Attribute._optional: |
607 | | - object.__setattr__(self, a, self._optional[a]) |
608 | | - else: |
609 | | - raise TypeError("Missing argument '{arg}'.".format(arg=a)) |
| 602 | + def __init__(self, name, default, validator, repr, cmp, hash, init, |
| 603 | + convert=None): |
| 604 | + # Cache this descriptor here to speed things up later. |
| 605 | + __bound_setattr = _obj_setattr.__get__(self, Attribute) |
| 606 | + |
| 607 | + __bound_setattr('name', name) |
| 608 | + __bound_setattr('default', default) |
| 609 | + __bound_setattr('validator', validator) |
| 610 | + __bound_setattr('repr', repr) |
| 611 | + __bound_setattr('cmp', cmp) |
| 612 | + __bound_setattr('hash', hash) |
| 613 | + __bound_setattr('init', init) |
| 614 | + __bound_setattr('convert', convert) |
610 | 615 |
|
611 | 616 | def __setattr__(self, name, value): |
612 | 617 | raise FrozenInstanceError() |
|
0 commit comments