Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df79954

Browse files
committed
Throw std::runtime_exception instead of char*.
This should make it easier to debug exceptions falling out of the C++ code, as the C++ runtime will print the error message (the `what()` of the exception) on its way to aborting the program. Did a regex replace; removed the `char*` handler in CALL_CPP_FULL; and remove a bunch of extra whitespace.
1 parent 9986196 commit df79954

File tree

9 files changed

+86
-93
lines changed

9 files changed

+86
-93
lines changed

lib/matplotlib/tri/_tri.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ TrapezoidMapTriFinder::initialize()
14001400
unsigned int nedges = _edges.size();
14011401
for (unsigned int index = 2; index < nedges; ++index) {
14021402
if (!add_edge_to_tree(_edges[index]))
1403-
throw "Triangulation is invalid";
1403+
throw std::runtime_error("Triangulation is invalid");
14041404
_tree->assert_valid(index == nedges-1);
14051405
}
14061406
}

src/_backend_agg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ BufferRegion *RendererAgg::copy_from_bbox(agg::rect_d in_rect)
101101
void RendererAgg::restore_region(BufferRegion &region)
102102
{
103103
if (region.get_data() == NULL) {
104-
throw "Cannot restore_region from NULL data";
104+
throw std::runtime_error("Cannot restore_region from NULL data");
105105
}
106106

107107
agg::rendering_buffer rbuf;
@@ -115,7 +115,7 @@ void
115115
RendererAgg::restore_region(BufferRegion &region, int xx1, int yy1, int xx2, int yy2, int x, int y )
116116
{
117117
if (region.get_data() == NULL) {
118-
throw "Cannot restore_region from NULL data";
118+
throw std::runtime_error("Cannot restore_region from NULL data");
119119
}
120120

121121
agg::rect_i &rrect = region.get_rect();

src/_contour.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ void QuadContourGenerator::append_contour_line_to_vertices(
398398
}
399399
if (PyList_Append(vertices_list, line.pyobj_steal())) {
400400
Py_XDECREF(vertices_list);
401-
throw "Unable to add contour line to vertices_list";
401+
throw std::runtime_error("Unable to add contour line to vertices_list");
402402
}
403403

404404
contour_line.clear();
@@ -475,7 +475,7 @@ void QuadContourGenerator::append_contour_to_vertices_and_codes(
475475
Py_XDECREF(vertices_list);
476476
Py_XDECREF(codes_list);
477477
contour.delete_contour_lines();
478-
throw "Unable to add contour line to vertices and codes lists";
478+
throw std::runtime_error("Unable to add contour line to vertices and codes lists");
479479
}
480480

481481
delete *line_it;
@@ -510,7 +510,7 @@ PyObject* QuadContourGenerator::create_contour(const double& level)
510510

511511
PyObject* vertices_list = PyList_New(0);
512512
if (vertices_list == 0)
513-
throw "Failed to create Python list";
513+
throw std::runtime_error("Failed to create Python list");
514514

515515
// Lines that start and end on boundaries.
516516
long ichunk, jchunk, istart, iend, jstart, jend;
@@ -602,12 +602,12 @@ PyObject* QuadContourGenerator::create_filled_contour(const double& lower_level,
602602

603603
PyObject* vertices = PyList_New(0);
604604
if (vertices == 0)
605-
throw "Failed to create Python list";
605+
throw std::runtime_error("Failed to create Python list");
606606

607607
PyObject* codes = PyList_New(0);
608608
if (codes == 0) {
609609
Py_XDECREF(vertices);
610-
throw "Failed to create Python list";
610+
throw std::runtime_error("Failed to create Python list");
611611
}
612612

613613
long ichunk, jchunk, istart, iend, jstart, jend;
@@ -644,7 +644,7 @@ PyObject* QuadContourGenerator::create_filled_contour(const double& lower_level,
644644
if (tuple == 0) {
645645
Py_XDECREF(vertices);
646646
Py_XDECREF(codes);
647-
throw "Failed to create Python tuple";
647+
throw std::runtime_error("Failed to create Python tuple");
648648
}
649649

650650
// No error checking here as filling in a brand new pre-allocated tuple.

src/_image.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void pcolor(CoordinateArray &x,
3535
OutputArray &out)
3636
{
3737
if (rows >= 32768 || cols >= 32768) {
38-
throw "rows and cols must both be less than 32768";
38+
throw std::runtime_error("rows and cols must both be less than 32768");
3939
}
4040

4141
float x_min = bounds[0];
@@ -49,18 +49,18 @@ void pcolor(CoordinateArray &x,
4949

5050
// Check we have something to output to
5151
if (rows == 0 || cols == 0) {
52-
throw "Cannot scale to zero size";
52+
throw std::runtime_error("Cannot scale to zero size");
5353
}
5454

5555
if (d.dim(2) != 4) {
56-
throw "data must be in RGBA format";
56+
throw std::runtime_error("data must be in RGBA format");
5757
}
5858

5959
// Check dimensions match
6060
unsigned long nx = x.dim(0);
6161
unsigned long ny = y.dim(0);
6262
if (nx != (unsigned long)d.dim(1) || ny != (unsigned long)d.dim(0)) {
63-
throw "data and axis dimensions do not match";
63+
throw std::runtime_error("data and axis dimensions do not match");
6464
}
6565

6666
// Allocate memory for pointer arrays
@@ -150,22 +150,22 @@ void pcolor2(CoordinateArray &x,
150150

151151
// Check we have something to output to
152152
if (rows == 0 || cols == 0) {
153-
throw "rows or cols is zero; there are no pixels";
153+
throw std::runtime_error("rows or cols is zero; there are no pixels");
154154
}
155155

156156
if (d.dim(2) != 4) {
157-
throw "data must be in RGBA format";
157+
throw std::runtime_error("data must be in RGBA format");
158158
}
159159

160160
// Check dimensions match
161161
unsigned long nx = x.dim(0);
162162
unsigned long ny = y.dim(0);
163163
if (nx != (unsigned long)d.dim(1) + 1 || ny != (unsigned long)d.dim(0) + 1) {
164-
throw "data and axis bin boundary dimensions are incompatible";
164+
throw std::runtime_error("data and axis bin boundary dimensions are incompatible");
165165
}
166166

167167
if (bg.dim(0) != 4) {
168-
throw "bg must be in RGBA format";
168+
throw std::runtime_error("bg must be in RGBA format");
169169
}
170170

171171
std::vector<int> irows(rows);

src/_path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ void get_path_collection_extents(agg::trans_affine &master_transform,
393393
extent_limits &extent)
394394
{
395395
if (offsets.size() != 0 && offsets.dim(1) != 2) {
396-
throw "Offsets array must be Nx2";
396+
throw std::runtime_error("Offsets array must be Nx2");
397397
}
398398

399399
size_t Npaths = paths.size();
@@ -728,7 +728,7 @@ template <class VerticesArray, class ResultArray>
728728
void affine_transform_2d(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result)
729729
{
730730
if (vertices.size() != 0 && vertices.dim(1) != 2) {
731-
throw "Invalid vertices array.";
731+
throw std::runtime_error("Invalid vertices array.");
732732
}
733733

734734
size_t n = vertices.size();
@@ -758,7 +758,7 @@ template <class VerticesArray, class ResultArray>
758758
void affine_transform_1d(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result)
759759
{
760760
if (vertices.dim(0) != 2) {
761-
throw "Invalid vertices array.";
761+
throw std::runtime_error("Invalid vertices array.");
762762
}
763763

764764
double x;

src/array.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ class empty
5252

5353
T &operator()(int i, int j = 0, int k = 0)
5454
{
55-
throw "Accessed empty array";
55+
throw std::runtime_error("Accessed empty array");
5656
}
5757

5858
const T &operator()(int i, int j = 0, int k = 0) const
5959
{
60-
throw "Accessed empty array";
60+
throw std::runtime_error("Accessed empty array");
6161
}
6262

6363
sub_t operator[](int i) const

src/ft2font.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
#define NO_IMPORT_ARRAY
44

5-
#include <string>
65
#include <algorithm>
6+
#include <stdexcept>
7+
#include <string>
78

89
#include "ft2font.h"
910
#include "mplutils.h"
@@ -115,7 +116,7 @@ void FT2Image::draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y)
115116
}
116117
}
117118
} else {
118-
throw "Unknown pixel mode";
119+
throw std::runtime_error("Unknown pixel mode");
119120
}
120121

121122
m_dirty = true;
@@ -124,7 +125,7 @@ void FT2Image::draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y)
124125
void FT2Image::draw_rect(unsigned long x0, unsigned long y0, unsigned long x1, unsigned long y1)
125126
{
126127
if (x0 > m_width || x1 > m_width || y0 > m_height || y1 > m_height) {
127-
throw "Rect coords outside image bounds";
128+
throw std::runtime_error("Rect coords outside image bounds");
128129
}
129130

130131
size_t top = y0 * m_width;
@@ -170,7 +171,7 @@ int FT2Font::get_path_count()
170171
// this code is from agg's decompose_ft_outline with minor modifications
171172

172173
if (!face->glyph) {
173-
throw "No glyph loaded";
174+
throw std::runtime_error("No glyph loaded");
174175
}
175176

176177
FT_Outline &outline = face->glyph->outline;
@@ -208,7 +209,7 @@ int FT2Font::get_path_count()
208209

209210
// A contour cannot start with a cubic control point!
210211
if (tag == FT_CURVE_TAG_CUBIC) {
211-
throw "A contour cannot start with a cubic control point";
212+
throw std::runtime_error("A contour cannot start with a cubic control point");
212213
} else if (tag == FT_CURVE_TAG_CONIC) {
213214
starts_with_last = true;
214215
} else {
@@ -246,7 +247,7 @@ int FT2Font::get_path_count()
246247
}
247248

248249
if (tag != FT_CURVE_TAG_CONIC) {
249-
throw "Invalid font";
250+
throw std::runtime_error("Invalid font");
250251
}
251252

252253
count += 2;
@@ -262,7 +263,7 @@ int FT2Font::get_path_count()
262263
default: // FT_CURVE_TAG_CUBIC
263264
{
264265
if (point + 1 > limit || FT_CURVE_TAG(tags[1]) != FT_CURVE_TAG_CUBIC) {
265-
throw "Invalid font";
266+
throw std::runtime_error("Invalid font");
266267
}
267268

268269
point += 2;
@@ -492,13 +493,13 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
492493
int error = FT_Open_Face(_ft2Library, &open_args, 0, &face);
493494

494495
if (error == FT_Err_Unknown_File_Format) {
495-
throw "Can not load face. Unknown file format.";
496+
throw std::runtime_error("Can not load face. Unknown file format.");
496497
} else if (error == FT_Err_Cannot_Open_Resource) {
497-
throw "Can not load face. Can not open resource.";
498+
throw std::runtime_error("Can not load face. Can not open resource.");
498499
} else if (error == FT_Err_Invalid_File_Format) {
499-
throw "Can not load face. Invalid file format.";
500+
throw std::runtime_error("Can not load face. Invalid file format.");
500501
} else if (error) {
501-
throw "Can not load face.";
502+
throw std::runtime_error("Can not load face.");
502503
}
503504

504505
// set a default fontsize 12 pt at 72dpi
@@ -507,7 +508,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
507508
error = FT_Set_Char_Size(face, 12 * 64, 0, 72 * (unsigned int)hinting_factor, 72);
508509
if (error) {
509510
FT_Done_Face(face);
510-
throw "Could not set the fontsize";
511+
throw std::runtime_error("Could not set the fontsize");
511512
}
512513

513514
if (open_args.stream != NULL) {
@@ -551,25 +552,25 @@ void FT2Font::set_size(double ptsize, double dpi)
551552
FT_Set_Transform(face, &transform, 0);
552553

553554
if (error) {
554-
throw "Could not set the fontsize";
555+
throw std::runtime_error("Could not set the fontsize");
555556
}
556557
}
557558

558559
void FT2Font::set_charmap(int i)
559560
{
560561
if (i >= face->num_charmaps) {
561-
throw "i exceeds the available number of char maps";
562+
throw std::runtime_error("i exceeds the available number of char maps");
562563
}
563564
FT_CharMap charmap = face->charmaps[i];
564565
if (FT_Set_Charmap(face, charmap)) {
565-
throw "Could not set the charmap";
566+
throw std::runtime_error("Could not set the charmap");
566567
}
567568
}
568569

569570
void FT2Font::select_charmap(unsigned long i)
570571
{
571572
if (FT_Select_Charmap(face, (FT_Encoding)i)) {
572-
throw "Could not set the charmap";
573+
throw std::runtime_error("Could not set the charmap");
573574
}
574575
}
575576

@@ -624,7 +625,7 @@ void FT2Font::set_text(
624625
}
625626
error = FT_Load_Glyph(face, glyph_index, flags);
626627
if (error) {
627-
throw "could not load glyph";
628+
throw std::runtime_error("could not load glyph");
628629
}
629630
// ignore errors, jump to next glyph
630631

@@ -634,7 +635,7 @@ void FT2Font::set_text(
634635
error = FT_Get_Glyph(face->glyph, &thisGlyph);
635636

636637
if (error) {
637-
throw "could not get glyph";
638+
throw std::runtime_error("could not get glyph");
638639
}
639640
// ignore errors, jump to next glyph
640641

@@ -670,14 +671,14 @@ void FT2Font::load_char(long charcode, FT_Int32 flags)
670671
int error = FT_Load_Char(face, (unsigned long)charcode, flags);
671672

672673
if (error) {
673-
throw "Could not load charcode";
674+
throw std::runtime_error("Could not load charcode");
674675
}
675676

676677
FT_Glyph thisGlyph;
677678
error = FT_Get_Glyph(face->glyph, &thisGlyph);
678679

679680
if (error) {
680-
throw "Could not get glyph";
681+
throw std::runtime_error("Could not get glyph");
681682
}
682683

683684
glyphs.push_back(thisGlyph);
@@ -688,14 +689,14 @@ void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
688689
int error = FT_Load_Glyph(face, glyph_index, flags);
689690

690691
if (error) {
691-
throw "Could not load glyph";
692+
throw std::runtime_error("Could not load glyph");
692693
}
693694

694695
FT_Glyph thisGlyph;
695696
error = FT_Get_Glyph(face->glyph, &thisGlyph);
696697

697698
if (error) {
698-
throw "Could not load glyph";
699+
throw std::runtime_error("Could not load glyph");
699700
}
700701

701702
glyphs.push_back(thisGlyph);
@@ -729,7 +730,7 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
729730
error = FT_Glyph_To_Bitmap(
730731
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
731732
if (error) {
732-
throw "Could not convert glyph to bitmap";
733+
throw std::runtime_error("Could not convert glyph to bitmap");
733734
}
734735

735736
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
@@ -750,7 +751,7 @@ void FT2Font::get_xys(bool antialiased, std::vector<double> &xys)
750751
error = FT_Glyph_To_Bitmap(
751752
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
752753
if (error) {
753-
throw "Could not convert glyph to bitmap";
754+
throw std::runtime_error("Could not convert glyph to bitmap");
754755
}
755756

756757
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
@@ -773,7 +774,7 @@ void FT2Font::draw_glyph_to_bitmap(FT2Image &im, int x, int y, size_t glyphInd,
773774
sub_offset.y = 0; // int((yd - (double)y) * 64.0);
774775

775776
if (glyphInd >= glyphs.size()) {
776-
throw "glyph num is out of range";
777+
throw std::runtime_error("glyph num is out of range");
777778
}
778779

779780
error = FT_Glyph_To_Bitmap(&glyphs[glyphInd],
@@ -782,7 +783,7 @@ void FT2Font::draw_glyph_to_bitmap(FT2Image &im, int x, int y, size_t glyphInd,
782783
1 // destroy image
783784
);
784785
if (error) {
785-
throw "Could not convert glyph to bitmap";
786+
throw std::runtime_error("Could not convert glyph to bitmap");
786787
}
787788

788789
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyphInd];
@@ -798,7 +799,7 @@ void FT2Font::get_glyph_name(unsigned int glyph_number, char *buffer)
798799
PyOS_snprintf(buffer, 128, "uni%08x", glyph_number);
799800
} else {
800801
if (FT_Get_Glyph_Name(face, glyph_number, buffer, 128)) {
801-
throw "Could not get glyph names.";
802+
throw std::runtime_error("Could not get glyph names.");
802803
}
803804
}
804805
}

0 commit comments

Comments
 (0)