Building an extension that calls the Python interpreter recursively.
Parzival Herzog
parz at shaw.SpamBucket.ca
Wed Apr 10 12:45:53 EDT 2002
I am planning to build a Python extension module for the
ML/I macro processor. I am not too experienced at
building Python extensions, and I have some questions
about the design of this extension. I would appreciate
any comments.
ML/I is a text processor, reading a stream of input text
and producing (zero or more) streams of output text.
During ML/I's processing, I want ML/I's macro language
to be able to evaluate Python statements and expressions,
the purpose being 1) to compute strings not easily
computable with ML/I's macro language; 2) to access
external information via Python's facilities; and 3) to
record information parsed by ML/I into Python data
structures.
What I plan to do is to implement an extension type
whose Python prototype would look something like this:
class ML1:
def __init(self,...)
...
def SetInputStream(self, stream):
''' record file-like object representing the input stream'''
def SetErrorStream(self, stream):
''' record file-like object representing the error output stream'''
def AddOutputStream(self, stream):
''' record an additional file-like object representing an output
stream'''
def process(self, globals,locals)
'''ML/I will consume input, and produce output,
evaluating Python expressions in the context of
globals, locals.
'''
Now during process(), ML/I will compute strings S representing
Python expressions or statements, and evaluate them with
PyRun_String(S,type,globals,locals)
I would like to know if this scheme is feasible, i.e.
1) Can the Python interpreter be re-invoked from an
extension module?
2) Will it process its evaluation of S in the same
interpreter context that originally invoked ML1.process?
I.e. will results left by PyRun_String be accessible
after process() returns?
3) Are there gotchas or constraints on the global
interpreter lock that I should be aware of under these
circumstances?
Thanks,
Parzival
More information about the Python-list
mailing list