Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8bca56b
Fix #668 (partial)
Mar 1, 2019
a731c2a
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 1, 2019
069910b
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
7ffc9db
Tests
Mar 5, 2019
6303c45
Revert "Tests"
Mar 5, 2019
945451c
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
034e437
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
f997d68
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 6, 2019
0e4ff95
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 11, 2019
878c8c5
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 13, 2019
35e1033
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 13, 2019
1073711
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 14, 2019
86028da
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 14, 2019
4d4bca7
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 18, 2019
ac04f76
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 18, 2019
bab03da
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 20, 2019
85ec685
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 27, 2019
be7466d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 28, 2019
01781c0
Exp
Mar 27, 2019
f544f8a
Limit concurrency
Mar 28, 2019
01bd719
Concurrency limit
Mar 28, 2019
2f8e564
Drop cache after analysis
Mar 28, 2019
9a933bd
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 28, 2019
4ad56bb
Fix regression
Mar 28, 2019
c280c55
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 3, 2019
b21b8b8
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 7, 2019
babaee4
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 8, 2019
092efd6
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 8, 2019
b97594d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 10, 2019
2a50eb9
Fix test
Apr 10, 2019
9c7f58d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 10, 2019
00faa5f
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 12, 2019
4c8aa92
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 12, 2019
1a67bc8
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 12, 2019
7a13215
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 16, 2019
3d907d8
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 16, 2019
94a274f
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 29, 2019
b029962
Fix #1022
Apr 29, 2019
229cf6f
Fix test
Apr 30, 2019
e97ec8e
Reference tests
Apr 30, 2019
ebadd0b
Fix IO Base
Apr 30, 2019
ac21b1d
Add test
May 1, 2019
d9cb97f
Merge master
May 1, 2019
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
26 changes: 20 additions & 6 deletions src/Analysis/Ast/Impl/Specializations/BuiltinsSpecializations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,29 @@ public static IMember CollectionItem(IPythonModule module, IPythonFunctionOverlo
public static IMember Open(IPythonModule declaringModule, IPythonFunctionOverload overload, IArgumentSet argSet) {
var mode = argSet.GetArgumentValue<IPythonConstant>("mode");

var bytes = false;
if (mode != null) {
var modeString = mode.GetString();
bytes = modeString != null && modeString.Contains("b");
var binary = false;
var writable = false;
var readWrite = false;

var modeString = mode?.GetString();
if (modeString != null) {
binary = modeString.Contains("b");
writable = modeString.Contains("w") || modeString.Contains("a") || modeString.Contains("x");
readWrite = writable && modeString.Contains("r");
}

string returnTypeName;
var io = declaringModule.Interpreter.ModuleResolution.GetImportedModule("io");
var ioBase = io?.GetMember(bytes ? "BufferedIOBase" : "TextIOWrapper")?.GetPythonType();
return ioBase != null ? new PythonInstance(ioBase) : null;
if (binary) {
Comment thread
jakebailey marked this conversation as resolved.
returnTypeName = writable ?
readWrite ? "BufferedRandom" : "BufferedWriter"
: "BufferedReader";
} else {
returnTypeName = "TextIOWrapper";
}

var returnType = io?.GetMember(returnTypeName)?.GetPythonType();
return returnType != null ? new PythonInstance(returnType) : null;
}
}
}
5 changes: 4 additions & 1 deletion src/Analysis/Ast/Impl/Types/PythonFunctionOverload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ public IMember Call(IArgumentSet args, IPythonType self, Node callLocation = nul
var name = StaticReturnValue.GetPythonType()?.Name;
var typeDefVar = DeclaringModule.Analysis.GlobalScope.Variables[name];
if (typeDefVar?.Value is IGenericTypeDefinition gtp2) {
return gtp2.Constraints.FirstOrDefault();
// See if the instance (self) type satisfies one of the constraints.
return selfClassType.Mro.Any(b => gtp2.Constraints.Any(c => c.Equals(b)))
? selfClassType
: gtp2.Constraints.FirstOrDefault();
}

break;
Expand Down
32 changes: 32 additions & 0 deletions src/Analysis/Ast/Test/ValuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ with X():
.Which.Should().HaveMember("x_method");
}

[TestMethod, Priority(0)]
public async Task WithIOStatement() {
const string code = @"
with open('a', 'r') as r:
pass
with open('a', 'rb') as rb:
pass
with open('a', 'w') as w:
pass
with open('a', 'wb') as wb:
pass
with open('a', 'rw') as rw:
pass
with open('a', 'rwb') as rwb:
pass
with open('a', 'x') as x:
pass
with open('a', 'xb') as xb:
pass
";
var analysis = await GetAnalysisAsync(code);

analysis.Should().HaveVariable("r").OfType("TextIOWrapper")
.And.HaveVariable("rb").OfType("BufferedReader")
.And.HaveVariable("w").OfType("TextIOWrapper")
.And.HaveVariable("wb").OfType("BufferedWriter")
.And.HaveVariable("rw").OfType("TextIOWrapper")
.And.HaveVariable("rwb").OfType("BufferedRandom")
.And.HaveVariable("x").OfType("TextIOWrapper")
.And.HaveVariable("xb").OfType("BufferedWriter");
}

[TestMethod, Priority(0)]
public async Task Global() {
const string code = @"
Expand Down