/client/mktab.py

https://github.com/n1nj4sec/pupy · Python · 135 lines · 126 code · 5 blank · 4 comment · 8 complexity · fc7130f634f76559475f709e98f8b7c3 MD5 · raw file

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