compat.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2011, Bernhard Leiner
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms,
  5. # with or without modification, are permitted provided
  6. # that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain
  9. # the above copyright notice, this list of conditions
  10. # and the following disclaimer.
  11. # * Redistributions in binary form must reproduce
  12. # the above copyright notice, this list of conditions
  13. # and the following disclaimer in the documentation
  14. # and/or other materials provided with the distribution.
  15. # * Neither the name of the author nor the names
  16. # of its contributors may be used to endorse
  17. # or promote products derived from this software
  18. # without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  22. # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. # IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  25. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  26. # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  28. # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  30. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  32. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. '''Compatibility functions for python 2 and 3.
  34. @author Bernhard Leiner (bleiner AT gmail com)
  35. @version 1.0
  36. '''
  37. __docformat__ = "javadoc"
  38. import sys
  39. if sys.version_info[0] >= 3:
  40. def asbytes(s):
  41. if isinstance(s, bytes):
  42. return s
  43. return s.encode('latin1')
  44. def asstr(s):
  45. if isinstance(s, str):
  46. return s
  47. return s.decode('latin1')
  48. else:
  49. asbytes = str
  50. asstr = str