https://github.com/python/cpython/commit/7c97dc8c9594c71bd3d1f69758a27de45f57e4c3
commit: 7c97dc8c9594c71bd3d1f69758a27de45f57e4c3
branch: main
author: Crowthebird <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2024-05-02T06:32:20-07:00
summary:

gh-118216: Don't consider dotted `__future__` imports (#118267)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst
M Doc/whatsnew/3.13.rst
M Lib/test/test_future_stmt/test_future.py
M Python/compile.c
M Python/future.c

diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index 52fb9752cf4132..3ccf17be9796d5 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -288,6 +288,10 @@ Other Language Changes
   class scopes are not inlined into their parent scope. (Contributed by
   Jelle Zijlstra in :gh:`109118` and :gh:`118160`.)
 
+* ``from __future__ import ...`` statements are now just normal
+  relative imports if dots are present before the module name.
+  (Contributed by Jeremiah Gabriel Pascual in :gh:`118216`.)
+
 
 New Modules
 ===========
diff --git a/Lib/test/test_future_stmt/test_future.py 
b/Lib/test/test_future_stmt/test_future.py
index 2c8ceb664cb362..69ae58b0fbcae3 100644
--- a/Lib/test/test_future_stmt/test_future.py
+++ b/Lib/test/test_future_stmt/test_future.py
@@ -203,6 +203,25 @@ def test_syntactical_future_repl(self):
         out = kill_python(p)
         self.assertNotIn(b'SyntaxError: invalid syntax', out)
 
+    def test_future_dotted_import(self):
+        with self.assertRaises(ImportError):
+            exec("from .__future__ import spam")
+
+        code = dedent(
+            """
+            from __future__ import print_function
+            from ...__future__ import ham
+            """
+        )
+        with self.assertRaises(ImportError):
+            exec(code)
+
+        code = """
+            from .__future__ import nested_scopes
+            from __future__ import barry_as_FLUFL
+        """
+        self.assertSyntaxError(code, lineno=2)
+
 class AnnotationsFutureTestCase(unittest.TestCase):
     template = dedent(
         """
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst
new file mode 100644
index 00000000000000..937cdffefda6f1
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst 
@@ -0,0 +1 @@
+Don't consider :mod:`__future__` imports with dots before the module name.
diff --git a/Python/compile.c b/Python/compile.c
index 4e94f9297a32d1..feedd988834397 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3849,7 +3849,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
     }
 
     if (location_is_after(LOC(s), c->c_future.ff_location) &&
-        s->v.ImportFrom.module &&
+        s->v.ImportFrom.module && s->v.ImportFrom.level == 0 &&
         _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__"))
     {
         Py_DECREF(names);
diff --git a/Python/future.c b/Python/future.c
index 399345bd8fcbd9..8d94d515605dcd 100644
--- a/Python/future.c
+++ b/Python/future.c
@@ -77,7 +77,7 @@ future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject 
*filename)
          *  are another future statement and a doc string.
          */
 
-        if (s->kind == ImportFrom_kind) {
+        if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) {
             identifier modname = s->v.ImportFrom.module;
             if (modname &&
                 _PyUnicode_EqualToASCIIString(modname, "__future__")) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to