Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions Src/IronPython.Modules/_bytesio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,7 @@ public override List readlines([DefaultParameterValue(null)]object hint) {
return lines;
}

[Documentation("seek(pos, whence=0) -> int. Change stream position.\n\n"
+ "Seek to byte offset pos relative to position indicated by whence:\n"
+ " 0 Start of stream (the default). pos should be >= 0;\n"
+ " 1 Current position - pos may be negative;\n"
+ " 2 End of stream - pos usually negative.\n"
+ "Returns the new absolute position."
)]
public BigInteger seek(int pos, int whence = 0) {
private BigInteger seek(int pos, int whence) {
_checkClosed();

switch (whence) {
Expand All @@ -281,13 +274,16 @@ public BigInteger seek(int pos, int whence = 0) {
}
}

public BigInteger seek(int pos, BigInteger whence) => seek(pos, (int)whence);

public BigInteger seek(int pos, double whence) => throw PythonOps.TypeError("integer argument expected, got float");

public BigInteger seek(double pos, [DefaultParameterValue(0)]object whence) => throw PythonOps.TypeError("'float' object cannot be interpreted as an index");
public BigInteger seek(double pos, [Optional]object whence) => throw PythonOps.TypeError("'float' object cannot be interpreted as an index");

public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [DefaultParameterValue(0)]object whence) {
[Documentation("seek(pos, whence=0) -> int. Change stream position.\n\n"
+ "Seek to byte offset pos relative to position indicated by whence:\n"
+ " 0 Start of stream (the default). pos should be >= 0;\n"
+ " 1 Current position - pos may be negative;\n"
+ " 2 End of stream - pos usually negative.\n"
+ "Returns the new absolute position."
)]
public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [Optional]object whence) {
_checkClosed();

int posInt = (int)pos;
Expand All @@ -297,9 +293,9 @@ public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [Defau
case Extensible<int> v:
return seek(posInt, v);
case BigInteger v:
return seek(posInt, v);
return seek(posInt, (int)v);
case Extensible<BigInteger> v:
return seek(posInt, v);
return seek(posInt, (int)v.Value);
case double _:
case Extensible<double> _:
throw PythonOps.TypeError("integer argument expected, got float");
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython.Modules/_fileio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ public override BigInteger readinto(CodeContext/*!*/ context, object buf) {
+ "seeking beyond the end of a file).\n"
+ "Note that not all file objects are seekable."
)]
public override BigInteger seek(CodeContext/*!*/ context, BigInteger offset, [DefaultParameterValue(0)]object whence) {
public override BigInteger seek(CodeContext/*!*/ context, BigInteger offset, [Optional]object whence) {
_checkClosed();

return _readStream.Seek((long)offset, (SeekOrigin)GetInt(whence));
}

public BigInteger seek(double offset, [DefaultParameterValue(0)]object whence) {
public BigInteger seek(double offset, [Optional]object whence) {
_checkClosed();

throw PythonOps.TypeError("an integer is required");
Expand Down
27 changes: 14 additions & 13 deletions Src/IronPython.Modules/_io.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// 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.Linq.Expressions;

using System.Numerics;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

Expand Down Expand Up @@ -241,7 +240,7 @@ public virtual List readlines([DefaultParameterValue(null)]object hint) {
return res;
}

public virtual BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [DefaultParameterValue(0)]object whence) {
public virtual BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [Optional]object whence) {
throw UnsupportedOperation(context, "seek");
}

Expand Down Expand Up @@ -911,13 +910,13 @@ public override BigInteger tell(CodeContext/*!*/ context) {
return res - _readBuf.Count + _readBufPos;
}

public BigInteger seek(double offset, [DefaultParameterValue(0)]object whence) {
public BigInteger seek(double offset, [Optional]object whence) {
_checkClosed();

throw PythonOps.TypeError("an integer is required");
}

public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [DefaultParameterValue(0)]object whence) {
public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [Optional]object whence) {
int whenceInt = GetInt(whence);
if (whenceInt < 0 || whenceInt > 2) {
throw PythonOps.ValueError("invalid whence ({0}, should be 0, 1, or 2)", whenceInt);
Expand Down Expand Up @@ -1251,13 +1250,13 @@ public override BigInteger tell(CodeContext/*!*/ context) {
return res + _writeBuf.Count;
}

public BigInteger seek(double offset, [DefaultParameterValue(0)]object whence) {
public BigInteger seek(double offset, [Optional]object whence) {
_checkClosed();

throw PythonOps.TypeError("an integer is required");
}

public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [DefaultParameterValue(0)]object whence) {
public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [Optional]object whence) {
int whenceInt = GetInt(whence);
if (whenceInt < 0 || whenceInt > 2) {
throw PythonOps.ValueError("invalid whence ({0}, should be 0, 1, or 2)", whenceInt);
Expand Down Expand Up @@ -1646,13 +1645,13 @@ public override BigInteger readinto(CodeContext/*!*/ context, object buf) {
return base.readinto(context, buf);
}

public BigInteger seek(double offset, [DefaultParameterValue(0)]object whence) {
public BigInteger seek(double offset, [Optional]object whence) {
_checkClosed();

throw PythonOps.TypeError("an integer is required");
}

public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [DefaultParameterValue(0)]object whence) {
public override BigInteger seek(CodeContext/*!*/ context, BigInteger pos, [Optional]object whence) {
int whenceInt = GetInt(whence);
if (whenceInt < 0 || whenceInt > 2) {
throw PythonOps.ValueError("invalid whence ({0}, should be 0, 1, or 2)", whenceInt);
Expand Down Expand Up @@ -2289,13 +2288,13 @@ public override object detach(CodeContext/*!*/ context) {
return res;
}

public BigInteger seek(double offset, [DefaultParameterValue(0)]object whence) {
public BigInteger seek(double offset, [Optional]object whence) {
_checkClosed();

throw PythonOps.TypeError("an integer is required");
}

public override BigInteger seek(CodeContext/*!*/ context, BigInteger cookie, [DefaultParameterValue(0)]object whence) {
public override BigInteger seek(CodeContext/*!*/ context, BigInteger cookie, [Optional]object whence) {
int whenceInt = GetInt(whence);
if (closed) {
throw PythonOps.ValueError("tell on closed file");
Expand Down Expand Up @@ -3116,6 +3115,8 @@ private static int GetInt(object i, int defaultValue) {
}

private static int GetInt(object i, string msg, params object[] args) {
if (i == Missing.Value) return 0;

int res;
if (TryGetInt(i, out res)) {
return res;
Expand Down
10 changes: 10 additions & 0 deletions Tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,4 +1352,14 @@ class test(object):

self.assertIs(test().defaultValue, noValue)

def test_ipy2_gh546(self):
"""https://github.com/IronLanguages/ironpython2/issues/546"""
from io import StringIO
class Test(StringIO): pass
Test().seek(0)

from io import BytesIO
class Test(BytesIO): pass
Test().seek(0)

run_test(__name__)