Issue1634

classification
Title: sys.getrefcount not working as expected
Type: behaviour Severity: major
Components: Core Versions: 2.5.2b1
Milestone:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: thijs, zyasoft
Priority: Keywords:

Created on 2010-07-14.17:03:24 by thijs, last changed 2010-07-14.18:54:23 by zyasoft.

Messages
msg5894 (view) Author: Thijs Triemstra (thijs) Date: 2010-07-14.17:03:23
$ jython
Jython 2.5.2b1 (trunk:7078, Jul 14 2010, 18:47:14) 
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_20
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> o = object()
>>> sys.getrefcount(o)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '<reflected field public org.python.core.PyObject o' object has no attribute 'getrefcount'
>>> 
Thijs-Triemstra:~ thijstriemstra$ python
Python 2.6.5 (r265:79063, Jun 17 2010, 10:48:26) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> o = object()
>>> sys.getrefcount(o)
2
>>>

You can reproduce this also by running the PyAMF trunk test suite.
msg5896 (view) Author: Jim Baker (zyasoft) Date: 2010-07-14.18:36:33
Jython does not implement reference counting, and in general it never will.

I should note that it is certainly possible to simulate in app code, perhaps through a  canonical mapping of objects that are to be ref counted to atomic counters, which can then be incremented/decremented as desired.

We will need to do something similar once we decide to implement the C Extension API, following similar experience in PyPy. But this would be restricted to objects managed with respect to the C API.
msg5897 (view) Author: Jim Baker (zyasoft) Date: 2010-07-14.18:54:22
One could also potentially implement sys.getrefcount using JVMTI to introspect the heap:
http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/jvmti/jvmti.html#IterateOverReachableObjects

However, some big issues: it would certainly not the O(1) op one expects with sys.getrefcount(o); and JVMTI is not guaranteed to exist, and its usage certainly would violate the security model in most/all containers.

I don't know how AMF is using this, but presumably this is related to deep-copy ops, in which case the approach used by the copy module might be preferable.
History
Date User Action Args
2010-07-14 18:54:23zyasoftsetmessages: + msg5897
2010-07-14 18:36:34zyasoftsetstatus: open -> closed
resolution: wont fix
messages: + msg5896
nosy: + zyasoft
2010-07-14 17:03:25thijscreate