Skip to content

Commit b1cb22b

Browse files
committed
OIDs: Add dotted_form and byte_form properties
This introduces two new properties, dotted_form, for querying the dotted form of the OID, and byte_form, for querying the raw OID encoding. By making __bytes__ and __hash__ rely on byte_form, infinite recursion is prevented when deriving a subclass that overrides __bytes__. Signed-off-by: Alexander Scheel <[email protected]>
1 parent 51817f5 commit b1cb22b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

gssapi/raw/oids.pyx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ cdef class OID:
129129
if self._free_on_dealloc:
130130
free(self.raw_oid.elements)
131131

132-
def __bytes__(self):
132+
@property
133+
def byte_form(self):
133134
return (<char*>self.raw_oid.elements)[:self.raw_oid.length]
134135

136+
def __bytes__(self):
137+
return self.byte_form
138+
135139
def _decode_asn1ber(self):
136-
ber_encoding = self.__bytes__()
140+
ber_encoding = self.byte_form
137141
# NB(directxman12): indexing a byte string yields an int in Python 3,
138142
# but yields a substring in Python 2
139143
if six.PY2:
@@ -156,12 +160,15 @@ cdef class OID:
156160
pos += 1
157161
return decoded
158162

163+
@property
164+
def dotted_form(self):
165+
return '.'.join(str(x) for x in self._decode_asn1ber())
166+
159167
def __repr__(self):
160-
dotted_oid = '.'.join(str(x) for x in self._decode_asn1ber())
161-
return "<OID {0}>".format(dotted_oid)
168+
return "<OID {0}>".format(self.dotted_form)
162169

163170
def __hash__(self):
164-
return hash(self.__bytes__())
171+
return hash(self.byte_form)
165172

166173
def __richcmp__(OID self, OID other, op):
167174
if op == 2 or op == 3: # ==

0 commit comments

Comments
 (0)