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
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
3 changes: 1 addition & 2 deletions src/Analysis/Ast/Impl/Extensions/PythonTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public static bool IsGeneric(this IPythonType value)

public static void TransferDocumentationAndLocation(this IPythonType s, IPythonType d) {
if (s != d && s is PythonType src && d is PythonType dst) {
dst.TrySetTypeId(src.TypeId);
var documentation = src.Documentation;
if (!string.IsNullOrEmpty(documentation)) {
dst.SetDocumentationProvider(_ => documentation);
dst.SetDocumentation(documentation);
}
dst.SetLocation(src.Location);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Ast/Impl/Modules/ModuleCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public string ReadCachedModule(string filePath) {
if (assemblyTime > cacheTime) {
cachedFileOlderThanAssembly = true;
} else {
return _fs.ReadAllText(cachePath);
return _fs.ReadTextWithRetry(cachePath);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Ast/Impl/Modules/PythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected virtual string LoadContent() {
if (ContentState < State.Loading) {
ContentState = State.Loading;
try {
var code = FileSystem.ReadAllText(FilePath);
var code = FileSystem.ReadTextWithRetry(FilePath);
ContentState = State.Loaded;
return code;
} catch (IOException) { } catch (UnauthorizedAccessException) { }
Expand Down
8 changes: 4 additions & 4 deletions src/Analysis/Ast/Impl/Modules/SpecializedModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.IO;
using Microsoft.Python.Analysis.Specializations.Typing;
using Microsoft.Python.Core;
using Microsoft.Python.Core.IO;

namespace Microsoft.Python.Analysis.Modules {
/// <summary>
Expand All @@ -27,15 +27,15 @@ namespace Microsoft.Python.Analysis.Modules {
/// Specialization is helpful when it is easier to express module members
/// behavior to the analyzer in code. Example of specialization is 'typing'
/// module. Specialized module can use actual library module as a source
/// of documentation for its members. See <see cref="Typing.TypingModule"/>.
/// of documentation for its members. See <see cref="TypingModule"/>.
/// </remarks>
public abstract class SpecializedModule : PythonModule {
protected SpecializedModule(string name, string modulePath, IServiceContainer services)
: base(name, modulePath, ModuleType.Specialized, null, services) { }

protected override string LoadContent() {
// Exceptions are handled in the base
return FileSystem.FileExists(FilePath) ? FileSystem.ReadAllText(FilePath) : string.Empty;
return FileSystem.FileExists(FilePath) ? FileSystem.ReadTextWithRetry(FilePath) : string.Empty;
}
}
}
3 changes: 2 additions & 1 deletion src/Analysis/Ast/Impl/Modules/StubPythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using Microsoft.Python.Core;
using Microsoft.Python.Core.IO;

namespace Microsoft.Python.Analysis.Modules {
/// <summary>
Expand All @@ -30,7 +31,7 @@ public StubPythonModule(string moduleName, string stubPath, bool isTypeshed, ISe

protected override string LoadContent() {
// Exceptions are handled in the base
return FileSystem.FileExists(FilePath) ? FileSystem.ReadAllText(FilePath) : string.Empty;
return FileSystem.FileExists(FilePath) ? FileSystem.ReadTextWithRetry(FilePath) : string.Empty;
}

protected override string[] GetScrapeArguments(IPythonInterpreter factory) => Array.Empty<string>();
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Impl/Extensions/IOExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ public static FileStream OpenWithRetry(this IFileSystem fs, string file, FileMod
return null;
}

public static string ReadTextWithRetry(this IFileSystem fs, string file) {
// Retry for up to one second
for (var retries = 100; retries > 0; --retries) {
try {
return fs.ReadAllText(file);
} catch (UnauthorizedAccessException) {
Thread.Sleep(10);
}
}
return null;
}

public static void WriteTextWithRetry(this IFileSystem fs, string filePath, string text) {
try {
using (var stream = fs.OpenWithRetry(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) {
Expand Down