@@ -590,10 +590,14 @@ export async function evaluteExpression(
590590 expression : string | Buffer ,
591591 emitter : EventEmitter ,
592592 receiver : ( data : Buffer ) => void ,
593- pythonInterpreterPath ?: string
593+ pythonInterpreterPath ?: string ,
594+ dynamicWrapping = false
594595) : Promise < string | null > {
595596 let command = "" ;
596- if ( pythonInterpreterPath ) {
597+ if (
598+ pythonInterpreterPath &&
599+ ( ! dynamicWrapping || expression . includes ( ";" ) || expression . includes ( ":" ) )
600+ ) {
597601 command = wrapExpressionWithPrint (
598602 pythonInterpreterPath ,
599603 expression instanceof Buffer ? expression . toString ( "utf-8" ) : expression
@@ -689,7 +693,11 @@ export async function fsExists(
689693 target = ""
690694) : Promise < boolean > {
691695 try {
692- await executeCommand ( port , `import os\nos.stat('${ target } ')` , emitter ) ;
696+ await executeCommand (
697+ port ,
698+ `import os as _pe_os\n_pe_os.stat('${ target } ')\ndel _pe_os` ,
699+ emitter
700+ ) ;
693701
694702 return true ;
695703 } catch {
@@ -720,10 +728,11 @@ export async function fsListContents(
720728 try {
721729 const result = await executeCommand (
722730 port ,
723- `import os\nfor f in os .ilistdir(${
731+ `import os as _pe_os \nfor f in _pe_os .ilistdir(${
724732 remotePath ? `"${ remotePath } "` : ""
725733 } ):\n` +
726- " print('{:12} {}{}'.format(f[3]if len(f)>3 else 0,f[0],'/'if f[1]&0x4000 else ''))" ,
734+ " print('{:12} {}{}'.format(f[3]if len(f)>3 else 0,f[0],'/'if f[1]&0x4000 else ''))\n" +
735+ "del _pe_os" ,
727736 emitter
728737 ) ;
729738
@@ -753,16 +762,17 @@ export async function fsListContentsRecursive(
753762 // to reduce the amount of bytes sent
754763 // TODO: add max depth !!
755764 const cmd = `
756- import os
765+ import os as _pe_os
757766def __pe_recursive_ls(src):
758- for f in os .ilistdir(src):
767+ for f in _pe_os .ilistdir(src):
759768 is_dir = f[1] & 0x4000
760769 path = src + ('/' if src[-1] != '/' else '') + f[0]
761770 print('{:12} {}{}'.format(f[3] if len(f) > 3 else 0, path, '/' if is_dir else ''))
762771 if is_dir:
763772 __pe_recursive_ls(src + ('/' if src[-1] != '/' else '') + f[0])
764773__pe_recursive_ls(${ target . length > 0 ? `'${ target } '` : `'/'` } )
765774del __pe_recursive_ls
775+ del _pe_os
766776` ;
767777
768778 try {
@@ -784,13 +794,14 @@ export async function fsStat(
784794 // TODO: maybe move computation from board to host
785795 // os.stat_result(insert the eval result of repr(os.stat))
786796 const command = `
787- import os
797+ import os as _pe_os
788798def __pe_get_file_info(file_path):
789- stat = os .stat(file_path)
799+ stat = _pe_os .stat(file_path)
790800 creation_time = stat[9]
791801 modification_time = stat[8]
792802 size = stat[6]
793803 print('{"creationTime": ' + str(creation_time) + ', "modificationTime": ' + str(modification_time) + ', "size": ' + str(size) + ', "isDir": ' + str((stat[0] & 0o170000) == 0o040000).lower() + '}')
804+ del _pe_os
794805` ;
795806
796807 try {
@@ -833,7 +844,8 @@ export async function fsFileSize(
833844 emitter : EventEmitter ,
834845 item : string
835846) : Promise < number | undefined > {
836- const command = "import os\nprint(os.stat('" + item + "')[6])" ;
847+ const command =
848+ "import os as _pe_os\nprint(_pe_os.stat('" + item + "')[6])\ndel _pe_os" ;
837849
838850 try {
839851 const result = await executeCommand ( port , command , emitter ) ;
@@ -1156,7 +1168,7 @@ export async function fsMkdir(
11561168) : Promise < void > {
11571169 await executeCommand (
11581170 port ,
1159- `import os\nos .mkdir('${ target } ')` ,
1171+ `import os as _pe_os\n_pe_os .mkdir('${ target } ')\ndel _pe_os ` ,
11601172 emitter ,
11611173 undefined ,
11621174 silentFail
@@ -1182,7 +1194,7 @@ export async function fsRmdir(
11821194) : Promise < void > {
11831195 await executeCommand (
11841196 port ,
1185- `import os\nos .rmdir('${ target } ')` ,
1197+ `import os as _pe_os\n_pe_os .rmdir('${ target } ')\ndel _pe_os ` ,
11861198 emitter ,
11871199 undefined ,
11881200 silentFail
@@ -1205,18 +1217,19 @@ export async function fsRmdirRecursive(
12051217 //const commandShort = `import os; def __pe_deltree(target): [__pe_deltree((current:=target + d) if target == '/' else (current:=target + '/' + d)) or os.remove(current) for d in os.listdir(target)]; os.rmdir(target) if target != '/' else None; __pe_deltree('${target}'); del __pe_deltree`;
12061218
12071219 const command = `
1208- import os
1220+ import os as _pe_os
12091221def __pe_deltree(target):
1210- for d in os .listdir(target):
1222+ for d in _pe_os .listdir(target):
12111223 current = target.rstrip('/') + '/' + d
12121224 try:
12131225 __pe_deltree(current)
12141226 except OSError:
1215- os .remove(current)
1227+ _pe_os .remove(current)
12161228 if target != '/':
1217- os .rmdir(target)
1229+ _pe_os .rmdir(target)
12181230__pe_deltree('${ target } ')
12191231del __pe_deltree
1232+ del _pe_os
12201233` ;
12211234
12221235 await executeCommand ( port , command , emitter ) ;
@@ -1227,7 +1240,11 @@ export async function fsRemove(
12271240 target : string ,
12281241 emitter : EventEmitter
12291242) : Promise < void > {
1230- await executeCommand ( port , `import os\nos.remove('${ target } ')` , emitter ) ;
1243+ await executeCommand (
1244+ port ,
1245+ `import os as _pe_os\n_pe_os.remove('${ target } ')\ndel _pe_os` ,
1246+ emitter
1247+ ) ;
12311248}
12321249
12331250export async function fsRename (
@@ -1238,7 +1255,7 @@ export async function fsRename(
12381255) : Promise < void > {
12391256 await executeCommand (
12401257 port ,
1241- `import os\nos .rename('${ oldName } ','${ newName } ')` ,
1258+ `import os as _pe_os\n_pe_os .rename('${ oldName } ','${ newName } ')\ndel _pe_os ` ,
12421259 emitter
12431260 ) ;
12441261}
@@ -1276,14 +1293,15 @@ export async function fsIsDir(
12761293 emitter : EventEmitter
12771294) : Promise < boolean > {
12781295 const command = `
1279- import os
1296+ import os as _pe_os
12801297def __pe_is_dir(file_path):
12811298 try:
1282- return (os .stat(file_path)[0] & 0o170000) == 0o040000
1299+ return (_pe_os .stat(file_path)[0] & 0o170000) == 0o040000
12831300 except OSError:
12841301 return False
12851302print(__pe_is_dir('${ target } '))
12861303del __pe_is_dir
1304+ del _pe_os
12871305` ;
12881306
12891307 const { data, error } = await executeCommandWithResult (
@@ -1315,26 +1333,26 @@ export async function fsCalcFilesHashes(
13151333 emitter : EventEmitter
13161334) : Promise < HashResponse [ ] > {
13171335 const command = `
1318- import uhashlib
1319- import ubinascii
1320- import os
1321- import json
1336+ import uhashlib as _pe_uhashlib
1337+ import ubinascii as _pe_ubinascii
1338+ import os as _pe_os
1339+ import json as _pe_json
13221340
13231341def __pe_hash_file(file):
13241342 try:
1325- if os .stat(file)[6] > 200 * 1024:
1326- print(json .dumps({"file": file, "error": "File too large"}))
1343+ if _pe_os .stat(file)[6] > 200 * 1024:
1344+ print(_pe_json .dumps({"file": file, "error": "File too large"}))
13271345 return
13281346 with open(file, 'rb') as f:
1329- h = uhashlib .sha256()
1347+ h = _pe_uhashlib .sha256()
13301348 while True:
13311349 data = f.read(512)
13321350 if not data:
13331351 break
13341352 h.update(data)
1335- print(json .dumps({"file": file, "hash": ubinascii .hexlify(h.digest()).decode()}))
1353+ print(_pe_json .dumps({"file": file, "hash": _pe_ubinascii .hexlify(h.digest()).decode()}))
13361354 except Exception as e:
1337- print(json .dumps({"file": file, "error": f"{e.__class__.__name__}: {e}"}))
1355+ print(_pe_json .dumps({"file": file, "error": f"{e.__class__.__name__}: {e}"}))
13381356` ;
13391357
13401358 await executeCommand ( port , command , emitter ) ;
@@ -1361,7 +1379,7 @@ def __pe_hash_file(file):
13611379
13621380 await executeCommand (
13631381 port ,
1364- "del __pe_hash_file" ,
1382+ "del __pe_hash_file\ndel _pe_os\ndel _pe_uhashlib\ndel _pe_ubinascii\ndel _pe_json " ,
13651383 emitter ,
13661384 undefined ,
13671385 true ,
@@ -1434,9 +1452,9 @@ export async function runRemoteFile(
14341452 try {
14351453 await executeCommand (
14361454 port ,
1437- `import os; _pe_dir=os .getcwd(); os .chdir('${ dirnamePosix (
1455+ `import os as _pe_os ; _pe_dir=_pe_os .getcwd(); _pe_os .chdir('${ dirnamePosix (
14381456 file
1439- ) } '); del os ;`,
1457+ ) } '); del _pe_os ;`,
14401458 emitter ,
14411459 undefined ,
14421460 true ,
@@ -1452,7 +1470,7 @@ export async function runRemoteFile(
14521470 // run as extra command call so it gets executed even if an interrupt happens
14531471 await executeCommand (
14541472 port ,
1455- "import os; os .chdir(_pe_dir); del _pe_dir" ,
1473+ "import os as _pe_os\n_pe_os .chdir(_pe_dir)\ndel _pe_dir\ndel _pe_os " ,
14561474 emitter ,
14571475 undefined ,
14581476 true ,
@@ -1659,3 +1677,23 @@ export async function interactiveCtrlD(
16591677//export async function fsFactoryReset(port: SerialPort): Promise<void> {
16601678// await executeCommand(port, "import os\nos.mkfs('/flash')");
16611679//}
1680+
1681+ /**
1682+ * Performs garbage collection on the connected board.
1683+ *
1684+ * @param port The serial port to write to.
1685+ * @param emitter The event emitter to listen to for interrupt events.
1686+ */
1687+ export async function doGarbageCollection (
1688+ port : SerialPort ,
1689+ emitter : EventEmitter
1690+ ) : Promise < void > {
1691+ await executeCommand (
1692+ port ,
1693+ "import gc as __pe_gc\n__pe_gc.collect()\ndel __pe_gc" ,
1694+ emitter ,
1695+ undefined ,
1696+ true ,
1697+ true
1698+ ) ;
1699+ }
0 commit comments