joshuarauh commited on
Commit
c55aa08
·
verified ·
1 Parent(s): 777994f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
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
- sympy_globals = {
350
- 'sympy': sympy,
351
- '__builtins__': __builtins__
 
352
  }
353
 
354
- # Add all sympy attributes
355
- for name in dir(sympy):
356
- sympy_globals[name] = getattr(sympy, name)
 
357
 
358
- logger.debug(f"Global namespace keys: {list(sympy_globals.keys())}")
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, sympy_globals, local_vars)
368
  except NameError as e:
369
  logger.error(f"NameError during execution: {str(e)}")
370
- # Log the actual error traceback
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__') and
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()