Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -346,28 +346,32 @@ def extract_and_run_sympy_code(response_text):
|
|
346 |
logger.debug(f"Code after removing imports:\n{sympy_code}")
|
347 |
|
348 |
# Create globals dict
|
349 |
-
|
350 |
-
|
351 |
-
|
|
|
352 |
}
|
353 |
|
354 |
-
# Add all
|
355 |
-
for name in dir(
|
356 |
-
|
|
|
357 |
|
358 |
-
logger.debug(f"Global namespace keys: {list(
|
359 |
|
360 |
# Create locals dict and output buffer
|
361 |
local_vars = {}
|
|
|
|
|
362 |
output_buffer = io.StringIO()
|
363 |
|
364 |
logger.debug("Attempting to execute code")
|
365 |
# Execute with try/except to catch specific error
|
366 |
try:
|
367 |
-
exec(sympy_code,
|
368 |
except NameError as e:
|
369 |
logger.error(f"NameError during execution: {str(e)}")
|
370 |
-
|
371 |
import traceback
|
372 |
logger.error(f"Full traceback:\n{traceback.format_exc()}")
|
373 |
return f"Error executing SymPy code: {str(e)}"
|
@@ -384,8 +388,7 @@ def extract_and_run_sympy_code(response_text):
|
|
384 |
print("-" * 25)
|
385 |
for var_name, value in local_vars.items():
|
386 |
if (not var_name.startswith('__') and
|
387 |
-
not hasattr(value, '__module__')
|
388 |
-
var_name != 'sympy'):
|
389 |
print(f"{var_name}: {value}")
|
390 |
|
391 |
result = output_buffer.getvalue()
|
|
|
346 |
logger.debug(f"Code after removing imports:\n{sympy_code}")
|
347 |
|
348 |
# Create globals dict
|
349 |
+
import sys
|
350 |
+
module = sys.modules[__name__] # Get the current module
|
351 |
+
globals_dict = {
|
352 |
+
'__builtins__': __builtins__,
|
353 |
}
|
354 |
|
355 |
+
# Add all imported SymPy names to globals
|
356 |
+
for name in dir(module):
|
357 |
+
if not name.startswith('_'): # Skip private names
|
358 |
+
globals_dict[name] = getattr(module, name)
|
359 |
|
360 |
+
logger.debug(f"Global namespace keys: {list(globals_dict.keys())}")
|
361 |
|
362 |
# Create locals dict and output buffer
|
363 |
local_vars = {}
|
364 |
+
import io
|
365 |
+
from contextlib import redirect_stdout
|
366 |
output_buffer = io.StringIO()
|
367 |
|
368 |
logger.debug("Attempting to execute code")
|
369 |
# Execute with try/except to catch specific error
|
370 |
try:
|
371 |
+
exec(sympy_code, globals_dict, local_vars)
|
372 |
except NameError as e:
|
373 |
logger.error(f"NameError during execution: {str(e)}")
|
374 |
+
logger.debug(f"Available names: {list(globals_dict.keys())}")
|
375 |
import traceback
|
376 |
logger.error(f"Full traceback:\n{traceback.format_exc()}")
|
377 |
return f"Error executing SymPy code: {str(e)}"
|
|
|
388 |
print("-" * 25)
|
389 |
for var_name, value in local_vars.items():
|
390 |
if (not var_name.startswith('__') and
|
391 |
+
not hasattr(value, '__module__')):
|
|
|
392 |
print(f"{var_name}: {value}")
|
393 |
|
394 |
result = output_buffer.getvalue()
|