-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickyaml.c
1015 lines (921 loc) · 32.7 KB
/
quickyaml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* YAML dumper for NICOS, the Networked Instrument Control System of the FRM-II
* Copyright (c) 2016-2023 by the NICOS contributors (see AUTHORS)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module authors:
* Georg Brandl <[email protected]>
*
******************************************************************************/
#include <Python.h>
#include <structmember.h>
#include <numpy/arrayobject.h>
#define BYTES_FMT "y"
#define TO_UNICODE PyObject_Str
#define TO_INT PyNumber_Long
#define bool int
#define TRUE 1
#define FALSE 0
#define ARRAY_AS_SEQ 0
#define ARRAY_AS_MAP 1
#define UNICODE_LS 0x2028
#define UNICODE_PS 0x2029
#define UNICODE_NONCHAR_RESERVED 0xfffe
#define CHR_DELETE 0x7f
#define CHR_APP_PROGRAM_CMD 0x9f
/******************************************************************************/
/* flowlist object */
/* This is an empty list subclass, the equivalent of
class flowlist(list):
pass
It is used to signal to the dumper to use flow representation
for the list, as opposed to block representation.
*/
static PyTypeObject flowlist_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"quickyaml.flowlist",
sizeof(PyListObject),
0,
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0 /* tp_free */
};
/******************************************************************************/
/* Helpers */
static char *HEX = "0123456789abcdef";
/* Determine if the given Unicode string needs quoting and escaping when
represented as a YAML scalar.
There are several factors that determine this:
* Empty strings must always be quoted
* Control characters must always be escaped
* Some characters are unsafe anywhere in a bare string
(e.g. the comma, as it separates elements in flow style)
* Some characters are unsafe at the beginning of a string
(e.g. the bang, which introduces tags, or digits, which
introduce numbers)
* Some characters are unsafe at the end (whitespace)
* Some strings indicate other data types (booleans or nulls)
In general, this function errs on the safe side and might quote
a few too many strings, which only slightly influences the
human readability and does not impact loading.
*/
static bool
needs_quoting(PyObject *string, Py_ssize_t len)
{
Py_ssize_t i;
Py_UCS4 buf[5];
if (len == 0)
return TRUE;
for (i = 0; i < len; i++) {
Py_UCS4 ch = PyUnicode_ReadChar(string, i);
/* Characters disallowed everywhere: */
if (ch < 32 ||
ch == '"' || ch == '\\' || ch == ',' || ch == '?' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}' ||
ch == ':' || ch == '#' || ch == UNICODE_LS || ch == UNICODE_PS ||
(ch >= CHR_DELETE && ch <= CHR_APP_PROGRAM_CMD) ||
ch >= UNICODE_NONCHAR_RESERVED)
return TRUE;
/* Characters disallowed at the beginning: */
if (i == 0 && (ch == '&' || ch == '*' || ch == '!' ||
ch == '|' || ch == '>' || ch == '\'' ||
ch == '%' || ch == '@' || ch == '`' ||
ch == '-' || ch == '+' || ch == ' ' ||
ch == '=' || ch == '~' ||
(ch >= '0' && ch <= '9')))
return TRUE;
/* Characters disallowed at the end: */
if (i == len - 1 && ch == ' ')
return TRUE;
}
/* Words that would be null or booleans: */
if (len == 2) {
PyUnicode_AsUCS4(string, buf, 5, FALSE);
if (tolower(buf[0]) == 'o' &&
tolower(buf[1]) == 'n')
return TRUE;
if (tolower(buf[0]) == 'n' &&
tolower(buf[1]) == 'o')
return TRUE;
} else if (len == 3) {
PyUnicode_AsUCS4(string, buf, 5, FALSE);
if (tolower(buf[0]) == 'y' &&
tolower(buf[1]) == 'e' &&
tolower(buf[2]) == 's')
return TRUE;
if (tolower(buf[0]) == 'o' &&
tolower(buf[1]) == 'f' &&
tolower(buf[2]) == 'f')
return TRUE;
} else if (len == 4) {
PyUnicode_AsUCS4(string, buf, 5, FALSE);
if (tolower(buf[0]) == 't' &&
tolower(buf[1]) == 'r' &&
tolower(buf[2]) == 'u' &&
tolower(buf[3]) == 'e')
return TRUE;
if (tolower(buf[0]) == 'n' &&
tolower(buf[1]) == 'u' &&
tolower(buf[2]) == 'l' &&
tolower(buf[3]) == 'l')
return TRUE;
} else if (len == 5) {
PyUnicode_AsUCS4(string, buf, 5, FALSE);
if (tolower(buf[0]) == 'f' &&
tolower(buf[1]) == 'a' &&
tolower(buf[2]) == 'l' &&
tolower(buf[3]) == 's' &&
tolower(buf[4]) == 'e')
return TRUE;
}
return FALSE;
}
/* Quote and escape the given string (given by content and length) into a new
string object.
*/
static PyObject *
quote_string(PyObject *string, Py_ssize_t len)
{
Py_ssize_t i = 0;
Py_ssize_t size = 2 * len + 3;
Py_UCS4 *dst, *buf_end;
Py_UCS4 *buffer = PyMem_Malloc(sizeof(Py_UCS4) * size);
if (!buffer)
return NULL;
buf_end = buffer + size;
dst = buffer;
*dst++ = '"';
for (i = 0; i < len; i++) {
if (buf_end - dst < 11) {
/* Need to reallocate. */
Py_UCS4 *new_buffer;
Py_ssize_t offset = dst - buffer;
size *= 2;
new_buffer = PyMem_Realloc(buffer, sizeof(Py_UCS4) * size);
if (!new_buffer) {
PyMem_Free(buffer);
return NULL;
}
buffer = new_buffer;
buf_end = buffer + size;
dst = buffer + offset;
}
Py_UCS4 ch = PyUnicode_ReadChar(string, i);
if (ch == '\\') {
*dst++ = '\\';
*dst++ = '\\';
} else if (ch == '"') {
*dst++ = '\\';
*dst++ = '"';
} else if (ch == '\n') {
*dst++ = '\\';
*dst++ = 'n';
} else if (ch == '\t') {
*dst++ = '\\';
*dst++ = 't';
} else if (ch < 32 || (ch >= CHR_DELETE && ch <= CHR_APP_PROGRAM_CMD)) {
*dst++ = '\\';
*dst++ = 'x';
*dst++ = HEX[(ch & 0xf0) >> 4];
*dst++ = HEX[ch & 0x0f];
} else if (ch == UNICODE_LS || ch == UNICODE_PS ||
ch >= UNICODE_NONCHAR_RESERVED) {
*dst++ = '\\';
*dst++ = 'U';
*dst++ = HEX[(ch & 0xf0000000) >> 28];
*dst++ = HEX[(ch & 0x0f000000) >> 24];
*dst++ = HEX[(ch & 0x00f00000) >> 20];
*dst++ = HEX[(ch & 0x000f0000) >> 16];
*dst++ = HEX[(ch & 0x0000f000) >> 12];
*dst++ = HEX[(ch & 0x00000f00) >> 8];
*dst++ = HEX[(ch & 0x000000f0) >> 4];
*dst++ = HEX[ch & 0x0000000f];
} else {
*dst++ = ch;
}
}
*dst++ = '"';
return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, dst - buffer);
}
/* Write a single literal string to the output. */
static bool
write_literal(PyObject *write, const char *str)
{
PyObject *result = PyObject_CallFunction(write, BYTES_FMT, str);
Py_XDECREF(result);
return !!result;
}
/* Write a single bytestring object to the output. */
static bool
write_object(PyObject *write, PyObject *str)
{
PyObject *result = PyObject_CallFunctionObjArgs(write, str, NULL);
Py_XDECREF(result);
return !!result;
}
/* Make a string with n spaces for indentation. */
static void
make_indent(char *buffer, int n)
{
int j;
for (j = 0; j < n; ++j)
*buffer++ = ' ';
*buffer = '\0';
}
/******************************************************************************/
/* Dumper functions */
/* This stores local settings during the dumping process. */
typedef struct {
int array_handling;
int indentwidth;
int maxwidth;
int curindent;
int curcol;
PyObject *callback;
} dumpdata;
static bool dispatch_dump(PyObject *, PyObject *, dumpdata *, bool, bool);
/* Here we define functions for dumping each supported type. */
static bool
dump_none(PyObject *write, dumpdata *data)
{
data->curcol += 4;
return write_literal(write, "null");
}
static bool
dump_bool(PyObject *obj, PyObject *write, dumpdata *data)
{
data->curcol += obj == Py_True ? 4 : 5;
return write_literal(write, obj == Py_True ? "true" : "false");
}
static bool
dump_int(PyObject *obj, PyObject *write, dumpdata *data)
{
bool success = FALSE;
/* Easy: format the integer as a unicode string, and write it out. */
PyObject *converted = TO_UNICODE(obj);
PyObject *encoded = NULL;
if (!converted)
return FALSE;
if (!(encoded = PyUnicode_AsUTF8String(converted)))
goto exit;
success = write_object(write, encoded);
data->curcol += PyBytes_Size(encoded);
exit:
Py_XDECREF(encoded);
Py_DECREF(converted);
return success;
}
static bool
dump_float(PyObject *obj, PyObject *write, dumpdata *data)
{
bool success = FALSE;
PyObject *converted = NULL;
PyObject *encoded = NULL;
/* Not as easy: special floats are represented slightly differently in YAML,
so catch them beforehand. */
double val = PyFloat_AsDouble(obj);
if (Py_IS_NAN(val)) {
data->curcol += 4;
return write_literal(write, ".nan");
} else if (Py_IS_INFINITY(val)) {
bool pos = copysign(1., val) == 1.;
data->curcol += pos ? 4 : 5;
return write_literal(write, pos ? ".inf": "-.inf");
}
if (!(converted = TO_UNICODE(obj)))
return FALSE;
if (!(encoded = PyUnicode_AsUTF8String(converted)))
goto exit;
success = write_object(write, encoded);
data->curcol += PyBytes_Size(encoded);
exit:
Py_XDECREF(encoded);
Py_DECREF(converted);
return success;
}
static bool
dump_unicode(PyObject *obj, PyObject *write, dumpdata *data)
{
bool success = FALSE;
Py_ssize_t len;
PyObject *encoded;
len = PyUnicode_GetLength(obj);
/* Let's see if we need to quote and escape the string. */
if (needs_quoting(obj, len)) {
PyObject *buffer = quote_string(obj, len);
if (!buffer)
return FALSE;
encoded = PyUnicode_AsUTF8String(buffer);
data->curcol += PyUnicode_GetLength(buffer);
Py_DECREF(buffer);
} else {
encoded = PyUnicode_AsUTF8String(obj);
data->curcol += len;
}
success = write_object(write, encoded);
Py_DECREF(encoded);
return success;
}
static bool
dump_bytes(PyObject *obj, PyObject *write, dumpdata *data)
{
bool success = FALSE;
char *content;
Py_ssize_t len;
PyObject *decoded;
PyBytes_AsStringAndSize(obj, &content, &len);
/* Since YAML deals with Unicode strings, require UTF-8 here.
But we use the "replace" error handler to be lenient. */
if (!(decoded = PyUnicode_DecodeUTF8(content, len, "replace")))
return FALSE;
success = dump_unicode(decoded, write, data);
Py_DECREF(decoded);
return success;
}
/* Dump sequence in block representation. */
static bool
dump_seq(PyObject *obj, PyObject *write, dumpdata *data, bool map_value)
{
bool success = FALSE;
char buffer[255];
Py_ssize_t len, i;
/* Convert to "fast sequence" and determine the length. */
PyObject *seq = PySequence_Fast(obj, "");
if (!seq)
return FALSE;
len = PySequence_Fast_GET_SIZE(seq);
if (len == 0) {
/* Empty list is always inline, and written as []. */
data->curcol += 2;
Py_DECREF(seq);
return write_literal(write, "[]");
}
if (map_value) {
/* In map values, we need to start on a new line. */
if (!write_literal(write, "\n"))
goto exit;
data->curcol = 0;
}
for (i = 0; i < len; ++i) {
PyObject *element = PySequence_Fast_GET_ITEM(seq, i);
/* Create string with leading indentation and the bullet point. */
if (!map_value && i == 0) {
/* In the first item, we don't need to add indentation. */
buffer[0] = '-';
make_indent(&buffer[1], data->indentwidth - 1);
} else if (i <= 1) {
/* In subsequent lines, we add indentation and bullet point. */
make_indent(buffer, data->curindent + data->indentwidth);
buffer[data->curindent] = '-';
}
/* Write it out. */
if (!write_literal(write, buffer))
goto exit;
data->curcol += strlen(buffer);
data->curindent += data->indentwidth;
/* Write the item. */
if (!dispatch_dump(element, write, data, FALSE, FALSE))
goto exit;
data->curindent -= data->indentwidth;
/* If the item itself didn't start a new line, do it here. */
if (data->curcol != 0) {
if (!write_literal(write, "\n"))
goto exit;
data->curcol = 0;
}
}
success = TRUE;
exit:
Py_DECREF(seq);
return success;
}
/* Dump sequence in flowing representation. */
static bool
dump_flowseq(PyObject *obj, PyObject *write, dumpdata *data)
{
char buffer[255] = "";
bool success = FALSE;
Py_ssize_t len, i;
int indent_to;
/* Convert to "fast sequence" and determine the length. */
PyObject *seq = PySequence_Fast(obj, "");
if (!seq)
return FALSE;
len = PySequence_Fast_GET_SIZE(seq);
/* Write the opening bracket. */
if (!write_literal(write, "["))
goto exit;
data->curcol += 1;
/* This is what we indent to after a linebreak. */
indent_to = data->curcol;
for (i = 0; i < len; ++i) {
PyObject *element = PySequence_Fast_GET_ITEM(seq, i);
/* Write the item. */
if (!dispatch_dump(element, write, data, TRUE, FALSE))
goto exit;
/* Break line if over max width. */
if (i < len - 1 && data->curcol > data->maxwidth - 1) {
if (buffer[0] == '\0') {
buffer[0] = ',';
buffer[1] = '\n';
make_indent(&buffer[2], indent_to);
}
if (!write_literal(write, buffer))
goto exit;
data->curcol = indent_to;
} else {
/* Write only the separator. */
if (i < len - 1) {
if (!write_literal(write, ", "))
goto exit;
data->curcol += 2;
}
}
}
/* Write the closing bracket. */
if (!write_literal(write, "]"))
goto exit;
data->curcol += 1;
success = TRUE;
exit:
Py_DECREF(seq);
return success;
}
static bool
dump_dict(PyObject *obj, PyObject *write, dumpdata *data, bool map_value)
{
char buffer[255];
bool success = FALSE;
PyObject *items, *item = NULL, *iter = NULL;
Py_ssize_t i = 0;
if (PyDict_Size(obj) == 0) {
/* Empty dict is always inline {}. */
data->curcol += 2;
return write_literal(write, "{}");
}
/* Get the iterator over items. */
if (!(items = PyObject_CallMethod(obj, "items", "")))
return FALSE;
if (!(iter = PyObject_GetIter(items)))
goto exit;
if (map_value) {
/* In map values, we need to start on a new line. */
if (!write_literal(write, "\n"))
goto exit;
data->curcol = 0;
}
while ((item = PyIter_Next(iter))) {
assert(PyTuple_Check(item));
assert(PyTuple_GET_SIZE(item) == 2);
PyObject *key = PyTuple_GET_ITEM(item, 0);
PyObject *value = PyTuple_GET_ITEM(item, 1);
bool value_is_list = (Py_TYPE(value) == &PyList_Type);
/* Create string with leading indentation. */
if (!map_value && i == 0) {
/* In the first item, we don't need to add indentation. */
buffer[0] = '\0';
} else if (i <= 1) {
/* In subsequent lines, we add indentation. */
make_indent(buffer, data->curindent);
}
/* Write it out. */
if (!write_literal(write, buffer))
goto exit;
data->curcol += strlen(buffer);
/* Write the key. */
if (!dispatch_dump(key, write, data, TRUE, FALSE))
goto exit;
/* Write the separator. We try to be smart and not emit a trailing
space after the colon if we know the value will start on a new line. */
if ((value_is_list && PyList_GET_SIZE(value) > 0) ||
(PyObject_IsInstance(value, (PyObject *)&PyDict_Type) && PyDict_Size(value) > 0) ||
(PyArray_Check(value) && PyArray_NDIM(value) > 1))
{
if (!write_literal(write, ":"))
goto exit;
data->curcol += 1;
} else {
if (!write_literal(write, ": "))
goto exit;
data->curcol += 2;
}
/* Special case: no additional indent for lists inside dicts. */
if (!value_is_list)
data->curindent += data->indentwidth;
/* Write the value. */
if (!dispatch_dump(value, write, data, FALSE, TRUE))
goto exit;
if (!value_is_list)
data->curindent -= data->indentwidth;
/* If the value itself didn't start a new line, do it here. */
if (data->curcol != 0) {
if (!write_literal(write, "\n"))
goto exit;
data->curcol = 0;
}
Py_DECREF(item);
i += 1;
}
item = NULL;
/* If PyIter_Next returns NULL, an exception may be set on error. */
if (!PyErr_Occurred())
success = TRUE;
exit:
Py_XDECREF(item);
Py_XDECREF(iter);
Py_DECREF(items);
return success;
}
/* Dump the innermost slice of a ndarray.
The most efficient way to do this is to create a tuple of primitive types
and use the normal flowseq code.
*/
static bool
dump_numpy_inner(PyArrayObject *obj, PyObject *write, dumpdata *data, npy_intp len,
PyObject *(*converter)(PyObject *))
{
npy_intp i;
bool success = FALSE;
PyObject *result = PyTuple_New(len);
if (!result)
return FALSE;
for (i = 0; i < len; ++i) {
PyObject *converted;
PyObject *item = PySequence_GetItem((PyObject *)obj, i);
if (!item) {
Py_DECREF(result);
return FALSE;
}
if (!(converted = converter(item))) {
Py_DECREF(item);
Py_DECREF(result);
return FALSE;
}
Py_DECREF(item);
PyTuple_SET_ITEM(result, i, converted);
}
success = dump_flowseq(result, write, data);
Py_DECREF(result);
return success;
}
/* Dump numpy array directly: we dump every dimension but the last as a
mapping with indices as keys, and the last dimension as a flow sequence.
Only numeric types are supported.
*/
static bool
dump_numpy_array(PyArrayObject *obj, PyObject *write, dumpdata *data, bool map_value)
{
char buffer[255];
char index[32];
npy_intp i;
int ndim = PyArray_NDIM(obj);
npy_intp len = PyArray_DIM(obj, 0);
if (ndim == 1) {
if (PyArray_ISINTEGER(obj)) {
return dump_numpy_inner(obj, write, data, len, TO_INT);
} else if (PyArray_ISFLOAT(obj)) {
return dump_numpy_inner(obj, write, data, len, PyNumber_Float);
} else {
PyErr_SetString(PyExc_ValueError, "only integer and float arrays "
"can be dumped");
return FALSE;
}
}
if (map_value) {
/* In map values, we need to start on a new line. */
if (!write_literal(write, "\n"))
return FALSE;
data->curcol = 0;
}
for (i = 0; i < len; ++i) {
PyObject *slice = PySequence_GetItem((PyObject *)obj, i);
if (!slice)
return FALSE;
/* Create string with leading indentation. */
if (data->array_handling == ARRAY_AS_SEQ) {
if (!map_value && i == 0) {
/* In the first item, we don't need to add indentation. */
buffer[0] = '-';
make_indent(&buffer[1], data->indentwidth - 1);
} else if (i <= 1) {
/* In subsequent lines, we add indentation and bullet point. */
make_indent(buffer, data->curindent + data->indentwidth);
buffer[data->curindent] = '-';
}
} else {
if (!map_value && i == 0) {
/* In the first item, we don't need to add indentation. */
buffer[0] = '\0';
} else if (i <= 1) {
/* In subsequent lines, we add indentation. */
make_indent(buffer, data->curindent);
}
}
/* Write it out. */
if (!write_literal(write, buffer)) {
Py_DECREF(slice);
return FALSE;
}
data->curcol += strlen(buffer);
if (data->array_handling == ARRAY_AS_MAP) {
/* Write the index. */
if (ndim == 2)
snprintf(index, 31, "%" NPY_INTP_FMT ": ", i);
else
snprintf(index, 31, "%" NPY_INTP_FMT ":", i);
if (!write_literal(write, index)) {
Py_DECREF(slice);
return FALSE;
}
data->curcol += strlen(index);
}
data->curindent += data->indentwidth;
/* Write the value. */
if (!dump_numpy_array((PyArrayObject *)slice, write, data,
data->array_handling == ARRAY_AS_MAP)) {
Py_DECREF(slice);
return FALSE;
}
Py_DECREF(slice);
data->curindent -= data->indentwidth;
/* If the value itself didn't start a new line, do it here. */
if (data->curcol != 0) {
if (!write_literal(write, "\n"))
return FALSE;
data->curcol = 0;
}
}
return TRUE;
}
/* Main dispatcher for dumping. */
static bool
dispatch_dump(PyObject *obj, PyObject *write, dumpdata *data, bool inline_only, bool map_value)
{
PyTypeObject *type;
bool success = FALSE;
/* Switch on the exact type for primitive types. */
type = Py_TYPE(obj);
if (obj == Py_None) {
success = dump_none(write, data);
} else if (obj == Py_False || obj == Py_True) {
success = dump_bool(obj, write, data);
} else if (type == &PyLong_Type) {
success = dump_int(obj, write, data);
} else if (type == &PyFloat_Type) {
success = dump_float(obj, write, data);
} else if (type == &PyUnicode_Type) {
success = dump_unicode(obj, write, data);
} else if (type == &PyBytes_Type) {
success = dump_bytes(obj, write, data);
} else if (type == &flowlist_type) {
success = dump_flowseq(obj, write, data);
} else if (PyObject_IsInstance(obj, (PyObject *)&PyList_Type)) {
/* For lists we allow subtypes to catch e.g. readonlylist. */
if (inline_only)
goto disallow;
success = dump_seq(obj, write, data, map_value);
} else if (PyObject_IsInstance(obj, (PyObject *)&PyTuple_Type)) {
if (inline_only)
goto disallow;
success = dump_seq(obj, write, data, map_value);
} else if (PyObject_IsInstance(obj, (PyObject *)&PyDict_Type)) {
/* For dictionaries we allow subtypes to catch OrderedDicts. */
if (inline_only)
goto disallow;
success = dump_dict(obj, write, data, map_value);
} else if (PyArray_Check(obj)) {
success = dump_numpy_array((PyArrayObject *)obj, write, data, map_value);
} else if (data->callback) {
/* If we have a callback, try to call it. */
PyObject *dumped = PyObject_CallFunctionObjArgs(data->callback, obj, NULL);
if (dumped) {
/* It needs to return a bytestring. */
if (PyBytes_Check(dumped)) {
success = write_object(write, dumped);
} else {
PyErr_Format(PyExc_ValueError, "callback must return bytes, not %s",
Py_TYPE(dumped)->tp_name);
}
Py_DECREF(dumped);
}
} else {
PyErr_Format(PyExc_ValueError, "cannot dump type %s", type->tp_name);
}
return success;
disallow:
PyErr_Format(PyExc_ValueError, "type %s not allowed as mapping key or "
"flow sequence element", type->tp_name);
return FALSE;
}
/******************************************************************************/
/* Dumper object */
typedef struct {
PyObject_HEAD
int array_handling;
int indentwidth;
int maxwidth;
PyObject *callback;
} DumperObject;
/* Constructor, handles converting arguments to instance members. */
static int
dumper_init(DumperObject *self, PyObject *args, PyObject *kwds)
{
int indentwidth = 4;
int maxwidth = 120;
int array_handling = ARRAY_AS_SEQ;
static char *kwlist[] = {"array_handling", "indent", "width", "callback", 0};
self->callback = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|iiO:Dumper", kwlist,
&array_handling, &indentwidth, &maxwidth,
&self->callback))
return -1;
Py_XINCREF(self->callback);
if (indentwidth < 2 || indentwidth > 8) {
PyErr_SetString(PyExc_ValueError, "indent must be between 2 and 8");
return -1;
}
if (maxwidth <= indentwidth) {
PyErr_SetString(PyExc_ValueError, "width must be >= indent");
return -1;
}
self->indentwidth = indentwidth;
self->maxwidth = maxwidth;
self->array_handling = array_handling;
return 0;
}
/* Destructor, needs to decrement contained references. */
static void
dumper_dealloc(DumperObject *self)
{
Py_CLEAR(self->callback);
}
static PyObject *
dumper_dump(DumperObject *self, PyObject *args)
{
PyObject *obj, *stream, *write;
dumpdata data;
data.array_handling = self->array_handling;
data.indentwidth = self->indentwidth;
data.maxwidth = self->maxwidth;
data.callback = self->callback;
data.curindent = 0;
data.curcol = 0;
/* Parse arguments. */
if (!PyArg_ParseTuple(args, "OO:dump", &obj, &stream))
return NULL;
if (!(write = PyObject_GetAttrString(stream, "write")))
return NULL;
/* Dump the object. */
if (!dispatch_dump(obj, write, &data, FALSE, FALSE)) {
Py_DECREF(write);
return NULL;
}
/* Add a newline if necessary. */
if (data.curcol != 0)
if (!write_literal(write, "\n")) {
Py_DECREF(write);
return NULL;
}
/* If all went ok, return None. */
Py_DECREF(write);
Py_RETURN_NONE;
}
static PyMethodDef dumper_methods[] = {
{"dump", (PyCFunction)dumper_dump, METH_VARARGS, NULL},
{NULL}
};
static PyMemberDef dumper_members[] = {
{NULL}
};
static PyTypeObject DumperType = {
PyVarObject_HEAD_INIT(NULL, 0)
"quickyaml.Dumper", /* tp_name */
sizeof(DumperObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)dumper_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
dumper_methods, /* tp_methods */
dumper_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)dumper_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
0 /* tp_free */
};
/******************************************************************************/
/* Module definitions */
static PyMethodDef functions[] = {
{NULL}
};
static bool
init_inner(PyObject *module)
{
flowlist_type.tp_base = &PyList_Type;
if (PyType_Ready(&flowlist_type) < 0)
return FALSE;
if (PyType_Ready(&DumperType) < 0)
return FALSE;
Py_INCREF((PyObject *)&flowlist_type);
PyModule_AddObject(module, "flowlist", (PyObject *)&flowlist_type);
Py_INCREF((PyObject *)&DumperType);
PyModule_AddObject(module, "Dumper", (PyObject *)&DumperType);
PyModule_AddObject(module, "ARRAY_AS_SEQ", PyLong_FromLong(ARRAY_AS_SEQ));
PyModule_AddObject(module, "ARRAY_AS_MAP", PyLong_FromLong(ARRAY_AS_MAP));
return TRUE;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"quickyaml",
0, /* m_doc */
-1, /* m_size */
functions, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL /* m_free */
};