Skip to content

Implementing __getattr__ breaks copy and deepcopy #63563

Closed

Description

@KassymDorsel
BPO 19364
Nosy @avassalotti, @ericsnowcurrently
Superseder
  • bpo-16251: pickle special methods are looked up on the instance rather than the type
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-10-25.02:39:12.112>
    created_at = <Date 2013-10-23.13:49:43.084>
    labels = ['type-bug', 'library']
    title = 'Implementing __getattr__ breaks copy and deepcopy'
    updated_at = <Date 2013-10-25.02:39:12.110>
    user = 'https://bugs.python.org/KassymDorsel'

    bugs.python.org fields:

    activity = <Date 2013-10-25.02:39:12.110>
    actor = 'eric.snow'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-10-25.02:39:12.112>
    closer = 'eric.snow'
    components = ['Library (Lib)']
    creation = <Date 2013-10-23.13:49:43.084>
    creator = 'Kassym.Dorsel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 19364
    keywords = []
    message_count = 4.0
    messages = ['201024', '201089', '201200', '201201']
    nosy_count = 3.0
    nosy_names = ['alexandre.vassalotti', 'eric.snow', 'Kassym.Dorsel']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '16251'
    type = 'behavior'
    url = 'https://bugs.python.org/issue19364'
    versions = ['Python 2.7']

    Activity

    1. KassymDorsel commented on Oct 23, 2013

      KassymDorselmannequin
      MannequinAuthor

      When __getattr__ is implemented without also implementing __copy__ and __deepcopy__ trying to (deep)copy the class fails.

      >>> import copy
      >>> class foo():
      ...   def __getattr__(self, attr):
      ...     return None
      ...
      >>> f = foo()
      >>> copy(f)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: 'module' object is not callable

      The copy module checks if a class has implemented __copy__ using hasattr:

      if hasattr(x, '__copy__'):
        ...

      An easy fix would be to use:

      if getattr(x, '__copy__', None):
        ...

      In Python 3 this change has already been made.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Oct 23, 2013
    3. ericsnowcurrently commented on Oct 24, 2013

      @ericsnowcurrently
      Member

      Your example worked correctly, since the copy module is indeed not copiable. :) Did you mean "from copy import copy"?

    4. KassymDorsel commented on Oct 25, 2013

      KassymDorselmannequin
      MannequinAuthor

      Yes. You're correct. Sorry for the confusion. Below is an updated snippet of code.

      >>> from copy import copy
      >>> class foo():
      ...   def __getattr__(self, attr):
      ...     return None
      ... 
      >>> f = foo()
      >>> copy(f)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 76, in copy
          return copier(x)
        File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 125, in _copy_inst
          return x.__copy__()
      TypeError: 'NoneType' object is not callable
    5. ericsnowcurrently commented on Oct 25, 2013

      @ericsnowcurrently
      Member

      I knew this sounded familiar. Take a look at bpo-16251.

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions