clist has a bug that limits how a clist instance can be used.

Bug #319792 reported by Ray C Horn
2
Affects Status Importance Assigned to Milestone
pyax
Fix Committed
Medium
Kevin Shuk

Bug Description

clist uses the following method however __repr__ methods must take care to use some method other than the str() method when producing a representation of a clist because the method below causes stack overflow problems at runtime.

    def __repr__(self):
        return "clist(%s)" %str(self)

A better method would be to represent the clist as a list of items rather than as a representation of the whole list.

This bug affects how clist instances get handled by a method that needs to represent the clist - this does not happen often however when it does happen the method above fails.

The reason is str() uses __repr__() - this is common for all Python objects that seek to use str() to form a representation of an object where the item being rendered by str() is the object itself.

Revision history for this message
Ray C Horn (mrpython94596) wrote :

Try this instead:

    def __repr__(self):
        return "clist(%s)" % (','.join([str(item) for item in self]))

This method fails whenever any items in a clist happens to be a clist instance.

Revision history for this message
Kevin Shuk (surf) wrote :

Thank you for the report & suggested fix. I did not realise the bit about str() using __repr__() - learn something new every day!

My plan is to come up with a test case that shows the broken behavior, then apply your suggested fix and merge into trunk as soon as possible.

Changed in pyax:
assignee: nobody → surf
importance: Undecided → Medium
status: New → In Progress
Revision history for this message
Ray C Horn (mrpython94596) wrote : Re: [Bug 319792] Re: clist has a bug that limits how a clist instance canbe used.

I have run into the str() and __repr__() problem many times while coding my
huge (125,000+ lines) library of reusable code and I have had to code around
it by ensuring I never issue a str() during a __repr__() when the result can
be recursive.

See also the following from a quick Google search:

[Python-3000] PEP: str(container) should call str(item), not repr ... -->
http://mail.python.org/pipermail/python-3000/2008-May/013876.html

http://www.google.com/search?rlz=1C1GGLS_enUS291US304&sourceid=chrome&ie=UTF-8&q=str+and+__repr__+for+python

Enjoy. :-)

--------------------------------------------------
From: "Kevin Shuk" <email address hidden>
Sent: Thursday, January 22, 2009 2:37 PM
To: <email address hidden>
Subject: [Bug 319792] Re: clist has a bug that limits how a clist instance
canbe used.

> Thank you for the report & suggested fix. I did not realise the bit
> about str() using __repr__() - learn something new every day!
>
> My plan is to come up with a test case that shows the broken behavior,
> then apply your suggested fix and merge into trunk as soon as possible.
>
> ** Changed in: pyax
> Importance: Undecided => Medium
> Assignee: (unassigned) => Kevin Shuk (surf)
> Status: New => In Progress
>
> --
> clist has a bug that limits how a clist instance can be used.
> https://bugs.launchpad.net/bugs/319792
> You received this bug notification because you are a direct subscriber
> of the bug.
>
> Status in Salesforce.com Apex API library for Python: In Progress
>
> Bug description:
> clist uses the following method however __repr__ methods must take care to
> use some method other than the str() method when producing a
> representation of a clist because the method below causes stack overflow
> problems at runtime.
>
>
> def __repr__(self):
> return "clist(%s)" %str(self)
>
> A better method would be to represent the clist as a list of items rather
> than as a representation of the whole list.
>
> This bug affects how clist instances get handled by a method that needs to
> represent the clist - this does not happen often however when it does
> happen the method above fails.
>
> The reason is str() uses __repr__() - this is common for all Python
> objects that seek to use str() to form a representation of an object where
> the item being rendered by str() is the object itself.
>

Revision history for this message
Kevin Shuk (surf) wrote :

Committed to trunk rev 63

Changed in pyax:
status: In Progress → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.