Skip to content

Commit

Permalink
fix: python versions < 3.4 broken by previous addition (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
aghoward authored Feb 12, 2025
1 parent 03d8cbd commit 5a06278
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ let terminal: Terminal | undefined;

const framework = workspace.getConfiguration('pyright').get<TestingFramework>('testing.provider', 'unittest');

function validPythonModule(pythonPath: string, moduleName: string) {
function pythonSupportsPathFinder(pythonPath: string) {
try {
const pythonProcess = child_process.spawnSync(
pythonPath,
['-c', `from importlib.machinery import PathFinder; assert PathFinder.find_spec("${moduleName}") is not None`],
['-c', 'from sys import version_info; exit(0) if (version_info[0] >= 3 and version_info[1] >= 4) else exit(1)'],
{ encoding: 'utf8' },
);
if (pythonProcess.error) return false;
Expand All @@ -23,6 +23,19 @@ function validPythonModule(pythonPath: string, moduleName: string) {
}
}

function validPythonModule(pythonPath: string, moduleName: string) {
const pythonArgs = pythonSupportsPathFinder(pythonPath)
? ['-c', `from importlib.machinery import PathFinder; assert PathFinder.find_spec("${moduleName}") is not None`]
: ['-m', moduleName, '--help'];
try {
const pythonProcess = child_process.spawnSync(pythonPath, pythonArgs, { encoding: 'utf8' });
if (pythonProcess.error) return false;
return pythonProcess.status === 0;
} catch (ex) {
return false;
}
}

async function runTest(uri: string, testFunction?: string) {
const workspaceUri = Uri.parse(workspace.root).toString();
const relativeFileUri = uri.replace(`${workspaceUri}/`, '');
Expand Down

0 comments on commit 5a06278

Please sign in to comment.