"metaclass conflict" error: where is noconflict ?

Hi,

I have a class derived from two parents (in blue below), which gives me the following error:

$ python -u ./failover_pickle_demo09.py
Traceback (most recent call last):
File “./failover_pickle_demo09.py”, line 291, in
class ListControl(wx.Frame, CopyAndPaste):
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Googling suggested I should add

from noconflict import classmaker

and

metaclass=classmaker()

to this class.

However, I don’t seem able to find where to get the noconflict module from.

Do any of you where noconflict could be downloaded/installed from ?

Thanks,

Ron.

P.S.: I use Windows XP/cygwin, Python 2.5.2, wx-2.8-msw-unicode

Ron, Googling just now, I found this:

http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-20-sect-17.html

.
You’ll see "Having saved all the code from this recipe’s Solution
into noconflict.py somewhere along your Python sys.path ..."therein.

Bob

···

At 08:01 AM 2/19/2009, Barak, Ron wrote:

Content-Language: en-US

Content-Type: multipart/alternative;

boundary=“000_7F0503CD69378F49BE0DC30661C6CCF60E68800Fenbmail01lsicom

Hi,

I have a class derived from two parents (in blue below), which gives me
the following error:

$ python -u ./failover_pickle_demo09.py

Traceback (most recent call last):

File “./failover_pickle_demo09.py”, line 291, in

class
ListControl(wx.Frame,
CopyAndPaste):

TypeError: Error when calling the metaclass bases

metaclass conflict: the metaclass of a derived class

must be a (non-strict) subclass of the metaclasses of all its bases

Googling suggested I should add

from noconflict import classmaker

and

metaclass=classmaker()

to this class.

However, I don’t seem able to find where to get the
noconflict module from.

Do any of you where noconflict
could be downloaded/installed from ?

Thanks,

Ron.

P.S.: I use Windows XP/cygwin, Python 2.5.2,
wx-2.8-msw-unicode


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org


http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

No virus found in this incoming message.

Checked by AVG -
www.avg.com

Version: 8.0.237 / Virus Database: 270.10.25/1958 - Release Date:
02/18/09 08:57:00

Barak, Ron wrote:

Hi,

I have a class derived from two parents (in blue below), which gives me
the following error:

$ python -u ./failover_pickle_demo09.py
Traceback (most recent call last):
  File "./failover_pickle_demo09.py", line 291, in <module>
    class ListControl(wx.Frame, CopyAndPaste):
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases
Googling suggested I should add

from noconflict import classmaker
and

__metaclass__=classmaker()
to this class.

However, I don't seem able to find where to get the noconflict module from.

Do any of you where noconflict could be downloaded/installed from ?

http://lmgtfy.com/?q=noconflict+python+module&l=1

regards
Steve

···

--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Hi Chris,

From: chris@rebertia.com [mailto:chris@rebertia.com] On
Behalf Of Chris Rebert
Sent: Thursday, February 19, 2009 22:58
To: Barak, Ron
Cc: python-list@python.org; wxpython-users@lists.wxwidgets.org
Subject: Re: “metaclass conflict” error: where is noconflict ?

Hi,

I have a class derived from two parents (in blue below),
which gives
me the following error:

$ python -u ./failover_pickle_demo09.py Traceback (most recent call
last):
File “./failover_pickle_demo09.py”, line 291, in
class ListControl(wx.Frame, CopyAndPaste):
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases Googling
suggested I should add from noconflict import classmaker and

metaclass=classmaker()
to this class.

However, I don’t seem able to find where to get the
noconflict module from.

Do any of you where noconflict could be downloaded/installed from ?

From what I could google, you should in theory be able to fix
the problem (without using any 3rd party module) by doing:

class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
pass

class ListControl(wx.Frame, CopyAndPaste):
metaclass = ListControlMeta
#rest of class…

Cheers,
Chris

Applying your suggestion:

class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
pass

class ListControl(wx.Frame, CopyAndPaste):
def init(self, parent, id, title, list, max_list_width, log_stream, style=wx.DEFAULT_FRAME_STYLE):

    __metaclass__= ListControlMeta

    wx.Frame.__init__(self,parent,id,title,size=(max_list_width,-1), style=style)
    self.list = list
    self.log_stream = log_stream
    self.list_ctrl = wx.ListCtrl(self, -1, style=wx.LC_REPORT | wx.LC_NO_HEADER)
    self.list_ctrl.InsertColumn(0, title)
    for i,line_ in enumerate(list):
        self.list_ctrl.InsertStringItem(i, line_)
            ...

I get:

$ python -u ./failover_pickle_demo09.py
Traceback (most recent call last):
File “./failover_pickle_demo09.py”, line 319, in
class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict

So, back to trying to understand…

Thanks and bye,
Ron.

···

-----Original Message-----
On Thu, Feb 19, 2009 at 5:01 AM, Barak, Ron Ron.Barak@lsi.com wrote:


Follow the path of the Iguana…
http://rebertia.com

Hi Chris,

From: Chris Rebert [mailto:clp2@rebertia.com]
Sent: Sunday, February 22, 2009 11:48
To: Barak, Ron
Cc: python-list@python.org; wxpython-users@lists.wxwidgets.org
Subject: Re: "metaclass conflict" error: where is noconflict ?

> Hi Chris,
>
>> From: chris@rebertia.com [
> mailto:chris@rebertia.com] On
>> Behalf Of Chris Rebert
>> Sent: Thursday, February 19, 2009 22:58
>> To: Barak, Ron
>> Cc: python-list@python.org; wxpython-users@lists.wxwidgets.org
>> Subject: Re: "metaclass conflict" error: where is noconflict ?
>>
>> > Hi,
>> >
>> > I have a class derived from two parents (in blue below),
>> which gives
>> > me the following error:
>> >
>> > $ python -u ./failover_pickle_demo09.py Traceback (most
recent call
>> > last):
>> > File "./failover_pickle_demo09.py", line 291, in <module>
>> > class ListControl(wx.Frame, CopyAndPaste):
>> > TypeError: Error when calling the metaclass bases
>> > metaclass conflict: the metaclass of a derived class
must be a
>> > (non-strict) subclass of the metaclasses of all its
bases Googling
>> > suggested I should add from noconflict import classmaker and
>> >
>> > __metaclass__=classmaker()
>> > to this class.
>> >
>> > However, I don't seem able to find where to get the
>> noconflict module from.
>> >
>> > Do any of you where noconflict could be
downloaded/installed from ?
>>
>> From what I could google, you should in theory be able to fix the
>> problem (without using any 3rd party module) by doing:
>>
>> class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
>> pass
>>
>> class ListControl(wx.Frame, CopyAndPaste):
>> __metaclass__ = ListControlMeta
>> #rest of class...
>>
>> Cheers,
>> Chris
>
> Applying your suggestion:
>
> class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
> pass
>
> class ListControl(wx.Frame, CopyAndPaste):
> def __init__(self, parent, id, title, list, max_list_width,
> log_stream,
> style=wx.DEFAULT_FRAME_STYLE):
>
> __metaclass__= ListControlMeta
>
>
> wx.Frame.__init__(self,parent,id,title,size=(max_list_width,-1),
> style=style)
> self.list = list
> self.log_stream = log_stream
> self.list_ctrl = wx.ListCtrl(self, -1, style=wx.LC_REPORT |
> wx.LC_NO_HEADER)
> self.list_ctrl.InsertColumn(0, title)
> for i,line_ in enumerate(list):
> self.list_ctrl.InsertStringItem(i, line_)
> ...
>
> I get:
>
> $ python -u ./failover_pickle_demo09.py Traceback (most recent call
> last):
> File "./failover_pickle_demo09.py", line 319, in <module>
> class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
> TypeError: Error when calling the metaclass bases
> multiple bases have instance lay-out conflict

>From what I recall, that basically means that type(wx.Frame) and
type(CopyAndPaste) are both C classes that are are mutually
incompatible. It's basically the same reason you can't
subclass from both `list` and `dict` or two other built-in
types (you get the exact same error).

Sounds like the only way to workaround this would be to do
some coding in C or to use composition rather than
inheritance for one of ListControl's superclasses.

The wx.Frame may be coded in C, but the CopyAndPaste class, which I wrote, is not (see it's listing below).
Could you have a look at the CopyAndPaste class, and see if something in its construction strikes you as susspicious ?

Thanks,
Ron.

$ cat ./CopyAndPaste.py
#!/usr/bin/env python

import wx

class CopyAndPaste():
    def __init__(self):
        pass

    def set_copy_and_paste(self):
        """
        Setting clipboard copy-and-pasting (only copying from the application to the
        clipboard is supported).
        The "menu" menu is hidded, and is only there to facilitate the acceleration table.
        Both CTRL-C and CTRL-Ins are supported.
        """
        #print line()+". set_copy_and_paste started"
        #print line()+". self.__dict__:",self.__dict__
        menu = wx.Menu()
        copy_ = menu.Append(-1, "&Copy\tCtrl-Ins") # Copy with accelerator
        minimize_ = menu.Append(-1, "Minimize") #
        close_ = menu.Append(-1, "Close window\tCtrl-W") # Close window
        exit_ = menu.Append(-1, "E&xit application\tCtrl-X") # Close window
        """
        #copy2_ = menu.Append(-1, "&Copy\tCtrl-C") # Copy with accelerator
        paste_ = menu.Append(-1, "&Paste\tShift-Ins") # Paste with accelerator
        paste2_ = menu.Append(-1, "&Paste\tCtrl-V") # Paste with accelerator
        """
        
        self.Bind(wx.EVT_MENU, self.on_copy, copy_)
        self.Bind(wx.EVT_MENU, self.on_minimize, minimize_)
        self.Bind(wx.EVT_MENU, self.on_close, close_)
        self.Bind(wx.EVT_MENU, self.on_exit, exit_)
        #self.Bind(wx.EVT_MENU, self.on_paste, paste_)
                  
        menuBar = wx.MenuBar()
        self.SetMenuBar(menuBar)

        acceltbl = wx.AcceleratorTable( [
                (wx.ACCEL_CTRL, ord('C'), copy_.GetId()),
                (wx.ACCEL_CTRL, ord('W'), close_.GetId()),
                (wx.ACCEL_CTRL, ord('X'), exit_.GetId()),
                (wx.ACCEL_CTRL, ord('Q'), exit_.GetId()),
                (wx.ACCEL_CTRL, wx.WXK_INSERT, copy_.GetId()),
                (wx.ACCEL_CTRL, wx.WXK_NUMPAD_INSERT, copy_.GetId()),
                #(wx.ACCEL_CTRL, ord('V'), paste_.GetId()),
                #(wx.ACCEL_SHIFT, wx.WXK_INSERT, paste_.GetId()),
            ])
        self.SetAcceleratorTable(acceltbl)

        # Setting popup menu

        self.popupmenu = menu
        self.Bind(wx.EVT_CONTEXT_MENU, self.on_show_popup)

    """
    def on_paste(self, evt):
        wx.MessageBox("You selected 'paste'")
        """

    def on_show_popup(self, evt):
        pos = evt.GetPosition()
        pos = self.list_ctrl.ScreenToClient(pos)
        self.PopupMenu(self.popupmenu, pos)

    def get_data_for_clipboard(self,format="text"):
        """
        Return data ready to be copied to the clipboard.

        This is an abstract method - concrete subclasses must override this.

        Sample implementation of get_data_for_clipboard() is:

        def get_data_for_clipboard(self,format="text"):
            first_selected = self.list_ctrl.GetFirstSelected()
            selected_item_count = self.list_ctrl.GetSelectedItemCount()
            text_for_clipboard = ""
            for i in range(first_selected,first_selected+selected_item_count):
                text_for_clipboard = "%s%s\n" % (text_for_clipboard, self.list_ctrl.GetItemText(i))
            return(text_for_clipboard)
        """
        raise NotImplementedError

    def on_copy(self, evt):
        """
        """
        text_for_clipboard = self.get_data_for_clipboard()

        data = wx.TextDataObject()
        data.SetText(text_for_clipboard)
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(data)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox("Unable to copy to the clipboard", "Error")

    def on_minimize(self, evt):
        self.Iconize()
        #self.parent.Iconize()

    def on_close(self, evt):
        self.Close()
        #self.parent.Close()

    def on_exit(self, evt):
        try:
            self.Parent.Close()
        except AttributeError:
            self.Close()

if __name__ == "__main__":

    app = wx.App(redirect=False)
    copy_and_paste = CopyAndPaste()
    app.MainLoop()

···

-----Original Message-----
On Sun, Feb 22, 2009 at 1:37 AM, Barak, Ron <Ron.Barak@lsi.com> wrote:
>> -----Original Message-----
>> On Thu, Feb 19, 2009 at 5:01 AM, Barak, Ron > <Ron.Barak@lsi.com> wrote:

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

Hi Michele,

I tried understanding how to avoid the metaclasses clashing, and I did read the link you referred to below,
as well as the relevant O’Reilly cookbook chapter (Recipe 20.17. Solving Metaclass Conflicts), but seems I do not understand the correct syntax to use, as I either get

$ python -u ./failover_pickle_demo09.py
Traceback (most recent call last):
File “./failover_pickle_demo09.py”, line 323, in
class ListControlMeta(wx.Frame, CopyAndPaste):
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

or

$ python -u ./failover_pickle_demo09.py
Traceback (most recent call last):
File “./failover_pickle_demo09.py”, line 322, in
class ListControlMeta(type(wx.Frame), type(CopyAndPaste)):
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict

And, mind you, all I wanted to do was inherit from two classes: wxPython’s wx.Frame and my CopyAndPaste.

Namely -

I have a subclass of wxPython’s wx.Frame called ListControl.
I wanted to add copy (from copy-and-paste) capabilities to this ListControl class.

I changed the header of this class to be:

class ListControl(wx.Frame, CopyAndPaste):

But, the addition of CopyAndPaste to the class parents got me into this quagmire of metaclasses.

Bye,
Ron.

···

-----Original Message-----
From: Michele Simionato [mailto:michele.simionato@gmail.com]
Sent: Friday, February 20, 2009 07:23
To: python-list@python.org
Subject: Re: “metaclass conflict” error: where is noconflict ?

On Feb 19, 9:58 pm, Chris Rebert c...@rebertia.com wrote:

On Thu, Feb 19, 2009 at 5:01 AM, Barak, Ron > Ron.Ba...@lsi.com wrote:

Hi,

I have a class derived from two parents (in blue below),
which gives

me the following error:

$ python -u ./failover_pickle_demo09.py Traceback (most
recent call

last):
File “./failover_pickle_demo09.py”, line 291, in
class ListControl(wx.Frame, CopyAndPaste):
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases
Googling

suggested I should add from noconflict import classmaker and

metaclass=classmaker()
to this class.

However, I don’t seem able to find where to get the
noconflict module from.

Do any of you where noconflict could be
downloaded/installed from ?

It refers to this cookbook recipe:
http://code.activestate.com/recipes/204197/

See also the printed cookbook:

http://books.google.it/books?id=1Shx_VXS6ioC&pg=PA786&lpg=PA78
6&dq=python+cookbook+noconflict&source=bl&ots=BB413AZ8R7&sig=cnAB-E9rNFolHBEZQTIm_d4Mj3o&hl=it&ei=GT2eSfCtBdWa_gadppHYCw&sa=X&oi=book_result&resnum=2&ct=result#PPA786,M1

However, there is no module to download, and this is on purpose:
instead of blindly apply a “fix” one should understand what the conflict is: then it is quite easy to solve it by hand. The recipe discussion explains it all.

Hi Ron,

And, mind you, all I wanted to do was inherit from two classes: wxPython's
wx.Frame and my CopyAndPaste.

Namely -

I have a subclass of wxPython's wx.Frame called ListControl.
I wanted to add copy (from copy-and-paste) capabilities to this ListControl
class.

I changed the header of this class to be:

class ListControl(wx.Frame, CopyAndPaste):

But, the addition of CopyAndPaste to the class parents got me into this
quagmire of metaclasses.

This should work:

class CopyAndPaste(object):
    def __init__(self, anArgumentForCopyAndPaste, *args, **kwargs):
        self.myData = anArgumentForCopyAndPaste
        super(CopyAndPaste, self).__init__(*args, **kwargs)

    def ...

class ListControl(CopyAndPaste, wx.Frame):
    def __init__(self, parent):
        copyAndPasteArg = 'whatever'
        super(ListControl, self).__init__(copyAndPasteArg, parent,
style=wx.FRAME_FLOAT_ON_PARENT)

    def ...

ListControl.__init__ will call CopyAndPaste.__init__ which will call
wx.Frame.__init__ in turn. Args and kwargs will contain parent and
style. Printing args and kwargs may help seeing what is going on.

Cheers, Frank

···

2009/2/22 Barak, Ron <Ron.Barak@lsi.com>:

Hi Frank,

From: Frank Niessink [mailto:frank@niessink.com]
Sent: Sunday, February 22, 2009 16:26
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] How to inherit from two classes
without metaclass clashing ?

Hi Ron,

And, mind you, all I wanted to do was inherit from two classes:
wxPython’s wx.Frame and my CopyAndPaste.

Namely -

I have a subclass of wxPython’s wx.Frame called ListControl.
I wanted to add copy (from copy-and-paste) capabilities to this
ListControl class.

I changed the header of this class to be:

class ListControl(wx.Frame, CopyAndPaste):

But, the addition of CopyAndPaste to the class parents got me into
this quagmire of metaclasses.

This should work:

class CopyAndPaste(object):
def init(self, anArgumentForCopyAndPaste, *args, **kwargs):
self.myData = anArgumentForCopyAndPaste
super(CopyAndPaste, self).init(*args, **kwargs)

def ...

class ListControl(CopyAndPaste, wx.Frame):
def init(self, parent):
copyAndPasteArg = ‘whatever’
super(ListControl, self).init(copyAndPasteArg, parent,
style=wx.FRAME_FLOAT_ON_PARENT)

def ...

ListControl.init will call CopyAndPaste.init which
will call wx.Frame.init in turn. Args and kwargs will
contain parent and style. Printing args and kwargs may help
seeing what is going on.

I probably don’t understand you completely, especially regarding the sentence: “Args and kwargs will contain parent and style”.
Namely, my ListControl header looks now like so,

class ListControl(wx.Frame, CopyAndPaste):

def __init__(self, parent, id, title, list, max_list_width, log_stream, style=wx.DEFAULT_FRAME_STYLE):

    ...

And I don’t see how it could be changed to def init(self, parent) without loosing functionality. What am I missing ?

Anyway, with my incomplete (and faulty?) understanding, I tried to implement your suggestions, like so:

class ListControl(wx.Frame, CopyAndPaste):

def __init__(self, parent, id, title, list, max_list_width, log_stream, style=wx.DEFAULT_FRAME_STYLE):

    super(ListControl, self).__init__(self,parent,id,title,size=(max_list_width,-1), style=style)
            self.list = list
    self.log_stream = log_stream
    self.list_ctrl = wx.ListCtrl(self, -1, style=wx.LC_REPORT | wx.LC_NO_HEADER)
    self.list_ctrl.InsertColumn(0, title)
    for i,line_ in enumerate(list):
        self.list_ctrl.InsertStringItem(i, line_)

and above script calls the CopyAndPaste module:

class CopyAndPaste(object):
def init(self, *args, **kwargs):
super(CopyAndPaste, self).init(*args, **kwargs)

But, I am still getting the infamous exception:

$ python -u failover_pickle_demo09.py
Traceback (most recent call last):
File “failover_pickle_demo09.py”, line 329, in
class ListControl(wx.Frame, CopyAndPaste):
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Is there a way I can ask a class what its metaclasses are ?

Bye,
Ron.

···

-----Original Message-----
2009/2/22 Barak, Ron Ron.Barak@lsi.com:

Cheers, Frank

Hi Ron,

···

2009/2/22 Barak, Ron <Ron.Barak@lsi.com>:

I probably don't understand you completely, especially regarding the
sentence: "Args and kwargs will contain parent and style".
Namely, my ListControl header looks now like so,

class ListControl(wx.Frame, CopyAndPaste):

You need to switch the order of the base classes: class
ListControl(CopyAndPaste, wx.Frame)

Cheers, Frank