Memory leak ?? [resolved - thank you]
Kim Petersen
kp at kyborg.dk
Fri Jul 11 04:24:23 EDT 2003
A.M. Kuchling wrote:
> On Thu, 10 Jul 2003 14:34:05 +0200,
> Kim Petersen <kp at kyborg.dk> wrote:
>
>>Using python-2.2.2-26 on RH9 (shrike) x86 -fully patched
>>
>>The following program slowly eats up more and more memory when run on
>>large datasets... can anyone tell what the trouble is?
>
>
> Your code uses eval(), which is pretty heavyweight because it has to
> tokenize, parse, and then evaluate the string. There have been a few memory
> leaks in eval(), and perhaps you're running into one of them. Try using
> int() or float() to convert strings to numbers instead of eval. As a bonus,
> your program will be faster and much more secure (could an attacker tweak
> your logfiles so you end up eval()ing os.unlink('/etc/passwd')?).
Thank you very much - it was eval()
this solved my trouble (calling get_list instead of eval) - is there a
more generic/efficient way of solving reading a list/expression? (i know
this one will fail for some strings for instance):
def get_value(str):
str=str.strip()
if str.lower()=='none':
return None
elif str[0] in ['"',"'"]:
return str[1:-1]
else:
if str[-1]=='j':
return complex(str)
elif '.' in str or 'e' in str:
return float(str)
else:
return int(str)
def get_list(str):
try:
if str[0]=='(':
robj=tuple
else:
robj=list
items=str.strip()[1:-1].split(', ')
return robj(map(get_value,items))
except:
traceback.print_exc()
print str
return []
--
Med Venlig Hilsen / Regards
Kim Petersen - Kyborg A/S (Udvikling)
IT - Innovationshuset
Havneparken 2
7100 Vejle
Tlf. +4576408183 || Fax. +4576408188
More information about the Python-list
mailing list