Add __bytes__ to ObjectId to support bytes(oid) in Python 3

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      We could add __bytes__ to ObjectId to support bytes(oid) in Python 3. Currently this is not supported:

      >>> bytes(ObjectId())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: cannot convert 'ObjectId' object to bytes
      

      The implementation can just be an alias for the binary property:

      class ObjectId(object):
      ...
          def __bytes__(self):
              return self.binary
      

      This was requested in PYTHON-1843.

              Assignee:
              Unassigned
              Reporter:
              Shane Harvey
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated: