compat.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. various library utilities (also for compatibility with python2.4)
  3. """
  4. try:
  5. from struct import Struct
  6. except ImportError:
  7. import struct
  8. class Struct(object):
  9. __slots__ = ["format", "size"]
  10. def __init__(self, format):
  11. self.format = format
  12. self.size = struct.calcsize(format)
  13. def pack(self, *args):
  14. return struct.pack(self.format, *args)
  15. def unpack(self, data):
  16. return struct.unpack(self.format, data)
  17. try:
  18. all = all
  19. except NameError:
  20. def all(seq):
  21. for elem in seq:
  22. if not elem:
  23. return False
  24. return True
  25. try:
  26. callable = callable
  27. except NameError:
  28. def callable(obj):
  29. return hasattr(obj, "__call__")
  30. try:
  31. import select
  32. except ImportError:
  33. def select(*args):
  34. raise ImportError("select not supported on this platform")
  35. else:
  36. # jython
  37. if hasattr(select, 'cpython_compatible_select'):
  38. from select import cpython_compatible_select as select
  39. else:
  40. from select import select