From 3ff93939a3ccc2a2d5f424483af3db56e369cb73 Mon Sep 17 00:00:00 2001
From: Antonio Rojas <arojas@archlinux.org>
Date: Sat, 20 Jun 2026 11:26:31 +0200
Subject: [PATCH] Support planarity 5

Adapt to planarity 5 API changes while keeping compatibility with v4
---
 src/sage/graphs/planarity-compat.h | 5 +++++
 src/sage/graphs/planarity.pyx      | 8 ++++----
 2 files changed, 9 insertions(+), 4 deletions(-)
 create mode 100644 src/sage/graphs/planarity-compat.h

diff --git a/src/sage/graphs/planarity-compat.h b/src/sage/graphs/planarity-compat.h
new file mode 100644
index 00000000000..7048d92210d
--- /dev/null
+++ b/src/sage/graphs/planarity-compat.h
@@ -0,0 +1,5 @@
+#include <planarity/graphLib.h>
+
+#if GP_PROJECTVERSION_MAJOR < 5
+#define gp_EnsureVertexCapacity gp_InitGraph
+#endif
diff --git a/src/sage/graphs/planarity.pyx b/src/sage/graphs/planarity.pyx
index b9c8db149be..ad5d6c8e8b6 100644
--- a/src/sage/graphs/planarity.pyx
+++ b/src/sage/graphs/planarity.pyx
@@ -3,7 +3,7 @@
 Wrapper for Boyer's (C) planarity algorithm
 """
 
-cdef extern from "planarity/graph.h":
+cdef extern from "planarity-compat.h":
     ctypedef struct vertexRec:
         int link[2]
         int index
@@ -24,7 +24,7 @@ cdef extern from "planarity/graph.h":
 
     cdef graphP gp_New()
     cdef void gp_Free(graphP *pGraph)
-    cdef int gp_InitGraph(graphP theGraph, int N)
+    cdef int gp_EnsureVertexCapacity(graphP theGraph, int N)
     cdef int gp_AddEdge(graphP theGraph, int u, int ulink, int v, int vlink)
     cdef int gp_Embed(graphP theGraph, int embedFlags)
     cdef int gp_SortVertices(graphP theGraph)
@@ -144,9 +144,9 @@ def is_planar(g, kuratowski=False, set_pos=False, set_embedding=False, immutable
     cdef graphP theGraph
     theGraph = gp_New()
     cdef int status
-    status = gp_InitGraph(theGraph, g.order())
+    status = gp_EnsureVertexCapacity(theGraph, g.order())
     if status != OK:
-        raise RuntimeError("gp_InitGraph status is not ok")
+        raise RuntimeError("gp_EnsureVertexCapacity status is not ok")
     for u, v in g.edge_iterator(labels=False):
         status = gp_AddEdge(theGraph, ffrom[u], 0, ffrom[v], 0)
         if status == NOTOK:
