Skip to content

Commit 871b685

Browse files
committed
*: Simpler conversion to bytes
According to the cython documentation, indexing a C string returns a copy as bytes. Therefore, calling `bytes()` explicitely makes an extra, unnecessary copy.
1 parent f4debe4 commit 871b685

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

src/mbedtls/_md.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ cdef class MDBase:
119119
raise MemoryError()
120120
try:
121121
check_error(self._finish(output))
122-
return bytes(output[:self.digest_size])
122+
return output[:self.digest_size]
123123
finally:
124124
free(output)
125125

src/mbedtls/_random.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cdef class _Entropy:
4242
raise MemoryError()
4343
try:
4444
check_error(_rnd.mbedtls_entropy_func(&self._ctx, output, length))
45-
return bytes(output[:length])
45+
return output[:length]
4646
finally:
4747
free(output)
4848

@@ -90,7 +90,7 @@ cdef class Random:
9090
check_error(
9191
_rnd.mbedtls_ctr_drbg_random(&self._ctx, output, length)
9292
)
93-
ret = bytes(output[:length])
93+
ret = output[:length]
9494
_plt.mbedtls_platform_zeroize(output, length)
9595
return ret
9696
finally:

src/mbedtls/cipher/_cipher.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ cdef class Cipher(_CipherBase):
279279
&self._dec_ctx,
280280
&iv[0] if iv.size else NULL, iv.size,
281281
&input[0], input.size, output, &olen))
282-
return bytes(output[:olen])
282+
return output[:olen]
283283
finally:
284284
free(output)
285285

@@ -325,7 +325,7 @@ cdef class AEADCipher(_CipherBase):
325325
&iv[0], iv.size, pad, ad.size,
326326
&input[0], input.size, output, &olen,
327327
tag, sizeof(tag)))
328-
return bytes(output[:olen]), bytes(tag[:16])
328+
return output[:olen], tag[:16]
329329
finally:
330330
free(output)
331331

@@ -354,7 +354,7 @@ cdef class AEADCipher(_CipherBase):
354354
&iv[0], iv.size, pad, ad.size,
355355
&input[0], input.size, output, &olen,
356356
&tag[0], tag.size))
357-
return bytes(output[:olen])
357+
return output[:olen]
358358
finally:
359359
free(output)
360360

src/mbedtls/hkdf.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def hkdf(
5959
0 if info is None else info.size,
6060
okm, length
6161
))
62-
return bytes(okm[:length])
62+
return okm[:length]
6363
finally:
6464
free(okm)
6565

@@ -103,7 +103,7 @@ def extract(
103103
&key[0], key.size,
104104
prk
105105
))
106-
return bytes(prk[:hmac.digest_size])
106+
return prk[:hmac.digest_size]
107107
finally:
108108
free(prk)
109109

@@ -148,6 +148,6 @@ def expand(
148148
0 if info is None else info.size,
149149
okm, length
150150
))
151-
return bytes(okm[:length])
151+
return okm[:length]
152152
finally:
153153
free(okm)

src/mbedtls/mpi.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ cdef class MPI:
103103
try:
104104
check_error(_mpi.mbedtls_mpi_write_binary(
105105
&self._ctx, output, length))
106-
return bytes(output[:length])[::-1 if byteorder == "little" else 1]
106+
return output[:length][::-1 if byteorder == "little" else 1]
107107
except Exception as exc:
108108
raise OverflowError from exc
109109
finally:

src/mbedtls/pk.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ cdef class CipherBase:
299299
&output[0], &sig_len,
300300
&_rnd.mbedtls_ctr_drbg_random, &__rng._ctx))
301301
assert sig_len != 0
302-
return bytes(output[:sig_len])
302+
return output[:sig_len]
303303
finally:
304304
free(output)
305305

@@ -348,7 +348,7 @@ cdef class CipherBase:
348348
&self._ctx, &message[0], message.size,
349349
output, &olen, self.key_size,
350350
&_rnd.mbedtls_ctr_drbg_random, &__rng._ctx))
351-
return bytes(output[:olen])
351+
return output[:olen]
352352
finally:
353353
free(output)
354354

@@ -371,7 +371,7 @@ cdef class CipherBase:
371371
&self._ctx, &message[0], message.size,
372372
output, &olen, self.key_size,
373373
&_rnd.mbedtls_ctr_drbg_random, &__rng._ctx))
374-
return bytes(output[:olen])
374+
return output[:olen]
375375
finally:
376376
free(output)
377377

@@ -396,7 +396,7 @@ cdef class CipherBase:
396396
try:
397397
olen = check_error(
398398
_pk.mbedtls_pk_write_key_der(&self._ctx, output, osize))
399-
return bytes(output[osize - olen:osize])
399+
return output[osize - olen:osize]
400400
finally:
401401
free(output)
402402

@@ -412,7 +412,7 @@ cdef class CipherBase:
412412
try:
413413
check_error(
414414
_pk.mbedtls_pk_write_key_pem(&self._ctx, output, osize))
415-
return bytes(output[0:osize]).decode("ascii")
415+
return output[0:osize].decode("ascii")
416416
finally:
417417
free(output)
418418

@@ -443,7 +443,7 @@ cdef class CipherBase:
443443
try:
444444
olen = check_error(
445445
_pk.mbedtls_pk_write_pubkey_der(&self._ctx, output, osize))
446-
return bytes(output[osize - olen:osize])
446+
return output[osize - olen:osize]
447447
finally:
448448
free(output)
449449

@@ -459,7 +459,7 @@ cdef class CipherBase:
459459
try:
460460
check_error(
461461
_pk.mbedtls_pk_write_pubkey_pem(&self._ctx, output, osize))
462-
return bytes(output[0:osize]).decode("ascii")
462+
return output[0:osize].decode("ascii")
463463
finally:
464464
free(output)
465465

@@ -845,7 +845,7 @@ cdef class DHServer(DHBase):
845845
&self._ctx, self.key_size, &output[0], &olen,
846846
&_rnd.mbedtls_ctr_drbg_random, &__rng._ctx))
847847
assert olen != 0
848-
return bytes(output[:olen])
848+
return output[:olen]
849849
finally:
850850
free(output)
851851

@@ -1039,7 +1039,7 @@ cdef class ECDHServer(ECDHBase):
10391039
&self._ctx, &olen, &output[0], _mpi.MBEDTLS_MPI_MAX_SIZE,
10401040
&_rnd.mbedtls_ctr_drbg_random, &__rng._ctx))
10411041
assert olen != 0
1042-
return bytes(output[:olen])
1042+
return output[:olen]
10431043
finally:
10441044
free(output)
10451045

@@ -1076,7 +1076,7 @@ cdef class ECDHClient(ECDHBase):
10761076
&self._ctx, &olen, &output[0], _mpi.MBEDTLS_MPI_MAX_SIZE,
10771077
&_rnd.mbedtls_ctr_drbg_random, &__rng._ctx))
10781078
assert olen != 0
1079-
return bytes(output[:olen])
1079+
return output[:olen]
10801080
finally:
10811081
free(output)
10821082

src/mbedtls/x509.pyx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ cdef class CRT(Certificate):
157157
try:
158158
written = check_error(x509.mbedtls_x509_crt_info(
159159
&output[0], osize, prefix, &self._ctx))
160-
return bytes(output[:written]).decode("utf8")
160+
return output[:written].decode("utf8")
161161
finally:
162162
free(output)
163163

@@ -239,7 +239,7 @@ cdef class CRT(Certificate):
239239
try:
240240
written = x509.mbedtls_x509_dn_gets(
241241
&c_buf[0], osize, &self._ctx.issuer)
242-
return bytes(c_buf[:written]).decode("utf8")
242+
return c_buf[:written].decode("utf8")
243243
finally:
244244
free(c_buf)
245245

@@ -284,7 +284,7 @@ cdef class CRT(Certificate):
284284
try:
285285
written = x509.mbedtls_x509_dn_gets(
286286
&c_buf[0], osize, &self._ctx.subject)
287-
return bytes(c_buf[:written]).decode("utf8")
287+
return c_buf[:written].decode("utf8")
288288
finally:
289289
free(c_buf)
290290

@@ -511,7 +511,7 @@ cdef class _CRTWriter:
511511
try:
512512
written = check_error(x509.mbedtls_x509write_crt_der(
513513
&self._ctx, &output[0], osize, NULL, NULL))
514-
return bytes(output[osize - written:osize])
514+
return output[osize - written:osize]
515515
finally:
516516
free(output)
517517

@@ -665,7 +665,7 @@ cdef class CSR(Certificate):
665665
try:
666666
written = check_error(x509.mbedtls_x509_csr_info(
667667
&output[0], osize, prefix, &self._ctx))
668-
return bytes(output[:written]).decode("utf8")
668+
return output[:written].decode("utf8")
669669
finally:
670670
free(output)
671671

@@ -705,7 +705,7 @@ cdef class CSR(Certificate):
705705
try:
706706
written = x509.mbedtls_x509_dn_gets(
707707
&c_buf[0], osize, &self._ctx.subject)
708-
return bytes(c_buf[:written]).decode("utf8")
708+
return c_buf[:written].decode("utf8")
709709
finally:
710710
free(c_buf)
711711

@@ -841,7 +841,7 @@ cdef class _CSRWriter:
841841
try:
842842
written = check_error(x509.mbedtls_x509write_csr_der(
843843
&self._ctx, &output[0], osize, NULL, NULL))
844-
return bytes(output[osize - written:osize])
844+
return output[osize - written:osize]
845845
finally:
846846
free(output)
847847

@@ -891,7 +891,7 @@ cdef class CRL(Certificate):
891891
try:
892892
written = check_error(x509.mbedtls_x509_crl_info(
893893
&output[0], osize, prefix, &self._ctx))
894-
return bytes(output[:written]).decode("utf8")
894+
return output[:written].decode("utf8")
895895
finally:
896896
free(output)
897897

@@ -948,7 +948,7 @@ cdef class CRL(Certificate):
948948
try:
949949
written = x509.mbedtls_x509_dn_gets(
950950
&c_buf[0], osize, &self._ctx.issuer)
951-
return bytes(c_buf[:written]).decode("utf8")
951+
return c_buf[:written].decode("utf8")
952952
finally:
953953
free(c_buf)
954954

0 commit comments

Comments
 (0)