Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001-2012 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE
  12. #
  13. ##############################################################################
  14. # Copied from ZODB/utils.py
  15. from binascii import hexlify
  16. from ._compat import _bytes
  17. def non_negative(int_val):
  18. if int_val < 0:
  19. # Coerce to non-negative.
  20. int_val &= 0x7FFFFFFFFFFFFFFF
  21. return int_val
  22. def positive_id(obj): #pragma NO COVER
  23. """Return id(obj) as a non-negative integer."""
  24. return non_negative(id(obj))
  25. def oid_repr(oid):
  26. if isinstance(oid, _bytes) and len(oid) == 8:
  27. # Convert to hex and strip leading zeroes.
  28. as_hex = hexlify(oid).lstrip(b'0')
  29. # Ensure two characters per input byte.
  30. chunks = [b'0x']
  31. if len(as_hex) & 1:
  32. chunks.append(b'0')
  33. elif as_hex == b'':
  34. as_hex = b'00'
  35. chunks.append(as_hex)
  36. return b''.join(chunks)
  37. else:
  38. return repr(oid)