Opened 4 years ago

Closed 23 months ago

Last modified 23 months ago

#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:

Status badges

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: Changed 2 years ago by 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)

comment:2 Changed 2 years ago by dimpase

If one first strips off all the comments then it's perhaps doable.

comment:3 Changed 2 years ago by vbraun

Compared to the other fragile hacks in the preparser this would be minor issue imho

comment:4 Changed 2 years ago by gh-timokau

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 slelievre

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 embray

  • Milestone changed from sage-8.0 to sage-8.7

comment:7 in reply to: ↑ 1 Changed 2 years ago by embray

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 embray

  • 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 mmezzarobba

  • 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 mmezzarobba

  • Status changed from needs_review to positive_review

comment:11 Changed 23 months ago by chapoton

  • Resolution set to duplicate
  • Status changed from positive_review to closed

comment:12 Changed 23 months ago by jhpalmieri

Sorry, I missed this ticket when I created #27719.

Note: See TracTickets for help on using tickets.