PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Tools/pyname_patch.py

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