system.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 2007 Matt Chaput. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # 1. Redistributions of source code must retain the above copyright notice,
  7. # this list of conditions and the following disclaimer.
  8. #
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY MATT CHAPUT ``AS IS'' AND ANY EXPRESS OR
  14. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. # EVENT SHALL MATT CHAPUT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. #
  24. # The views and conclusions contained in the software and documentation are
  25. # those of the authors and should not be interpreted as representing official
  26. # policies, either expressed or implied, of Matt Chaput.
  27. import sys
  28. from struct import Struct, calcsize
  29. IS_LITTLE = sys.byteorder == "little"
  30. _INT_SIZE = calcsize("!i")
  31. _SHORT_SIZE = calcsize("!H")
  32. _LONG_SIZE = calcsize("!Q")
  33. _FLOAT_SIZE = calcsize("!f")
  34. _DOUBLE_SIZE = calcsize("!d")
  35. _byte_struct = Struct("!B")
  36. _sbyte_struct = Struct("!b")
  37. _ushort_struct = Struct("!H")
  38. _int_struct = Struct("!i")
  39. _uint_struct = Struct("!I")
  40. _long_struct = Struct("!q")
  41. _ulong_struct = Struct("!Q")
  42. _float_struct = Struct("!f")
  43. _double_struct = Struct("!d")
  44. _ushort_le_struct = Struct("<H")
  45. _uint_le_struct = Struct("<I")
  46. pack_byte = _byte_struct.pack
  47. pack_sbyte = _sbyte_struct.pack
  48. pack_ushort = _ushort_struct.pack
  49. pack_int = _int_struct.pack
  50. pack_uint = _uint_struct.pack
  51. pack_long = _long_struct.pack
  52. pack_ulong = _ulong_struct.pack
  53. pack_float = _float_struct.pack
  54. pack_double = _double_struct.pack
  55. pack_ushort_le = _ushort_le_struct.pack
  56. pack_uint_le = _uint_le_struct.pack
  57. unpack_byte = _byte_struct.unpack # ord() might be faster
  58. unpack_sbyte = _sbyte_struct.unpack
  59. unpack_ushort = _ushort_struct.unpack
  60. unpack_int = _int_struct.unpack
  61. unpack_uint = _uint_struct.unpack
  62. unpack_long = _long_struct.unpack
  63. unpack_ulong = _ulong_struct.unpack
  64. unpack_float = _float_struct.unpack
  65. unpack_double = _double_struct.unpack
  66. unpack_ushort_le = _ushort_le_struct.unpack
  67. unpack_uint_le = _uint_le_struct.unpack
  68. if sys.version_info[0] < 3:
  69. emptybytes = ""
  70. else:
  71. emptybytes = "".encode("latin-1")