Skip to content

Commit e2c8fab

Browse files
committed
Fix bug when running eval(obj) when obj is a result of compile()
1 parent a774d39 commit e2c8fab

File tree

9 files changed

+1959
-1918
lines changed

9 files changed

+1959
-1918
lines changed

www/src/ast_to_js.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ function copy_position(target, origin){
130130
target.end_col_offset = origin.end_col_offset
131131
}
132132

133+
$B.copy_position = copy_position
134+
133135
function encode_position(lineno, end_lineno, col_offset, end_col_offset){
134136
var res
135137
if(end_lineno == lineno){

www/src/brython.js

Lines changed: 24 additions & 11 deletions
Large diffs are not rendered by default.

www/src/brython.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

www/src/brython_stdlib.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

www/src/py_builtin_functions.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,25 @@ var $$eval = _b_.eval = function(){
734734
var _frame_obj = $B.frame_obj
735735

736736
if(src.__class__ === code){
737+
if(src.mode == 'exec' && mode == 'eval'){
738+
return _b_.None
739+
}
737740
_ast = src._ast
738741
if(_ast.$js_ast){
739742
_ast = _ast.$js_ast
740743
}else{
741744
_ast = $B.ast_py_to_js(_ast)
742745
}
746+
if(_ast instanceof $B.ast.Expression){
747+
// transform `expr` into `varname = expr` so that the exec_func
748+
// can return varname
749+
var expr_name = '_' + $B.UUID()
750+
var name = new $B.ast.Name(expr_name, new $B.ast.Store())
751+
$B.copy_position(name, _ast.body)
752+
var assign = new $B.ast.Assign([name], _ast.body)
753+
$B.copy_position(assign, _ast.body)
754+
_ast = new $B.ast.Module([assign])
755+
}
743756
}
744757

745758
try{
@@ -776,18 +789,31 @@ var $$eval = _b_.eval = function(){
776789
if(mode == 'eval'){
777790
// must set locals, might be used if expression is like
778791
// "True and True"
779-
js = `var __file__ = '${filename}'\n` +
780-
`var locals = ${local_name}\nreturn ${js}`
792+
if(src.__class__ === _b_.code){
793+
js += `\nreturn locals.${expr_name}`
794+
}else{
795+
js = `var __file__ = '${filename}'\n` +
796+
`var locals = ${local_name};\n` +
797+
'return ' + js
798+
}
781799
}else if(src.single_expression){
782-
js = `var __file__ = '${filename}'\n` +
783-
`var result = ${js}\n` +
784-
`if(result !== _b_.None){\n` +
785-
`_b_.print(result)\n` +
786-
`}`
800+
if(src.__class__ === _b_.code){
801+
js += `var result = locals.${expr_name}\n` +
802+
`if(result !== _b_.None){\n` +
803+
`_b_.print(result)\n` +
804+
`}`
805+
806+
}else{
807+
js = `var __file__ = '${filename}'\n` +
808+
`var result = ${js}\n` +
809+
`if(result !== _b_.None){\n` +
810+
`_b_.print(result)\n` +
811+
`}`
812+
}
787813
}
788814

789815
try{
790-
var exec_func = new Function('$B', '_b_',
816+
var exec_func = new Function('$B', '_b_', 'locals',
791817
local_name, global_name,
792818
'frame', '_frame_obj', js)
793819
}catch(err){
@@ -800,7 +826,7 @@ var $$eval = _b_.eval = function(){
800826
}
801827

802828
try{
803-
var res = exec_func($B, _b_,
829+
var res = exec_func($B, _b_, exec_locals,
804830
exec_locals, exec_globals, frame, _frame_obj)
805831
}catch(err){
806832
if($B.get_option('debug') > 2){

www/src/py_flags.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
(function($B){
33
$B.builtin_class_flags = {
44
builtins: {
5-
1073763586: ['FutureWarning', 'EnvironmentError', 'BaseExceptionGroup', 'ReferenceError', 'TypeError', 'FileNotFoundError', 'UnboundLocalError', 'UnicodeTranslateError', 'UnicodeWarning', 'IndentationError', 'BufferError', 'AssertionError', 'BytesWarning', 'ProcessLookupError', 'StopIteration', 'LookupError', 'ImportWarning', 'IOError', 'FileExistsError', 'NameError', 'IndexError', 'FloatingPointError', 'RecursionError', 'SyntaxError', 'ModuleNotFoundError', 'TimeoutError', 'IsADirectoryError', 'Warning', 'ResourceWarning', 'ConnectionResetError', 'MemoryError', 'RuntimeWarning', 'SystemExit', 'Exception', 'NotADirectoryError', 'OverflowError', 'TabError', 'PermissionError', 'BaseException', 'KeyError', 'ArithmeticError', 'UnicodeError', 'NotImplementedError', 'KeyboardInterrupt', 'AttributeError', 'DeprecationWarning', 'UserWarning', 'ConnectionError', 'BlockingIOError', 'BrokenPipeError', 'OSError', 'RuntimeError', 'ConnectionAbortedError', 'InterruptedError', 'SystemError', 'ValueError', 'EncodingWarning', 'WindowsError', '_IncompleteInputError', 'SyntaxWarning', 'ConnectionRefusedError', 'PendingDeprecationWarning', 'PythonFinalizationError', 'ImportError', 'UnicodeEncodeError', 'GeneratorExit', 'ChildProcessError', 'StopAsyncIteration', 'UnicodeDecodeError', 'ZeroDivisionError', 'EOFError'],
5+
1073763586: ['SystemExit', 'SystemError', 'NotADirectoryError', 'BlockingIOError', 'IndexError', 'ConnectionRefusedError', 'BytesWarning', 'ModuleNotFoundError', 'TabError', 'ImportError', 'UnboundLocalError', '_IncompleteInputError', 'BaseExceptionGroup', 'ValueError', 'UnicodeDecodeError', 'EnvironmentError', 'FutureWarning', 'DeprecationWarning', 'SyntaxWarning', 'EncodingWarning', 'FloatingPointError', 'EOFError', 'NotImplementedError', 'OverflowError', 'TypeError', 'PermissionError', 'Warning', 'SyntaxError', 'ArithmeticError', 'StopAsyncIteration', 'InterruptedError', 'RuntimeWarning', 'UnicodeTranslateError', 'Exception', 'ReferenceError', 'ProcessLookupError', 'UnicodeWarning', 'ConnectionResetError', 'KeyboardInterrupt', 'RuntimeError', 'AssertionError', 'ImportWarning', 'PendingDeprecationWarning', 'UnicodeEncodeError', 'IndentationError', 'OSError', 'UserWarning', 'MemoryError', 'RecursionError', 'GeneratorExit', 'BrokenPipeError', 'BaseException', 'KeyError', 'AttributeError', 'FileNotFoundError', 'ConnectionError', 'PythonFinalizationError', 'StopIteration', 'NameError', 'FileExistsError', 'ZeroDivisionError', 'ChildProcessError', 'IOError', 'IsADirectoryError', 'LookupError', 'UnicodeError', 'WindowsError', 'TimeoutError', 'ResourceWarning', 'ConnectionAbortedError', 'BufferError'],
66
1073763848: ['ExceptionGroup'],
77
20975874: ['bool'],
88
4199682: ['float', 'bytearray'],
99
138417410: ['bytes'],
10-
21762: ['classmethod', 'filter', 'super', 'enumerate', 'zip', 'reversed', 'property', 'map', 'staticmethod'],
11-
5378: ['complex', 'object'],
10+
21762: ['classmethod', 'staticmethod', 'filter', 'reversed', 'super', 'zip', 'map', 'enumerate', 'property'],
11+
5378: ['object', 'complex'],
1212
541087042: ['dict'],
13-
4216066: ['set', 'frozenset'],
13+
4216066: ['frozenset', 'set'],
1414
20976898: ['int'],
1515
37770530: ['list'],
1616
20770: ['memoryview'],
@@ -21,10 +21,10 @@ $B.builtin_class_flags = {
2121
2155896066: ['type'],
2222
},
2323
types: {
24-
20866: ['getset_descriptor', 'async_generator', 'classmethod_descriptor', 'frame', 'coroutine', 'method-wrapper', 'generator', 'member_descriptor'],
24+
20866: ['async_generator', 'member_descriptor', 'classmethod_descriptor', 'coroutine', 'frame', 'method-wrapper', 'getset_descriptor', 'generator'],
2525
22914: ['builtin_function_or_method'],
26-
20738: ['cell', 'traceback'],
27-
4354: ['ellipsis', 'NotImplementedType', 'NoneType', 'code'],
26+
20738: ['traceback', 'cell'],
27+
4354: ['NotImplementedType', 'NoneType', 'code', 'ellipsis'],
2828
153858: ['function'],
2929
20802: ['mappingproxy'],
3030
153986: ['method_descriptor'],

www/src/stdlib_paths.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

www/src/version_info.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22
__BRYTHON__.implementation = [3, 13, 2, 'dev', 0]
33
__BRYTHON__.version_info = [3, 13, 0, 'final', 0]
4-
__BRYTHON__.compiled_date = "2025-07-18 14:32:34.066104"
5-
__BRYTHON__.timestamp = 1752841954065
4+
__BRYTHON__.compiled_date = "2025-07-19 07:50:21.885039"
5+
__BRYTHON__.timestamp = 1752904221884
66
__BRYTHON__.builtin_module_names = ["_ajax",
77
"_ast",
88
"_base64",

0 commit comments

Comments
 (0)