Skip to content

Commit 58bebdf

Browse files
authored
Replace BytesLikeAttribute usage with IBufferProtocol in builtins (#987)
1 parent e77073b commit 58bebdf

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Src/IronPython/Modules/Builtin.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,18 @@ public static object compile(CodeContext/*!*/ context, [NotNull]_ast.AST source,
187187
}
188188

189189
[Documentation("")] // provided by first overload
190-
public static object compile(CodeContext/*!*/ context, [BytesLike, NotNull]IList<byte> source, [NotNull]string filename, [NotNull]string mode, object? flags = null, bool dont_inherit = false, int optimize = -1) {
190+
public static object compile(CodeContext/*!*/ context, [NotNull]IBufferProtocol source, [NotNull]string filename, [NotNull]string mode, object? flags = null, bool dont_inherit = false, int optimize = -1) {
191191
// TODO: implement optimize
192192
var sourceCodeKind = ValidateCompileMode(mode);
193193

194-
byte[] bytes = source as byte[] ?? ((source is Bytes b) ? b.UnsafeByteArray : source.ToArray());
194+
using var buffer = source.GetBuffer();
195+
byte[] bytes = buffer.AsUnsafeArray() ?? buffer.ToArray();
195196
var contentProvider = new MemoryStreamContentProvider(context.LanguageContext, bytes, filename);
196197
var sourceUnit = context.LanguageContext.CreateSourceUnit(contentProvider, filename, sourceCodeKind);
197198

198199
return CompileHelper(context, sourceUnit, mode, flags, dont_inherit);
199200
}
200201

201-
public static object compile(CodeContext/*!*/ context, [NotNull]MemoryView source, [NotNull]string filename, [NotNull]string mode, object? flags = null, bool dont_inherit = false, int optimize = -1)
202-
=> compile(context, source.tobytes(), filename, mode, flags, dont_inherit, optimize);
203-
204202
[Documentation("")] // provided by first overload
205203
public static object compile(CodeContext/*!*/ context, [NotNull]string source, [NotNull]string filename, [NotNull]string mode, object? flags = null, bool dont_inherit = false, int optimize = -1) {
206204
// TODO: implement optimize
@@ -296,12 +294,13 @@ public static object eval(CodeContext/*!*/ context, [NotNull]FunctionCode code,
296294

297295
[Documentation("")] // provided by first overload
298296
[LightThrowing]
299-
public static object eval(CodeContext/*!*/ context, [BytesLike, NotNull]IList<byte> expression, PythonDictionary? globals = null, object? locals = null) {
297+
public static object eval(CodeContext/*!*/ context, [NotNull]IBufferProtocol expression, PythonDictionary? globals = null, object? locals = null) {
300298
if (locals != null && !PythonOps.IsMappingType(context, locals)) {
301299
throw PythonOps.TypeError("locals must be mapping");
302300
}
303301

304-
byte[] bytes = expression as byte[] ?? ((expression is Bytes b) ? b.UnsafeByteArray : expression.ToArray());
302+
using var buffer = expression.GetBuffer();
303+
byte[] bytes = buffer.AsUnsafeArray() ?? buffer.ToArray();
305304

306305
// Count number of whitespace characters to skip at the beginning.
307306
// It assumes an ASCII compatible encoding (like UTF-8 or Latin-1) but excludes UTF-16 or UTF-32.
@@ -373,8 +372,9 @@ public static void exec(CodeContext/*!*/ context, [NotNull]string code, PythonDi
373372
}
374373

375374
[Documentation("")] // provided by first overload
376-
public static void exec(CodeContext/*!*/ context, [BytesLike, NotNull]IList<byte> code, PythonDictionary? globals = null, object? locals = null) {
377-
byte[] bytes = code as byte[] ?? ((code is Bytes b) ? b.UnsafeByteArray : code.ToArray());
375+
public static void exec(CodeContext/*!*/ context, [NotNull]IBufferProtocol code, PythonDictionary? globals = null, object? locals = null) {
376+
using var buffer = code.GetBuffer();
377+
byte[] bytes = buffer.AsUnsafeArray() ?? buffer.ToArray();
378378
SourceUnit source = context.LanguageContext.CreateSourceUnit(
379379
new MemoryStreamContentProvider(context.LanguageContext, bytes, "<string>"),
380380
"<string>",

0 commit comments

Comments
 (0)