Skip to content

Commit 64403aa

Browse files
authored
Normalize access of Bytes/ByteArray backing field (#693)
* Use GetUnsafeByteArray accessor * Change GetUnsafeByteArray to an UnsafeByteArray property * Add ByteArray.UnsafeByteList accessor * Fix test failure * Update test__socket assertions * Use Bytes.Make * Update error message
1 parent dfc017a commit 64403aa

24 files changed

Lines changed: 85 additions & 103 deletions

File tree

Src/IronPython.Modules/ModuleOps.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#if FEATURE_CTYPES
66

77
using System;
8+
using System.Collections.Generic;
89
using System.Diagnostics;
910
using System.Numerics;
1011
using System.Runtime.InteropServices;
@@ -153,18 +154,18 @@ public static CTypes._Array TryCheckCharArray(object o) {
153154

154155
public static byte[] TryCheckBytes(object o) {
155156
if (o is Bytes bytes) {
156-
if (bytes._bytes.Length == 0) {
157+
if (bytes.Count == 0) {
157158
// OpCodes.Ldelema refuses to get address of empty array
158159
// So we feed it with a fake one (cp34892)
159160
return FakeZeroLength;
160161
}
161-
return bytes._bytes;
162+
return bytes.UnsafeByteArray;
162163
}
163164
return null;
164165
}
165166

166167
public static byte[] GetBytes(Bytes bytes) {
167-
return bytes._bytes;
168+
return bytes.UnsafeByteArray;
168169
}
169170

170171
public static CTypes._Array TryCheckWCharArray(object o) {
@@ -600,7 +601,7 @@ public static byte GetChar(object value, object type) {
600601
}
601602

602603
if(value is Bytes bytesVal && bytesVal.Count == 1) {
603-
return bytesVal._bytes[0];
604+
return ((IList<byte>)bytesVal)[0];
604605
}
605606

606607
if (PythonOps.TryGetBoundAttr(value, "_as_parameter_", out object asParam)) {
@@ -632,4 +633,4 @@ public static object IntPtrToObject(IntPtr address) {
632633

633634
}
634635
}
635-
#endif
636+
#endif

Src/IronPython.Modules/_ctypes/ArrayType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public _Array from_buffer_copy(CodeContext/*!*/ context, Bytes array, [DefaultPa
131131
_Array res = (_Array)CreateInstance(context);
132132
res._memHolder = new MemoryHolder(((INativeType)this).Size);
133133
for (int i = 0; i < ((INativeType)this).Size; i++) {
134-
res._memHolder.WriteByte(i, array._bytes[i]);
134+
res._memHolder.WriteByte(i, ((IList<byte>)array)[i]);
135135
}
136136
return res;
137137
}

Src/IronPython.Modules/_ctypes/CData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal virtual PythonTuple GetBufferInfo() {
9696
#region IBufferProtocol Members
9797

9898
object IBufferProtocol.GetItem(int index) {
99-
return new Bytes(GetBytes(index, NativeType.Size));
99+
return Bytes.Make(GetBytes(index, NativeType.Size));
100100
}
101101

102102
void IBufferProtocol.SetItem(int index, object value) {
@@ -145,7 +145,7 @@ PythonTuple IBufferProtocol.SubOffsets {
145145
}
146146

147147
Bytes IBufferProtocol.ToBytes(int start, int? end) {
148-
return new Bytes(GetBytes(start, NativeType.Size));
148+
return Bytes.Make(GetBytes(start, NativeType.Size));
149149
}
150150

151151
PythonList IBufferProtocol.ToList(int start, int? end) {

Src/IronPython.Modules/_datetime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ public datetime(int year,
724724
}
725725

726726
public datetime([NotNull]Bytes bytes) {
727-
var byteArray = bytes._bytes;
727+
var byteArray = bytes.UnsafeByteArray;
728728
if (byteArray.Length != 10) {
729729
throw PythonOps.TypeError("an integer is required");
730730
}

Src/IronPython.Modules/_socket.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.Generic;
1010
using System.Diagnostics;
1111
using System.IO;
12+
using System.Linq;
1213
using System.Net;
1314
using System.Net.Sockets;
1415
using System.Net.Security;
@@ -471,7 +472,7 @@ public int recv_into(ByteArray buffer, int nbytes=0, int flags=0) {
471472
)]
472473
public int recv_into(MemoryView buffer, int nbytes=0, int flags=0) {
473474
int bytesRead;
474-
byte[] byteBuffer = buffer.tobytes().ToByteArray();
475+
byte[] byteBuffer = buffer.tobytes().ToArray();
475476
try {
476477
bytesRead = _socket.Receive(byteBuffer, (SocketFlags)flags);
477478
}
@@ -556,7 +557,7 @@ public PythonTuple recvfrom_into(PythonArray buffer, int nbytes=0, int flags=0)
556557
public PythonTuple recvfrom_into(MemoryView buffer, int nbytes=0, int flags=0){
557558

558559
int bytesRead;
559-
byte[] byteBuffer = buffer.tobytes().ToByteArray();
560+
byte[] byteBuffer = buffer.tobytes().ToArray();
560561
IPEndPoint remoteIPEP = new IPEndPoint(IPAddress.Any, 0);
561562
EndPoint remoteEP = remoteIPEP;
562563

@@ -636,7 +637,7 @@ private Exception MakeRecvException(Exception e, SocketError errorCode=SocketErr
636637
+ "had room to buffer your data for a network send"
637638
)]
638639
public int send(Bytes data, int flags=0) {
639-
byte[] buffer = data.GetUnsafeByteArray();
640+
byte[] buffer = data.UnsafeByteArray;
640641
try {
641642
return _socket.Send(buffer, (SocketFlags)flags);
642643
} catch (Exception e) {
@@ -693,11 +694,11 @@ public void sendall(string data, int flags=0) {
693694
+ "had room to buffer your data for a network send"
694695
)]
695696
public void sendall(Bytes data, int flags=0) {
696-
sendallWorker(data.GetUnsafeByteArray(), flags);
697+
sendallWorker(data.UnsafeByteArray, flags);
697698
}
698699

699700
public void sendall(MemoryView data, int flags = 0) {
700-
sendallWorker(data.tobytes().GetUnsafeByteArray(), flags);
701+
sendallWorker(data.tobytes().UnsafeByteArray, flags);
701702
}
702703

703704
private void sendallWorker(byte[] buffer, int flags) {
@@ -757,7 +758,7 @@ public int sendto(string data, int flags, PythonTuple address) {
757758
+ "had room to buffer your data for a network send"
758759
)]
759760
public int sendto(Bytes data, int flags, PythonTuple address) {
760-
byte[] buffer = data.GetUnsafeByteArray();
761+
byte[] buffer = data.UnsafeByteArray;
761762
EndPoint remoteEP = TupleToEndPoint(_context, address, _socket.AddressFamily, out _hostName);
762763
try {
763764
return _socket.SendTo(buffer, (SocketFlags)flags, remoteEP);
@@ -1659,7 +1660,7 @@ public static string inet_ntop(CodeContext/*!*/ context, int addressFamily, Byte
16591660
)) {
16601661
throw PythonOps.ValueError("invalid length of packed IP address string");
16611662
}
1662-
byte[] ipBytes = packedIP.GetUnsafeByteArray();
1663+
byte[] ipBytes = packedIP.UnsafeByteArray;
16631664
if (addressFamily == (int)AddressFamily.InterNetworkV6) {
16641665
return IPv6BytesToColonHex(ipBytes);
16651666
}
@@ -2516,7 +2517,7 @@ Writes the string s into the SSL object. Returns the number
25162517
public int write(CodeContext/*!*/ context, Bytes data) {
25172518
EnsureSslStream(true);
25182519

2519-
byte[] buffer = data.GetUnsafeByteArray();
2520+
byte[] buffer = data.UnsafeByteArray;
25202521
try {
25212522
_sslStream.Write(buffer);
25222523
return buffer.Length;

Src/IronPython.Modules/_ssl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void load_verify_locations(CodeContext context, [DefaultParameterValue(nu
184184
var cabuf = cadata as IBufferProtocol;
185185
if (cabuf != null) {
186186
int pos = 0;
187-
byte[] contents = cabuf.ToBytes(0, null).ToByteArray();
187+
byte[] contents = cabuf.ToBytes(0, null).ToArray();
188188
while(pos < contents.Length) {
189189
byte[] curr = new byte[contents.Length - pos];
190190
Array.Copy(contents, pos, curr, 0, contents.Length - pos);

Src/IronPython.Modules/_struct.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void pack_into(CodeContext/*!*/ context, [NotNull]ArrayModule.array/*!*/
194194
throw Error(context, $"pack_into requires a buffer of at least {size} bytes");
195195
}
196196

197-
var data = pack(context, args).GetUnsafeByteArray();
197+
var data = pack(context, args).UnsafeByteArray;
198198

199199
for (int i = 0; i < data.Length; i++) {
200200
existing[i + offset] = data[i];
@@ -205,13 +205,13 @@ public void pack_into(CodeContext/*!*/ context, [NotNull]ArrayModule.array/*!*/
205205
}
206206

207207
public void pack_into(CodeContext/*!*/ context, [NotNull]ByteArray/*!*/ buffer, int offset, params object[] args) {
208-
IList<byte> existing = buffer._bytes;
208+
IList<byte> existing = buffer.UnsafeByteList;
209209

210210
if (offset + size > existing.Count) {
211211
throw Error(context, $"pack_into requires a buffer of at least {size} bytes");
212212
}
213213

214-
var data = pack(context, args).GetUnsafeByteArray();
214+
var data = pack(context, args).UnsafeByteArray;
215215

216216
for (int i = 0; i < data.Length; i++) {
217217
existing[i + offset] = data[i];

Src/IronPython.Modules/bz2/BZ2Compressor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Bytes compress([BytesConversion]IList<byte> data) {
5252

5353
this.bz2Output.Write(bytes, 0, bytes.Length);
5454

55-
return new Bytes(this.GetLatestData());
55+
return Bytes.Make(this.GetLatestData());
5656
}
5757

5858
[Documentation(@"flush() -> string
@@ -63,7 +63,7 @@ You must not use the compressor object after calling this method.
6363
public Bytes flush() {
6464
this.bz2Output.Close();
6565

66-
return new Bytes(this.GetLatestData());
66+
return Bytes.Make(this.GetLatestData());
6767
}
6868

6969
/// <summary>

Src/IronPython.Modules/bz2/BZ2Decompressor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Bytes unused_data {
4040

4141
Array.Copy(buffer, this.lastSuccessfulPosition, unused, 0, unusedCount);
4242

43-
return new Bytes(unused);
43+
return Bytes.Make(unused);
4444
}
4545
}
4646

Src/IronPython.Modules/bz2/BZ2Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private static byte[] ToArrayNoCopy(this IList<byte> bytes) {
2929

3030
Bytes bytesP = bytes as Bytes;
3131
if (bytesP != null) {
32-
return bytesP.GetUnsafeByteArray();
32+
return bytesP.UnsafeByteArray;
3333
}
3434

3535
return bytes.ToArray();

0 commit comments

Comments
 (0)