Skip to content

Commit e8f4f18

Browse files
committed
Fix bug and remove truncate
1 parent 31b9b25 commit e8f4f18

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

Src/IronPython/Modules/_io.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,7 @@ public override object read(CodeContext/*!*/ context, object size = null) {
457457
public Bytes readall(CodeContext/*!*/ context) {
458458
List<Bytes> res = new List<Bytes>();
459459
int count = 0;
460-
for (; ; )
461-
{
460+
for (; ; ) {
462461
object cur = read(context, DEFAULT_BUFFER_SIZE);
463462
if (cur == null) {
464463
break;
@@ -665,17 +664,6 @@ public object raw {
665664

666665
#region _BufferedIOMixin
667666

668-
public override BigInteger truncate(CodeContext/*!*/ context, object pos = null) {
669-
if (_rawIO != null) {
670-
return _rawIO.truncate(context, pos);
671-
}
672-
673-
return GetBigInt(
674-
PythonOps.Invoke(context, _raw, "truncate", pos),
675-
"truncate() should return integer"
676-
);
677-
}
678-
679667
public override void close(CodeContext/*!*/ context) {
680668
if (!closed) {
681669
try {
@@ -775,7 +763,9 @@ public override object read(CodeContext/*!*/ context, object length = null) {
775763
object? obj = _rawIO != null ? _rawIO.read(context, length) : PythonOps.Invoke(context, _raw, "read", length);
776764
if (obj is null) return null;
777765
if (obj is Bytes bytes) {
778-
_absPos += bytes.Count;
766+
if (_absPos != -1) {
767+
_absPos += bytes.Count;
768+
}
779769
return bytes;
780770
}
781771
throw PythonOps.TypeError("'read()' should have returned bytes");
@@ -789,12 +779,11 @@ public override object read(CodeContext/*!*/ context, object length = null) {
789779
if (length < 0) {
790780
List<Bytes> chunks = new List<Bytes>();
791781
int count = 0;
792-
if (TryResetReadBuf(out Bytes res)) {
782+
if (_readBuf.Count > 0 && TryResetReadBuf(out Bytes res)) {
793783
chunks.Add(res);
794784
count += chunks[0].Count;
795785
}
796-
for (; ; )
797-
{
786+
for (; ; ) {
798787
var chunk = CallRawRead(context, -1);
799788
if (chunk == null || chunk.Count == 0) {
800789
if (count == 0) {
@@ -840,8 +829,8 @@ public override object read(CodeContext/*!*/ context, object length = null) {
840829
}
841830
if (remaining >= _readBuf.Count - _readBufPos) {
842831
remaining -= _readBuf.Count - _readBufPos;
843-
if (TryResetReadBuf(out Bytes res2)) {
844-
chunks.Add(res2);
832+
if (TryResetReadBuf(out res)) {
833+
chunks.Add(res);
845834
}
846835
} else {
847836
byte[] bytes = new byte[remaining];
@@ -1566,8 +1555,7 @@ private Bytes ReadNoLock(CodeContext/*!*/ context, int length) {
15661555
chunks.Add(ResetReadBuf());
15671556
count += chunks[0].Count;
15681557
}
1569-
for (; ; )
1570-
{
1558+
for (; ; ) {
15711559
Bytes chunk = (Bytes)_inner.read(context, -1);
15721560
if (chunk == null || chunk.Count == 0) {
15731561
if (count == 0) {
@@ -2532,8 +2520,7 @@ public override object readline(CodeContext/*!*/ context, int limit = -1) {
25322520
}
25332521

25342522
int pos, endPos;
2535-
for (; ; )
2536-
{
2523+
for (; ; ) {
25372524
if (_readTranslate) {
25382525
// Newlines have already been translated into "\n"
25392526
pos = line.IndexOf('\n', start);
@@ -3119,15 +3106,15 @@ private static HashSet<char> MakeSet(string chars) {
31193106
return res;
31203107
}
31213108

3122-
private static BigInteger GetBigInt(object i, string msg) {
3109+
private static BigInteger GetBigInt(object? i, string msg) {
31233110
if (TryGetBigInt(i, out BigInteger res)) {
31243111
return res;
31253112
}
31263113

31273114
throw PythonOps.TypeError(msg);
31283115
}
31293116

3130-
private static bool TryGetBigInt(object i, out BigInteger res) {
3117+
private static bool TryGetBigInt(object? i, out BigInteger res) {
31313118
if (i is BigInteger bi) {
31323119
res = bi;
31333120
return true;

Src/IronPython/Runtime/PythonList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,8 @@ public void insert(int index, object? value) {
868868

869869
public object? pop(int index) {
870870
lock (this) {
871-
index = PythonOps.FixIndex(index, _size);
872871
if (_size == 0) throw PythonOps.IndexError("pop off of empty list");
872+
index = PythonOps.FixIndex(index, _size);
873873

874874
object? ret = _data[index];
875875
_size -= 1;

0 commit comments

Comments
 (0)