problems with eval()
Brian Forney
bforney at cs.wisc.edu
Fri Apr 27 01:02:01 EDT 2001
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
I cannot figure out why this is happening. If I print out the eval() of the
compiled code, I see "None" displayed. So, the eval() is not returning what I
would expect.
I'm pretty much at wit's end with this. (Maybe I am too sleep deprived to
see the real problem. :) I would like to know what I am doing
wrong, but I am ready to abandon this. If anyone would be kind to tell me
what I am doing wrong, I would greatly appreciate any help.
Here's the smallest piece of Python code that reproduces the problem:
--------------------
#!/s/std/bin/env python
import copy;
import re;
class SubstituteClass:
""" """
__path = None;
__newStr = None;
def __init__(self, path):
self.__path = copy.deepcopy(path);
def subFN(self, matchObj):
expr = matchObj.group('expr');
compObj = compile(expr, '<string>', 'exec');
return eval(compObj, {'path':self.__path});
FO = open("test.txt", "r");
str = FO.read();
reo = re.compile("\<(?P<expr>.+?)\>");
subClass = SubstituteClass("somePath/thatIsNotBogus.config");
reo.sub(subClass.subFN, str);
--------------------
Here's the file test.txt that is read in by the Python code:
--------------------
error = results/cache/<import re; re.sub("config", "barnes.error",
path);>
--------------------
Notice the embedded Python code in test.txt.
Thanks,
Brian
-----
www.cs.wisc.edu/~bforney
ubi caritas et amor
More information about the Python-list
mailing list