Ticket #11942: trac_11942-exceptions.2.patch

File trac_11942-exceptions.2.patch, 9.7 KB (added by jdemeyer, 8 years ago)

Use LookupError? instead of ValueError? or KeyError?

  • sage/graphs/base/c_graph.pyx

    # HG changeset patch
    # User Keshav Kini <keshav.kini@gmail.com>
    # Date 1325758636 -28800
    # Node ID b7972f3ef7b74e19afdea560b813b0d864f49ad2
    # Parent  b6d33f5d4138b4cdb9279a8f0c3737103ac56e31
    Use more semantically appropriate exceptions in C graphs
    
    diff --git a/sage/graphs/base/c_graph.pyx b/sage/graphs/base/c_graph.pyx
    a b  
    140140            sage: S.check_vertex(12)
    141141            Traceback (most recent call last):
    142142            ...
    143             RuntimeError: Vertex (12) is not a vertex of the graph.
     143            LookupError: Vertex (12) is not a vertex of the graph.
    144144            sage: S.check_vertex(24)
    145145            Traceback (most recent call last):
    146146            ...
    147             RuntimeError: Vertex (24) is not a vertex of the graph.
     147            LookupError: Vertex (24) is not a vertex of the graph.
    148148            sage: S.check_vertex(-19)
    149149            Traceback (most recent call last):
    150150            ...
    151             RuntimeError: Vertex (-19) is not a vertex of the graph.
     151            LookupError: Vertex (-19) is not a vertex of the graph.
    152152
    153153        ::
    154154
     
    158158            sage: D.check_vertex(12)
    159159            Traceback (most recent call last):
    160160            ...
    161             RuntimeError: Vertex (12) is not a vertex of the graph.
     161            LookupError: Vertex (12) is not a vertex of the graph.
    162162            sage: D.check_vertex(24)
    163163            Traceback (most recent call last):
    164164            ...
    165             RuntimeError: Vertex (24) is not a vertex of the graph.
     165            LookupError: Vertex (24) is not a vertex of the graph.
    166166            sage: D.check_vertex(-19)
    167167            Traceback (most recent call last):
    168168            ...
    169             RuntimeError: Vertex (-19) is not a vertex of the graph.
     169            LookupError: Vertex (-19) is not a vertex of the graph.
    170170        """
    171171        if not self.has_vertex(n):
    172             raise RuntimeError("Vertex (%d) is not a vertex of the graph." % n)
     172            raise LookupError("Vertex ({0}) is not a vertex of the graph.".format(n))
    173173
    174174    cdef int add_vertex_unsafe(self, int k):
    175175        """
     
    244244            sage: G.add_arc(2, 5)
    245245            Traceback (most recent call last):
    246246            ...
    247             RuntimeError: Vertex (5) is not a vertex of the graph.
     247            LookupError: Vertex (5) is not a vertex of the graph.
    248248            sage: G.add_arc(1, 3)
    249249            sage: G.has_arc(1, 3)
    250250            True
     
    260260            sage: G.add_arc(2,5)
    261261            Traceback (most recent call last):
    262262            ...
    263             RuntimeError: Vertex (5) is not a vertex of the graph.
     263            LookupError: Vertex (5) is not a vertex of the graph.
    264264            sage: G.add_arc(1, 3)
    265265            sage: G.has_arc(1, 3)
    266266            True
     
    312312        """
    313313        if k >= (2 * self.active_vertices.size):
    314314            raise RuntimeError(
    315                 "Requested vertex is past twice the allocated range: " +
     315                "Requested vertex is past twice the allocated range: "
    316316                "use realloc.")
    317317        if (k >= self.active_vertices.size or
    318318            (k == -1 and self.active_vertices.size == self.num_verts)):
     
    10531053            3
    10541054        """
    10551055        if not self.has_vertex(v):
    1056             raise RuntimeError("Vertex (%d) is not a vertex of the graph." % v)
     1056            raise LookupError("Vertex ({0}) is not a vertex of the graph.".format(v))
    10571057        return self.in_degrees[v]
    10581058
    10591059    def _out_degree(self, int v):
     
    10751075            0
    10761076        """
    10771077        if not self.has_vertex(v):
    1078             raise RuntimeError("Vertex (%d) is not a vertex of the graph." % v)
     1078            raise LookupError("Vertex ({0}) is not a vertex of the graph.".format(v))
    10791079        return self.out_degrees[v]
    10801080
    10811081    def _num_verts(self):
     
    27382738        """
    27392739
    27402740        if not self._directed:
    2741             raise ValueError("The graph should be directed !")
     2741            raise ValueError("Input must be a directed graph.")
    27422742
    27432743        # Activated vertices
    27442744        cdef bitset_t activated
     
    29242924            sage: list(g.breadth_first_search(0))
    29252925            [0, 1, 4, 5, 2, 6, 3, 9, 7, 8]
    29262926
    2927         TEST:
     2927        TESTS:
    29282928
    29292929        A vertex which does not belong to the graph::
    29302930
    29312931            sage: list(g.breadth_first_search(-9))
    29322932            Traceback (most recent call last):
    29332933            ...
    2934             ValueError: The given vertex is not an element of the graph !
     2934            LookupError: Vertex (-9) is not a vertex of the graph.
    29352935
    29362936        An empty graph::
    2937            
     2937
    29382938            sage: list(Graph().breadth_first_search(''))
    29392939            Traceback (most recent call last):
    29402940            ...
    2941             ValueError: The given vertex is not an element of the graph !
     2941            LookupError: Vertex ('') is not a vertex of the graph.
    29422942        """
    29432943        self.graph = graph
    29442944        self.direction = direction
     
    29522952                                 self.graph._cg)
    29532953
    29542954        if v_id == -1:
    2955             raise ValueError("The given vertex is not an element of the graph !")
     2955            raise LookupError("Vertex ({0}) is not a vertex of the graph.".format(repr(v)))
    29562956
    29572957        self.stack = [v_id]
    29582958
  • sage/graphs/base/dense_graph.pyx

    diff --git a/sage/graphs/base/dense_graph.pyx b/sage/graphs/base/dense_graph.pyx
    a b  
    378378            sage: G.add_arc(4,7)
    379379            Traceback (most recent call last):
    380380            ...
    381             RuntimeError: Vertex (7) is not a vertex of the graph.
     381            LookupError: Vertex (7) is not a vertex of the graph.
    382382            sage: G.has_arc(1,0)
    383383            False
    384384            sage: G.has_arc(0,1)
     
    891891            sage: D.get_edge_label(2,4)
    892892            Traceback (most recent call last):
    893893            ...
    894             RuntimeError: 2, 4 not an edge of the graph.
     894            LookupError: (2, 4) is not an edge of the graph.
    895895
    896896        """
    897897        if not self.has_edge(u, v, None):
    898             raise RuntimeError("%s, %s not an edge of the graph."%(u, v))
     898            raise LookupError("({0}, {1}) is not an edge of the graph.".format(repr(u), repr(v)))
    899899        return None
    900900
    901901    def has_edge(self, object u, object v, object l):
  • sage/graphs/base/sparse_graph.pyx

    diff --git a/sage/graphs/base/sparse_graph.pyx b/sage/graphs/base/sparse_graph.pyx
    a b  
    5454    sage: S.all_arcs(7,3)
    5555    Traceback (most recent call last):
    5656    ...
    57     RuntimeError: Vertex (7) is not a vertex of the graph.
     57    LookupError: Vertex (7) is not a vertex of the graph.
    5858    sage: S._num_verts()
    5959    10
    6060    sage: S._num_arcs()
     
    6868    sage: S.add_arc_label(0,1,-1)
    6969    Traceback (most recent call last):
    7070    ...
    71     RuntimeError: Label (-1) must be a nonnegative integer.
     71    ValueError: Label (-1) must be a nonnegative integer.
    7272    sage: S.add_arc(0,1)
    7373    sage: S.arc_label(0,1)
    7474    0
     
    534534            sage: G.add_arc(4,7)
    535535            Traceback (most recent call last):
    536536            ...
    537             RuntimeError: Vertex (7) is not a vertex of the graph.
     537            LookupError: Vertex (7) is not a vertex of the graph.
    538538            sage: G.has_arc(1,0)
    539539            False
    540540            sage: G.has_arc(0,1)
     
    954954            sage: G.add_arc_label(4,7)
    955955            Traceback (most recent call last):
    956956            ...
    957             RuntimeError: Vertex (7) is not a vertex of the graph.
     957            LookupError: Vertex (7) is not a vertex of the graph.
    958958            sage: G.has_arc(1,0)
    959959            False
    960960            sage: G.has_arc(0,1)
     
    967967        self.check_vertex(u)
    968968        self.check_vertex(v)
    969969        if l < 0:
    970             raise RuntimeError("Label (%d) must be a nonnegative integer."%l)
     970            raise ValueError("Label ({0}) must be a nonnegative integer.".format(l))
    971971        self.add_arc_label_unsafe(u,v,l)
    972972
    973973    cdef int arc_label_unsafe(self, int u, int v):
     
    12061206        self.check_vertex(u)
    12071207        self.check_vertex(v)
    12081208        if l < 0:
    1209             raise RuntimeError("Label (%d) must be a nonnegative integer."%l)
     1209            raise ValueError("Label ({0}) must be a nonnegative integer.".format(l))
    12101210        self.del_arc_label_unsafe(u,v,l)
    12111211
    12121212    cdef int has_arc_label_unsafe(self, int u, int v, int l):
     
    12721272        self.check_vertex(u)
    12731273        self.check_vertex(v)
    12741274        if l < 0:
    1275             raise RuntimeError("Label (%d) must be a nonnegative integer."%l)
     1275            raise ValueError("Label ({0}) must be a nonnegative integer.".format(l))
    12761276        return self.has_arc_label_unsafe(u,v,l) == 1
    12771277
    12781278##############################
     
    17001700        """
    17011701        cdef int l_int
    17021702        if not self.has_vertex(u):
    1703             raise RuntimeError("%s not a vertex of the graph."%u)
     1703            raise LookupError("({0}) is not a vertex of the graph.".format(repr(u)))
    17041704        if not self.has_vertex(v):
    1705             raise RuntimeError("%s not a vertex of the graph."%v)
     1705            raise LookupError("({0}) is not a vertex of the graph.".format(repr(v)))
    17061706        cdef int u_int = get_vertex(u, self.vertex_ints, self.vertex_labels,
    17071707                      self._cg)
    17081708        cdef int v_int = get_vertex(v, self.vertex_ints, self.vertex_labels,
    17091709                      self._cg)
    17101710        if not (<SparseGraph>self._cg).has_arc_unsafe(u_int, v_int):
    1711             raise RuntimeError("%s, %s not an edge of the graph."%(u,v))
     1711            raise LookupError("({0}, {1}) is not an edge of the graph.".format(repr(u),repr(v)))
    17121712        if self.multiple_edges(None):
    17131713            return [self.edge_labels[l_int] if l_int != 0 else None
    17141714                     for l_int in self._cg.all_arcs(u_int, v_int)]