ustr.py 1.7 KB

1234567891011121314151617181920212223242526272829
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. import sys
  14. from libs.constants import DEFAULT_ENCODING
  15. def ustr(x):
  16. '''py2/py3 unicode helper'''
  17. if sys.version_info < (3, 0, 0):
  18. from PyQt4.QtCore import QString
  19. if type(x) == str:
  20. return x.decode(DEFAULT_ENCODING)
  21. if type(x) == QString:
  22. #https://blog.csdn.net/friendan/article/details/51088476
  23. #https://blog.csdn.net/xxm524/article/details/74937308
  24. return unicode(x.toUtf8(), DEFAULT_ENCODING, 'ignore')
  25. return x
  26. else:
  27. return x