From 7a3528202a76c8c1bea949474a08eecc461ec8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Fri, 13 Mar 2020 22:04:32 -0400 Subject: [PATCH 1/2] Rename BytesConversionAttribute to BytesLikeAttribute --- Src/IronPython.Modules/_codecs.cs | 48 ++++++------ Src/IronPython.Modules/_multiprocessing.cs | 2 +- Src/IronPython.Modules/_pickle.cs | 2 +- Src/IronPython.Modules/_struct.cs | 12 +-- Src/IronPython.Modules/bz2/BZ2Compressor.cs | 2 +- Src/IronPython.Modules/bz2/BZ2Decompressor.cs | 2 +- Src/IronPython.Modules/hashlib/_hashlib.cs | 2 +- Src/IronPython.Modules/hashlib/_md5.cs | 2 +- Src/IronPython.Modules/hashlib/_sha1.cs | 2 +- Src/IronPython.Modules/hashlib/_sha256.cs | 4 +- Src/IronPython.Modules/hashlib/_sha512.cs | 4 +- Src/IronPython.Modules/marshal.cs | 2 +- Src/IronPython.Modules/mmap.cs | 2 +- Src/IronPython.Modules/nt.cs | 8 +- Src/IronPython.Modules/pyexpat.cs | 2 +- Src/IronPython.Modules/zlib/Compress.cs | 2 +- Src/IronPython.Modules/zlib/Decompress.cs | 2 +- Src/IronPython.Modules/zlib/ZlibModule.cs | 8 +- Src/IronPython/Modules/Builtin.cs | 6 +- Src/IronPython/Modules/array.cs | 4 +- .../Runtime/Binding/PythonOverloadResolver.cs | 2 +- Src/IronPython/Runtime/ByteArray.cs | 76 +++++++++--------- Src/IronPython/Runtime/Bytes.cs | 78 +++++++++---------- .../Runtime/BytesConversionAttribute.cs | 15 ---- Src/IronPython/Runtime/Operations/IntOps.cs | 2 +- .../Runtime/Operations/StringOps.cs | 4 +- Src/IronPython/Runtime/Types/NewTypeMaker.cs | 7 +- 27 files changed, 143 insertions(+), 159 deletions(-) delete mode 100644 Src/IronPython/Runtime/BytesConversionAttribute.cs diff --git a/Src/IronPython.Modules/_codecs.cs b/Src/IronPython.Modules/_codecs.cs index 71d8c95c4..952611328 100644 --- a/Src/IronPython.Modules/_codecs.cs +++ b/Src/IronPython.Modules/_codecs.cs @@ -54,7 +54,7 @@ public static void register_error(CodeContext/*!*/ context, [NotNull]string name #region ASCII Encoding - public static PythonTuple ascii_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null) + public static PythonTuple ascii_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null) => DoDecode(context, "ascii", PythonAsciiEncoding.Instance, input, errors, input.Count).ToPythonTuple(); public static PythonTuple ascii_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -94,12 +94,12 @@ public static PythonTuple charmap_encode(CodeContext context, [NotNull]string in /// /// Decodes the input string using the provided string mapping. /// - public static PythonTuple charmap_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors, [NotNull]string map) { + public static PythonTuple charmap_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors, [NotNull]string map) { EncodingMap em = new EncodingMap(map, compileForDecoding: true, compileForEncoding: false); return DoDecode(context, "charmap", new EncodingMapEncoding(em), input, errors, input.Count).ToPythonTuple(); } - public static PythonTuple charmap_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, IDictionary? map = null) { + public static PythonTuple charmap_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, IDictionary? map = null) { if (map != null) { return DoDecode(context, "charmap", new CharmapEncoding(map), input, errors, input.Count).ToPythonTuple(); } else { @@ -153,7 +153,7 @@ public static object encode(CodeContext/*!*/ context, object? obj, [NotNull, Dis public static PythonTuple escape_decode(CodeContext/*!*/ context, [NotNull]string data, string? errors = null) => escape_decode(StringOps.DoEncodeUtf8(context, data), errors); - public static PythonTuple escape_decode([BytesConversion,NotNull]IList data, string? errors = null) { + public static PythonTuple escape_decode([BytesLike,NotNull]IList data, string? errors = null) { var res = LiteralParser.ParseBytes(data, 0, data.Count, isRaw: false, normalizeLineEndings: false, getErrorHandler(errors)); return PythonTuple.MakeTuple(Bytes.Make(res.ToArray()), data.Count); @@ -178,7 +178,7 @@ public static PythonTuple escape_decode([BytesConversion,NotNull]IList dat [DisallowNull] private static byte[]? _replacementMarker; - public static PythonTuple/*!*/ escape_encode([BytesConversion,NotNull]IList data, string? errors = null) { + public static PythonTuple/*!*/ escape_encode([BytesLike,NotNull]IList data, string? errors = null) { List buf = new List(data.Count); foreach (byte b in data) { switch (b) { @@ -203,7 +203,7 @@ public static PythonTuple escape_decode([BytesConversion,NotNull]IList dat #region Latin-1 Functions - public static PythonTuple latin_1_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null) + public static PythonTuple latin_1_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null) => DoDecode(context, "latin-1", Encoding.GetEncoding("iso-8859-1"), input, errors, input.Count).ToPythonTuple(); public static PythonTuple latin_1_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -215,7 +215,7 @@ public static PythonTuple latin_1_encode(CodeContext context, [NotNull]string in #if FEATURE_ENCODING [PythonHidden(PlatformsAttribute.PlatformFamily.Unix)] - public static PythonTuple mbcs_decode(CodeContext/*!*/ context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple mbcs_decode(CodeContext/*!*/ context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "mbcs", MbcsEncoding, input, errors, input.Count).ToPythonTuple(); [PythonHidden(PlatformsAttribute.PlatformFamily.Unix)] @@ -229,7 +229,7 @@ public static PythonTuple mbcs_encode(CodeContext/*!*/ context, [NotNull]string #if FEATURE_ENCODING [PythonHidden(PlatformsAttribute.PlatformFamily.Unix)] - public static PythonTuple code_page_decode(CodeContext context, int codepage, [BytesConversion, NotNull]IList input, string? errors = null, bool final = false) { + public static PythonTuple code_page_decode(CodeContext context, int codepage, [BytesLike, NotNull]IList input, string? errors = null, bool final = false) { // TODO: Use Win32 API MultiByteToWideChar https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar string encodingName = $"cp{codepage}"; Encoding encoding = Encoding.GetEncoding(codepage); @@ -255,7 +255,7 @@ public static PythonTuple raw_unicode_escape_decode(CodeContext/*!*/ context, [N return raw_unicode_escape_decode(context, DoEncode(context, "utf-8", Encoding.UTF8, input, "strict").Item1, errors); } - public static PythonTuple raw_unicode_escape_decode(CodeContext/*!*/ context, [BytesConversion,NotNull]IList input, string? errors = null) { + public static PythonTuple raw_unicode_escape_decode(CodeContext/*!*/ context, [BytesLike,NotNull]IList input, string? errors = null) { return PythonTuple.MakeTuple( StringOps.RawDecode(context, input, "raw-unicode-escape", errors), input.Count @@ -279,7 +279,7 @@ public static PythonTuple unicode_escape_decode(CodeContext/*!*/ context, [NotNu return unicode_escape_decode(context, DoEncode(context, "utf-8", Encoding.UTF8, input, "strict").Item1, errors); } - public static PythonTuple unicode_escape_decode(CodeContext/*!*/ context, [BytesConversion,NotNull]IList input, string? errors = null) { + public static PythonTuple unicode_escape_decode(CodeContext/*!*/ context, [BytesLike,NotNull]IList input, string? errors = null) { return PythonTuple.MakeTuple( StringOps.RawDecode(context, input, "unicode-escape", errors), input.Count @@ -300,7 +300,7 @@ public static PythonTuple unicode_escape_encode(CodeContext/*!*/ context, [NotNu public static PythonTuple readbuffer_encode(CodeContext/*!*/ context, [NotNull]string input, string? errors = null) => readbuffer_encode(DoEncode(context, "utf-8", Encoding.UTF8, input, "strict").Item1, errors); - public static PythonTuple readbuffer_encode([BytesConversion,NotNull]IList input, string? errors = null) + public static PythonTuple readbuffer_encode([BytesLike,NotNull]IList input, string? errors = null) => PythonTuple.MakeTuple(new Bytes(input), input.Count); #endregion @@ -312,7 +312,7 @@ public static PythonTuple unicode_internal_decode(CodeContext context, [NotNull] return PythonTuple.MakeTuple(input, input.Length); } - public static PythonTuple unicode_internal_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null) { + public static PythonTuple unicode_internal_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null) { PythonOps.Warn(context, PythonExceptions.DeprecationWarning, "unicode_internal codec has been deprecated"); return DoDecode(context, "unicode-internal", Encoding.Unicode, input, errors, input.Count).ToPythonTuple(); } @@ -322,7 +322,7 @@ public static PythonTuple unicode_internal_encode(CodeContext context, [NotNull] return DoEncode(context, "unicode-internal", Encoding.Unicode, input, errors, false).ToPythonTuple(); } - public static PythonTuple unicode_internal_encode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null) { + public static PythonTuple unicode_internal_encode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null) { PythonOps.Warn(context, PythonExceptions.DeprecationWarning, "unicode_internal codec has been deprecated"); return PythonTuple.MakeTuple(new Bytes(input), input.Count); } @@ -331,7 +331,7 @@ public static PythonTuple unicode_internal_encode(CodeContext context, [BytesCon #region Utf-16 Functions - public static PythonTuple utf_16_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) { + public static PythonTuple utf_16_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) { PythonTuple res = utf_16_ex_decode(context, input, errors, 0, final); return PythonTuple.MakeTuple(res[0], res[1]); } @@ -339,7 +339,7 @@ public static PythonTuple utf_16_decode(CodeContext context, [BytesConversion,No public static PythonTuple utf_16_encode(CodeContext context, [NotNull]string input, string? errors = null) => DoEncode(context, "utf-16", Utf16LeBomEncoding, input, errors, true).ToPythonTuple(); - public static PythonTuple utf_16_ex_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, int byteorder = 0, bool final = false) { + public static PythonTuple utf_16_ex_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, int byteorder = 0, bool final = false) { Tuple res; @@ -389,7 +389,7 @@ private static int NumEligibleUtf16Bytes(IList input, bool final, bool isL private static byte[] BOM_UTF16_LE => _bom_utf16_le ??= Utf16LeBomEncoding.GetPreamble(); [DisallowNull] private static byte[]? _bom_utf16_le; - public static PythonTuple utf_16_le_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple utf_16_le_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "utf-16-le", Utf16LeEncoding, input, errors, NumEligibleUtf16Bytes(input, final, isLE: true)).ToPythonTuple(); public static PythonTuple utf_16_le_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -407,7 +407,7 @@ public static PythonTuple utf_16_le_encode(CodeContext context, [NotNull]string private static byte[] BOM_UTF16_BE => _bom_utf16_be ??= Utf16BeBomEncoding.GetPreamble(); [DisallowNull] private static byte[]? _bom_utf16_be; - public static PythonTuple utf_16_be_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple utf_16_be_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "utf-16-be", Utf16BeEncoding, input, errors, NumEligibleUtf16Bytes(input, final, isLE: false)).ToPythonTuple(); public static PythonTuple utf_16_be_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -419,7 +419,7 @@ public static PythonTuple utf_16_be_encode(CodeContext context, [NotNull]string #if FEATURE_ENCODING - public static PythonTuple utf_7_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple utf_7_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "utf-7", Encoding.UTF7, input, errors, NumEligibleUtf7Bytes(input, final)).ToPythonTuple(); public static PythonTuple utf_7_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -451,7 +451,7 @@ private static int NumEligibleUtf7Bytes(IList input, bool final) { private static Encoding Utf8Encoding => _utf8Encoding ??= new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); [DisallowNull] private static Encoding? _utf8Encoding; - public static PythonTuple utf_8_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple utf_8_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "utf-8", Utf8Encoding, input, errors, NumEligibleUtf8Bytes(input, final)).ToPythonTuple(); public static PythonTuple utf_8_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -488,7 +488,7 @@ private static int NumEligibleUtf8Bytes(IList input, bool final) { #region Utf-32 Functions - public static PythonTuple utf_32_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) { + public static PythonTuple utf_32_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) { PythonTuple res = utf_32_ex_decode(context, input, errors, 0, final); return PythonTuple.MakeTuple(res[0], res[1]); } @@ -496,7 +496,7 @@ public static PythonTuple utf_32_decode(CodeContext context, [BytesConversion,No public static PythonTuple utf_32_encode(CodeContext context, [NotNull]string input, string? errors = null) => DoEncode(context, "utf-32", Utf32LeBomEncoding, input, errors, includePreamble: true).ToPythonTuple(); - public static PythonTuple utf_32_ex_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, int byteorder = 0, bool final = false) { + public static PythonTuple utf_32_ex_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, int byteorder = 0, bool final = false) { int numBytes = NumEligibleUtf32Bytes(input, final); Tuple res; @@ -542,7 +542,7 @@ private static int NumEligibleUtf32Bytes(IList input, bool final) { private static byte[] BOM_UTF32_LE => _bom_utf32_le ??= Utf32LeBomEncoding.GetPreamble(); [DisallowNull] private static byte[]? _bom_utf32_le; - public static PythonTuple utf_32_le_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple utf_32_le_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "utf-32-le", Utf32LeEncoding, input, errors, NumEligibleUtf32Bytes(input, final)).ToPythonTuple(); public static PythonTuple utf_32_le_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -561,7 +561,7 @@ public static PythonTuple utf_32_le_encode(CodeContext context, [NotNull]string private static byte[] BOM_UTF32_BE => _bom_utf32_be ??= Utf32BeBomEncoding.GetPreamble(); [DisallowNull] private static byte[]? _bom_utf32_be; - public static PythonTuple utf_32_be_decode(CodeContext context, [BytesConversion,NotNull]IList input, string? errors = null, bool final = false) + public static PythonTuple utf_32_be_decode(CodeContext context, [BytesLike,NotNull]IList input, string? errors = null, bool final = false) => DoDecode(context, "utf-32-be", Utf32BeEncoding, input, errors, NumEligibleUtf32Bytes(input, final)).ToPythonTuple(); public static PythonTuple utf_32_be_encode(CodeContext context, [NotNull]string input, string? errors = null) @@ -573,7 +573,7 @@ public static PythonTuple utf_32_be_encode(CodeContext context, [NotNull]string #region Private implementation - private static Tuple DoDecode(CodeContext context, string encodingName, Encoding encoding, [BytesConversion]IList input, string? errors, int numBytes) { + private static Tuple DoDecode(CodeContext context, string encodingName, Encoding encoding, [BytesLike]IList input, string? errors, int numBytes) { var decoded = StringOps.DoDecode(context, input, errors, encodingName, encoding, numBytes, out int numConsumed); return Tuple.Create(decoded, numConsumed); } diff --git a/Src/IronPython.Modules/_multiprocessing.cs b/Src/IronPython.Modules/_multiprocessing.cs index 17ca75f6d..6ebf1f0e4 100644 --- a/Src/IronPython.Modules/_multiprocessing.cs +++ b/Src/IronPython.Modules/_multiprocessing.cs @@ -48,7 +48,7 @@ [In] SocketFlags socketFlags ); [PythonHidden(PlatformsAttribute.PlatformFamily.Unix)] - public static int send(int handle, [BytesConversion]IList buf) { + public static int send(int handle, [BytesLike]IList buf) { return send(new IntPtr(handle), buf.ToArray(), buf.Count, 0); } } diff --git a/Src/IronPython.Modules/_pickle.cs b/Src/IronPython.Modules/_pickle.cs index e20421e48..882c6020c 100644 --- a/Src/IronPython.Modules/_pickle.cs +++ b/Src/IronPython.Modules/_pickle.cs @@ -98,7 +98,7 @@ public static object load(CodeContext/*!*/ context, object file) { + "reconstructed object. Characters in the string beyond the end of the first\n" + "pickle are ignored." )] - public static object loads(CodeContext/*!*/ context, [BytesConversion]IList @string) { + public static object loads(CodeContext/*!*/ context, [BytesLike]IList @string) { return new UnpicklerObject(context, new PythonStringInput(PythonOps.MakeString(@string))).load(context); } diff --git a/Src/IronPython.Modules/_struct.cs b/Src/IronPython.Modules/_struct.cs index e356b9ed9..c2750982f 100644 --- a/Src/IronPython.Modules/_struct.cs +++ b/Src/IronPython.Modules/_struct.cs @@ -229,7 +229,7 @@ public void pack_into(CodeContext/*!*/ context, [NotNull]ByteArray/*!*/ buffer, } [Documentation("deserializes the string using the structs specified format")] - public PythonTuple/*!*/ unpack(CodeContext/*!*/ context, [BytesConversion][NotNull]IList @string) { + public PythonTuple/*!*/ unpack(CodeContext/*!*/ context, [BytesLike][NotNull]IList @string) { if (@string.Count != size) { throw Error(context, $"unpack requires a string argument of length {size}"); } @@ -359,7 +359,7 @@ public void pack_into(CodeContext/*!*/ context, [NotNull]ByteArray/*!*/ buffer, => unpack(context, buffer.ToByteArray()); [Documentation("reads the current format from the specified array")] - public PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [BytesConversion][NotNull]IList/*!*/ buffer, int offset = 0) { + public PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [BytesLike][NotNull]IList/*!*/ buffer, int offset = 0) { int bytesAvail = buffer.Count - offset; if (bytesAvail < size) { throw Error(context, $"unpack_from requires a buffer of at least {size} bytes"); @@ -374,7 +374,7 @@ public void pack_into(CodeContext/*!*/ context, [NotNull]ByteArray/*!*/ buffer, } [Documentation("iteratively unpack the current format from the specified array.")] - public PythonUnpackIterator iter_unpack(CodeContext/*!*/ context, [BytesConversion][NotNull]IList/*!*/ buffer, int offset = 0) { + public PythonUnpackIterator iter_unpack(CodeContext/*!*/ context, [BytesLike][NotNull]IList/*!*/ buffer, int offset = 0) { return new PythonUnpackIterator(this, context, buffer, offset); } @@ -810,7 +810,7 @@ public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNull]Byte } [Documentation("Unpack the string containing packed C structure data, according to fmt.\nRequires len(string) == calcsize(fmt).")] - public static PythonTuple/*!*/ unpack(CodeContext/*!*/ context, object fmt, [BytesConversion][NotNull]IList/*!*/ buffer) { + public static PythonTuple/*!*/ unpack(CodeContext/*!*/ context, object fmt, [BytesLike][NotNull]IList/*!*/ buffer) { return GetStructFromCache(context, fmt).unpack(context, buffer); } @@ -820,7 +820,7 @@ public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNull]Byte } [Documentation("Unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")] - public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, object fmt, [BytesConversion][NotNull]IList/*!*/ buffer, int offset = 0) { + public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, object fmt, [BytesLike][NotNull]IList/*!*/ buffer, int offset = 0) { return GetStructFromCache(context, fmt).unpack_from(context, buffer, offset); } @@ -830,7 +830,7 @@ public static void pack_into(CodeContext/*!*/ context, object fmt, [NotNull]Byte } [Documentation("Iteratively unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")] - public static PythonUnpackIterator/*!*/ iter_unpack(CodeContext/*!*/ context, object fmt, [BytesConversion][NotNull]IList/*!*/ buffer, int offset = 0) { + public static PythonUnpackIterator/*!*/ iter_unpack(CodeContext/*!*/ context, object fmt, [BytesLike][NotNull]IList/*!*/ buffer, int offset = 0) { return GetStructFromCache(context, fmt).iter_unpack(context, buffer, offset); } diff --git a/Src/IronPython.Modules/bz2/BZ2Compressor.cs b/Src/IronPython.Modules/bz2/BZ2Compressor.cs index cb8bfa600..4bf3f02bd 100644 --- a/Src/IronPython.Modules/bz2/BZ2Compressor.cs +++ b/Src/IronPython.Modules/bz2/BZ2Compressor.cs @@ -47,7 +47,7 @@ compressed data whenever possible. When you've finished providing data to compress, call the flush() method to finish the compression process, and return what is left in the internal buffers. ")] - public Bytes compress([BytesConversion]IList data) { + public Bytes compress([BytesLike]IList data) { byte[] bytes = data.ToArrayNoCopy(); this.bz2Output.Write(bytes, 0, bytes.Length); diff --git a/Src/IronPython.Modules/bz2/BZ2Decompressor.cs b/Src/IronPython.Modules/bz2/BZ2Decompressor.cs index 905369b67..64ba4b08a 100644 --- a/Src/IronPython.Modules/bz2/BZ2Decompressor.cs +++ b/Src/IronPython.Modules/bz2/BZ2Decompressor.cs @@ -52,7 +52,7 @@ of decompressed data whenever possible. If you try to decompress data was found after the end of stream, it'll be ignored and saved in unused_data attribute. ")] - public Bytes decompress([BytesConversion]IList data) { + public Bytes decompress([BytesLike]IList data) { if (_finished) throw PythonOps.EofError("End of stream was already found"); diff --git a/Src/IronPython.Modules/hashlib/_hashlib.cs b/Src/IronPython.Modules/hashlib/_hashlib.cs index 6080c03d9..a54c0163d 100644 --- a/Src/IronPython.Modules/hashlib/_hashlib.cs +++ b/Src/IronPython.Modules/hashlib/_hashlib.cs @@ -41,7 +41,7 @@ internal HashBase(string name, int blocksize, int digestsize) { protected abstract void CreateHasher(); [Documentation("update(string) -> None (update digest with string data)")] - public void update([BytesConversion]IList newBytes) { + public void update([BytesLike]IList newBytes) { byte[] bytes = newBytes.ToArray(); lock (_hasher) { _hasher.TransformBlock(bytes, 0, bytes.Length, bytes, 0); diff --git a/Src/IronPython.Modules/hashlib/_md5.cs b/Src/IronPython.Modules/hashlib/_md5.cs index cf399d422..0bf53b588 100644 --- a/Src/IronPython.Modules/hashlib/_md5.cs +++ b/Src/IronPython.Modules/hashlib/_md5.cs @@ -34,7 +34,7 @@ public static MD5Object md5(ArrayModule.array data) { } [Documentation("md5([data]) -> object (new md5 object)")] - public static MD5Object md5([BytesConversion]IList data) { + public static MD5Object md5([BytesLike]IList data) { return new MD5Object(data); } diff --git a/Src/IronPython.Modules/hashlib/_sha1.cs b/Src/IronPython.Modules/hashlib/_sha1.cs index 944d6b6b3..20f4a66c1 100644 --- a/Src/IronPython.Modules/hashlib/_sha1.cs +++ b/Src/IronPython.Modules/hashlib/_sha1.cs @@ -44,7 +44,7 @@ public static sha sha1(ArrayModule.array data) { } [Documentation("sha1([data]) -> object (object used to calculate hash)")] - public static sha sha1([BytesConversion]IList data) { + public static sha sha1([BytesLike]IList data) { return new sha(data); } diff --git a/Src/IronPython.Modules/hashlib/_sha256.cs b/Src/IronPython.Modules/hashlib/_sha256.cs index c0f399489..eb9568749 100644 --- a/Src/IronPython.Modules/hashlib/_sha256.cs +++ b/Src/IronPython.Modules/hashlib/_sha256.cs @@ -28,7 +28,7 @@ public static Sha256Object sha256(ArrayModule.array data) { return new Sha256Object(data.ToByteArray()); } - public static Sha256Object sha256([BytesConversion]IList data) { + public static Sha256Object sha256([BytesLike]IList data) { return new Sha256Object(data); } @@ -64,7 +64,7 @@ public static Sha224Object sha224(ArrayModule.array data) { return new Sha224Object(data.ToByteArray()); } - public static Sha224Object sha224([BytesConversion]IList data) { + public static Sha224Object sha224([BytesLike]IList data) { return new Sha224Object(data); } diff --git a/Src/IronPython.Modules/hashlib/_sha512.cs b/Src/IronPython.Modules/hashlib/_sha512.cs index 09d2518fb..3f5de231e 100644 --- a/Src/IronPython.Modules/hashlib/_sha512.cs +++ b/Src/IronPython.Modules/hashlib/_sha512.cs @@ -28,7 +28,7 @@ public static Sha512Object sha512(ArrayModule.array data) { return new Sha512Object(data.ToByteArray()); } - public static Sha512Object sha512([BytesConversion]IList data) { + public static Sha512Object sha512([BytesLike]IList data) { return new Sha512Object(data); } @@ -44,7 +44,7 @@ public static Sha384Object sha384(ArrayModule.array data) { return new Sha384Object(data.ToByteArray()); } - public static Sha384Object sha384([BytesConversion]IList data) { + public static Sha384Object sha384([BytesLike]IList data) { return new Sha384Object(data); } diff --git a/Src/IronPython.Modules/marshal.cs b/Src/IronPython.Modules/marshal.cs index 9f776a469..a07325644 100644 --- a/Src/IronPython.Modules/marshal.cs +++ b/Src/IronPython.Modules/marshal.cs @@ -30,7 +30,7 @@ public static Bytes dumps(object value, int version = PythonMarshal.version) { return Bytes.Make(MarshalOps.GetBytes(value, version)); } - public static object loads([BytesConversion]IList bytes) { + public static object loads([BytesLike]IList bytes) { return MarshalOps.GetObject(bytes.GetEnumerator()); } diff --git a/Src/IronPython.Modules/mmap.cs b/Src/IronPython.Modules/mmap.cs index 72aa50141..0ba6e345b 100644 --- a/Src/IronPython.Modules/mmap.cs +++ b/Src/IronPython.Modules/mmap.cs @@ -724,7 +724,7 @@ public object tell() { } } - public void write([BytesConversion]IList s) { + public void write([BytesLike]IList s) { using (new MmapLocker(this)) { EnsureWritable(); diff --git a/Src/IronPython.Modules/nt.cs b/Src/IronPython.Modules/nt.cs index e474316b5..e47c6ca3f 100644 --- a/Src/IronPython.Modules/nt.cs +++ b/Src/IronPython.Modules/nt.cs @@ -388,7 +388,7 @@ public static int getpid() { } #endif - public static PythonList listdir(CodeContext/*!*/ context, [BytesConversion][NotNull]IList path) + public static PythonList listdir(CodeContext/*!*/ context, [BytesLike][NotNull]IList path) => listdir(context, PythonOps.MakeString(path)); public static PythonList listdir(CodeContext/*!*/ context, string path = null) { @@ -426,7 +426,7 @@ public static object lstat(string path) { } [LightThrowing] - public static object lstat([BytesConversion]IList path) + public static object lstat([BytesLike]IList path) => lstat(PythonOps.MakeString(path)); @@ -1052,7 +1052,7 @@ public static object stat(string path, [ParamDictionary]IDictionary path, [ParamDictionary]IDictionary dict) + public static object stat([BytesLike]IList path, [ParamDictionary]IDictionary dict) => stat(PythonOps.MakeString(path), dict); public static string strerror(int code) { @@ -1305,7 +1305,7 @@ public static PythonTuple waitpid(int pid, object options) { } #endif - public static int write(CodeContext/*!*/ context, int fd, [BytesConversion]IList text) { + public static int write(CodeContext/*!*/ context, int fd, [BytesLike]IList text) { try { PythonContext pythonContext = context.LanguageContext; var pf = pythonContext.FileManager.GetFileFromId(pythonContext, fd); diff --git a/Src/IronPython.Modules/pyexpat.cs b/Src/IronPython.Modules/pyexpat.cs index 45dbbc533..2f846359e 100644 --- a/Src/IronPython.Modules/pyexpat.cs +++ b/Src/IronPython.Modules/pyexpat.cs @@ -598,7 +598,7 @@ public void Parse(CodeContext context, string data, bool isfinal = false) { Parse(context, bytes, isfinal); } - public void Parse(CodeContext context, [BytesConversion]IList data, bool isfinal = false) { + public void Parse(CodeContext context, [BytesLike]IList data, bool isfinal = false) { CheckParsingDone(context); if (data.Any()) { diff --git a/Src/IronPython.Modules/zlib/Compress.cs b/Src/IronPython.Modules/zlib/Compress.cs index 4c1cb0157..25ca57cea 100644 --- a/Src/IronPython.Modules/zlib/Compress.cs +++ b/Src/IronPython.Modules/zlib/Compress.cs @@ -49,7 +49,7 @@ internal Compress(int level, int method, int wbits, int memlevel, int strategy) After calling this function, some of the input data may still be stored in internal buffers for later processing. Call the flush() method to clear these buffers.")] - public Bytes compress([BytesConversion]IList data) + public Bytes compress([BytesLike]IList data) { byte[] input = data.ToArray(); byte[] output = new byte[ZlibModule.DEFAULTALLOC]; diff --git a/Src/IronPython.Modules/zlib/Decompress.cs b/Src/IronPython.Modules/zlib/Decompress.cs index f8ac24b8b..a24c98a7a 100644 --- a/Src/IronPython.Modules/zlib/Decompress.cs +++ b/Src/IronPython.Modules/zlib/Decompress.cs @@ -59,7 +59,7 @@ Call the flush() method to clear these buffers. If the max_length parameter is specified then the return value will be no longer than max_length. Unconsumed input data will be stored in the unconsumed_tail attribute.")] - public Bytes decompress([BytesConversion]IList value, int max_length=0) + public Bytes decompress([BytesLike]IList value, int max_length=0) { if(max_length < 0) throw new ArgumentException("max_length must be greater than zero"); diff --git a/Src/IronPython.Modules/zlib/ZlibModule.cs b/Src/IronPython.Modules/zlib/ZlibModule.cs index 259a2dbfe..69b6f9476 100644 --- a/Src/IronPython.Modules/zlib/ZlibModule.cs +++ b/Src/IronPython.Modules/zlib/ZlibModule.cs @@ -73,7 +73,7 @@ public static class ZlibModule An optional starting value can be specified. The returned checksum is a signed integer.")] - public static int adler32([BytesConversion]IList data, long baseValue=1L) + public static int adler32([BytesLike]IList data, long baseValue=1L) { return (int)Adler32.GetAdler32Checksum(baseValue, data.ToArray(), 0, data.Count); } @@ -82,7 +82,7 @@ public static int adler32([BytesConversion]IList data, long baseValue=1L) An optional starting value can be specified. The returned checksum is a signed integer.")] - public static BigInteger crc32([BytesConversion]IList data, long baseValue=0L) + public static BigInteger crc32([BytesLike]IList data, long baseValue=0L) { if(baseValue < int.MinValue || baseValue > uint.MaxValue) throw new ArgumentOutOfRangeException(nameof(baseValue)); @@ -93,7 +93,7 @@ public static BigInteger crc32([BytesConversion]IList data, long baseValue [Documentation(@"compress(string[, level]) -- Returned compressed string. Optional arg level is the compression level, in 1-9.")] - public static Bytes compress([BytesConversion]IList data, + public static Bytes compress([BytesLike]IList data, int level=Z_DEFAULT_COMPRESSION) { byte[] input = data.ToArray(); @@ -158,7 +158,7 @@ public static Compress compressobj( Optional arg wbits is the window buffer size. Optional arg bufsize is the initial output buffer size.")] - public static Bytes decompress([BytesConversion]IList data, + public static Bytes decompress([BytesLike]IList data, int wbits=MAX_WBITS, int bufsize=DEFAULTALLOC) { diff --git a/Src/IronPython/Modules/Builtin.cs b/Src/IronPython/Modules/Builtin.cs index f3103e9f6..29afcfc54 100644 --- a/Src/IronPython/Modules/Builtin.cs +++ b/Src/IronPython/Modules/Builtin.cs @@ -214,7 +214,7 @@ public static object compile(CodeContext/*!*/ context, _ast.AST source, string f } [Documentation("")] // provided by first overload - public static object compile(CodeContext/*!*/ context, [BytesConversion]IList source, string filename, string mode, object flags = null, object dont_inherit = null, int optimize = -1) { + public static object compile(CodeContext/*!*/ context, [BytesLike]IList source, string filename, string mode, object flags = null, object dont_inherit = null, int optimize = -1) { // TODO: implement optimize var sourceCodeKind = ValidateCompileMode(mode); @@ -338,7 +338,7 @@ public static object eval(CodeContext/*!*/ context, [NotNull]FunctionCode code, [Documentation("")] // provided by first overload [LightThrowing] - public static object eval(CodeContext/*!*/ context, [BytesConversion, NotNull]IList expression, PythonDictionary globals = null, object locals = null) { + public static object eval(CodeContext/*!*/ context, [BytesLike, NotNull]IList expression, PythonDictionary globals = null, object locals = null) { if (locals != null && !PythonOps.IsMappingType(context, locals)) { throw PythonOps.TypeError("locals must be mapping"); } @@ -415,7 +415,7 @@ public static void exec(CodeContext/*!*/ context, [NotNull]string code, PythonDi } [Documentation("")] // provided by first overload - public static void exec(CodeContext/*!*/ context, [BytesConversion, NotNull]IList code, PythonDictionary globals = null, object locals = null) { + public static void exec(CodeContext/*!*/ context, [BytesLike, NotNull]IList code, PythonDictionary globals = null, object locals = null) { byte[] bytes = code as byte[] ?? ((code is Bytes b) ? b.UnsafeByteArray : code.ToArray()); SourceUnit source = context.LanguageContext.CreateSourceUnit( new MemoryStreamContentProvider(context.LanguageContext, bytes, ""), diff --git a/Src/IronPython/Modules/array.cs b/Src/IronPython/Modules/array.cs index 6f006092a..4dae95180 100644 --- a/Src/IronPython/Modules/array.cs +++ b/Src/IronPython/Modules/array.cs @@ -123,7 +123,7 @@ public array([NotNull]string type) { _data = CreateData(_typeCode); } - public array([NotNull]string type, [BytesConversion, NotNull]IList initializer) : this(type) { + public array([NotNull]string type, [BytesLike, NotNull]IList initializer) : this(type) { frombytes(initializer); } @@ -332,7 +332,7 @@ public void fromfile(CodeContext/*!*/ context, [NotNull]PythonIOModule._IOBase f if (bytes.Count < bytesNeeded) throw PythonOps.EofError("file not large enough"); } - public void frombytes([BytesConversion, NotNull]IList s) { + public void frombytes([BytesLike, NotNull]IList s) { if ((s.Count % itemsize) != 0) throw PythonOps.ValueError("bytes length not a multiple of itemsize"); if (s is Bytes b) { diff --git a/Src/IronPython/Runtime/Binding/PythonOverloadResolver.cs b/Src/IronPython/Runtime/Binding/PythonOverloadResolver.cs index 7c6a477b2..5fa6cad00 100644 --- a/Src/IronPython/Runtime/Binding/PythonOverloadResolver.cs +++ b/Src/IronPython/Runtime/Binding/PythonOverloadResolver.cs @@ -69,7 +69,7 @@ public override bool CanConvertFrom(Type fromType, DynamicMetaObject fromArg, Pa if ((fromType == typeof(PythonList) || fromType.IsSubclassOf(typeof(PythonList)))) { if (toParameter.Type.IsGenericType && toParameter.Type.GetGenericTypeDefinition() == typeof(IList<>) && - toParameter.ParameterInfo.IsDefined(typeof(BytesConversionAttribute), false)) { + toParameter.ParameterInfo.IsDefined(typeof(BytesLikeAttribute), false)) { return false; } } diff --git a/Src/IronPython/Runtime/ByteArray.cs b/Src/IronPython/Runtime/ByteArray.cs index 64f120925..50028dfe0 100644 --- a/Src/IronPython/Runtime/ByteArray.cs +++ b/Src/IronPython/Runtime/ByteArray.cs @@ -237,7 +237,7 @@ public ByteArray capitalize() { public ByteArray center(int width) => center(width, (byte)' '); - public ByteArray center(int width, [BytesConversion, NotNull]IList fillchar) + public ByteArray center(int width, [BytesLike, NotNull]IList fillchar) => center(width, fillchar.ToByte("center", 2)); private ByteArray center(int width, byte fillchar) { @@ -251,11 +251,11 @@ private ByteArray center(int width, byte fillchar) { public ByteArray copy() => CopyThis(); - public int count([BytesConversion, NotNull]IList sub) => count(sub, null, null); + public int count([BytesLike, NotNull]IList sub) => count(sub, null, null); - public int count([BytesConversion, NotNull]IList sub, int? start) => count(sub, start, null); + public int count([BytesLike, NotNull]IList sub, int? start) => count(sub, start, null); - public int count([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int count([BytesLike, NotNull]IList sub, int? start, int? end) { lock (this) { return _bytes.CountOf(sub, start ?? 0, end ?? _bytes.Count); } @@ -279,19 +279,19 @@ public string decode(CodeContext context, [NotNull]Encoding encoding, [NotNull]s } } - public bool endswith([BytesConversion, NotNull]IList suffix) { + public bool endswith([BytesLike, NotNull]IList suffix) { lock (this) { return _bytes.EndsWith(suffix); } } - public bool endswith([BytesConversion, NotNull]IList suffix, int start) { + public bool endswith([BytesLike, NotNull]IList suffix, int start) { lock (this) { return _bytes.EndsWith(suffix, start); } } - public bool endswith([BytesConversion, NotNull]IList suffix, int start, int end) { + public bool endswith([BytesLike, NotNull]IList suffix, int start, int end) { lock (this) { return _bytes.EndsWith(suffix, start, end); } @@ -325,11 +325,11 @@ public ByteArray expandtabs(int tabsize) { } } - public int find([BytesConversion, NotNull]IList sub) => find(sub, null, null); + public int find([BytesLike, NotNull]IList sub) => find(sub, null, null); - public int find([BytesConversion, NotNull]IList sub, int start) => find(sub, start, null); + public int find([BytesLike, NotNull]IList sub, int start) => find(sub, start, null); - public int find([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int find([BytesLike, NotNull]IList sub, int? start, int? end) { lock (this) { return _bytes.Find(sub, start, end); } @@ -349,11 +349,11 @@ public static ByteArray fromhex([NotNull]string @string) { return new ByteArray(IListOfByteOps.FromHex(@string)); } - public int index([BytesConversion, NotNull]IList sub) => index(sub, null, null); + public int index([BytesLike, NotNull]IList sub) => index(sub, null, null); - public int index([BytesConversion, NotNull]IList sub, int? start) => index(sub, start, null); + public int index([BytesLike, NotNull]IList sub, int? start) => index(sub, start, null); - public int index([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int index([BytesLike, NotNull]IList sub, int? start, int? end) { lock (this) { int res = find(sub, start, end); if (res == -1) { @@ -485,7 +485,7 @@ public ByteArray ljust(int width) { return ljust(width, (byte)' '); } - public ByteArray ljust(int width, [BytesConversion, NotNull]IList fillchar) { + public ByteArray ljust(int width, [BytesLike, NotNull]IList fillchar) { return ljust(width, fillchar.ToByte("ljust", 2)); } @@ -515,7 +515,7 @@ public ByteArray lstrip() { } } - public ByteArray lstrip([BytesConversion]IList? chars) { + public ByteArray lstrip([BytesLike]IList? chars) { if (chars == null) return lstrip(); lock (this) { var res = _bytes.LeftStrip(chars); @@ -523,10 +523,10 @@ public ByteArray lstrip([BytesConversion]IList? chars) { } } - public static Bytes maketrans([BytesConversion, NotNull]IList from, [BytesConversion, NotNull]IList to) + public static Bytes maketrans([BytesLike, NotNull]IList from, [BytesLike, NotNull]IList to) => Bytes.maketrans(from, to); - public PythonTuple partition([BytesConversion, NotNull]IList sep) { + public PythonTuple partition([BytesLike, NotNull]IList sep) { if (sep.Count == 0) { throw PythonOps.ValueError("empty separator"); } @@ -547,7 +547,7 @@ public PythonTuple partition([BytesConversion, NotNull]IList sep) { return new PythonTuple(obj); } - public ByteArray replace([BytesConversion, NotNull]IList old, [BytesConversion, NotNull]IList @new, int count = -1) { + public ByteArray replace([BytesLike, NotNull]IList old, [BytesLike, NotNull]IList @new, int count = -1) { if (count == 0) { return CopyThis(); } @@ -555,11 +555,11 @@ public ByteArray replace([BytesConversion, NotNull]IList old, [BytesConver return new ByteArray(_bytes.Replace(old, @new, count)); } - public int rfind([BytesConversion, NotNull]IList sub) => rfind(sub, null, null); + public int rfind([BytesLike, NotNull]IList sub) => rfind(sub, null, null); - public int rfind([BytesConversion, NotNull]IList sub, int? start) => rfind(sub, start, null); + public int rfind([BytesLike, NotNull]IList sub, int? start) => rfind(sub, start, null); - public int rfind([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int rfind([BytesLike, NotNull]IList sub, int? start, int? end) { lock (this) { return _bytes.ReverseFind(sub, start, end); } @@ -571,11 +571,11 @@ public int rfind([BytesConversion, NotNull]IList sub, int? start, int? end public int rfind(int @byte, int? start, int? end) => rfind(new[] { @byte.ToByteChecked() }, start, end); - public int rindex([BytesConversion, NotNull]IList sub) => rindex(sub, null, null); + public int rindex([BytesLike, NotNull]IList sub) => rindex(sub, null, null); - public int rindex([BytesConversion, NotNull]IList sub, int? start) => rindex(sub, start, null); + public int rindex([BytesLike, NotNull]IList sub, int? start) => rindex(sub, start, null); - public int rindex([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int rindex([BytesLike, NotNull]IList sub, int? start, int? end) { int ret = rfind(sub, start, end); if (ret == -1) { throw PythonOps.ValueError("subsection not found"); @@ -594,7 +594,7 @@ public ByteArray rjust(int width) { return rjust(width, (byte)' '); } - public ByteArray rjust(int width, [BytesConversion, NotNull]IList fillchar) { + public ByteArray rjust(int width, [BytesLike, NotNull]IList fillchar) { return rjust(width, fillchar.ToByte("rjust", 2)); } @@ -616,7 +616,7 @@ private ByteArray rjust(int width, int fillchar) { } } - public PythonTuple rpartition([BytesConversion, NotNull]IList sep) { + public PythonTuple rpartition([BytesLike, NotNull]IList sep) { if (sep.Count == 0) { throw PythonOps.ValueError("empty separator"); } @@ -643,11 +643,11 @@ public PythonList rsplit() { } } - public PythonList rsplit([BytesConversion]IList? sep) { + public PythonList rsplit([BytesLike]IList? sep) { return rsplit(sep, -1); } - public PythonList rsplit([BytesConversion]IList? sep, int maxsplit) { + public PythonList rsplit([BytesLike]IList? sep, int maxsplit) { return _bytes.RightSplit(sep, maxsplit, x => new ByteArray(new List(x))); } @@ -658,7 +658,7 @@ public ByteArray rstrip() { } } - public ByteArray rstrip([BytesConversion]IList? chars) { + public ByteArray rstrip([BytesLike]IList? chars) { if (chars == null) return rstrip(); lock (this) { var res = _bytes.RightStrip(chars); @@ -672,11 +672,11 @@ public PythonList split() { } } - public PythonList split([BytesConversion]IList? sep) { + public PythonList split([BytesLike]IList? sep) { return split(sep, -1); } - public PythonList split([BytesConversion]IList? sep, int maxsplit) { + public PythonList split([BytesLike]IList? sep, int maxsplit) { lock (this) { return _bytes.Split(sep, maxsplit, x => new ByteArray(x)); } @@ -692,13 +692,13 @@ public PythonList splitlines(bool keepends) { } } - public bool startswith([BytesConversion, NotNull]IList prefix) { + public bool startswith([BytesLike, NotNull]IList prefix) { lock (this) { return _bytes.StartsWith(prefix); } } - public bool startswith([BytesConversion, NotNull]IList prefix, int start) { + public bool startswith([BytesLike, NotNull]IList prefix, int start) { lock (this) { int len = Count; if (start > len) { @@ -711,7 +711,7 @@ public bool startswith([BytesConversion, NotNull]IList prefix, int start) } } - public bool startswith([BytesConversion, NotNull]IList prefix, int start, int end) { + public bool startswith([BytesLike, NotNull]IList prefix, int start, int end) { lock (this) { return _bytes.StartsWith(prefix, start, end); } @@ -742,7 +742,7 @@ public ByteArray strip() { } } - public ByteArray strip([BytesConversion]IList? chars) { + public ByteArray strip([BytesLike]IList? chars) { if (chars == null) return strip(); lock (this) { var res = _bytes.Strip(chars); @@ -763,7 +763,7 @@ public ByteArray title() { } } - public ByteArray translate([BytesConversion]IList? table) { + public ByteArray translate([BytesLike]IList? table) { lock (this) { if (table != null) { if (table.Count != 256) { @@ -778,7 +778,7 @@ public ByteArray translate([BytesConversion]IList? table) { } - public ByteArray translate([BytesConversion]IList? table, [BytesConversion, NotNull]IList delete) { + public ByteArray translate([BytesLike]IList? table, [BytesLike, NotNull]IList delete) { lock (this) { return new ByteArray(_bytes.Translate(table, delete)); } @@ -809,7 +809,7 @@ public int __alloc__() { return _bytes.Count + 1; } - public bool __contains__([BytesConversion, NotNull]IList bytes) { + public bool __contains__([BytesLike, NotNull]IList bytes) { return this.IndexOf(bytes, 0) != -1; } diff --git a/Src/IronPython/Runtime/Bytes.cs b/Src/IronPython/Runtime/Bytes.cs index 1250c0c0f..c05c39b10 100644 --- a/Src/IronPython/Runtime/Bytes.cs +++ b/Src/IronPython/Runtime/Bytes.cs @@ -36,7 +36,7 @@ public Bytes([NotNull]IEnumerable source) { _bytes = source.Select(b => ((int)PythonOps.Index(b)).ToByteChecked()).ToArray(); } - public Bytes([BytesConversion, NotNull]IList bytes) { + public Bytes([BytesLike, NotNull]IList bytes) { _bytes = bytes.ToArray(); } @@ -87,7 +87,7 @@ public Bytes capitalize() { public Bytes center(int width) => center(width, (byte)' '); - public Bytes center(int width, [BytesConversion, NotNull]IList fillchar) + public Bytes center(int width, [BytesLike, NotNull]IList fillchar) => center(width, fillchar.ToByte("center", 2)); private Bytes center(int width, byte fillchar) { @@ -95,11 +95,11 @@ private Bytes center(int width, byte fillchar) { return res == null ? this : new Bytes(res); } - public int count([BytesConversion, NotNull]IList sub) => count(sub, null, null); + public int count([BytesLike, NotNull]IList sub) => count(sub, null, null); - public int count([BytesConversion, NotNull]IList sub, int? start) => count(sub, start, null); + public int count([BytesLike, NotNull]IList sub, int? start) => count(sub, start, null); - public int count([BytesConversion, NotNull]IList sub, int? start, int? end) + public int count([BytesLike, NotNull]IList sub, int? start, int? end) => _bytes.CountOf(sub, start ?? 0, end ?? Count); public int count(int @byte) => count(@byte, null, null); @@ -117,15 +117,15 @@ public string decode(CodeContext context, [NotNull]Encoding encoding, [NotNull]s return StringOps.DoDecode(context, _bytes, errors, StringOps.GetEncodingName(encoding, normalize: false), encoding); } - public bool endswith([BytesConversion, NotNull]IList suffix) { + public bool endswith([BytesLike, NotNull]IList suffix) { return _bytes.EndsWith(suffix); } - public bool endswith([BytesConversion, NotNull]IList suffix, int start) { + public bool endswith([BytesLike, NotNull]IList suffix, int start) { return _bytes.EndsWith(suffix, start); } - public bool endswith([BytesConversion, NotNull]IList suffix, int start, int end) { + public bool endswith([BytesLike, NotNull]IList suffix, int start, int end) { return _bytes.EndsWith(suffix, start, end); } @@ -149,11 +149,11 @@ public Bytes expandtabs(int tabsize) { return new Bytes(_bytes.ExpandTabs(tabsize)); } - public int find([BytesConversion, NotNull]IList sub) => find(sub, null, null); + public int find([BytesLike, NotNull]IList sub) => find(sub, null, null); - public int find([BytesConversion, NotNull]IList sub, int? start) => find(sub, start, null); + public int find([BytesLike, NotNull]IList sub, int? start) => find(sub, start, null); - public int find([BytesConversion, NotNull]IList sub, int? start, int? end) + public int find([BytesLike, NotNull]IList sub, int? start, int? end) => _bytes.Find(sub, start, end); public int find(int @byte) => find(@byte, null, null); @@ -167,11 +167,11 @@ public static Bytes fromhex([NotNull]string @string) { return new Bytes(IListOfByteOps.FromHex(@string)); } - public int index([BytesConversion, NotNull]IList sub) => index(sub, null, null); + public int index([BytesLike, NotNull]IList sub) => index(sub, null, null); - public int index([BytesConversion, NotNull]IList sub, int? start) => index(sub, start, null); + public int index([BytesLike, NotNull]IList sub, int? start) => index(sub, start, null); - public int index([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int index([BytesLike, NotNull]IList sub, int? start, int? end) { int res = find(sub, start, end); if (res == -1) { throw PythonOps.ValueError("subsection not found"); @@ -267,7 +267,7 @@ public Bytes ljust(int width) { return ljust(width, (byte)' '); } - public Bytes ljust(int width, [BytesConversion, NotNull]IList fillchar) { + public Bytes ljust(int width, [BytesLike, NotNull]IList fillchar) { return ljust(width, fillchar.ToByte("ljust", 2)); } @@ -294,13 +294,13 @@ public Bytes lstrip() { return res == null ? this : new Bytes(res); } - public Bytes lstrip([BytesConversion]IList? chars) { + public Bytes lstrip([BytesLike]IList? chars) { if (chars == null) return lstrip(); var res = _bytes.LeftStrip(chars); return res == null ? this : new Bytes(res); } - public static Bytes maketrans([BytesConversion, NotNull]IList from, [BytesConversion, NotNull]IList to) { + public static Bytes maketrans([BytesLike, NotNull]IList from, [BytesLike, NotNull]IList to) { if (from.Count != to.Count) throw PythonOps.ValueError("maketrans arguments must have same length"); var bytes = new byte[256]; @@ -313,7 +313,7 @@ public static Bytes maketrans([BytesConversion, NotNull]IList from, [Bytes return Make(bytes); } - public PythonTuple partition([BytesConversion, NotNull]IList sep) { + public PythonTuple partition([BytesLike, NotNull]IList sep) { if (sep.Count == 0) { throw PythonOps.ValueError("empty separator"); } @@ -334,7 +334,7 @@ public PythonTuple partition([BytesConversion, NotNull]IList sep) { return new PythonTuple(obj); } - public Bytes replace([BytesConversion, NotNull]IList old, [BytesConversion, NotNull]IList @new, int count = -1) { + public Bytes replace([BytesLike, NotNull]IList old, [BytesLike, NotNull]IList @new, int count = -1) { if (count == 0) { return this; } @@ -342,11 +342,11 @@ public Bytes replace([BytesConversion, NotNull]IList old, [BytesConversion return new Bytes(_bytes.Replace(old, @new, count)); } - public int rfind([BytesConversion, NotNull]IList sub) => rfind(sub, null, null); + public int rfind([BytesLike, NotNull]IList sub) => rfind(sub, null, null); - public int rfind([BytesConversion, NotNull]IList sub, int? start) => rfind(sub, start, null); + public int rfind([BytesLike, NotNull]IList sub, int? start) => rfind(sub, start, null); - public int rfind([BytesConversion, NotNull]IList sub, int? start, int? end) + public int rfind([BytesLike, NotNull]IList sub, int? start, int? end) => _bytes.ReverseFind(sub, start, end); public int rfind(int @byte) => rfind(@byte, null, null); @@ -356,11 +356,11 @@ public int rfind([BytesConversion, NotNull]IList sub, int? start, int? end public int rfind(int @byte, int? start, int? end) => rfind(new[] { @byte.ToByteChecked() }, start, end); - public int rindex([BytesConversion, NotNull]IList sub) => rindex(sub, null, null); + public int rindex([BytesLike, NotNull]IList sub) => rindex(sub, null, null); - public int rindex([BytesConversion, NotNull]IList sub, int? start) => rindex(sub, start, null); + public int rindex([BytesLike, NotNull]IList sub, int? start) => rindex(sub, start, null); - public int rindex([BytesConversion, NotNull]IList sub, int? start, int? end) { + public int rindex([BytesLike, NotNull]IList sub, int? start, int? end) { int ret = rfind(sub, start, end); if (ret == -1) { throw PythonOps.ValueError("subsection not found"); @@ -379,7 +379,7 @@ public Bytes rjust(int width) { return rjust(width, (byte)' '); } - public Bytes rjust(int width, [BytesConversion, NotNull]IList fillchar) { + public Bytes rjust(int width, [BytesLike, NotNull]IList fillchar) { return rjust(width, fillchar.ToByte("rjust", 2)); } @@ -397,7 +397,7 @@ private Bytes rjust(int width, byte fillchar) { return new Bytes(ret); } - public PythonTuple rpartition([BytesConversion, NotNull]IList sep) { + public PythonTuple rpartition([BytesLike, NotNull]IList sep) { if (sep.Count == 0) { throw PythonOps.ValueError("empty separator"); } @@ -420,11 +420,11 @@ public PythonList rsplit() { return _bytes.SplitInternal(null, -1, x => new Bytes(x)); } - public PythonList rsplit([BytesConversion]IList? sep) { + public PythonList rsplit([BytesLike]IList? sep) { return rsplit(sep, -1); } - public PythonList rsplit([BytesConversion]IList? sep, int maxsplit) { + public PythonList rsplit([BytesLike]IList? sep, int maxsplit) { return _bytes.RightSplit(sep, maxsplit, x => new Bytes(new List(x))); } @@ -433,7 +433,7 @@ public Bytes rstrip() { return res == null ? this : new Bytes(res); } - public Bytes rstrip([BytesConversion]IList? chars) { + public Bytes rstrip([BytesLike]IList? chars) { if (chars == null) return rstrip(); var res = _bytes.RightStrip(chars); return res == null ? this : new Bytes(res); @@ -443,11 +443,11 @@ public PythonList split() { return _bytes.SplitInternal(null, -1, x => new Bytes(x)); } - public PythonList split([BytesConversion]IList? sep) { + public PythonList split([BytesLike]IList? sep) { return split(sep, -1); } - public PythonList split([BytesConversion]IList? sep, int maxsplit) { + public PythonList split([BytesLike]IList? sep, int maxsplit) { return _bytes.Split(sep, maxsplit, x => new Bytes(x)); } @@ -459,11 +459,11 @@ public PythonList splitlines(bool keepends) { return _bytes.SplitLines(keepends, x => new Bytes(x)); } - public bool startswith([BytesConversion, NotNull]IList prefix) { + public bool startswith([BytesLike, NotNull]IList prefix) { return _bytes.StartsWith(prefix); } - public bool startswith([BytesConversion, NotNull]IList prefix, int start) { + public bool startswith([BytesLike, NotNull]IList prefix, int start) { int len = Count; if (start > len) return false; if (start < 0) { @@ -473,7 +473,7 @@ public bool startswith([BytesConversion, NotNull]IList prefix, int start) return _bytes.Substring(start).StartsWith(prefix); } - public bool startswith([BytesConversion, NotNull]IList prefix, int start, int end) { + public bool startswith([BytesLike, NotNull]IList prefix, int start, int end) { return _bytes.StartsWith(prefix, start, end); } @@ -494,7 +494,7 @@ public Bytes strip() { return res == null ? this : new Bytes(res); } - public Bytes strip([BytesConversion]IList? chars) { + public Bytes strip([BytesLike]IList? chars) { if (chars == null) return strip(); var res = _bytes.Strip(chars); return res == null ? this : new Bytes(res); @@ -509,7 +509,7 @@ public Bytes title() { return res == null ? this : new Bytes(res); } - public Bytes translate([BytesConversion]IList? table) { + public Bytes translate([BytesLike]IList? table) { if (table == null) { return this; } else if (table.Count != 256) { @@ -521,7 +521,7 @@ public Bytes translate([BytesConversion]IList? table) { return new Bytes(_bytes.Translate(table, null)); } - public Bytes translate([BytesConversion]IList? table, [BytesConversion, NotNull]IList delete) { + public Bytes translate([BytesLike]IList? table, [BytesLike, NotNull]IList delete) { if (Count == 0) { return this; } @@ -542,7 +542,7 @@ public Bytes zfill(int width) { return new Bytes(_bytes.ZeroFill(width, spaces)); } - public bool __contains__([BytesConversion, NotNull]IList bytes) { + public bool __contains__([BytesLike, NotNull]IList bytes) { return this.IndexOf(bytes, 0) != -1; } diff --git a/Src/IronPython/Runtime/BytesConversionAttribute.cs b/Src/IronPython/Runtime/BytesConversionAttribute.cs deleted file mode 100644 index 6e59fd7ce..000000000 --- a/Src/IronPython/Runtime/BytesConversionAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information. - -using System; - -namespace IronPython.Runtime { - /// - /// For IList arguments: Marks that the argument is typed to accept a bytes or - /// bytearray object. This attribute disallows passing a Python list object and - /// auto-applying our generic conversion. - /// - [AttributeUsage(AttributeTargets.Parameter)] - public sealed class BytesConversionAttribute : Attribute { } -} diff --git a/Src/IronPython/Runtime/Operations/IntOps.cs b/Src/IronPython/Runtime/Operations/IntOps.cs index ebb86d44a..136ec584c 100644 --- a/Src/IronPython/Runtime/Operations/IntOps.cs +++ b/Src/IronPython/Runtime/Operations/IntOps.cs @@ -528,7 +528,7 @@ public static Bytes to_bytes(Int32 value, int length, string byteorder, bool sig return Bytes.Make(res.ToArray()); } - public static BigInteger from_bytes([BytesConversion]IList bytes, string byteorder, bool signed=false) { + public static BigInteger from_bytes([BytesLike]IList bytes, string byteorder, bool signed=false) { // TODO: signed should be a keyword only argument // TODO: return int when possible? diff --git a/Src/IronPython/Runtime/Operations/StringOps.cs b/Src/IronPython/Runtime/Operations/StringOps.cs index f126eac5a..3fd62368d 100644 --- a/Src/IronPython/Runtime/Operations/StringOps.cs +++ b/Src/IronPython/Runtime/Operations/StringOps.cs @@ -288,7 +288,7 @@ public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull] } [StaticExtensionMethod] - public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull][BytesConversion]IList @object) { + public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull][BytesLike]IList @object) { if (cls == TypeCache.String) { return FastNew(context, @object); } else { @@ -297,7 +297,7 @@ public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull] } [StaticExtensionMethod] - public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull][BytesConversion]IList @object, string encoding = "utf-8", string errors = "strict") { + public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull][BytesLike]IList @object, string encoding = "utf-8", string errors = "strict") { if (cls == TypeCache.String) { if (@object is Bytes) return ((Bytes)@object).decode(context, encoding, errors); return new Bytes(@object).decode(context, encoding, errors); diff --git a/Src/IronPython/Runtime/Types/NewTypeMaker.cs b/Src/IronPython/Runtime/Types/NewTypeMaker.cs index 9f548478e..ac60f4062 100644 --- a/Src/IronPython/Runtime/Types/NewTypeMaker.cs +++ b/Src/IronPython/Runtime/Types/NewTypeMaker.cs @@ -410,17 +410,16 @@ private void OverrideConstructor(ConstructorInfo parentConstructor) { ParameterInfo pi = pis[origIndex]; // Defines attributes that might be used in .net methods for overload detection and other - // parameter based attribute logic. E.g. [BytesConversionAttribute] to make - // enable automatic cast between .net IList and string + // parameter based attribute logic if (pi.IsDefined(typeof(ParamArrayAttribute), false)) { pb.SetCustomAttribute(new CustomAttributeBuilder( typeof(ParamArrayAttribute).GetConstructor(ReflectionUtils.EmptyTypes), ArrayUtils.EmptyObjects)); } else if (pi.IsDefined(typeof(ParamDictionaryAttribute), false)) { pb.SetCustomAttribute(new CustomAttributeBuilder( typeof(ParamDictionaryAttribute).GetConstructor(ReflectionUtils.EmptyTypes), ArrayUtils.EmptyObjects)); - } else if (pi.IsDefined(typeof(BytesConversionAttribute), false)) { + } else if (pi.IsDefined(typeof(BytesLikeAttribute), false)) { pb.SetCustomAttribute(new CustomAttributeBuilder( - typeof(BytesConversionAttribute).GetConstructor(ReflectionUtils.EmptyTypes), ArrayUtils.EmptyObjects)); + typeof(BytesLikeAttribute).GetConstructor(ReflectionUtils.EmptyTypes), ArrayUtils.EmptyObjects)); } if ((pi.Attributes & ParameterAttributes.HasDefault) != 0) { From f4adb4f2fb8cb26336c5f2e4be7fcaf220ed06f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Fri, 13 Mar 2020 22:08:23 -0400 Subject: [PATCH 2/2] Add missing file --- Src/IronPython/Runtime/BytesLikeAttribute.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Src/IronPython/Runtime/BytesLikeAttribute.cs diff --git a/Src/IronPython/Runtime/BytesLikeAttribute.cs b/Src/IronPython/Runtime/BytesLikeAttribute.cs new file mode 100644 index 000000000..cd9228f01 --- /dev/null +++ b/Src/IronPython/Runtime/BytesLikeAttribute.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information. + +#nullable enable + +using System; + +namespace IronPython.Runtime { + /// + /// For IList arguments: Marks that the argument is typed to accept a bytes or + /// bytearray object. This attribute disallows passing a Python list object and + /// auto-applying our generic conversion. + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class BytesLikeAttribute : Attribute { } +}