Skip to content

Commit

Permalink
fix: python versions < 3.4 broken by previous addition
Browse files Browse the repository at this point in the history
My addition of quicker checking for python unittest modules breaks
python verison less than 3.4. `importlib.machinery.PathFinder` was added
in python 3.4 and will fail to import in verions prior to this.

This changes the code to only use the new check if the python version is
greater than or equal to 3.4. Note that `sys.version_info` was added in
version 2.0; unittest was added in python 2.1; older versions of pytest
support as low as python 2.7, so this change should be safe for any
python version supporting unit testing.
  • Loading branch information
aghoward committed Feb 12, 2025
1 parent 03d8cbd commit 9e6d1e1
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 9e6d1e1

Please sign in to comment.