Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

Commit

Permalink
Add some file mode checking (#1306)
Browse files Browse the repository at this point in the history
* Remove notepad 2

* Add additional file mode check

* Remove test part

* Remove empty lines

* Remove empty line 2
  • Loading branch information
simplicbe authored and slide committed Jul 21, 2016
1 parent 1124511 commit 5a7ffe1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Binary file removed External/Tools/Notepad2.exe
Binary file not shown.
18 changes: 13 additions & 5 deletions Languages/IronPython/IronPython/Runtime/PythonFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,23 +1217,24 @@ private static void TranslateAndValidateMode(string mode, out FileMode fmode, ou
mode = "r+";
} else if (mode[0] == 'w' || mode[0] == 'a') {
throw PythonOps.ValueError("universal newline mode can only be used with modes starting with 'r'");
} else {
} else if (mode[0] != 'r') {
mode = "r" + mode;
}
}

// process read/write/append
// process read/write/append and remove char from mode
seekEnd = false;
switch (mode[0]) {
case 'r': fmode = FileMode.Open; break;
case 'w': fmode = FileMode.Create; break;
case 'a': fmode = FileMode.Append; break;
case 'r': fmode = FileMode.Open; mode = mode.Remove(0, 1); break;
case 'w': fmode = FileMode.Create; mode = mode.Remove(0, 1); break;
case 'a': fmode = FileMode.Append; mode = mode.Remove(0, 1); break;
default:
throw PythonOps.ValueError("mode string must begin with one of 'r', 'w', 'a' or 'U', not '{0}'", inMode);
}

// process +
if (mode.IndexOf('+') != -1) {
mode = mode.Remove(mode.IndexOf('+'), 1);
faccess = FileAccess.ReadWrite;
if (fmode == FileMode.Append) {
fmode = FileMode.OpenOrCreate;
Expand All @@ -1247,6 +1248,13 @@ private static void TranslateAndValidateMode(string mode, out FileMode fmode, ou
default: throw new InvalidOperationException();
}
}

// Make some additional mode check after processing all valid modes.
if (mode.Length > 0) {
if (mode[0] != 'U' && mode[0] != 'b' && mode[0] != 't') {
throw PythonOps.ValueError("Invalid mode ('{0}')", inMode);
}
}
}

public void __init__(CodeContext/*!*/ context, [NotNull]Stream/*!*/ stream) {
Expand Down

0 comments on commit 5a7ffe1

Please sign in to comment.