0001 """Python.Interpreter COM Server 0002 0003 This module implements a very very simple COM server which 0004 exposes the Python interpreter. 0005 0006 This is designed more as a demonstration than a full blown COM server. 0007 General functionality and Error handling are both limited. 0008 0009 To use this object, ensure it is registered by running this module 0010 from Python.exe. Then, from Visual Basic, use "CreateObject('Python.Interpreter')", 0011 and call its methods! 0012 """ 0013 0014 from win32com.server.exception import Exception 0015 from pywintypes import UnicodeType 0016 import winerror 0017 0018 # Expose the Python interpreter. 0019 class Interpreter: 0020 """The interpreter object exposed via COM 0021 """ 0022 _public_methods_ = [ 'Exec', 'Eval' ] 0023 # All registration stuff to support fully automatic register/unregister 0024 _reg_verprogid_ = "Python.Interpreter.2" 0025 _reg_progid_ = "Python.Interpreter" 0026 _reg_desc_ = "Python Interpreter" 0027 _reg_clsid_ = "{30BD3490-2632-11cf-AD5B-524153480001}" 0028 _reg_class_spec_ = "win32com.servers.interp.Interpreter" 0029 0030 def __init__(self): 0031 self.dict = {} 0032 0033 def Eval(self, exp): 0034 """Evaluate an expression. 0035 """ 0036 if type(exp) not in [type(''),UnicodeType]: 0037 raise Exception(desc="Must be a string",scode=winerror.DISP_E_TYPEMISMATCH) 0038 0039 return eval(str(exp), self.dict) 0040 def Exec(self, exp): 0041 """Execute a statement. 0042 """ 0043 if type(exp) not in [type(''), UnicodeType]: 0044 raise Exception(desc="Must be a string",scode=winerror.DISP_E_TYPEMISMATCH) 0045 exec str(exp) in self.dict 0046 0047 def Register(): 0048 import win32com.server.register 0049 return win32com.server.register.UseCommandLine(Interpreter) 0050 0051 if __name__=='__main__': 0052 print "Registering COM server..." 0053 Register() 0054
Generated by PyXR 0.9.4