Google Группы больше не поддерживают новые публикации и подписки в сети Usenet. Опубликованный ранее контент останется доступен.

Convert obejct string repr to actual object

8 просмотров
Перейти к первому непрочитанному сообщению

Tor Erik Sønvisen

не прочитано,
8 окт. 2007 г., 15:19:5008.10.2007
– 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

не прочитано,
8 окт. 2007 г., 15:40:4908.10.2007
– 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

не прочитано,
8 окт. 2007 г., 15:42:2308.10.2007
– 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

не прочитано,
8 окт. 2007 г., 17:14:0808.10.2007
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

не прочитано,
8 окт. 2007 г., 18:47:2308.10.2007
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

не прочитано,
8 окт. 2007 г., 20:20:5008.10.2007
– 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

не прочитано,
12 окт. 2007 г., 12:41:1912.10.2007
– 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

не прочитано,
12 окт. 2007 г., 13:14:1112.10.2007
– 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

не прочитано,
15 окт. 2007 г., 11:48:2715.10.2007
– 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 новых сообщений