/pupy/client/sources-linux/mktab.py

https://github.com/therealsunjester/RatMuseum · Python · 126 lines · 117 code · 5 blank · 4 comment · 8 complexity · 1dc4b6b8f866d1b26f43f3cfead9484c 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_NoUserSiteDirectory
  62. int, Py_DontWriteBytecodeFlag
  63. int, Py_IgnoreEnvironmentFlag
  64. const char *, Py_FileSystemDefaultEncoding
  65. PyObject *, PyObject_Str, (PyObject *)
  66. PyObject *, PyList_New, (Py_ssize_t)
  67. int, PyList_SetItem, (PyObject *, Py_ssize_t, PyObject *)
  68. int, PyList_Append, (PyObject *, PyObject *)
  69. PyObject *, PyThreadState_GetDict, (void)
  70. int, PyObject_IsTrue, (PyObject *)
  71. void, PyErr_SetString, (PyObject *, const char *)
  72. void, PyEval_InitThreads, (void)
  73. void, PySys_SetArgvEx, (int, char **, int)
  74. PyObject *, PyFile_FromFile, (FILE *fp, char *name, char *mode, int (*close)(FILE*))
  75. void, PyFile_SetBufSize, (PyObject *, int)
  76. PyObject *, PyErr_NewException, (char *name, PyObject *base, PyObject *dict)
  77. int, PyModule_AddObject, (PyObject *, const char *, PyObject *)
  78. int, PyModule_AddStringConstant, (PyObject *module, const char *name, const char *value)
  79. PyObject*, PyDict_New, ()
  80. PyObject*, PyString_FromStringAndSize, (const char *v, Py_ssize_t len)
  81. int, PyDict_SetItem, (PyObject *p, PyObject *key, PyObject *val)
  82. '''.strip().splitlines()
  83. import string
  84. hfile = open("import-tab.h", "w")
  85. cfile = open("import-tab.c", "w")
  86. index = 0
  87. for decl in decls:
  88. if not decl or decl.startswith("//"):
  89. continue
  90. items = decl.split(',', 2)
  91. if len(items) == 3:
  92. # exported function with argument list
  93. restype, name, argtypes = map(string.strip, items)
  94. print >> hfile, '#define %(name)s ((%(restype)s(*)%(argtypes)s)imports[%(index)d].proc)' % locals()
  95. elif len(items) == 2:
  96. # exported data
  97. typ, name = map(string.strip, items)
  98. print >> hfile, '#define %(name)s (*(%(typ)s(*))imports[%(index)s].proc)' % locals()
  99. else:
  100. raise ValueError, "could not parse %r" % decl
  101. if name == "Py_InitModule4":
  102. print >> cfile, '#ifdef _DEBUG'
  103. print >> cfile, '\t{ "Py_InitModule4TraceRefs", NULL },' % locals()
  104. print >> cfile, '#else'
  105. print >> cfile, '# if defined (__x86_64__)'
  106. print >> cfile, '\t{ "Py_InitModule4_64", NULL },' % locals()
  107. print >> cfile, '# else'
  108. print >> cfile, '\t{ "Py_InitModule4", NULL },' % locals()
  109. print >> cfile, '# endif'
  110. print >> cfile, '#endif'
  111. else:
  112. print >> cfile, '\t{ "%(name)s", NULL },' % locals()
  113. index += 1
  114. hfile.close()
  115. cfile.close()