Skip to content

Commit 9abc993

Browse files
committed
Remove array overloads
1 parent 4c537a4 commit 9abc993

File tree

3 files changed

+17
-47
lines changed

3 files changed

+17
-47
lines changed

Src/IronPython.Modules/_ctypes/_ctypes.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,6 @@ private static IntPtr GetHandleFromObject(object dll, string errorMsg) {
688688
return intPtrHandle;
689689
}
690690

691-
private static void ValidateArraySizes(ArrayModule.array array, int offset, int size) {
692-
ValidateArraySizes(array.__len__() * array.itemsize, offset, size);
693-
}
694-
695691
private static void ValidateArraySizes(int arraySize, int offset, int size) {
696692
if (offset < 0) {
697693
throw PythonOps.ValueError("offset cannot be negative");

Src/IronPython.Modules/_struct.cs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ public void __init__(CodeContext/*!*/ context, object fmt) {
207207
}
208208

209209
[Documentation("Stores the deserialized data into the provided array")]
210-
public void pack_into(CodeContext/*!*/ context, [NotNone] ArrayModule.array/*!*/ buffer, int offset, params object[] args) {
211-
byte[] existing = buffer.ToByteArray();
210+
public void pack_into(CodeContext/*!*/ context, [NotNone] ByteArray/*!*/ buffer, int offset, params object[] args) {
211+
IList<byte> existing = buffer.UnsafeByteList;
212212

213-
if (offset + size > existing.Length) {
213+
if (offset + size > existing.Count) {
214214
throw Error(context, $"pack_into requires a buffer of at least {size} bytes");
215215
}
216216

@@ -219,23 +219,21 @@ public void pack_into(CodeContext/*!*/ context, [NotNone] ArrayModule.array/*!*/
219219
for (int i = 0; i < data.Length; i++) {
220220
existing[i + offset] = data[i];
221221
}
222-
223-
buffer.Clear();
224-
buffer.FromStream(new MemoryStream(existing));
225222
}
226223

227-
public void pack_into(CodeContext/*!*/ context, [NotNone] ByteArray/*!*/ buffer, int offset, params object[] args) {
228-
IList<byte> existing = buffer.UnsafeByteList;
224+
public void pack_into(CodeContext/*!*/ context, [NotNone] IBufferProtocol/*!*/ buffer, int offset, params object[] args) {
225+
using var existing = buffer.GetBufferNoThrow(BufferFlags.Writable)
226+
?? throw PythonOps.TypeError("argument must be read-write bytes-like object, not {0}", PythonOps.GetPythonTypeName(buffer));
229227

230-
if (offset + size > existing.Count) {
228+
var span = existing.AsSpan();
229+
230+
if (offset + size > span.Length) {
231231
throw Error(context, $"pack_into requires a buffer of at least {size} bytes");
232232
}
233233

234234
var data = pack(context, args).UnsafeByteArray;
235235

236-
for (int i = 0; i < data.Length; i++) {
237-
existing[i + offset] = data[i];
238-
}
236+
data.CopyTo(span.Slice(offset));
239237
}
240238

241239
[Documentation("deserializes the string using the structs specified format")]
@@ -374,9 +372,6 @@ public void pack_into(CodeContext/*!*/ context, [NotNone] ByteArray/*!*/ buffer,
374372
return PythonTuple.MakeTuple(res);
375373
}
376374

377-
public PythonTuple/*!*/ unpack(CodeContext/*!*/ context, [NotNone] ArrayModule.array/*!*/ buffer)
378-
=> unpack(context, buffer.ToByteArray());
379-
380375
[Documentation("reads the current format from the specified array")]
381376
public PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [BytesLike][NotNone] IList<byte>/*!*/ buffer, int offset = 0) {
382377
int bytesAvail = buffer.Count - offset;
@@ -387,21 +382,11 @@ public void pack_into(CodeContext/*!*/ context, [NotNone] ByteArray/*!*/ buffer,
387382
return unpack(context, buffer.Substring(offset, size));
388383
}
389384

390-
[Documentation("reads the current format from the specified array")]
391-
public PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [NotNone] ArrayModule.array/*!*/ buffer, int offset = 0) {
392-
return unpack_from(context, buffer.ToByteArray(), offset);
393-
}
394-
395385
[Documentation("iteratively unpack the current format from the specified array.")]
396386
public PythonUnpackIterator iter_unpack(CodeContext/*!*/ context, [BytesLike][NotNone] IList<byte>/*!*/ buffer) {
397387
return new PythonUnpackIterator(this, context, buffer);
398388
}
399389

400-
[Documentation("iteratively unpack the current format from the specified array.")]
401-
public PythonUnpackIterator iter_unpack(CodeContext/*!*/ context, [NotNone] ArrayModule.array/*!*/ buffer) {
402-
return new PythonUnpackIterator(this, context, buffer.ToByteArray());
403-
}
404-
405390
[Documentation("gets the number of bytes that the serialized string will occupy or are required to deserialize the data")]
406391
public int size {
407392
get {
@@ -836,11 +821,11 @@ public static int calcsize(CodeContext/*!*/ context, object fmt) {
836821
}
837822

838823
[Documentation("Pack the values v1, v2, ... according to fmt.\nWrite the packed bytes into the writable buffer buf starting at offset.")]
839-
public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNone] ArrayModule.array/*!*/ buffer, int offset, params object[] args) {
824+
public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNone] ByteArray/*!*/ buffer, int offset, params object[] args) {
840825
GetStructFromCache(context, fmt).pack_into(context, buffer, offset, args);
841826
}
842827

843-
public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNone] ByteArray/*!*/ buffer, int offset, params object[] args) {
828+
public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNone] IBufferProtocol/*!*/ buffer, int offset, params object[] args) {
844829
GetStructFromCache(context, fmt).pack_into(context, buffer, offset, args);
845830
}
846831

@@ -849,30 +834,16 @@ public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNone] Byt
849834
return GetStructFromCache(context, fmt).unpack(context, buffer);
850835
}
851836

852-
[Documentation("Unpack the string containing packed C structure data, according to fmt.\nRequires len(string) == calcsize(fmt).")]
853-
public static PythonTuple/*!*/ unpack(CodeContext/*!*/ context, object fmt, [NotNone] ArrayModule.array/*!*/ buffer) {
854-
return GetStructFromCache(context, fmt).unpack(context, buffer);
855-
}
856-
857837
[Documentation("Unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
858838
public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, object fmt, [BytesLike][NotNone] IList<byte>/*!*/ buffer, int offset = 0) {
859839
return GetStructFromCache(context, fmt).unpack_from(context, buffer, offset);
860840
}
861841

862-
[Documentation("Unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
863-
public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, object fmt, [NotNone] ArrayModule.array/*!*/ buffer, int offset = 0) {
864-
return GetStructFromCache(context, fmt).unpack_from(context, buffer, offset);
865-
}
866-
867842
[Documentation("Iteratively unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
868843
public static PythonUnpackIterator/*!*/ iter_unpack(CodeContext/*!*/ context, object fmt, [BytesLike][NotNone] IList<byte>/*!*/ buffer) {
869844
return GetStructFromCache(context, fmt).iter_unpack(context, buffer);
870845
}
871846

872-
[Documentation("Iteratively unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
873-
public static PythonUnpackIterator/*!*/ iter_unpack(CodeContext/*!*/ context, object fmt, [NotNone] ArrayModule.array/*!*/ buffer) {
874-
return GetStructFromCache(context, fmt).iter_unpack(context, buffer);
875-
}
876847
#endregion
877848

878849
#region Write Helpers

Src/IronPython.Modules/re.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,15 @@ static string ValidateString(object? str) {
734734
return bytes.MakeString();
735735
case ByteArray byteArray:
736736
return byteArray.MakeString();
737-
case ArrayModule.array array:
738-
return Bytes.Make(array.ToByteArray()).MakeString();
739737
#if FEATURE_MMAP
740738
case MmapModule.MmapDefault mmapFile:
741739
return mmapFile.GetSearchString().MakeString();
742740
#endif
741+
case IBufferProtocol bufferProtocol: {
742+
using var buffer = bufferProtocol.GetBuffer();
743+
return buffer.AsReadOnlySpan().MakeString();
744+
}
745+
743746
default:
744747
throw PythonOps.TypeError($"expected string or bytes-like object");
745748
}

0 commit comments

Comments
 (0)