@@ -6,30 +6,12 @@ import { EXTENSION_ROOT_DIR } from '../constants';
66import { traceError , traceLog } from '../logging' ;
77import { captureTelemetry } from '../telemetry' ;
88import { EventName } from '../telemetry/constants' ;
9- import { PythonEnvironment as PythonEnvironmentEnvExt , PythonProcess } from '../envExt/types' ;
9+ import { PythonEnvironment as PythonEnvironmentEnvExt } from '../envExt/types' ;
1010import { runInBackground } from '../envExt/api.internal' ;
1111
1212const SERVER_PATH = path . join ( EXTENSION_ROOT_DIR , 'python_files' , 'python_server.py' ) ;
13-
14- /**
15- * Wrapper interface to unify ch.ChildProcess and PythonProcess from env extension
16- */
17- interface PythonProcessWrapper {
18- readonly stdout : NodeJS . ReadableStream ;
19- readonly stdin : NodeJS . WritableStream ;
20- readonly stderr : NodeJS . ReadableStream ;
21- kill ( signal ?: string ) : boolean ;
22- }
23-
2413let serverInstance : PythonServer | undefined ;
2514
26- /**
27- * Clear the server instance. Used when the environment changes and we need to recreate the server.
28- */
29- export function clearServerInstance ( ) : void {
30- serverInstance = undefined ;
31- }
32-
3315export interface ExecutionResult {
3416 status : boolean ;
3517 output : string ;
@@ -51,7 +33,7 @@ class PythonServerImpl implements PythonServer, Disposable {
5133
5234 onCodeExecuted = this . _onCodeExecuted . event ;
5335
54- constructor ( private connection : rpc . MessageConnection , private pythonServer : PythonProcessWrapper ) {
36+ constructor ( private connection : rpc . MessageConnection , private killProcess : ( ) => void ) {
5537 this . initialize ( ) ;
5638 this . input ( ) ;
5739 }
@@ -108,9 +90,8 @@ class PythonServerImpl implements PythonServer, Disposable {
10890
10991 public interrupt ( ) : void {
11092 // Passing SIGINT to interrupt only would work for Mac and Linux
111- if ( this . pythonServer . kill ( 'SIGINT' ) ) {
112- traceLog ( 'Python REPL server interrupted' ) ;
113- }
93+ this . killProcess ( ) ;
94+ traceLog ( 'Python REPL server interrupted' ) ;
11495 }
11596
11697 public async checkValidCommand ( code : string ) : Promise < boolean > {
@@ -129,33 +110,6 @@ class PythonServerImpl implements PythonServer, Disposable {
129110 }
130111}
131112
132- /**
133- * Wrap a ch.ChildProcess to match PythonProcessWrapper interface
134- */
135- function wrapChildProcess ( proc : ch . ChildProcess ) : PythonProcessWrapper {
136- return {
137- stdout : proc . stdout ! ,
138- stdin : proc . stdin ! ,
139- stderr : proc . stderr ! ,
140- kill : ( signal ?: string ) => proc . kill ( signal as NodeJS . Signals ) ,
141- } ;
142- }
143-
144- /**
145- * Wrap a PythonProcess from env extension to match PythonProcessWrapper interface
146- */
147- function wrapEnvExtProcess ( proc : PythonProcess ) : PythonProcessWrapper {
148- return {
149- stdout : proc . stdout ,
150- stdin : proc . stdin as NodeJS . WritableStream ,
151- stderr : proc . stderr ,
152- kill : ( ) => {
153- proc . kill ( ) ;
154- return true ;
155- } ,
156- } ;
157- }
158-
159113export function createPythonServer ( interpreter : string [ ] , cwd ?: string ) : PythonServer {
160114 if ( serverInstance ) {
161115 return serverInstance ;
@@ -178,7 +132,7 @@ export function createPythonServer(interpreter: string[], cwd?: string): PythonS
178132 new rpc . StreamMessageReader ( pythonServer . stdout ! ) ,
179133 new rpc . StreamMessageWriter ( pythonServer . stdin ! ) ,
180134 ) ;
181- serverInstance = new PythonServerImpl ( connection , wrapChildProcess ( pythonServer ) ) ;
135+ serverInstance = new PythonServerImpl ( connection , ( ) => pythonServer . kill ( 'SIGINT' ) ) ;
182136 return serverInstance ;
183137}
184138
@@ -210,6 +164,6 @@ export async function createPythonServerEnvExt(
210164 new rpc . StreamMessageReader ( pythonProcess . stdout ) ,
211165 new rpc . StreamMessageWriter ( pythonProcess . stdin as NodeJS . WritableStream ) ,
212166 ) ;
213- serverInstance = new PythonServerImpl ( connection , wrapEnvExtProcess ( pythonProcess ) ) ;
167+ serverInstance = new PythonServerImpl ( connection , ( ) => pythonProcess . kill ( ) ) ;
214168 return serverInstance ;
215169}
0 commit comments