From 3042cfd81dd5f13be6236a44ba294b427db6e33c Mon Sep 17 00:00:00 2001 From: dcoudert Date: Sun, 3 Nov 2024 17:56:12 +0100 Subject: [PATCH 1/2] fix issue 38900: preserve backend with unpikling --- src/sage/graphs/base/graph_backends.pyx | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/sage/graphs/base/graph_backends.pyx b/src/sage/graphs/base/graph_backends.pyx index 4363a812995..bb07f948d4f 100644 --- a/src/sage/graphs/base/graph_backends.pyx +++ b/src/sage/graphs/base/graph_backends.pyx @@ -668,6 +668,8 @@ cdef class GenericGraphBackend(SageObject): produces a copy of ``self``. The function returned is always :func:`unpickle_graph_backend`. + EXAMPLES: + Pickling of the static graph backend makes pickling of immutable graphs and digraphs work:: @@ -703,20 +705,30 @@ cdef class GenericGraphBackend(SageObject): sage: gi = g.copy(immutable=True) sage: loads(dumps(gi)) == gi True + + TESTS: + + Check that :issue:`38900` is fixed: + + sage: from itertools import product + sage: for sparse, immutable in product([True, False], [True, False]): + ....: G = Graph([[0, 1, 2], [(0, 1)]], sparse=sparse, immutable=immutable) + ....: H = loads(dumps(G)) + ....: if type(G._backend) != type(H._backend): + ....: print(sparse, immutable, type(G._backend), type(H._backend)) """ from sage.graphs.base.static_sparse_backend import StaticSparseBackend from sage.graphs.base.sparse_graph import SparseGraphBackend from sage.graphs.base.dense_graph import DenseGraphBackend - # implementation, data_structure, multiedges, directed, loops + # data_structure, multiedges, directed, loops if isinstance(self, CGraphBackend): - implementation = "c_graph" if isinstance(self, SparseGraphBackend): data_structure = "sparse" elif isinstance(self, DenseGraphBackend): data_structure = "dense" elif isinstance(self, StaticSparseBackend): - implementation = "static_sparse" + data_structure = "static_sparse" else: raise Exception multiedges = ( self)._multiple_edges @@ -735,7 +747,8 @@ cdef class GenericGraphBackend(SageObject): return (unpickle_graph_backend, (directed, vertices, edges, {'loops': loops, - 'multiedges': multiedges})) + 'multiedges': multiedges, + 'data_structure': data_structure})) def unpickle_graph_backend(directed, vertices, edges, kwds): @@ -779,6 +792,5 @@ def unpickle_graph_backend(directed, vertices, edges, kwds): else: from sage.graphs.graph import Graph as constructor - G = constructor(data=edges, **kwds) - G.add_vertices(vertices) + G = constructor(data=[vertices, edges], format='vertices_and_edges', **kwds) return G._backend From d45dac9ef368239c7c8bca8981a656e2b26d24c3 Mon Sep 17 00:00:00 2001 From: dcoudert Date: Tue, 5 Nov 2024 09:40:40 +0100 Subject: [PATCH 2/2] PR #38919: fix documentation --- src/sage/graphs/base/graph_backends.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/base/graph_backends.pyx b/src/sage/graphs/base/graph_backends.pyx index bb07f948d4f..518fe1b6700 100644 --- a/src/sage/graphs/base/graph_backends.pyx +++ b/src/sage/graphs/base/graph_backends.pyx @@ -708,7 +708,7 @@ cdef class GenericGraphBackend(SageObject): TESTS: - Check that :issue:`38900` is fixed: + Check that :issue:`38900` is fixed:: sage: from itertools import product sage: for sparse, immutable in product([True, False], [True, False]):