[Python-checkins] bpo-45866: pegen strips directory of "generated from" header (GH-29777)

vstinner webhook-mailer at python.org
Fri Nov 26 05:50:38 EST 2021


https://github.com/python/cpython/commit/253b7a0a9fef1d72a4cb87b837885576e68e917c
commit: 253b7a0a9fef1d72a4cb87b837885576e68e917c
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-11-26T11:50:34+01:00
summary:

bpo-45866: pegen strips directory of "generated from" header (GH-29777)

"make regen-all" now produces the same output when run from a
directory other than the source tree: when building Python out of the
source tree.

files:
A Misc/NEWS.d/next/Build/2021-11-25-13-53-36.bpo-45866.ZH1W8N.rst
M Parser/parser.c
M Tools/peg_generator/pegen/c_generator.py
M Tools/peg_generator/pegen/grammar_parser.py
M Tools/peg_generator/pegen/python_generator.py

diff --git a/Misc/NEWS.d/next/Build/2021-11-25-13-53-36.bpo-45866.ZH1W8N.rst b/Misc/NEWS.d/next/Build/2021-11-25-13-53-36.bpo-45866.ZH1W8N.rst
new file mode 100644
index 0000000000000..efa046d91e08e
--- /dev/null
+++ b/Misc/NEWS.d/next/Build/2021-11-25-13-53-36.bpo-45866.ZH1W8N.rst
@@ -0,0 +1,4 @@
+``make regen-all`` now produces the same output when run from a directory
+other than the source tree: when building Python out of the source tree.
+pegen now strips directory of the "generated by pygen from <FILENAME" header
+Patch by Victor Stinner.
diff --git a/Parser/parser.c b/Parser/parser.c
index 1cf6e356ad4bf..b139e55705250 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -1,4 +1,4 @@
-// @generated by pegen from ./Grammar/python.gram
+// @generated by pegen from python.gram
 #include "pegen.h"
 
 #if defined(Py_DEBUG) && defined(Py_BUILD_CORE)
diff --git a/Tools/peg_generator/pegen/c_generator.py b/Tools/peg_generator/pegen/c_generator.py
index 10dc94b04fefd..9cfbf38b40a77 100644
--- a/Tools/peg_generator/pegen/c_generator.py
+++ b/Tools/peg_generator/pegen/c_generator.py
@@ -1,4 +1,5 @@
 import ast
+import os.path
 import re
 from dataclasses import dataclass, field
 from enum import Enum
@@ -416,7 +417,8 @@ def out_of_memory_goto(self, expr: str, goto_target: str) -> None:
 
     def generate(self, filename: str) -> None:
         self.collect_rules()
-        self.print(f"// @generated by pegen from {filename}")
+        basename = os.path.basename(filename)
+        self.print(f"// @generated by pegen from {basename}")
         header = self.grammar.metas.get("header", EXTENSION_PREFIX)
         if header:
             self.print(header.rstrip("\n"))
diff --git a/Tools/peg_generator/pegen/grammar_parser.py b/Tools/peg_generator/pegen/grammar_parser.py
index 6e9f7d3d11d29..bf31fe5326364 100644
--- a/Tools/peg_generator/pegen/grammar_parser.py
+++ b/Tools/peg_generator/pegen/grammar_parser.py
@@ -1,5 +1,5 @@
 #!/usr/bin/env python3.8
-# @generated by pegen from ./Tools/peg_generator/pegen/metagrammar.gram
+# @generated by pegen from metagrammar.gram
 
 import ast
 import sys
diff --git a/Tools/peg_generator/pegen/python_generator.py b/Tools/peg_generator/pegen/python_generator.py
index 25a48bfab5d6c..5329d0ebe5e64 100644
--- a/Tools/peg_generator/pegen/python_generator.py
+++ b/Tools/peg_generator/pegen/python_generator.py
@@ -1,3 +1,4 @@
+import os.path
 import token
 from typing import IO, Any, Dict, Optional, Sequence, Set, Text, Tuple
 
@@ -212,7 +213,8 @@ def generate(self, filename: str) -> None:
         self.collect_rules()
         header = self.grammar.metas.get("header", MODULE_PREFIX)
         if header is not None:
-            self.print(header.rstrip("\n").format(filename=filename))
+            basename = os.path.basename(filename)
+            self.print(header.rstrip("\n").format(filename=basename))
         subheader = self.grammar.metas.get("subheader", "")
         if subheader:
             self.print(subheader)



More information about the Python-checkins mailing list