|
From: <bc...@wo...> - 2001-06-01 10:27:51
|
[Rich Seddon on jython-users]
>We're seeing some behavior with the copy module in
>Jython.
>
>In the following test program, __del__ is only called
>for for the original objects, never for the copied
>ones.
>
>Is this normal?
Nah, it's a bug.
>The same program (without java calls,
>of course) for CPython shows __del__ being called for
>both the original and copied objects.
The problem occur because __del__ is only called when it is defined in
the class dict at the time the class is created. This is slightly more
restrictive than CPython where a __del__ attribute can be added at any
time.
When using copy.copy() & copy.deepcopy(), the new instance is first
created as an empty dummy class and only later is the correct class
assigned to the instance. Because the empty dummy class didn't define a
__del__ method, the method will not be called even when a __del__ method
is acquired later.
The patch below should be usefull as a workaround.
regards,
finn
--- I:\Python21\Lib\copy.py Sat Jan 20 16:47:18 2001
+++ i:\java\jython.CVS\Lib\copy.py Fri Jun 01 10:04:43 2001
@@ -121,7 +121,10 @@
args = x.__getinitargs__()
y = apply(x.__class__, args)
else:
- y = _EmptyClass()
+ if hasattr(x.__class__, '__del__'):
+ y = _EmptyClassDel()
+ else:
+ y = _EmptyClass()
y.__class__ = x.__class__
if hasattr(x, '__getstate__'):
state = x.__getstate__()
@@ -237,7 +240,10 @@
args = deepcopy(args, memo)
y = apply(x.__class__, args)
else:
- y = _EmptyClass()
+ if hasattr(x.__class__, '__del__'):
+ y = _EmptyClassDel()
+ else:
+ y = _EmptyClass()
y.__class__ = x.__class__
memo[id(x)] = y
if hasattr(x, '__getstate__'):
@@ -260,6 +266,12 @@
# Helper for instance creation without calling __init__
class _EmptyClass:
pass
+
+# Helper for instance creation without calling __init__. Used when
+# the source class contains a __del__ attribute.
+class _EmptyClassDel:
+ def __del__(self):
+ pass
def _test():
l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'],
|