From 51ddf8b036dd167a29c56632b8855d27fd361838 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Sat, 26 Apr 2025 07:14:37 +0000
Subject: [PATCH v1 2/2] Avoid including plpython.h in plpy_util.h

Adding plpython_base.h to avoid including plpython.h in plpy_util.h (that
produced a circular include).
---
 src/pl/plpython/plpy_util.h     |  2 +-
 src/pl/plpython/plpython.h      | 26 +--------------------
 src/pl/plpython/plpython_base.h | 41 +++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 26 deletions(-)
 create mode 100644 src/pl/plpython/plpython_base.h

diff --git a/src/pl/plpython/plpy_util.h b/src/pl/plpython/plpy_util.h
index 6f491b0f95b..797692a9eec 100644
--- a/src/pl/plpython/plpy_util.h
+++ b/src/pl/plpython/plpy_util.h
@@ -6,7 +6,7 @@
 #ifndef PLPY_UTIL_H
 #define PLPY_UTIL_H
 
-#include "plpython.h"
+#include "plpython_base.h"
 
 extern PGDLLEXPORT PyObject *PLyUnicode_Bytes(PyObject *unicode);
 extern PGDLLEXPORT char *PLyUnicode_AsString(PyObject *unicode);
diff --git a/src/pl/plpython/plpython.h b/src/pl/plpython/plpython.h
index 06fc1a5440f..fddcc96acdc 100644
--- a/src/pl/plpython/plpython.h
+++ b/src/pl/plpython/plpython.h
@@ -12,31 +12,7 @@
 #ifndef PLPYTHON_H
 #define PLPYTHON_H
 
-/* postgres.h needs to be included before Python.h, as usual */
-#if !defined(POSTGRES_H)
-#error postgres.h must be included before plpython.h
-#elif defined(Py_PYTHON_H)
-#error Python.h must be included via plpython.h
-#endif
-
-/*
- * Enable Python Limited API
- *
- * XXX currently not enabled on MSVC because of build failures
- */
-#if !defined(_MSC_VER)
-#define Py_LIMITED_API 0x03020000
-#endif
-
-/*
- * Pull in Python headers via a wrapper header, to control the scope of
- * the system_header pragma therein.
- */
-#include "plpython_system.h"
-
-/* define our text domain for translations */
-#undef TEXTDOMAIN
-#define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
+#include "plpython_base.h"
 
 /*
  * Used throughout, so it's easier to just include it everywhere.
diff --git a/src/pl/plpython/plpython_base.h b/src/pl/plpython/plpython_base.h
new file mode 100644
index 00000000000..7abbcb8b233
--- /dev/null
+++ b/src/pl/plpython/plpython_base.h
@@ -0,0 +1,41 @@
+/*-------------------------------------------------------------------------
+ *
+ * plpython_base.h - Python as a procedural language for PostgreSQL
+ *
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/pl/plpython/plpython_base.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef PLPYTHON_BASE_H
+#define PLPYTHON_BASE_H
+
+/* postgres.h needs to be included before Python.h, as usual */
+#if !defined(POSTGRES_H)
+#error postgres.h must be included before plpython_base.h
+#elif defined(Py_PYTHON_H)
+#error Python.h must be included via plpython_base.h
+#endif
+
+/*
+ * Enable Python Limited API
+ *
+ * XXX currently not enabled on MSVC because of build failures
+ */
+#if !defined(_MSC_VER)
+#define Py_LIMITED_API 0x03020000
+#endif
+
+/*
+ * Pull in Python headers via a wrapper header, to control the scope of
+ * the system_header pragma therein.
+ */
+#include "plpython_system.h"
+
+/* define our text domain for translations */
+#undef TEXTDOMAIN
+#define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
+
+#endif							/* PLPYTHON_BASE_H */
-- 
2.34.1

