This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Method for cell objects to access contents
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jhylton Nosy List: arigo, georg.brandl, jhylton, paulcannon, tim.peters
Priority: normal Keywords: patch

Created on 2005-03-25 03:40 by paulcannon, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-cellvalue.patch paulcannon, 2005-03-25 03:40 Patch to create a value() method for cell objects
Messages (5)
msg48053 - (view) Author: paul cannon (paulcannon) Date: 2005-03-25 03:40
Having poked around a little bit, I found it's not
simple to get at the contents of a cell object from
Python. It's not the sort of thing that one needs to be
doing very frequently, but I've run into a few
situations recently where it would be really useful
from a debugging standpoint.

You can get at a cell object containing a given value
by making a quick closure and looking at the
func_closure attribute:

  (lambda x: lambda: x)(some_value).func_closure[0]

but there's not anything we can easily do with that
object to find out what it's pointing at. The str()
representation only tells us the id of the contained value.

This trivial patch creates a "value()" method for cell
objects that returns a new reference to the contained
object, for introspection purposes.

I should mention it's not the only way to accomplish
this; you can also get at the contents of a cell by
creating a new function from a code object and
manufacturing its func_closures tuple from the cell you
already have:

def get_cell_value(cell):
    return type(lambda: 0)(
        (lambda x: lambda: x)(0).func_code, {}, None,
None, (cell,)
    )()

..but that's non-obvious and not particularly convenient.
msg48054 - (view) Author: paul cannon (paulcannon) Date: 2005-03-25 03:42
Logged In: YES 
user_id=222090

I should mention the patch applies against both 2.4 and the
latest 2.5 from CVS.
msg48055 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-04-03 14:43
Logged In: YES 
user_id=4771

I guess this can be safely sneaked in 2.5, as cells are quite undocumented internal objects anyway.  I believe it would be more natural to access the value as an attribute "c.value", though.  (Possibly even a read-write attribute?)

Probably not in 2.4, though; there is a no-new-feature policy.
msg48056 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-03-18 08:01
Logged In: YES 
user_id=849994

Added a "cell_contents" attribute to cell objects in rev. 43131.
msg48057 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-03-18 18:35
Logged In: YES 
user_id=31435

Jeremy, I vaguely recall that you deliberately made cell
objects non-inspectable.  Is that right?  If so and you
still care about that, note that it's no longer true.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41756
2005-03-25 03:40:42paulcannoncreate