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 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
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.
Try using simplejson.
Diez
> 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.
This recipe should get you most of what you need:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
HTH
Michael
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.
I can't help but wonder, do you often find yourself having to locate an
object from its hexadecimal address?
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.