Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable created in exec changes type if in list comprehension #39239

Open
2 tasks done
pranavsetpal opened this issue Jan 1, 2025 · 4 comments
Open
2 tasks done

Variable created in exec changes type if in list comprehension #39239

pranavsetpal opened this issue Jan 1, 2025 · 4 comments
Labels

Comments

@pranavsetpal
Copy link

pranavsetpal commented Jan 1, 2025

Steps To Reproduce

Run

#main.sage
exec("n = 6")

print(n)
print(type(n))

ls = [i for i in range(n)]

using sage main.sage

Expected Behavior

Should output

6                                                                                                             
<class 'int'>

This output can be seen by commenting out the last line, i.e. by running

exec("n = 6")

print(n)
print(type(n))

Actual Behavior

Returns error

<function numerical_approx at 0x7f2775c46d40>                                                                 
<class 'function'>                                                                                            
Traceback (most recent call last):                                                                            
  File "/home/pranav/bug/main.sage.py", line 11, in <module>                                                  
    ls = [i for i in range(n)]                                                                                
                     ^^^^^^^^                                                                                 
TypeError: 'function' object cannot be interpreted as an integer                                              
                                                                                                              
shell returned 1

Additional Information

This issue only occurs if the variable is created by the exec function. Further, trying to run n = Integer(n) immediately after the exec statement is not useful since sage already assumes n to be a function

Environment

  • OS: NixOS 24.05
  • Sage Version: SageMath version 10.5, Release Date: 2024-12-04

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@DaveWitteMorris
Copy link
Member

I cannot reproduce the problem: I get the correct output in both cases (with the final line and without). This is with sagemath 10.6.beta2 on MacOS 14.6.1.

@pranavsetpal
Copy link
Author

pranavsetpal commented Jan 2, 2025

I'm trying to build sagemath 10.6.beta2 on my system and test it. I'll let you know as soon as that gets done.

Update: Still facing a few errors compiling; made a support post here https://groups.google.com/g/sage-support/c/aWN-OmMgU1c

@pranavsetpal
Copy link
Author

I have now tested it on v10.6.beta2 and still receiving the same issue

@pranavsetpal
Copy link
Author

I was debugging this behavior a bit more and a difference in importing sage seems to cause the issue (wth??)

from sage.all import *

zero = Integer(0)
exec("n = 6")

print(n)
print(type(n))

ls = [zero for i in range(n)]

this gives the error, but

from sage.all import AA,AbelianGroup, ... # list of all packages gotten from "from sage import all; print( ','.join(dir(all)) )"

zero = Integer(0)
exec("n = 6")

print(n)
print(type(n))

ls = [zero for i in range(n)]

this gives the expected behavior.

This is most likely an issue on my end, but could one more person confirm they cannot reproduce this and I'll close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants