Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7435

Re: Recursion error in metaclass

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'passes': 0.05; 'figures': 0.07; 'terry': 0.07; 'python': 0.08; 'assert': 0.09; 'dict': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'pm,': 0.10; 'subject:error': 0.11; 'def': 0.12; 'broken': 0.14; 'wrote:': 0.14; 'bases,': 0.16; 'carefully.': 0.16; 'discarded': 0.16; 'docstring': 0.16; 'forwards': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'mro': 0.16; 'reedy': 0.16; 'spam"': 0.16; 'endless': 0.19; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'loop': 0.22; '(or': 0.24; 'pass': 0.27; 'class': 0.29; 'it.': 0.31; 'steven': 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr :python-list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.35; "d'aprano": 0.35; 'fails': 0.35; 'skip:@ 10': 0.35; 'skip:{ 10': 0.35; 'source,': 0.35; 'test': 0.35; 'none': 0.37; 'skip:o 20': 0.37; 'received:org': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'empty': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'getting': 0.40; 'want,': 0.74; '11:34': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Recursion error in metaclass
Date Sat, 11 Jun 2011 01:33:25 -0400
References <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10
In-Reply-To <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.124.1307770420.11593.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 82.94.164.166
X-Trace 1307770420 news.xs4all.nl 49181 [::ffff:82.94.164.166]:60791
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7435

Show key headers only | View raw


On 6/10/2011 11:34 PM, Steven D'Aprano wrote:
> I have a metaclass in Python 3.1:
>
> class MC1(type):
>      @staticmethod
>      def get_mro(bases):
>          print('get_mro called')
>          return type('K', bases, {}).__mro__[1:]

The call to type figures out the proper metaclass from bases and 
forwards the call to that (or to its __new__ method). See 
Objects/typeobject.c in the source, or read the docs on metaclasses 
carefully. If the proper metaclass is MC1, ...


>      def __new__(cls, name, bases, dict):
>          mro = None
>          docstring = dict.get('__doc__')
>          if docstring == 'ham':
>              mro = cls.get_mro(bases)

and you unconditionally call get_mro again, to call this again...

>              dict['__doc__'] = "spam spam spam"
>          # Create the class we want, and return it.
>          K = super().__new__(cls, name, bases, dict)
>          if mro:
>              assert K.__mro__ == (K,) + mro
>          return K

you are in an endless loop.

Since uou do not pass dict to get_mro. it passes {} to type and MC1 and 
the test for docstring fails and the loop is broken and the empty class 
is discarded after getting its mro.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recursion error in metaclass Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-11 03:34 +0000
  Re: Recursion error in metaclass Terry Reedy <tjreedy@udel.edu> - 2011-06-11 01:33 -0400
    Re: Recursion error in metaclass Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-11 11:38 +0000
      Re: Recursion error in metaclass Terry Reedy <tjreedy@udel.edu> - 2011-06-11 15:39 -0400
      Re: Recursion error in metaclass Daniel Urban <urban.dani@gmail.com> - 2011-06-11 22:51 +0200

csiph-web