[issue19364] Implementing __getattr__ breaks copy and deepcopy

2013-10-23 Thread Kassym Dorsel

New submission from Kassym Dorsel:

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 "", line 1, in 
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.

--
components: Library (Lib)
messages: 201024
nosy: Kassym.Dorsel, alexandre.vassalotti
priority: normal
severity: normal
status: open
title: Implementing __getattr__ breaks copy and deepcopy
type: behavior
versions: Python 2.7

___
Python tracker 
<http://bugs.python.org/issue19364>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue19364] Implementing __getattr__ breaks copy and deepcopy

2013-10-24 Thread Kassym Dorsel

Kassym Dorsel added the comment:

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 "", line 1, in 
  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

--

___
Python tracker 
<http://bugs.python.org/issue19364>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com