forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpythonStartup.ts
More file actions
79 lines (70 loc) · 3.28 KB
/
pythonStartup.ts
File metadata and controls
79 lines (70 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ExtensionContext, Uri } from 'vscode';
import * as path from 'path';
import { copy, createDirectory, getConfiguration, onDidChangeConfiguration } from '../common/vscodeApis/workspaceApis';
import { EXTENSION_ROOT_DIR } from '../constants';
import { IInterpreterService } from '../interpreter/contracts';
import { IServiceContainer } from '../ioc/types';
import { getPythonMinorVersion } from '../repl/replUtils';
async function applyPythonStartupSetting(context: ExtensionContext): Promise<void> {
const config = getConfiguration('python');
const pythonrcSetting = config.get<boolean>('terminal.shellIntegration.enabled');
if (pythonrcSetting) {
const storageUri = context.storageUri || context.globalStorageUri;
try {
await createDirectory(storageUri);
} catch {
// already exists, most likely
}
const destPath = Uri.joinPath(storageUri, 'pythonrc.py');
const sourcePath = path.join(EXTENSION_ROOT_DIR, 'python_files', 'pythonrc.py');
await copy(Uri.file(sourcePath), destPath, { overwrite: true });
context.environmentVariableCollection.replace('PYTHONSTARTUP', destPath.fsPath);
} else {
context.environmentVariableCollection.delete('PYTHONSTARTUP');
}
}
export async function registerPythonStartup(context: ExtensionContext): Promise<void> {
await applyPythonStartupSetting(context);
context.subscriptions.push(
onDidChangeConfiguration(async (e) => {
if (e.affectsConfiguration('python.terminal.shellIntegration.enabled')) {
await applyPythonStartupSetting(context);
}
}),
);
}
async function applyBasicReplSetting(context: ExtensionContext, serviceContainer?: IServiceContainer): Promise<void> {
const config = getConfiguration('python');
const shellIntegrationEnabled = config.get<boolean>('terminal.shellIntegration.enabled');
if (shellIntegrationEnabled && serviceContainer) {
// Only disable PyREPL (set PYTHON_BASIC_REPL=1) when shell integration is enabled
// and Python version is 3.13 or higher
try {
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const pythonMinorVersion = await getPythonMinorVersion(undefined, interpreterService);
if ((pythonMinorVersion ?? 0) >= 13) {
context.environmentVariableCollection.replace('PYTHON_BASIC_REPL', '1');
return;
}
} catch {
// If we can't get the Python version, don't set PYTHON_BASIC_REPL
}
}
// Remove PYTHON_BASIC_REPL if shell integration is disabled or Python < 3.13
context.environmentVariableCollection.delete('PYTHON_BASIC_REPL');
}
export async function registerBasicRepl(
context: ExtensionContext,
serviceContainer?: IServiceContainer,
): Promise<void> {
await applyBasicReplSetting(context, serviceContainer);
context.subscriptions.push(
onDidChangeConfiguration(async (e) => {
if (e.affectsConfiguration('python.terminal.shellIntegration.enabled')) {
await applyBasicReplSetting(context, serviceContainer);
}
}),
);
}