|
9 | 9 |
|
10 | 10 | extern "C" {
|
11 | 11 |
|
| 12 | +static int convert_string_enum(PyObject *obj, const char *name, const char **names, int *values, int *result) |
| 13 | +{ |
| 14 | + PyObject *bytesobj; |
| 15 | + char *str; |
| 16 | + |
| 17 | + if (obj == NULL || obj == Py_None) { |
| 18 | + return 1; |
| 19 | + } |
| 20 | + |
| 21 | + if (PyUnicode_Check(obj)) { |
| 22 | + bytesobj = PyUnicode_AsASCIIString(obj); |
| 23 | + if (bytesobj == NULL) { |
| 24 | + return 0; |
| 25 | + } |
| 26 | + } else if (PyBytes_Check(obj)) { |
| 27 | + Py_INCREF(obj); |
| 28 | + bytesobj = obj; |
| 29 | + } else { |
| 30 | + PyErr_Format(PyExc_TypeError, "%s must be bytes or unicode", name); |
| 31 | + return 0; |
| 32 | + } |
| 33 | + |
| 34 | + str = PyBytes_AsString(bytesobj); |
| 35 | + if (str == NULL) { |
| 36 | + Py_DECREF(bytesobj); |
| 37 | + return 0; |
| 38 | + } |
| 39 | + |
| 40 | + for ( ; *names != NULL; names++, values++) { |
| 41 | + if (strncmp(str, *names, 64) == 0) { |
| 42 | + *result = *values; |
| 43 | + Py_DECREF(bytesobj); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + PyErr_Format(PyExc_ValueError, "invalid %s value", name); |
| 49 | + Py_DECREF(bytesobj); |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
12 | 53 | int convert_from_method(PyObject *obj, const char *name, converter func, void *p)
|
13 | 54 | {
|
14 | 55 | PyObject *value;
|
@@ -76,76 +117,29 @@ int convert_bool(PyObject *obj, void *p)
|
76 | 117 |
|
77 | 118 | int convert_cap(PyObject *capobj, void *capp)
|
78 | 119 | {
|
79 |
| - PyObject *capstrobj; |
80 |
| - char *capstr; |
81 |
| - agg::line_cap_e *cap = (agg::line_cap_e *)capp; |
82 |
| - |
83 |
| - if (capobj == NULL || capobj == Py_None) { |
84 |
| - return 1; |
85 |
| - } |
86 |
| - |
87 |
| - capstrobj = PyUnicode_AsASCIIString(capobj); |
88 |
| - if (capstrobj == NULL) { |
89 |
| - return 0; |
90 |
| - } |
| 120 | + const char *names[] = {"butt", "round", "projecting", NULL}; |
| 121 | + int values[] = {agg::butt_cap, agg::round_cap, agg::square_cap}; |
| 122 | + int result; |
91 | 123 |
|
92 |
| - capstr = PyBytes_AsString(capstrobj); |
93 |
| - if (capstr == NULL) { |
94 |
| - Py_DECREF(capstrobj); |
| 124 | + if (!convert_string_enum(capobj, "capstyle", names, values, &result)) { |
95 | 125 | return 0;
|
96 | 126 | }
|
97 | 127 |
|
98 |
| - if (strncmp(capstr, "butt", 5) == 0) { |
99 |
| - *cap = agg::butt_cap; |
100 |
| - } else if (strncmp(capstr, "round", 6) == 0) { |
101 |
| - *cap = agg::round_cap; |
102 |
| - } else if (strncmp(capstr, "projecting", 11) == 0) { |
103 |
| - *cap = agg::square_cap; |
104 |
| - } else { |
105 |
| - PyErr_Format(PyExc_ValueError, "Unknown capstyle '%s'", capstr); |
106 |
| - Py_DECREF(capstrobj); |
107 |
| - return 0; |
108 |
| - } |
109 |
| - |
110 |
| - Py_DECREF(capstrobj); |
111 |
| - |
| 128 | + *(agg::line_cap_e *)capp = (agg::line_cap_e)result; |
112 | 129 | return 1;
|
113 | 130 | }
|
114 | 131 |
|
115 | 132 | int convert_join(PyObject *joinobj, void *joinp)
|
116 | 133 | {
|
117 |
| - PyObject *joinstrobj; |
118 |
| - char *joinstr; |
119 |
| - agg::line_join_e *join = (agg::line_join_e *)joinp; |
120 |
| - |
121 |
| - if (joinobj == NULL || joinobj == Py_None) { |
122 |
| - return 1; |
123 |
| - } |
124 |
| - |
125 |
| - joinstrobj = PyUnicode_AsASCIIString(joinobj); |
126 |
| - if (joinstrobj == NULL) { |
127 |
| - return 0; |
128 |
| - } |
| 134 | + const char *names[] = {"miter", "round", "bevel", NULL}; |
| 135 | + int values[] = {agg::miter_join_revert, agg::round_join, agg::bevel_join}; |
| 136 | + int result; |
129 | 137 |
|
130 |
| - joinstr = PyBytes_AsString(joinstrobj); |
131 |
| - if (joinstr == NULL) { |
132 |
| - Py_DECREF(joinstrobj); |
| 138 | + if (!convert_string_enum(joinobj, "joinstyle", names, values, &result)) { |
133 | 139 | return 0;
|
134 | 140 | }
|
135 | 141 |
|
136 |
| - if (strncmp(joinstr, "miter", 6) == 0) { |
137 |
| - *join = agg::miter_join_revert; |
138 |
| - } else if (strncmp(joinstr, "round", 6) == 0) { |
139 |
| - *join = agg::round_join; |
140 |
| - } else if (strncmp(joinstr, "bevel", 6) == 0) { |
141 |
| - *join = agg::bevel_join; |
142 |
| - } else { |
143 |
| - PyErr_Format(PyExc_ValueError, "Unknown joinstyle '%s'", joinstr); |
144 |
| - Py_DECREF(joinstrobj); |
145 |
| - return 0
E377
span>; |
146 |
| - } |
147 |
| - |
148 |
| - Py_DECREF(joinstrobj); |
| 142 | + *(agg::line_join_e *)joinp = (agg::line_join_e)result; |
149 | 143 | return 1;
|
150 | 144 | }
|
151 | 145 |
|
@@ -497,32 +491,18 @@ int convert_gcagg(PyObject *pygc, void *gcp)
|
497 | 491 | int convert_offset_position(PyObject *obj, void *offsetp)
|
498 | 492 | {
|
499 | 493 | e_offset_position *offset = (e_offset_position *)offsetp;
|
500 |
| - PyObject *offsetstrobj; |
501 |
| - char *offsetstr; |
| 494 | + const char *names[] = {"data", NULL}; |
| 495 | + int values[] = {OFFSET_POSITION_DATA}; |
| 496 | + int result; |
502 | 497 |
|
503 | 498 | *offset = OFFSET_POSITION_FIGURE;
|
504 | 499 |
|
505 |
| - if (obj == NULL || obj == Py_None) { |
506 |
| - return 1; |
507 |
| - } |
508 |
| - |
509 |
| - offsetstrobj = PyUnicode_AsASCIIString(obj); |
510 |
| - if (offsetstrobj == NULL) { |
511 |
| - return 0; |
512 |
| - } |
513 |
| - |
514 |
| - offsetstr = PyBytes_AsString(offsetstrobj); |
515 |
| - if (offsetstr == NULL) { |
516 |
| - Py_DECREF(offsetstrobj); |
517 |
| - return 0; |
518 |
| - } |
519 |
| - |
520 |
| - if (strncmp(offsetstr, "data", 5) == 0) { |
521 |
| - *offset = OFFSET_POSITION_DATA; |
| 500 | + if (convert_string_enum(obj, "offset_position", names, values, &result)) { |
| 501 | + *offset = (e_offset_position)result; |
| 502 | + } else { |
| 503 | + PyErr_Clear(); |
522 | 504 | }
|
523 | 505 |
|
524 |
| - Py_DECREF(offsetstrobj); |
525 |
| - |
526 | 506 | return 1;
|
527 | 507 | }
|
528 | 508 |
|
|
0 commit comments