Skip to content

Commit c2ef2a2

Browse files
committed
Misc bytes change
1 parent 8e0f3df commit c2ef2a2

File tree

5 files changed

+69
-83
lines changed

5 files changed

+69
-83
lines changed

Src/IronPython/Runtime/ByteArray.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public ByteArray capitalize() {
263263
public ByteArray center(int width) => center(width, (byte)' ');
264264

265265
public ByteArray center(int width, [BytesLike, NotNone] IList<byte> fillchar)
266-
=> center(width, fillchar.ToByte("center", 2));
266+
=> center(width, fillchar.ToByte(nameof(center), 2));
267267

268268
private ByteArray center(int width, byte fillchar) {
269269
lock (this) {
@@ -301,7 +301,7 @@ public int count([BytesLike, NotNone] IList<byte> sub, object? start, object? en
301301
int istart = start != null ? Converter.ConvertToIndex(start) : 0;
302302
lock (this) {
303303
int iend = end != null ? Converter.ConvertToIndex(end) : _bytes.Count;
304-
return _bytes.CountOf(sub, istart, iend);
304+
return _bytes.CountOf(sub, istart, iend);
305305
}
306306
}
307307

@@ -585,13 +585,13 @@ public bool isupper() {
585585
/// in the sequence seq. The separator between elements is the
586586
/// string providing this method
587587
/// </summary>
588-
public ByteArray join(object? sequence) {
589-
IEnumerator seq = PythonOps.GetEnumerator(sequence);
588+
public ByteArray join(object? iterable) {
589+
IEnumerator seq = PythonOps.GetEnumerator(iterable);
590590
if (!seq.MoveNext()) {
591591
return new ByteArray();
592592
}
593593

594-
// check if we have just a sequnce of just one value - if so just
594+
// check if we have a sequence of just one value - if so just
595595
// return that value.
596596
object? curVal = seq.Current;
597597
if (!seq.MoveNext()) {
@@ -613,34 +613,32 @@ public ByteArray join(object? sequence) {
613613
return new ByteArray(ret);
614614
}
615615

616-
public ByteArray join([NotNone] PythonList sequence) {
617-
if (sequence.__len__() == 0) {
616+
public ByteArray join([NotNone] PythonList iterable) {
617+
if (iterable.__len__() == 0) {
618618
return new ByteArray();
619619
}
620620

621621
lock (this) {
622-
if (sequence.__len__() == 1) {
623-
return JoinOne(sequence[0]);
622+
if (iterable.__len__() == 1) {
623+
return JoinOne(iterable[0]);
624624
}
625625

626626
List<byte> ret = new List<byte>();
627-
ByteOps.AppendJoin(sequence._data[0], 0, ret);
628-
for (int i = 1; i < sequence._size; i++) {
627+
ByteOps.AppendJoin(iterable._data[0], 0, ret);
628+
for (int i = 1; i < iterable._size; i++) {
629629
ret.AddRange(this);
630-
ByteOps.AppendJoin(sequence._data[i], i, ret);
630+
ByteOps.AppendJoin(iterable._data[i], i, ret);
631631
}
632632

633633
return new ByteArray(ret);
634634
}
635635
}
636636

637-
public ByteArray ljust(int width) {
638-
return ljust(width, (byte)' ');
639-
}
637+
public ByteArray ljust(int width)
638+
=> ljust(width, (byte)' ');
640639

641-
public ByteArray ljust(int width, [BytesLike, NotNone] IList<byte> fillchar) {
642-
return ljust(width, fillchar.ToByte("ljust", 2));
643-
}
640+
public ByteArray ljust(int width, [BytesLike, NotNone] IList<byte> fillchar)
641+
=> ljust(width, fillchar.ToByte(nameof(ljust), 2));
644642

645643
private ByteArray ljust(int width, byte fillchar) {
646644
lock (this) {
@@ -671,7 +669,7 @@ public ByteArray lstrip() {
671669
}
672670
}
673671

674-
public ByteArray lstrip([BytesLike]IList<byte>? chars) {
672+
public ByteArray lstrip([BytesLike] IList<byte>? chars) {
675673
if (chars == null) return lstrip();
676674
lock (this) {
677675
var res = _bytes.LeftStrip(chars);
@@ -806,13 +804,11 @@ public int rindex(BigInteger @byte, object? start)
806804
public int rindex(BigInteger @byte, object? start, object? end)
807805
=> rindex(Bytes.FromByte(@byte.ToByteChecked()), start, end);
808806

809-
public ByteArray rjust(int width) {
810-
return rjust(width, (byte)' ');
811-
}
807+
public ByteArray rjust(int width)
808+
=> rjust(width, (byte)' ');
812809

813-
public ByteArray rjust(int width, [BytesLike, NotNone] IList<byte> fillchar) {
814-
return rjust(width, fillchar.ToByte("rjust", 2));
815-
}
810+
public ByteArray rjust(int width, [BytesLike, NotNone] IList<byte> fillchar)
811+
=> rjust(width, fillchar.ToByte(nameof(rjust), 2));
816812

817813
private ByteArray rjust(int width, int fillchar) {
818814
byte fill = fillchar.ToByteChecked();
@@ -855,7 +851,7 @@ public PythonTuple rpartition([BytesLike, NotNone] IList<byte> sep) {
855851
}
856852

857853
[return: SequenceTypeInfo(typeof(ByteArray))]
858-
public PythonList rsplit([BytesLike]IList<byte>? sep = null, int maxsplit = -1) {
854+
public PythonList rsplit([BytesLike] IList<byte>? sep = null, int maxsplit = -1) {
859855
lock (this) {
860856
return _bytes.RightSplit(sep, maxsplit, x => new ByteArray(new List<byte>(x)));
861857
}
@@ -868,7 +864,7 @@ public ByteArray rstrip() {
868864
}
869865
}
870866

871-
public ByteArray rstrip([BytesLike]IList<byte>? chars) {
867+
public ByteArray rstrip([BytesLike] IList<byte>? chars) {
872868
if (chars == null) return rstrip();
873869
lock (this) {
874870
var res = _bytes.RightStrip(chars);
@@ -877,7 +873,7 @@ public ByteArray rstrip([BytesLike]IList<byte>? chars) {
877873
}
878874

879875
[return: SequenceTypeInfo(typeof(ByteArray))]
880-
public PythonList split([BytesLike]IList<byte>? sep = null, int maxsplit = -1) {
876+
public PythonList split([BytesLike] IList<byte>? sep = null, int maxsplit = -1) {
881877
lock (this) {
882878
return _bytes.Split(sep, maxsplit, x => new ByteArray(x));
883879
}
@@ -973,7 +969,7 @@ public ByteArray strip() {
973969
}
974970
}
975971

976-
public ByteArray strip([BytesLike]IList<byte>? chars) {
972+
public ByteArray strip([BytesLike] IList<byte>? chars) {
977973
if (chars == null) return strip();
978974
lock (this) {
979975
var res = _bytes.Strip(chars);
@@ -1000,22 +996,22 @@ private void ValidateTable(IList<byte>? table) {
1000996
}
1001997
}
1002998

1003-
public ByteArray translate([BytesLike]IList<byte>? table) {
999+
public ByteArray translate([BytesLike] IList<byte>? table) {
10041000
ValidateTable(table);
10051001
lock (this) {
10061002
return new ByteArray(_bytes.Translate(table, null));
10071003
}
10081004
}
10091005

10101006

1011-
public ByteArray translate([BytesLike]IList<byte>? table, [BytesLike, NotNone] IList<byte> delete) {
1007+
public ByteArray translate([BytesLike] IList<byte>? table, [BytesLike, NotNone] IList<byte> delete) {
10121008
ValidateTable(table);
10131009
lock (this) {
10141010
return new ByteArray(_bytes.Translate(table, delete));
10151011
}
10161012
}
10171013

1018-
public ByteArray translate([BytesLike]IList<byte>? table, object? delete) {
1014+
public ByteArray translate([BytesLike] IList<byte>? table, object? delete) {
10191015
if (delete is IBufferProtocol bufferProtocol) {
10201016
using var buffer = bufferProtocol.GetBuffer();
10211017
return translate(table, buffer.AsReadOnlySpan().ToArray());
@@ -1353,11 +1349,11 @@ private static ByteArray JoinOne(object? curVal) {
13531349
return new ByteArray(new List<byte>(bytes));
13541350
}
13551351
if (curVal is IBufferProtocol bp) {
1356-
using (IPythonBuffer buf = bp.GetBuffer()) {
1357-
return new ByteArray(new ArrayData<byte>(buf.AsReadOnlySpan()));
1358-
}
1352+
using var buf = bp.GetBufferNoThrow();
1353+
if (buf is null) throw ByteOps.JoinSequenceError(curVal, 0);
1354+
return new ByteArray(new ArrayData<byte>(buf.AsReadOnlySpan()));
13591355
}
1360-
throw PythonOps.TypeError("can only join an iterable of bytes");
1356+
throw ByteOps.JoinSequenceError(curVal, 0);
13611357
}
13621358

13631359
private ByteArray CopyThis() {

Src/IronPython/Runtime/Bytes.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ public Bytes center(int width)
214214
=> center(width, (byte)' ');
215215

216216
public Bytes center(int width, [BytesLike, NotNone] IList<byte> fillchar)
217-
=> center(width, fillchar.ToByte("center", 2));
217+
=> center(width, fillchar.ToByte(nameof(center), 2));
218218

219219
private Bytes center(int width, byte fillchar) {
220220
var res = _bytes.TryCenter(width, fillchar);
221-
return res == null ? this.AsBytes() : new Bytes(res);
221+
return res == null ? AsBytes() : new Bytes(res);
222222
}
223223

224224
public int count([BytesLike, NotNone] IList<byte> sub)
@@ -456,8 +456,8 @@ public int index(BigInteger @byte, object? start, object? end)
456456
/// in the sequence seq. The separator between elements is the
457457
/// string providing this method
458458
/// </summary>
459-
public Bytes join(object? sequence) {
460-
IEnumerator seq = PythonOps.GetEnumerator(sequence);
459+
public Bytes join(object? iterable) {
460+
IEnumerator seq = PythonOps.GetEnumerator(iterable);
461461
if (!seq.MoveNext()) {
462462
return Empty;
463463
}
@@ -484,30 +484,28 @@ public Bytes join(object? sequence) {
484484
return new Bytes(ret);
485485
}
486486

487-
public Bytes join([NotNone] PythonList sequence) {
488-
if (sequence.__len__() == 0) {
487+
public Bytes join([NotNone] PythonList iterable) {
488+
if (iterable.__len__() == 0) {
489489
return Empty;
490-
} else if (sequence.__len__() == 1) {
491-
return JoinOne(sequence[0]);
490+
} else if (iterable.__len__() == 1) {
491+
return JoinOne(iterable[0]);
492492
}
493493

494494
List<byte> ret = new List<byte>();
495-
ByteOps.AppendJoin(sequence._data[0], 0, ret);
496-
for (int i = 1; i < sequence._size; i++) {
495+
ByteOps.AppendJoin(iterable._data[0], 0, ret);
496+
for (int i = 1; i < iterable._size; i++) {
497497
ret.AddRange(this);
498-
ByteOps.AppendJoin(sequence._data[i], i, ret);
498+
ByteOps.AppendJoin(iterable._data[i], i, ret);
499499
}
500500

501501
return new Bytes(ret);
502502
}
503503

504-
public Bytes ljust(int width) {
505-
return ljust(width, (byte)' ');
506-
}
504+
public Bytes ljust(int width)
505+
=> ljust(width, (byte)' ');
507506

508-
public Bytes ljust(int width, [BytesLike, NotNone] IList<byte> fillchar) {
509-
return ljust(width, fillchar.ToByte("ljust", 2));
510-
}
507+
public Bytes ljust(int width, [BytesLike, NotNone] IList<byte> fillchar)
508+
=> ljust(width, fillchar.ToByte(nameof(ljust), 2));
511509

512510
private Bytes ljust(int width, byte fillchar) {
513511
int spaces = width - Count;
@@ -662,13 +660,11 @@ public int rindex(BigInteger @byte, object? start)
662660
public int rindex(BigInteger @byte, object? start, object? end)
663661
=> rindex(Bytes.FromByte(@byte.ToByteChecked()), start, end);
664662

665-
public Bytes rjust(int width) {
666-
return rjust(width, (byte)' ');
667-
}
663+
public Bytes rjust(int width)
664+
=> rjust(width, (byte)' ');
668665

669-
public Bytes rjust(int width, [BytesLike, NotNone] IList<byte> fillchar) {
670-
return rjust(width, fillchar.ToByte("rjust", 2));
671-
}
666+
public Bytes rjust(int width, [BytesLike, NotNone] IList<byte> fillchar)
667+
=> rjust(width, fillchar.ToByte(nameof(rjust), 2));
672668

673669
private Bytes rjust(int width, byte fillchar) {
674670
int spaces = width - Count;
@@ -1059,9 +1055,11 @@ private static Bytes JoinOne(object? curVal) {
10591055
return new Bytes(b);
10601056
}
10611057
if (curVal is IBufferProtocol bp) {
1062-
return new Bytes(bp);
1058+
using var buf = bp.GetBufferNoThrow();
1059+
if (buf is null) throw ByteOps.JoinSequenceError(curVal, 0);
1060+
return Make(buf.AsReadOnlySpan().ToArray());
10631061
}
1064-
throw PythonOps.TypeError("can only join an iterable of bytes");
1062+
throw ByteOps.JoinSequenceError(curVal, 0);
10651063
}
10661064

10671065
internal static Bytes Concat(IList<Bytes> list, int length) {

Src/IronPython/Runtime/Operations/ByteOps.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
using System;
88
using System.Collections;
99
using System.Collections.Generic;
10-
using System.Linq;
1110
using System.Numerics;
1211

13-
using Microsoft.Scripting.Utils;
14-
1512
namespace IronPython.Runtime.Operations {
1613
public static partial class ByteOps {
1714
internal static byte ToByteChecked(this int item) {
@@ -85,12 +82,17 @@ internal static void AppendJoin(object? value, int index, List<byte> byteList) {
8582
if (value is IList<byte> bytesValue) {
8683
byteList.AddRange(bytesValue);
8784
} else if (value is IBufferProtocol bp) {
88-
using (IPythonBuffer buf = bp.GetBuffer()) {
89-
byteList.AddRange(buf.AsReadOnlySpan().ToArray());
90-
}
91-
} else {
92-
throw PythonOps.TypeError("sequence item {0}: expected bytes or byte array, {1} found", index.ToString(), PythonOps.GetPythonTypeName(value));
85+
using var buf = bp.GetBufferNoThrow();
86+
if (buf is null) throw JoinSequenceError(value, index);
87+
byteList.AddRange(buf.AsReadOnlySpan().ToArray());
9388
}
89+
else {
90+
throw JoinSequenceError(value, index);
91+
}
92+
}
93+
94+
internal static Exception JoinSequenceError(object? value, int index) {
95+
return PythonOps.TypeError("sequence item {0}: expected a bytes-like object, {1} found", index.ToString(), PythonOps.GetPythonTypeName(value));
9496
}
9597

9698
internal static IList<byte> CoerceBytes(object? obj) {

Src/IronPython/Runtime/Operations/IListOfByteOps.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -991,19 +991,9 @@ internal static int Find(this IList<byte> bytes, IList<byte> sub, int start, int
991991
return bytes.IndexOf(sub, start, end - start);
992992
}
993993

994-
internal static byte ToByte(this string self, string name, int pos) {
995-
if (self.Length != 1 || self[0] >= 256) {
996-
throw PythonOps.TypeError(name + "() argument " + pos + " must be char < 256, not string");
997-
}
998-
999-
return (byte)self[0];
1000-
}
1001-
1002994
internal static byte ToByte(this IList<byte> self, string name, int pos) {
1003-
if (self == null) {
1004-
throw PythonOps.TypeError(name + "() argument " + pos + " must be char < 256, not None");
1005-
} else if (self.Count != 1) {
1006-
throw PythonOps.TypeError(name + "() argument " + pos + " must be char < 256, not bytearray or bytes");
995+
if (self is null || self.Count != 1) {
996+
throw PythonOps.TypeError("{0} () argument {1} must a byte string of length 1, not {2}", name, pos, PythonOps.GetPythonTypeName(self));
1007997
}
1008998

1009999
return self[0];

WhatsNewInPython34.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ https://docs.python.org/3/whatsnew/3.4.html
44

55
PEPs
66
====
7-
- [ ] [PEP 453][]: Explicit Bootstrapping of PIP in Python Installations
7+
- [x] [PEP 453][]: Explicit Bootstrapping of PIP in Python Installations
88
- [ ] [PEP 446][]: Newly created file descriptors are non-inheritable
99
- [ ] [PEP 451][]: A `ModuleSpec` Type for the Import System
1010

@@ -21,7 +21,7 @@ Other Language Changes
2121
- [ ] All the UTF-* codecs (except UTF-7) now reject surrogates during both encoding and decoding unless the surrogatepass error handler is used, with the exception of the UTF-16 decoder and the UTF-16 encoder
2222
- [ ] New German EBCDIC codec `cp273`.
2323
- [ ] New Ukrainian codec `cp1125`.
24-
- [ ] `bytes.join()` and `bytearray.join()` now accept arbitrary buffer objects as arguments.
24+
- [x] `bytes.join()` and `bytearray.join()` now accept arbitrary buffer objects as arguments.
2525
- [ ] The `int` constructor now accepts any object that has an `__index__` method for its base argument.
2626
- [ ] Frame objects now have a `clear()` method that clears all references to local variables from the frame.
2727
- [ ] `memoryview` is now registered as a `Sequence`, and supports the `reversed()` builtin.

0 commit comments

Comments
 (0)