problems with eval()

Michael Hudson mwh21 at cam.ac.uk
Fri Apr 27 03:32:10 EDT 2001


Brian Forney <bforney at cs.wisc.edu> writes:

> Hi,
> 
> I am trying to use eval in Python 2.0. I am trying to use reo.sub() and a
> method that compiles and eval()'s some code from a text file to perform
> flexible replacements. Unfortunately, I see a TypeError in string.py:
> 
> Traceback (most recent call last):
>   File "test.py", line 27, in ?
>     reo.sub(subClass.subFN, str);
>   File "/s/python-2.0.0//lib/python2.0/sre.py", line 115, in _sub
>     return _subn(pattern, template, string, count)[0]
>   File "/s/python-2.0.0//lib/python2.0/sre.py", line 140, in _subn
>     return _join(s, string[:0]), n
>   File "/s/python-2.0.0//lib/python2.0/sre.py", line 89, in _join
>     return string.join(seq, sep[:0])
>   File "/s/python-2.0.0//lib/python2.0/string.py", line 129, in join
>     return sep.join(words)
> TypeError: sequence item 1: expected string, None found
 
[schnipp]

>     def subFN(self, matchObj):
> 	expr = matchObj.group('expr');
> 	compObj = compile(expr, '<string>', 'exec');

When you compile a piece of code to exec, it returns None.

> 	return eval(compObj, {'path':self.__path});

so this returns None, which causes your problems.

Try compiling with "single" instead.  Not sure this will work, but it
might (depending what code you're trying to execute).

Cheers,
M.

-- 
 The rapid establishment of social ties, even of a fleeting nature,
 advance not only that goal but its standing in the uberconscious
 mesh of communal psychic, subjective, and algorithmic interbeing.
 But I fear I'm restating the obvious.  -- Will Ware, comp.lang.python



More information about the Python-list mailing list