PageRenderTime 31ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Tools/pyname_patch.py

#
Python | 123 lines | 116 code | 1 blank | 6 comment | 8 complexity | 3f7c7c750be524ec91cd5a85d6b103eb MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #!/usr/bin/env python
  2. """
  3. From SWIG 1.3.37 we deprecated all SWIG symbols that start with Py,
  4. since they are inappropriate and discouraged in Python documentation
  5. (from http://www.python.org/doc/2.5.2/api/includes.html):
  6. "All user visible names defined by Python.h (except those defined by the included
  7. standard headers) have one of the prefixes "Py" or "_Py". Names beginning with
  8. "_Py" are for internal use by the Python implementation and should not be used
  9. by extension writers. Structure member names do not have a reserved prefix.
  10. Important: user code should never define names that begin with "Py" or "_Py".
  11. This confuses the reader, and jeopardizes the portability of the user code to
  12. future Python versions, which may define additional names beginning with one
  13. of these prefixes."
  14. This file is a simple script used for change all of these symbols, for user code
  15. or SWIG itself.
  16. """
  17. import re
  18. from shutil import copyfile
  19. import sys
  20. symbols = [
  21. #(old name, new name)
  22. ("PySequence_Base", "SwigPySequence_Base"),
  23. ("PySequence_Cont", "SwigPySequence_Cont"),
  24. ("PySwigIterator_T", "SwigPyIterator_T"),
  25. ("PyPairBoolOutputIterator", "SwigPyPairBoolOutputIterator"),
  26. ("PySwigIterator", "SwigPyIterator"),
  27. ("PySwigIterator_T", "SwigPyIterator_T"),
  28. ("PyMapIterator_T", "SwigPyMapIterator_T"),
  29. ("PyMapKeyIterator_T", "SwigPyMapKeyIterator_T"),
  30. ("PyMapValueIterator_T", "SwigPyMapValueITerator_T"),
  31. ("PyObject_ptr", "SwigPtr_PyObject"),
  32. ("PyObject_var", "SwigVar_PyObject"),
  33. ("PyOper", "SwigPyOper"),
  34. ("PySeq", "SwigPySeq"),
  35. ("PySequence_ArrowProxy", "SwigPySequence_ArrowProxy"),
  36. ("PySequence_Cont", "SwigPySequence_Cont"),
  37. ("PySequence_InputIterator", "SwigPySequence_InputIterator"),
  38. ("PySequence_Ref", "SwigPySequence_Ref"),
  39. ("PySwigClientData", "SwigPyClientData"),
  40. ("PySwigClientData_Del", "SwigPyClientData_Del"),
  41. ("PySwigClientData_New", "SwigPyClientData_New"),
  42. ("PySwigIterator", "SwigPyIterator"),
  43. ("PySwigIteratorClosed_T", "SwigPyIteratorClosed_T"),
  44. ("PySwigIteratorOpen_T", "SwigPyIteratorOpen_T"),
  45. ("PySwigIterator_T", "SwigPyIterator_T"),
  46. ("PySwigObject", "SwigPyObject"),
  47. ("PySwigObject_Check", "SwigPyObject_Check"),
  48. ("PySwigObject_GetDesc", "SwigPyObject_GetDesc"),
  49. ("PySwigObject_New", "SwigPyObject_New"),
  50. ("PySwigObject_acquire", "SwigPyObject_acquire"),
  51. ("PySwigObject_append", "SwigPyObject_append"),
  52. ("PySwigObject_as_number", "SwigPyObject_as_number"),
  53. ("PySwigObject_compare", "SwigPyObject_compare"),
  54. ("PySwigObject_dealloc", "SwigPyObject_dealloc"),
  55. ("PySwigObject_disown", "SwigPyObject_disown"),
  56. ("PySwigObject_format", "SwigPyObject_format"),
  57. ("PySwigObject_getattr", "SwigPyObject_getattr"),
  58. ("PySwigObject_hex", "SwigPyObject_hex"),
  59. ("PySwigObject_long", "SwigPyObject_long"),
  60. ("PySwigObject_next", "SwigPyObject_next"),
  61. ("PySwigObject_oct", "SwigPyObject_oct"),
  62. ("PySwigObject_own", "SwigPyObject_own"),
  63. ("PySwigObject_print", "SwigPyObject_print"),
  64. ("PySwigObject_repr", "SwigPyObject_repr"),
  65. ("PySwigObject_richcompare", "SwigPyObject_richcompare"),
  66. ("PySwigObject_str", "SwigPyObject_str"),
  67. ("PySwigObject_type", "SwigPyObject_type"),
  68. ("PySwigPacked", "SwigPyPacked"),
  69. ("PySwigPacked_Check", "SwigPyPacked_Check"),
  70. ("PySwigPacked_New", "SwigPyPacked_New"),
  71. ("PySwigPacked_UnpackData", "SwigPyPacked_UnpackData"),
  72. ("PySwigPacked_compare", "SwigPyPacked_compare"),
  73. ("PySwigPacked_dealloc", "SwigPyPacked_dealloc"),
  74. ("PySwigPacked_print", "SwigPyPacked_print"),
  75. ("PySwigPacked_repr", "SwigPyPacked_repr"),
  76. ("PySwigPacked_str", "SwigPyPacked_str"),
  77. ("PySwigPacked_type", "SwigPyPacked_type"),
  78. ("pyseq", "swigpyseq"),
  79. ("pyswigobject_type", "swigpyobject_type"),
  80. ("pyswigpacked_type", "swigpypacked_type"),
  81. ]
  82. res = [(re.compile("\\b(%s)\\b"%oldname), newname) for oldname, newname in symbols]
  83. def patch_file(fn):
  84. newf = []
  85. changed = False
  86. for line in open(fn):
  87. for r, newname in res:
  88. line, n = r.subn(newname, line)
  89. if n>0:
  90. changed = True
  91. newf.append(line)
  92. if changed:
  93. copyfile(fn, fn+".bak")
  94. f = open(fn, "w")
  95. f.write("".join(newf))
  96. f.close()
  97. return changed
  98. def main(fns):
  99. for fn in fns:
  100. try:
  101. if patch_file(fn):
  102. print "Patched file", fn
  103. except IOError:
  104. print "Error occured during patching", fn
  105. return
  106. if __name__=="__main__":
  107. if len(sys.argv) > 1:
  108. main(sys.argv[1:])
  109. else:
  110. print "Patch your interface file for SWIG's Py* symbol name deprecation."
  111. print "Usage:"
  112. print " %s files..."%sys.argv[0]