Skip to content

Commit bc7f689

Browse files
committed
Clean up pattern_as_dict
1 parent 6681652 commit bc7f689

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

kitty/fontconfig.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,31 @@ pyspacing(int val) {
2727

2828
static inline PyObject*
2929
pattern_as_dict(FcPattern *pat) {
30-
PyObject *ans = PyDict_New();
30+
PyObject *ans = PyDict_New(), *p = NULL, *list = NULL;
3131
if (ans == NULL) return NULL;
32+
3233
#define PS(x) PyUnicode_Decode((const char*)x, strlen((const char*)x), "UTF-8", "replace")
34+
3335
#define G(type, get, which, conv, name) { \
34-
type out; PyObject *p; \
36+
type out; \
3537
if (get(pat, which, 0, &out) == FcResultMatch) { \
36-
p = conv(out); if (p == NULL) { Py_CLEAR(ans); return NULL; } \
37-
if (PyDict_SetItemString(ans, #name, p) != 0) { Py_CLEAR(p); Py_CLEAR(ans); return NULL; } \
38+
p = conv(out); if (p == NULL) goto exit; \
39+
if (PyDict_SetItemString(ans, #name, p) != 0) goto exit; \
3840
Py_CLEAR(p); \
3941
}}
4042

4143
#define L(type, get, which, conv, name) { \
42-
type out; PyObject *p; int n = 0; \
43-
PyObject *l = PyList_New(0); \
44+
type out; int n = 0; \
45+
list = PyList_New(0); \
46+
if (!list) goto exit; \
4447
while (get(pat, which, n, &out) == FcResultMatch) { \
45-
p = conv(out); if (p == NULL) { Py_CLEAR(l); Py_CLEAR(ans); return NULL; } \
46-
if (PyList_Append(l, p) != 0) { Py_CLEAR(p); Py_CLEAR(l); Py_CLEAR(ans); return NULL; } \
48+
p = conv(out); if (p == NULL) goto exit; \
49+
if (PyList_Append(list, p) != 0) goto exit; \
4750
Py_CLEAR(p); \
4851
n++; \
4952
} \
50-
if (PyDict_SetItemString(ans, #name, l) != 0) { Py_CLEAR(l); Py_CLEAR(ans); return NULL; } \
51-
Py_CLEAR(l); \
53+
if (PyDict_SetItemString(ans, #name, list) != 0) goto exit; \
54+
Py_CLEAR(list); \
5255
}
5356
#define S(which, key) G(FcChar8*, FcPatternGetString, which, PS, key)
5457
#define LS(which, key) L(FcChar8*, FcPatternGetString, which, PS, key)
@@ -73,6 +76,10 @@ pattern_as_dict(FcPattern *pat) {
7376
B(FC_OUTLINE, outline);
7477
B(FC_COLOR, color);
7578
E(FC_SPACING, spacing, pyspacing);
79+
exit:
80+
if (PyErr_Occurred()) Py_CLEAR(ans);
81+
Py_CLEAR(p);
82+
Py_CLEAR(list);
7683

7784
return ans;
7885
#undef PS
@@ -81,6 +88,8 @@ pattern_as_dict(FcPattern *pat) {
8188
#undef B
8289
#undef E
8390
#undef G
91+
#undef L
92+
#undef LS
8493
}
8594

8695
static inline PyObject*

0 commit comments

Comments
 (0)