/client/sources-linux/mktab.py

https://github.com/AlessandroZ/pupy · Python · 119 lines · 110 code · 5 blank · 4 comment · 8 complexity · 803dc53d610653f1aab2a350e044bcdd MD5 · raw file

  1. # A script to generate helper files for dynamic linking to the Python dll
  2. #
  3. decls = '''
  4. void, Py_Initialize, (void)
  5. void, Py_InitializeEx, (int)
  6. int, PyRun_SimpleString, (char *)
  7. void, Py_Finalize, (void)
  8. char *, Py_GetPath, (void)
  9. void, PySys_SetPath, (char *)
  10. void, Py_SetPythonHome, (char *)
  11. void, Py_SetProgramName, (char *)
  12. PyObject *, PyMarshal_ReadObjectFromString, (char *, Py_ssize_t)
  13. PyObject *, PyObject_CallFunction, (PyObject *, char *, ...)
  14. int, PyString_AsStringAndSize, (PyObject *, char **, Py_ssize_t *)
  15. char *, PyString_AsString, (PyObject *)
  16. int, PyArg_ParseTuple, (PyObject *, char *, ...)
  17. PyObject *, PyErr_Format, (PyObject *, const char *, ...)
  18. PyObject *, PyImport_ImportModule, (char *)
  19. PyObject *, PyInt_FromLong, (long)
  20. long, PyInt_AsLong, (PyObject *)
  21. PyObject *, PyLong_FromVoidPtr, (void *)
  22. PyObject *, Py_InitModule4, (char *, PyMethodDef *, char *, PyObject *, int)
  23. PyObject *, PyTuple_New, (Py_ssize_t)
  24. int, PyTuple_SetItem, (PyObject*, Py_ssize_t, PyObject *)
  25. int, Py_IsInitialized, (void)
  26. int, PyObject_SetAttrString, (PyObject *, char *, PyObject *)
  27. PyObject *, PyCFunction_NewEx, (PyMethodDef *, PyObject *, PyObject *)
  28. PyObject *, PyObject_GetAttrString, (PyObject *, char *)
  29. PyObject *, Py_BuildValue, (char *, ...)
  30. PyObject *, PyObject_Call, (PyObject *, PyObject *, PyObject *)
  31. void, PySys_WriteStderr, (const char *, ...)
  32. PyObject *, PyErr_Occurred, (void)
  33. void, PyErr_Fetch, (PyObject **, PyObject **, PyObject **)
  34. void, PyErr_Clear, (void)
  35. int, PyObject_IsInstance, (PyObject *, PyObject *)
  36. PyObject, PyInt_Type
  37. PyObject, _Py_NoneStruct
  38. PyObject, _Py_ZeroStruct
  39. PyObject *, PyExc_ImportError
  40. PyObject *, PyExc_Exception
  41. char *, _Py_PackageContext
  42. PyGILState_STATE, PyGILState_Ensure, (void)
  43. void, PyGILState_Release, (PyGILState_STATE)
  44. void, PySys_SetObject, (char *, PyObject *)
  45. PyObject *, PySys_GetObject, (char *)
  46. PyObject *, PyString_FromString, (char *)
  47. int, Py_FdIsInteractive, (FILE *, char *)
  48. int, PyRun_InteractiveLoop, (FILE *, char *)
  49. void, PySys_SetArgv, (int, char **)
  50. PyObject *, PyImport_AddModule, (char *)
  51. PyObject *, PyModule_GetDict, (PyObject *)
  52. Py_ssize_t, PySequence_Length, (PyObject *)
  53. PyObject *, PySequence_GetItem, (PyObject *, Py_ssize_t)
  54. //int, PyCode_Check, (PyObject *)
  55. PyObject *, PyEval_EvalCode, (PyCodeObject *, PyObject *, PyObject *)
  56. void, PyErr_Print, (void)
  57. PyObject *, PyBool_FromLong, (long)
  58. int, Py_VerboseFlag
  59. int, Py_NoSiteFlag
  60. int, Py_OptimizeFlag
  61. int, Py_IgnoreEnvironmentFlag
  62. PyObject *, PyObject_Str, (PyObject *)
  63. PyObject *, PyList_New, (Py_ssize_t)
  64. int, PyList_SetItem, (PyObject *, Py_ssize_t, PyObject *)
  65. int, PyList_Append, (PyObject *, PyObject *)
  66. PyObject *, PyThreadState_GetDict, (void)
  67. int, PyObject_IsTrue, (PyObject *)
  68. void, PyErr_SetString, (PyObject *, const char *)
  69. void, PyEval_InitThreads, (void)
  70. void, PySys_SetArgvEx, (int, char **, int)
  71. PyObject *, PyFile_FromFile, (FILE *fp, char *name, char *mode, int (*close)(FILE*))
  72. void, PyFile_SetBufSize, (PyObject *, int)
  73. PyObject *, PyErr_NewException, (char *name, PyObject *base, PyObject *dict)
  74. int, PyModule_AddObject, (PyObject *, const char *, PyObject *)
  75. int, PyModule_AddStringConstant, (PyObject *module, const char *name, const char *value)
  76. '''.strip().splitlines()
  77. import string
  78. hfile = open("import-tab.h", "w")
  79. cfile = open("import-tab.c", "w")
  80. index = 0
  81. for decl in decls:
  82. if not decl or decl.startswith("//"):
  83. continue
  84. items = decl.split(',', 2)
  85. if len(items) == 3:
  86. # exported function with argument list
  87. restype, name, argtypes = map(string.strip, items)
  88. print >> hfile, '#define %(name)s ((%(restype)s(*)%(argtypes)s)imports[%(index)d].proc)' % locals()
  89. elif len(items) == 2:
  90. # exported data
  91. typ, name = map(string.strip, items)
  92. print >> hfile, '#define %(name)s (*(%(typ)s(*))imports[%(index)s].proc)' % locals()
  93. else:
  94. raise ValueError, "could not parse %r" % decl
  95. if name == "Py_InitModule4":
  96. print >> cfile, '#ifdef _DEBUG'
  97. print >> cfile, '\t{ "Py_InitModule4TraceRefs", NULL },' % locals()
  98. print >> cfile, '#else'
  99. print >> cfile, '# if defined (__x86_64__)'
  100. print >> cfile, '\t{ "Py_InitModule4_64", NULL },' % locals()
  101. print >> cfile, '# else'
  102. print >> cfile, '\t{ "Py_InitModule4", NULL },' % locals()
  103. print >> cfile, '# endif'
  104. print >> cfile, '#endif'
  105. else:
  106. print >> cfile, '\t{ "%(name)s", NULL },' % locals()
  107. index += 1
  108. hfile.close()
  109. cfile.close()