| 317 | #endif // Python 3 |
| 318 | |
| 319 | static void namd_python_initialize(void *interp) { |
| 320 | if ( static_interp ) return; |
| 321 | static_interp = (Tcl_Interp*) interp; |
| 322 | |
| 323 | #if PY_MAJOR_VERSION >= 3 |
| 324 | PyImport_AppendInittab("tcl", &PyInit_tcl); |
| 325 | PyImport_AppendInittab("namd", &PyInit_namd); |
| 326 | Py_InitializeEx(0); // do not initialize signal handlers |
| 327 | #else |
| 328 | Py_InitializeEx(0); // do not initialize signal handlers |
| 329 | Py_InitModule("tcl", namdPython_methods); |
| 330 | Py_InitModule("namd", namdPython_methods_empty); |
| 331 | #endif |
| 332 | |
| 333 | const char * python_code = "\n" |
| 334 | "import sys\n" |
| 335 | "import tcl\n" |
| 336 | "sys.stdout = tcl\n" |
| 337 | "\n" |
| 338 | "class _namd_wrapper(object):\n" |
| 339 | " tcl = __import__('tcl')\n" |
| 340 | " class _wrapped(object):\n" |
| 341 | " def __init__(self,_name):\n" |
| 342 | " self.name = _name\n" |
| 343 | " def __call__(self,*args):\n" |
| 344 | " return self.tcl.call(self.name,*args)\n" |
| 345 | " def __getattr__(self,name):\n" |
| 346 | " if self.tcl.call('info','commands',name) == name:\n" |
| 347 | " return self._wrapped(name)\n" |
| 348 | " else:\n" |
| 349 | " return self.tcl.call('param',name)\n" |
| 350 | " def __setattr__(self,name,val):\n" |
| 351 | " if self.tcl.call('info','commands',name) == name:\n" |
| 352 | " raise AttributeError\n" |
| 353 | " return self.tcl.call('param',name,val)\n" |
| 354 | " def __call__(self, **args):\n" |
| 355 | " for (name,val) in args.items():\n" |
| 356 | " self.tcl.call('param',name,val)\n" |
| 357 | "\n" |
| 358 | "sys.modules[__name__] = _namd_wrapper()\n" |
| 359 | "\n"; |
| 360 | |
| 361 | PyObject* mainmod = PyImport_AddModule("__main__"); |
| 362 | PyObject* globalDictionary = PyModule_GetDict(mainmod); |
| 363 | PyObject* namdmod = PyImport_AddModule("namd"); |
| 364 | PyObject* localDictionary = PyModule_GetDict(namdmod); |
| 365 | PyObject* result = PyRun_String(python_code, Py_file_input, globalDictionary, localDictionary); |
| 366 | |
| 367 | if ( 0 != PyRun_SimpleString("import tcl\nimport namd\n") ) { |
| 368 | NAMD_bug("namd_python_initialize failed"); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | int ScriptTcl::Tcl_python(ClientData, Tcl_Interp *interp, int argc, const char **argv) { |
| 373 | if ( argc < 2 ) { |