Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

interp.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Python.Interpreter COM Server
  2. This module implements a very very simple COM server which
  3. exposes the Python interpreter.
  4. This is designed more as a demonstration than a full blown COM server.
  5. General functionality and Error handling are both limited.
  6. To use this object, ensure it is registered by running this module
  7. from Python.exe. Then, from Visual Basic, use "CreateObject('Python.Interpreter')",
  8. and call its methods!
  9. """
  10. import winerror
  11. from win32com.server.exception import Exception
  12. # Expose the Python interpreter.
  13. class Interpreter:
  14. """The interpreter object exposed via COM"""
  15. _public_methods_ = ["Exec", "Eval"]
  16. # All registration stuff to support fully automatic register/unregister
  17. _reg_verprogid_ = "Python.Interpreter.2"
  18. _reg_progid_ = "Python.Interpreter"
  19. _reg_desc_ = "Python Interpreter"
  20. _reg_clsid_ = "{30BD3490-2632-11cf-AD5B-524153480001}"
  21. _reg_class_spec_ = "win32com.servers.interp.Interpreter"
  22. def __init__(self):
  23. self.dict = {}
  24. def Eval(self, exp):
  25. """Evaluate an expression."""
  26. if type(exp) != str:
  27. raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
  28. return eval(str(exp), self.dict)
  29. def Exec(self, exp):
  30. """Execute a statement."""
  31. if type(exp) != str:
  32. raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
  33. exec(str(exp), self.dict)
  34. def Register():
  35. import win32com.server.register
  36. return win32com.server.register.UseCommandLine(Interpreter)
  37. if __name__ == "__main__":
  38. print("Registering COM server...")
  39. Register()