compat.py 936 B

12345678910111213141516171819202122232425262728293031
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014 Rackspace
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. if sys.version_info >= (3, 0):
  17. unicode = str # Python 3.x
  18. def to_str(b, encoding):
  19. if hasattr(b, 'decode') and not isinstance(b, unicode):
  20. b = b.decode('utf-8')
  21. return b
  22. def to_bytes(s, encoding):
  23. if hasattr(s, 'encode') and not isinstance(s, bytes):
  24. s = s.encode('utf-8')
  25. return s