Skip to content

Commit 23bbd6e

Browse files
BCSharpslozier
andauthored
Prevent using str type arg in int.from_bytes (#958)
* Prevent using str type arg in int.from_bytes * Add NotNull attribute to from_bytes Co-authored-by: Stéphane Lozier <slozier@users.noreply.github.com>
1 parent 3417b2d commit 23bbd6e

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

Src/IronPython/Runtime/Bytes.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ internal static Bytes FromObject(CodeContext context, object? o) {
164164
return (Bytes)o;
165165
} else if (TryInvokeBytesOperator(context, o, out Bytes? res)) {
166166
return res;
167+
} else if (o is string || o is ExtensibleString) {
168+
throw PythonOps.TypeError("cannot convert unicode object to bytes");
167169
} else {
168170
return new Bytes(ByteOps.GetBytes(o, useHint: true, context).ToArray());
169171
}

Src/IronPython/Runtime/Operations/IntOps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public static Bytes to_bytes(Int32 value, int length, string byteorder, bool sig
528528
return Bytes.Make(res.ToArray());
529529
}
530530

531-
public static BigInteger from_bytes([BytesLike]IList<byte> bytes, string byteorder, bool signed=false) {
531+
public static BigInteger from_bytes([NotNull, BytesLike]IList<byte> bytes, string byteorder, bool signed=false) {
532532
// TODO: signed should be a keyword only argument
533533
// TODO: return int when possible?
534534

0 commit comments

Comments
 (0)