Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert obejct string repr to actual object

8 views
Skip to first unread message

Tor Erik Sønvisen

unread,
Oct 8, 2007, 3:19:50 PM10/8/07
to pytho...@python.org
Hi,

I've tried locating some code that can recreate an object from it's
string representation...
The object in question is really a dictionary containing other
dictionaries, lists, unicode strings, floats, ints, None, and
booleans.

I don't want to use eval, since I can't trust the source sending the
object.... I'm sure someone must have had the same need and created
code for it... Maybe Pypy has what I need??? Haven't looked though...

Regards,
Tor Erik

PS: The string repr is created by a server outside of my control...

Carsten Haese

unread,
Oct 8, 2007, 3:40:49 PM10/8/07
to pytho...@python.org
On Mon, 2007-10-08 at 21:19 +0200, Tor Erik Sønvisen wrote:
> Hi,
>
> I've tried locating some code that can recreate an object from it's
> string representation...
> The object in question is really a dictionary containing other
> dictionaries, lists, unicode strings, floats, ints, None, and
> booleans.
>
> I don't want to use eval, since I can't trust the source sending the
> object.

You could still use eval, but run a separate algorithm first to make
sure the string is "legal." For example, you could whip up a simple
PyParsing grammar to restrict the set of allowable strings, or compile
the string into byte code and inspect the byte code to look for red
flags like LOAD_NAME (with a name other than None) and CALL_FUNCTION.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net


Chris Mellon

unread,
Oct 8, 2007, 3:42:23 PM10/8/07
to Tor Erik Sønvisen, pytho...@python.org
On 10/8/07, Tor Erik Sønvisen <tore...@gmail.com> wrote:
> Hi,
>
> I've tried locating some code that can recreate an object from it's
> string representation...
> The object in question is really a dictionary containing other
> dictionaries, lists, unicode strings, floats, ints, None, and
> booleans.
>
> I don't want to use eval, since I can't trust the source sending the
> object.... I'm sure someone must have had the same need and created
> code for it... Maybe Pypy has what I need??? Haven't looked though...
>
> Regards,
> Tor Erik
>
> PS: The string repr is created by a server outside of my control...
> --


You'll need to write your own parser. PyParsing has an example that
can parse much of the Python syntax, you can probably extract the
object-literal parts of that and use it as a base for your
implementation.

Diez B. Roggisch

unread,
Oct 8, 2007, 5:14:08 PM10/8/07
to
Tor Erik Sønvisen schrieb:

> Hi,
>
> I've tried locating some code that can recreate an object from it's
> string representation...
> The object in question is really a dictionary containing other
> dictionaries, lists, unicode strings, floats, ints, None, and
> booleans.
>
> I don't want to use eval, since I can't trust the source sending the
> object.... I'm sure someone must have had the same need and created
> code for it... Maybe Pypy has what I need??? Haven't looked though...


Try using simplejson.

Diez

Steven D'Aprano

unread,
Oct 8, 2007, 6:47:23 PM10/8/07
to
On Mon, 08 Oct 2007 21:19:50 +0200, Tor Erik Sønvisen
wrote:

> I don't want to use eval, since I can't trust the source sending the


> object.... I'm sure someone must have had the same need and created code
> for it... Maybe Pypy has what I need??? Haven't looked though...


For the benefit of those who think they can make eval "safe", start here:

http://effbot.org/zone/librarybook-core-eval.htm


Here's a good solution:

http://effbot.org/zone/simple-iterator-parser.htm

--
Steven.

Michael Spencer

unread,
Oct 8, 2007, 8:20:50 PM10/8/07
to pytho...@python.org
Tor Erik Sønvisen wrote:
> Hi,
>
> I've tried locating some code that can recreate an object from it's
> string representation...
> The object in question is really a dictionary containing other
> dictionaries, lists, unicode strings, floats, ints, None, and
> booleans.
>
> I don't want to use eval, since I can't trust the source sending the
> object.... I'm sure someone must have had the same need and created
> code for it... Maybe Pypy has what I need??? Haven't looked though...
>
> Regards,
> Tor Erik
>
> PS: The string repr is created by a server outside of my control...

This recipe should get you most of what you need:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469

HTH

Michael

English, Mark

unread,
Oct 12, 2007, 12:41:19 PM10/12/07
to pytho...@python.org
> From: Tor Erik Sønvisen
> Date: October 8th 2007

> I've tried locating some code that can recreate an object from
> it's string representation...
On a related note I've wondered about this:
>>> class Foo(object): pass
>>> f = Foo()
>>> s = repr(f)
>>> s
'<__main__.Foo object at 0x007CBAB0>'

So how do I get f back from s ?
Obviously this is open to abuse. I just wondered if a mechanism existed short of writing
a C-extension which parses that string, casts the hex number to a PyObject *, INCREFs it,
and gives it back...
______________________________________________________________________

This email is intended only for the use of the individual(s) to whom it is addressed and may be privileged and confidential.
Unauthorised use or disclosure is prohibited.If you receive This e-mail in error, please advise immediately and delete the original message.
This message may have been altered without your or our knowledge and the sender does not accept any liability for any errors or omissions in the message.

Carsten Haese

unread,
Oct 12, 2007, 1:14:11 PM10/12/07
to pytho...@python.org
On Fri, 2007-10-12 at 17:41 +0100, English, Mark wrote:
> > From: Tor Erik Sønvisen
> > Date: October 8th 2007
> > I've tried locating some code that can recreate an object from
> > it's string representation...
> On a related note I've wondered about this:
> >>> class Foo(object): pass
> >>> f = Foo()
> >>> s = repr(f)
> >>> s
> '<__main__.Foo object at 0x007CBAB0>'
>
> So how do I get f back from s ?
> Obviously this is open to abuse. I just wondered if a mechanism existed short of writing
> a C-extension which parses that string, casts the hex number to a PyObject *, INCREFs it,
> and gives it back...

I can't help but wonder, do you often find yourself having to locate an
object from its hexadecimal address?

English, Mark

unread,
Oct 15, 2007, 11:48:27 AM10/15/07
to pytho...@python.org
X-Replace-Address: mark.ignoreth...@xxxrbccmxxx.ignorethisbittoo_and_removethosepreceding_xxxs_.com

On 12 Oct, 18:14, Carsten Haese <cars...@uniqsys.com> wrote:
> On Fri, 2007-10-12 at 17:41 +0100, English, Mark wrote:
> > > From: Tor Erik Sønvisen
> > > Date: October 8th 2007
> > > I've tried locating some code that can recreate an object from
> > > it's string representation...
> > On a related note I've wondered about this:
> > >>> class Foo(object): pass
> > >>> f = Foo()
> > >>> s = repr(f)
> > >>> s
> > '<__main__.Foo object at 0x007CBAB0>'
> > So how do I get f back from s ?
>
> I can't help but wonder, do you often find yourself having to locate an
> object from its hexadecimal address?
Never. This just didn't feel particularly repr-ish to me:
"For many types, this function makes an attempt to return a string that would yield an
object with the same value when passed to eval()"

I was just asking in case I was missing some mechanism which made new style class
instances fall under the heading of "many types" in this context.

0 new messages