Skip to content

Commit

Permalink
Avoid PATH overflow from SetupPython calls (#11215)
Browse files Browse the repository at this point in the history
Calling Installer.SetupPython from Python.Included makes sure Python is
installed and also adds the install location to the PATH env variable.
The location is added to the PATH even if it was already there, so this
can potentially lead to an env variable overflow with the message
'Environment variable name or value is too long', after which the only
way to get CPython3 back to work would be to restart Dynamo.

The problem is avoided by remembering if Python was setup using a local
field. This should reduce the number of calls to a maximum of 2, which
is not the best (1 would be) but is far from resulting in an overflow.
  • Loading branch information
mmisol authored Oct 29, 2020
1 parent c7a5e49 commit 4c6d232
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/Libraries/DSCPython/CPythonEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static object EvaluatePythonScript(
return null;
}

Python.Included.Installer.SetupPython().Wait();
InstallPython();

if (!PythonEngine.IsInitialized)
{
Expand Down Expand Up @@ -246,6 +246,21 @@ public static object EvaluatePythonScript(
}
}

private static bool isPythonInstalled = false;
/// <summary>
/// Makes sure Python is installed on the system and it's location added to the path.
/// NOTE: Calling SetupPython multiple times will add the install location to the path many times,
/// potentially causing the environment variable to overflow.
/// </summary>
private static void InstallPython()
{
if (!isPythonInstalled)
{
Python.Included.Installer.SetupPython().Wait();
isPythonInstalled = true;
}
}

/// <summary>
/// Creates and initializaes the global Python scope.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class ScriptMigrator
/// <returns></returns>
internal static string MigrateCode(string code)
{
Installer.SetupPython().Wait();
InstallPython();

if (!PythonEngine.IsInitialized)
{
Expand Down Expand Up @@ -70,6 +70,21 @@ internal static string MigrateCode(string code)
}
}

private static bool isPythonInstalled = false;
/// <summary>
/// Makes sure Python is installed on the system and it's location added to the path.
/// NOTE: Calling SetupPython multiple times will add the install location to the path many times,
/// potentially causing the environment variable to overflow.
/// </summary>
private static void InstallPython()
{
if (!isPythonInstalled)
{
Installer.SetupPython().Wait();
isPythonInstalled = true;
}
}

private static string Get2To3MigrationScript(Assembly asm)
{
return GetEmbeddedScript(asm, "Dynamo.PythonMigration.MigrationAssistant.migrate_2to3.py");
Expand Down

0 comments on commit 4c6d232

Please sign in to comment.