Ticket #13719: trac_13719.patch

File trac_13719.patch, 2.2 KB (added by jpflori, 6 years ago)

Handle correctly empty trees ; doctest in trees.pyx

  • sage/graphs/trees.pyx

    # HG changeset patch
    # User Jean-Pierre Flori <jean-pierre.flor@ssi.gouv.fr>
    # Date 1353343752 -3600
    # Node ID 6cd9b52cfb4ea028f78247322b490e16107a8c00
    # Parent  1557e9aeccb47fc8065714825c9823ea99761c34
    #13719: Handle tree with no vertices correctly
    
    diff --git a/sage/graphs/trees.pyx b/sage/graphs/trees.pyx
    a b  
    8585        """
    8686        if self.l != NULL:
    8787            sage_free(self.l)
     88            self.l = NULL
    8889        if self.current_level_sequence != NULL:
    8990            sage_free(self.current_level_sequence)
     91            self.current_level_sequence = NULL
    9092
    9193    def __str__(self):
    9294        r"""
     
    122124            sage: T = TreeIterator(5)
    123125            sage: [t for t in T] # indirect doctest
    124126            [Graph on 5 vertices, Graph on 5 vertices, Graph on 5 vertices]
     127
     128
     129        TESTS:
     130
     131        This used to be broken for trees with no vertices
     132        and was fixed in :trac:`13719` ::
     133
     134            sage: from sage.graphs.trees import TreeIterator
     135            sage: T = TreeIterator(0)
     136            sage: [t for t in T] # indirect doctest
     137            [Graph on 0 vertices]
    125138        """
    126139
    127140        if not self.first_time and self.q == 0:
    128141            raise StopIteration
    129142
    130143        if self.first_time == 1:
    131             self.l = <int *>sage_malloc(self.vertices * sizeof(int))
    132             self.current_level_sequence = <int *>sage_malloc(self.vertices * sizeof(int))
    133            
    134             if self.l == NULL or self.current_level_sequence == NULL:
    135                 raise MemoryError
     144            if self.vertices == 0:
     145                self.first_time = 0
     146                self.q = 0
     147            else:
     148                self.l = <int *>sage_malloc(self.vertices * sizeof(int))
     149                self.current_level_sequence = <int *>sage_malloc(self.vertices * sizeof(int))
    136150
    137             self.generate_first_level_sequence()
    138             self.first_time = 0
     151                if self.l == NULL or self.current_level_sequence == NULL:
     152                    raise MemoryError
     153
     154                self.generate_first_level_sequence()
     155                self.first_time = 0
    139156        else:
    140157            self.generate_next_level_sequence()
    141158