Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Nested classes
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nicolas M. Thiery  
View profile  
 More options Mar 13 2009, 4:03 am
From: "Nicolas M. Thiery" <Nicolas.Thi...@u-psud.fr>
Date: Fri, 13 Mar 2009 09:03:54 +0100
Local: Fri, Mar 13 2009 4:03 am
Subject: Nested classes
        Dear c-level python specialists,

Nested classes in python are really handy, and work pretty well,
except for a bug in pickling:

Let's define a nested class:

        sage: class Bla:                                                      
        ....:     class Ble:                                                  
        ....:         pass                                                    
        ....:

Let's fake Bla is defined in a module, not in the interpreter:

        sage: import __main__  
        sage: __main__.Bla = Bla

Dumping an element of Bla works just fine:
        sage: x = Bla()
        sage: dumps(x)
        "x\x9ck`\xd2H\x8e\x8f\xcfM\xcc\xcc\x8b\x8f\xe7r\xcaI\xe4*d\xcc/d\xaa-dN\xd2 \x03\x00v\xe3\x08'"

But dumping an element of Bla.Ble breaks:

        sage: x = Bla.Ble()
        sage: dumps(x)
        ---------------------------------------------------------------------------
        PicklingError                             Traceback (most recent call last)

        /home/nthiery/.sage/temp/zephyr/23765/_home_nthiery__sage_init_sage_0.py in <module>()

        /opt/sage/local/lib/python2.5/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.dumps (sage/structure/sage_object.c:5539)()

        PicklingError: Can't pickle __main__.Ble: attribute lookup __main__.Ble failed
        > /opt/sage-3.4.rc0/devel/sage-combinat/sage/sage_object.pyx(571)sage.structu re.sage_object.dumps (sage/structure/sage_object.c:5539)()
        ---------------------------------------------------------------------------

The reason behind this is twofold:

First, the name of the class is improperly set:

        sage: Bla.Ble
        <class __main__.Ble at 0xb1e7c98c>
        sage: Bla.Ble.__name__
        'Ble'

(which btw probably breaks other tools around)

Well, let's work around this:

        sage: Bla.Ble.__name__ = "Bla.Ble"

But then, a second problem: the code of cPickle (around line 1977)
tries to lookup the attribute "Bla.Ble" in __main__, instead of
successively looking up "Bla" in __main__, and then "Ble" in the result:

        sage: dumps(Bla.Ble)
        ---------------------------------------------------------------------------
        PicklingError                             Traceback (most recent call last)
        /home/nthiery/.sage/temp/zephyr/23765/_home_nthiery__sage_init_sage_0.py in <module>()
        /opt/sage/local/lib/python2.5/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.dumps (sage/structure/sage_object.c:5539)()
        PicklingError: Can't pickle __main__.Bla.Ble: attribute lookup __main__.Bla.Ble failed
        > /opt/sage-3.4.rc0/devel/sage-combinat/sage/sage_object.pyx(571)sage.structu re.sage_object.dumps (sage/structure/sage_object.c:5539)()

It would take me a couple hours to learn the syntax to write c-code
for python modules. Could any of the experts out there write me a
2-line patch upon cPickle:1977 to do the right thing (i.e. split
__name__, and make the attribute lookup for each bit)? Just a draft
will be fine; I am fine with reading and simple debuging.

Thanks in advance!

Best regards,
                                Nicolas

Here is the broken code:

        mod = PyImport_ImportModule(module_str);
        if (mod == NULL) {
                cPickle_ErrFormat(PicklingError,
                                  "Can't pickle %s: import of module %s "
                                  "failed",
                                  "OS", args, module);
                goto finally;
        }
        ############### HERE ####################
        klass = PyObject_GetAttrString(mod, name_str);
        if (klass == NULL) {
                cPickle_ErrFormat(PicklingError,
                                  "Can't pickle %s: attribute lookup %s.%s "
                                  "failed",
                                  "OSS", args, module, global_name);
                goto finally;
        }

--
Nicolas M. Thiéry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Witty  
View profile  
 More options Mar 13 2009, 2:37 pm
From: Carl Witty <carl.wi...@gmail.com>
Date: Fri, 13 Mar 2009 11:37:34 -0700
Local: Fri, Mar 13 2009 2:37 pm
Subject: Re: [sage-devel] Nested classes
On Fri, Mar 13, 2009 at 1:03 AM, Nicolas M. Thiery

<Nicolas.Thi...@u-psud.fr> wrote:
> But then, a second problem: the code of cPickle (around line 1977)
> tries to lookup the attribute "Bla.Ble" in __main__, instead of
> successively looking up "Bla" in __main__, and then "Ble" in the result:

May I suggest a different workaround?  (It's gross, but maybe less
gross than changing cPickle.)

sage: __main__.__dict__['Bla.Ble'] = Bla.Ble
sage: loads(dumps(x))
<__main__.Bla.Ble instance at 0x1a2e7eac>

Carl


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas M. Thiery  
View profile  
 More options Mar 13 2009, 3:55 pm
From: "Nicolas M. Thiery" <Nicolas.Thi...@u-psud.fr>
Date: Fri, 13 Mar 2009 20:55:29 +0100
Local: Fri, Mar 13 2009 3:55 pm
Subject: Re: [sage-devel] Re: Nested classes

On Fri, Mar 13, 2009 at 11:37:34AM -0700, Carl Witty wrote:

> On Fri, Mar 13, 2009 at 1:03 AM, Nicolas M. Thiery
> <Nicolas.Thi...@u-psud.fr> wrote:
> > But then, a second problem: the code of cPickle (around line 1977)
> > tries to lookup the attribute "Bla.Ble" in __main__, instead of
> > successively looking up "Bla" in __main__, and then "Ble" in the result:

> May I suggest a different workaround?  (It's gross, but maybe less
> gross than changing cPickle.)

> sage: __main__.__dict__['Bla.Ble'] = Bla.Ble
> sage: loads(dumps(x))
> <__main__.Bla.Ble instance at 0x1a2e7eac>

Well, that's a bug in python that should eventually be fixed in
python. But yeah, your workaround could be a good way out.

Thanks!

Now, you don't have by any chance a magic spell, so that whenever such
a nested class Foo.Bar is created, 'Foo.Bar' gets inserted in its
module as above? (I guess I can hack around for my application).

Cheers,
                                Nicolas
--
Nicolas M. Thiéry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Hansen  
View profile  
 More options Mar 13 2009, 4:01 pm
From: Mike Hansen <mhan...@gmail.com>
Date: Fri, 13 Mar 2009 13:01:12 -0700
Local: Fri, Mar 13 2009 4:01 pm
Subject: Re: [sage-devel] Re: Nested classes
On Fri, Mar 13, 2009 at 12:55 PM, Nicolas M. Thiery

<Nicolas.Thi...@u-psud.fr> wrote:
> Thanks!

> Now, you don't have by any chance a magic spell, so that whenever such
> a nested class Foo.Bar is created, 'Foo.Bar' gets inserted in its
> module as above? (I guess I can hack around for my application).

This is the purpose of metaclasses.  The best introduction / primer on
metaclasses that I've read is at
http://cleverdevil.org/computing/78/

--Mike


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Witty  
View profile  
 More options Mar 13 2009, 4:26 pm
From: Carl Witty <carl.wi...@gmail.com>
Date: Fri, 13 Mar 2009 13:26:11 -0700
Local: Fri, Mar 13 2009 4:26 pm
Subject: Re: [sage-devel] Re: Nested classes
On Fri, Mar 13, 2009 at 12:55 PM, Nicolas M. Thiery

I certainly wouldn't call it a bug -- at the most, a missing feature.
I'm not even convinced that a patched version would be an improvement.
 (For one thing, you would need to also patch the unpickle side, which
means that you can't write a pickle using your patch and read it in
older versions of Python.  We don't care much about that in Sage, but
I think it's important to the Python people.)

Carl


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas M. Thiery  
View profile  
 More options Mar 13 2009, 5:29 pm
From: "Nicolas M. Thiery" <Nicolas.Thi...@u-psud.fr>
Date: Fri, 13 Mar 2009 22:29:22 +0100
Local: Fri, Mar 13 2009 5:29 pm
Subject: Re: [sage-devel] Re: Nested classes
        Hi Mike!

> This is the purpose of metaclasses.  The best introduction / primer on
> metaclasses that I've read is at
> http://cleverdevil.org/computing/78/

Thanks, I'll have a look.

I am already playing with meta classes for UniqueRepresentation and
dynamic classes, and the two of them readily do not collaborate
without special attention. So as of now, I find the concept extremely
fun, but I try to keep its potential uses to a strict minimum :-)

Cheers,
                                Nicolas
--
Nicolas M. Thiéry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas M. Thiery  
View profile  
 More options May 5 2009, 3:30 am
From: "Nicolas M. Thiery" <Nicolas.Thi...@u-psud.fr>
Date: Tue, 5 May 2009 00:30:30 -0700
Local: Tues, May 5 2009 3:30 am
Subject: Re: [sage-devel] Re: Nested classes
        Dear Carl, dear all,

This is a call for comments and reviews for the new ticket #5986 whose
description is:

--------------------------------------------------------------------------- ---
With the python code below::

class A:
    class B:
        pass

Python 2.6 erroneously set B.__name__ to "B" instead of "A.B".

Furthermore, upon pickling (here in save_global) *and* unpickling (in
load_global) a class with name "A.B" in a module mod, the standard
cPickle module searches for "A.B" in mod.dict instead of looking up
"A" and then "B" in the result.

This patch works around this by a patch to cPickle.c which fixes the
name for B to its appropriate value A.B, and inserts 'A.B' = A.B in
mod.dict (hacky, but seems to work) the first time A.B is pickled, and
fixes load_global to implement a proper lookup upon unpickling.

It also ensures that sage/interfaces/sage0.py uses loads/dumps from
sage_object rather than calling directly cPickle.loads/dumps
(+1 by cwitty on this change)

Python source experts are more than welcome to rework/rewrite this
patch!

Depends on #5483 (explain-pickle) and #5985
--------------------------------------------------------------------------- ---

Thanks in advance!

Best,
                                Nicolas

On Fri, Mar 13, 2009 at 01:26:11PM -0700, Carl Witty wrote:

...

> > Well, that's a bug in python that should eventually be fixed in
> > python. But yeah, your workaround could be a good way out.

> I certainly wouldn't call it a bug -- at the most, a missing feature.
> I'm not even convinced that a patched version would be an improvement.
>  (For one thing, you would need to also patch the unpickle side, which
> means that you can't write a pickle using your patch and read it in
> older versions of Python.  We don't care much about that in Sage, but
> I think it's important to the Python people.)

Well, you can't pickle/unpickle it in the first place, so this can't
hurt more than currently.

--
Nicolas M. Thiéry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William Stein  
View profile  
 More options May 5 2009, 9:48 am
From: William Stein <wst...@gmail.com>
Date: Tue, 5 May 2009 06:48:06 -0700
Local: Tues, May 5 2009 9:48 am
Subject: Re: [sage-devel] Re: Nested classes
On Tue, May 5, 2009 at 12:30 AM, Nicolas M. Thiery

Have you posted this to the python-dev mailing list?  This patch will
definitely not go into Sage unless it will definitely go into Python
at some point in the future. What if you are simply wrong in your
assertion that Python "erroneously set B.__name__ to "B" instead of
"A.B"?"   Then you'll just be making it so Sage can never work with
any unpatched version of Python.   Note that (1) in the entire history
of the Sage project, we have never once patched the Python
interpreter, except to fix small bus we found whose fixes were
upstreamed (the only thing I can think of along those lines is a
readline bug on Itanium), and (2) if we patch Python, then Sage for
Debian/Ubuntu will never be upgraded to the version of Sage with this
patch until the patch is upstreamed into Python.

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas M. Thiery  
View profile  
 More options May 5 2009, 12:34 pm
From: "Nicolas M. Thiery" <Nicolas.Thi...@u-psud.fr>
Date: Tue, 5 May 2009 09:34:16 -0700
Local: Tues, May 5 2009 12:34 pm
Subject: Re: [sage-devel] Re: Nested classes

Not yet but this of course definitely is the plan.

(sorry, I already have trouble keeping up with my current average
patch submission of one per day).

> This patch will definitely not go into Sage unless it will
> definitely go into Python at some point in the future.  

What a nice way to start a polite negotiation between gentlemens.

(european vs american culture strikes back :-) )

> What if you are simply wrong in your assertion that Python
> "erroneously set B.__name__ to "B" instead of "A.B"?"  Then you'll
> just be making it so Sage can never work with any unpatched version
> of Python.  Note that (1) in the entire history of the Sage project,
> we have never once patched the Python interpreter, except to fix
> small bus we found whose fixes were upstreamed (the only thing I can
> think of along those lines is a readline bug on Itanium), and (2) if
> we patch Python, then Sage for Debian/Ubuntu will never be upgraded
> to the version of Sage with this patch until the patch is upstreamed
> into Python.

(1) Yes, maybe my assumption is wrong, though I strongly doubt it. In
any cases, what is for sure is that with the current version of
Python, nested classes cannot be pickled. And this is undoubtly a bug
(or missing feature if you prefer). The single purpose of my patch is
to fix this. Any other fix or workaround will do as well, and is more
than welcome.

(2) My patch does not touch the Python interpreter, but a copy of
cPickle within the Sage source tree. This does not prevent upgrading
python, as long as we keep our copy of cPickle reasonably updated. Of
course this is only acceptable as a temporary workaround.

(3) -1 on holding back further a large scale merge for a minor
technical detail that will eventually be resolved one way or the
other.

(4) My general feeling is that nested classes in Python are on the
edge of full maturity, with just a couple glitches here and
there. Typically, Florent mentionned to me that the devel version of
Sphynx now handled them correctly. This is why I feel confident using
them. And there is a clear advantage to using them (which I'll argue
about in my demo in Seattle).

Best,
                                Nicolas
--
Nicolas M. Thiéry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William Stein  
View profile  
 More options May 5 2009, 1:53 pm
From: William Stein <wst...@gmail.com>
Date: Tue, 5 May 2009 10:53:16 -0700
Local: Tues, May 5 2009 1:53 pm
Subject: Re: [sage-devel] Re: Nested classes
On Tue, May 5, 2009 at 9:34 AM, Nicolas M. Thiery

<Nicolas.Thi...@u-psud.fr> wrote:
> (2) My patch does not touch the Python interpreter, but a copy of
> cPickle within the Sage source tree. This does not prevent upgrading
> python, as long as we keep our copy of cPickle reasonably updated. Of
> course this is only acceptable as a temporary workaround.

Clever.  That's actually a very good idea, which mostly negates my
concerns.  Nonetheless, I would really like to hear from python-dev
about what *they* thinking about pickling and nested classes.  What if
there are some good reasons for the current design that  we're just
not thinking of?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas M. Thiery  
View profile  
 More options May 5 2009, 5:50 pm
From: "Nicolas M. Thiery" <Nicolas.Thi...@u-psud.fr>
Date: Tue, 5 May 2009 14:50:07 -0700
Local: Tues, May 5 2009 5:50 pm
Subject: Re: [sage-devel] Re: Nested classes

> Clever.  That's actually a very good idea, which mostly negates my
> concerns.

:-)

Credits shared with Carl and Mike!

> Nonetheless, I would really like to hear from python-dev about what
> *they* thinking about pickling and nested classes.  What if there
> are some good reasons for the current design that we're just not
> thinking of?

Definitely!

Best,
                                Nicolas
--
Nicolas M. Thiéry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »