#23091 closed defect (duplicate)
preparsing breaks from __future__ import directives
Reported by: | mmezzarobba | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | sage-duplicate/invalid/wontfix |
Component: | user interface | Keywords: | |
Cc: | Merged in: | ||
Authors: | Reviewers: | ||
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description
~/tmp$ cat > foo.sage from __future__ import print_function ~/tmp$ sage foo.sage File "foo.sage.py", line 5 from __future__ import print_function SyntaxError: from __future__ imports must occur at the beginning of the file (exit 1) ~/tmp$ cat foo.sage.py # This file was *autogenerated* from the file foo.sage from sage.all_cmdline import * # import sage library from __future__ import print_function
Change History (12)
comment:1 follow-up: ↓ 7 Changed 2 years ago by
comment:2 Changed 2 years ago by
If one first strips off all the comments then it's perhaps doable.
comment:3 Changed 2 years ago by
Compared to the other fragile hacks in the preparser this would be minor issue imho
comment:4 Changed 2 years ago by
I've written a small tool that injects code *after* all from __future__
imports:
https://github.com/timokau/python-inject
I've written that for a similar purpose (https://github.com/NixOS/nixpkgs/pull/53816), but haven't done significant testing yet. Maybe its relevant for this usecase.
comment:5 Changed 2 years ago by
My trick is to have
from __future__ import division, absolute_import, print_function
as the first line of my init dot sage file:
Maybe SageMath 9.0 could start by such imports, whether Python2-based or Python3-based, to encourage and guide everybody's transition to Python3, and later on SageMath 10.0 could be Python3-only.
comment:6 Changed 2 years ago by
- Milestone changed from sage-8.0 to sage-8.7
comment:7 in reply to: ↑ 1 Changed 2 years ago by
Replying to vdelecroix:
That would not be that simple to handle. Just checking for the regexp
^from __future__
would not be enough since this could appear in a string such as""" My module from __future__ """ def f(x): return x(I agree that this is very artificial)
Should be using the ast
module to parse out import statements.
comment:8 Changed 2 years ago by
- Milestone changed from sage-8.7 to sage-8.8
Ticket retargeted after milestone closed (if you don't believe this ticket is appropriate for the Sage 8.8 release please retarget manually)
comment:9 Changed 23 months ago by
- Milestone changed from sage-8.8 to sage-duplicate/invalid/wontfix
- Status changed from new to needs_review
fixed by #27719
comment:10 Changed 23 months ago by
- Status changed from needs_review to positive_review
comment:11 Changed 23 months ago by
- Resolution set to duplicate
- Status changed from positive_review to closed
comment:12 Changed 23 months ago by
Sorry, I missed this ticket when I created #27719.
That would not be that simple to handle. Just checking for the regexp
^from __future__
would not be enough since this could appear in a string such as(I agree that this is very artificial)