Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/format.c

raw symbols SCIP
1 /**
2 * HPy string formatting helpers.
3 *
4 * Note: these functions are runtime helper functions, i.e., they are not
5 * part of the HPy context ABI, but are available to HPy extensions to
6 * incorporate at compile time.
7 *
8 * The formatting helper functions are: ``HPyUnicode_FromFormat``,
9 * ``HPyUnicode_FromFormatV``, and ``HPyErr_Format``.
10 *
11 * Supported Formatting Units
12 * --------------------------
13 *
14 * ``%%`` - The literal % character.
15 *
16 * Compatible with C (s)printf:
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 *
19 * ``%c [int]``
20 *
21 * ``%d [int]``
22 *
23 * ``%u [unsigned int]``
24 *
25 * ``%ld [long]``
26 *
27 * ``%li [long]``
28 *
29 * ``%lu [unsigned long]``
30 *
31 * ``%lld [long long]``
32 *
33 * ``%lli [long long]``
34 *
35 * ``%llu [unsigned long long]``
36 *
37 * ``%zd [HPy_ssize_t]``
38 *
39 * ``%zi [HPy_ssize_t]``
40 *
41 * ``%zu [size_t]``
42 *
43 * ``%i [int]``
44 *
45 * ``%x [int]``
46 *
47 * ``%s [const char*]``
48 *
49 * ``%p [const void*]``
50 * Guaranteed to start with the literal '0x' regardless of what the
51 * platform’s printf yields. However, there is no guarantee for
52 * zero-padding after the '0x' prefix. Some systems pad the pointer
53 * to 32 or 64 digits depending on the architecture, some do not
54 * zero pad at all. Moreover, there is no guarantee whether the letters
55 * will be capitalized or not.
56 *
57 * Python specific:
58 * ~~~~~~~~~~~~~~~~
59 *
60 * ``%A [HPy]``
61 * The result of calling ``HPy_Ascii``.
62 *
63 * ``%U [HPy]``
64 * A Unicode object.
65 *
66 * ``%V [HPy, const char*]``
67 * A Unicode object (which may be ``HPy_NULL``) and a null-terminated C character
68 * array as a second parameter (which will be used, if the first parameter
69 * is ``HPy_NULL``).
70 *
71 * ``%S [HPy]``
72 * The result of calling ``HPy_Str``.
73 *
74 * ``%R [HPy]``
75 * The result of calling ``HPy_Repr``.
76 *
77 * Additional flags:
78 * ~~~~~~~~~~~~~~~~~
79 *
80 * The format is ``%[0]{width}.{precision}{formatting-unit}``.
81 *
82 * The ``precision`` flag for numbers gives the minimal number of digits
83 * (i.e., excluding the minus sign). Shorter numbers are padded with zeros.
84 * For strings it gives the maximum number of characters, i.e., the string
85 * may be shortened if it is longer than ``precision``.
86 *
87 * The ``width`` determines how many characters should be output. If the
88 * formatting result with ``width`` flag applied is shorter, then it is
89 * padded from left with spaces. If it is longer, the result will
90 * *not* be shortened.
91 *
92 * The ``0`` flag is supported only for numeric units: if present, the
93 * number is padded to desired ``width`` with zeros instead of spaces.
94 * Unlike with spaces padding, the minus sign is shifted to the leftmost
95 * position with zero padding.
96 *
97 * The ``width`` formatter unit is number of characters rather than bytes.
98 * The precision formatter unit is number of bytes for ``%s`` and ``%V``
99 * (if the HPy argument is ``HPy_NULL``), and a number of characters for
100 * ``%A``, ``%U``, ``%S``, ``%R`` and ``%V`` (if the HPy argument is not
101 * ``HPy_NULL``).
102 *
103 * Compatibility with CPython API
104 * ------------------------------
105 *
106 * HPy is more strict in these cases:
107 *
108 * CPython API ignores width, precision, zero-padding flag for formatting
109 * units that do not support them: ``%c`` and ``%p``, but HPy raises a system
110 * error in such cases.
111 *
112 * CPython API ignores zero-padding for "string" formatting units, while HPy
113 * raises a system error in such cases.
114 *
115 * Note: users should not rely on these system errors, as HPy may choose
116 * to support some of those flags in the future.
117 */
118
119
120 /* Implementation notes:
121 *
122 * The implementation was taken from CPython, but was adapted to be to operate
123 * only with utf-8 encoding, because that is how HPy exposes Python strings
124 * (i.e., HPyUnicode_AsUTF8AndSize). CPython starts with UCS1 and widens to
125 * UCS2,3,4 depending on the largest character encountered so far. We keep
126 * everything in utf-8, or in fact in ascii as long as we do not encounter any
127 * of the Python specific formatting units such as %S or %V. The only place where
128 * we actually need to have special handling for utf-8 variable width encoding
129 * is with the width and precision flags for Python specific formatting units.
130 *
131 * ------------------------------------------------------------------
132 * CPython copyrights:
133 *
134 * Unicode implementation based on original code by Fredrik Lundh,
135 * modified by Marc-Andre Lemburg <[email protected]>.
136 *
137 * Major speed upgrades to the method implementations at the Reykjavik
138 * NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.
139 *
140 * Copyright (c) Corporation for National Research Initiatives.
141 *
142 * ---------
143 * The original string type implementation is:
144 * Copyright (c) 1999 by Secret Labs AB
145 * Copyright (c) 1999 by Fredrik Lundh
146 *
147 * ------------------------------------------------------------------
148 *
149 * Please see the git history of the following files from CPython source
150 * code for more authors:
151 *
152 * - Objects/unicodeobject.c
153 * - Modules/_testcapi/unicode.c
154 */
155
156
157
158 #include "hpy.h"
159 #include <string.h>
160 #include <ctype.h>
161 #include <stdio.h>
162
163 // Maximum code point of Unicode 6.0: 0x10ffff (1,114,111).
164 #define MAX_UNICODE 0x10ffff
165
166 /* maximum number of characters required for output of %lld or %p.
167 We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits,
168 plus 1 for the sign. 53/22 is an upper bound for log10(256). */
169 #define MAX_LONG_LONG_CHARS (2 + (sizeof(long long)*53-1) / 22)
170
171 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
172 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
173
174 #define OVERALLOCATE_FACTOR 4
175
176 typedef struct {
177 char *data_utf8;
178 HPy_ssize_t size;
179 HPy_ssize_t pos;
180 bool memory_error;
181 } StrWriter;
182
183 static void StrWriter_Init(StrWriter *writer, HPy_ssize_t init_size)
184 {
185 memset(writer, 0, sizeof(*writer));
186 writer->data_utf8 = (char*) malloc(init_size);
187 writer->size = init_size;
188 }
189
190 static bool StrWriter_EnsureSpace(StrWriter *writer, HPy_ssize_t len)
191 {
192 if (len < (writer->size - writer->pos))
193 return true;
194
195 HPy_ssize_t add = (writer->size / OVERALLOCATE_FACTOR);
196 if (len > add)
197 add = len;
198 writer->size += add;
199 if (writer->size < 0)
200 writer->size = HPY_SSIZE_T_MAX;
201 char *prev = writer->data_utf8;
202 writer->data_utf8 = (char*) realloc(writer->data_utf8, writer->size);
203 if (!writer->data_utf8) {
204 free(prev);
205 writer->memory_error = true;
206 return false;
207 }
208 return true;
209 }
210
211 static void StrWriter_WriteCharRaw(StrWriter *writer, const int c)
212 {
213 assert((writer->size - writer->pos) > 0);
214 writer->data_utf8[writer->pos++] = c;
215 }
216
217 static bool StrWriter_WriteChar(StrWriter *writer, const int c)
218 {
219 if (!StrWriter_EnsureSpace(writer, 1))
220 return false;
221 StrWriter_WriteCharRaw(writer, c);
222 return true;
223 }
224
225 static void StrWriter_WriteCharNRaw(StrWriter *writer, char c, HPy_ssize_t n)
226 {
227 assert((writer->size - writer->pos) >= n);
228 memset(writer->data_utf8 + writer->pos, c, n);
229 writer->pos += n;
230 }
231
232 static void StrWriter_WriteRaw(StrWriter *writer, const char *utf8, HPy_ssize_t len)
233 {
234 assert((writer->size - writer->pos) >= len);
235 memcpy(writer->data_utf8 + writer->pos, utf8, len);
236 writer->pos += len;
237 }
238
239 static bool StrWriter_Write(StrWriter *writer, const char *utf8, HPy_ssize_t len)
240 {
241 if (!StrWriter_EnsureSpace(writer, len))
242 return false;
243 StrWriter_WriteRaw(writer, utf8, len);
244 return true;
245 }
246
247 static bool StrWriter_WriteWithWidth(StrWriter *writer, const char *utf8, HPy_ssize_t length,
248 HPy_ssize_t width, HPy_ssize_t precision)
249 {
250 assert(utf8 != NULL);
251 if ((precision == -1 || precision >= length)
252 && width <= length) {
253 return StrWriter_Write(writer, utf8, length);
254 }
255
256 if (precision != -1)
257 length = MIN(precision, length);
258
259 HPy_ssize_t arglen = MAX(length, width);
260 if (!StrWriter_EnsureSpace(writer, arglen))
261 return false;
262
263 if (width > length) {
264 HPy_ssize_t fill = width - length;
265 StrWriter_WriteCharNRaw(writer, ' ', fill);
266 }
267
268 assert((writer->size - writer->pos) >= length);
269 memcpy(writer->data_utf8 + writer->pos, utf8, length);
270 writer->pos += length;
271 return true;
272 }
273
274 static bool StrWriter_WriteUnicode(HPyContext *ctx, StrWriter *writer, HPy h,
275 HPy_ssize_t width, HPy_ssize_t precision)
276 {
277 assert(!HPy_IsNull(h) && HPyUnicode_Check(ctx, h));
278 HPy_ssize_t u_size;
279 const char *u_str = HPyUnicode_AsUTF8AndSize(ctx, h, &u_size);
280 if (!u_str)
281 return false;
282 // We cannot use strlen(u_str), this may not be ascii only string
283 HPy_ssize_t length = HPy_Length(ctx, h);
284
285 if ((precision == -1 || precision >= length)
286 && width <= length) {
287 return StrWriter_Write(writer, u_str, u_size);
288 }
289
290 if (precision != -1)
291 length = MIN(precision, length);
292
293 HPy_ssize_t extra = length - u_size;
294 HPy_ssize_t arglen = MAX(length, width) + extra;
295 if (!StrWriter_EnsureSpace(writer, arglen))
296 return false;
297
298 if (width > length) {
299 HPy_ssize_t fill = width - length;
300 StrWriter_WriteCharNRaw(writer, ' ', fill);
301 }
302
303 if (length == u_size || precision == -1) {
304 return StrWriter_Write(writer, u_str, u_size);
305 } else {
306 // we need to take "precision" many characters from the utf-8 string:
307 HPy_ssize_t chars_count = 0;
308 while (chars_count < precision) {
309 assert(chars_count < u_size);
310 assert((writer->size - writer->pos) > 0);
311 writer->data_utf8[writer->pos++] = *u_str;
312 if ((*u_str & 0xc0) != 0x80)
313 chars_count++;
314 u_str++;
315 }
316 return true;
317 }
318 }
319
320 static bool StrWriter_DupAndWriteUnicode(HPyContext *ctx, StrWriter *writer, HPy h_unicode,
321 HPy_ssize_t width, HPy_ssize_t precision)
322 {
323 // We are dupping the handle, such that we can release the C string
324 // as soon as possible by closing the handle immediately here
325 HPy h = HPy_Dup(ctx, h_unicode);
326 bool u_result = StrWriter_WriteUnicode(ctx, writer, h, width, precision);
327 HPy_Close(ctx, h);
328 return u_result;
329 }
330
331 static bool StrWriter_WriteFunResult(HPyContext *ctx, StrWriter *writer, HPy (fun)(HPyContext*, HPy), HPy arg,
332 HPy_ssize_t width, HPy_ssize_t precision)
333 {
334 if (HPy_IsNull(arg)) {
335 HPyErr_SetString(ctx, ctx->h_SystemError,
336 "HPy_NULL passed as value for formatting unit '%S' or '%R' or '%A'");
337 return false;
338 }
339 HPy h = fun(ctx, arg);
340 if (HPy_IsNull(h))
341 return false;
342 bool u_result = StrWriter_WriteUnicode(ctx, writer, h, width, precision);
343 HPy_Close(ctx, h);
344 return u_result;
345 }
346
347 static void StrWriter_Close(StrWriter *writer)
348 {
349 free(writer->data_utf8);
350 writer->data_utf8 = NULL;
351 }
352
353 static HPy StrWriter_ToUnicode(HPyContext *ctx, StrWriter *writer)
354 {
355 if (writer->data_utf8 == NULL && !writer->memory_error) {
356 return HPy_NULL;
357 }
358 if (writer->memory_error || !StrWriter_Write(writer, "\0", 1)) {
359 HPyErr_SetString(ctx, ctx->h_MemoryError, "cannot allocate memory for string format");
360 return HPy_NULL;
361 }
362 HPy result = HPyUnicode_FromString(ctx, writer->data_utf8);
363 StrWriter_Close(writer);
364 return result;
365 }
366
367 static const char*
368 unicode_fromformat_arg(HPyContext *ctx, StrWriter *writer, const char *f, va_list *vargs)
369 {
370 HPy_ssize_t len;
371 HPy_ssize_t width;
372 HPy_ssize_t precision;
373 int longflag;
374 int longlongflag;
375 int size_tflag;
376 HPy_ssize_t fill;
377
378 f++;
379 if (*f == '%') {
380 if (!StrWriter_WriteChar(writer, '%'))
381 return NULL;
382 f++;
383 return f;
384 }
385
386 bool zeropad = false;
387 if (*f == '0') {
388 zeropad = true;
389 f++;
390 }
391
392 /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */
393 width = -1;
394 if (isdigit(*f)) {
395 width = *f - '0';
396 f++;
397 while (isdigit((unsigned)*f)) {
398 if (width > (HPy_ssize_t) (HPY_SSIZE_T_MAX - ((int)*f - '0')) / 10) {
399 HPyErr_SetString(ctx, ctx->h_ValueError,
400 "width too big");
401 return NULL;
402 }
403 width = (width * 10) + (*f - '0');
404 f++;
405 }
406 }
407 precision = -1;
408 if (*f == '.') {
409 f++;
410 if (isdigit((unsigned)*f)) {
411 precision = (*f - '0');
412 f++;
413 while (isdigit((unsigned)*f)) {
414 if (precision > (HPy_ssize_t) (HPY_SSIZE_T_MAX - ((int)*f - '0')) / 10) {
415 HPyErr_SetString(ctx, ctx->h_ValueError,
416 "precision too big");
417 return NULL;
418 }
419 precision = (precision * 10) + (*f - '0');
420 f++;
421 }
422 }
423 if (*f == '%') {
424 /* "%.3%s" => f points to "3" */
425 f--;
426 }
427 }
428 if (*f == '\0') {
429 /* bogus format "%.123" => go backward, f points to "3" */
430 f--;
431 }
432
433 /* Handle %ld, %lu, %lld and %llu. */
434 longflag = 0;
435 longlongflag = 0;
436 size_tflag = 0;
437 if (*f == 'l') {
438 if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') {
439 longflag = 1;
440 ++f;
441 }
442 else if (f[1] == 'l' &&
443 (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) {
444 longlongflag = 1;
445 f += 2;
446 }
447 }
448 /* handle the size_t flag. */
449 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) {
450 size_tflag = 1;
451 ++f;
452 }
453
454 if (zeropad) {
455 switch (*f) {
456 case 's':
457 case 'U':
458 case 'V':
459 case 'S':
460 case 'R':
461 case 'A':
462 case 'p':
463 case 'c':
464 HPyErr_Format(ctx, ctx->h_SystemError, "formatting unit '%%%c' does not support 0-padding", *f);
465 return NULL;
466 }
467 }
468
469 switch (*f) {
470 case 'c':
471 {
472 if (precision != -1 || width != -1) {
473 HPyErr_SetString(ctx, ctx->h_SystemError,
474 "formatting unit '%c' does not support width nor precision");
475 return NULL;
476 }
477 int ordinal = va_arg(*vargs, int);
478 if (ordinal < 0 || ordinal > MAX_UNICODE) {
479 HPyErr_SetString(ctx, ctx->h_OverflowError,
480 "character argument not in range(0x110000)");
481 return NULL;
482 }
483 if (!StrWriter_WriteChar(writer, ordinal))
484 return NULL;
485 break;
486 }
487
488 case 'i':
489 case 'd':
490 case 'u':
491 case 'x':
492 {
493 /* used by sprintf */
494 char buffer[MAX_LONG_LONG_CHARS];
495 // size_t arglen;
496
497 if (*f == 'u') {
498 if (longflag) {
499 len = snprintf(buffer, sizeof(buffer), "%lu", va_arg(*vargs, unsigned long));
500 }
501 else if (longlongflag) {
502 len = snprintf(buffer, sizeof(buffer), "%llu", va_arg(*vargs, unsigned long long));
503 }
504 else if (size_tflag) {
505 len = snprintf(buffer, sizeof(buffer), "%zu", va_arg(*vargs, size_t));
506 }
507 else {
508 len = snprintf(buffer, sizeof(buffer), "%u", va_arg(*vargs, unsigned int));
509 }
510 }
511 else if (*f == 'x') {
512 len = snprintf(buffer, sizeof(buffer), "%x", va_arg(*vargs, int));
513 }
514 else {
515 if (longflag) {
516 len = snprintf(buffer, sizeof(buffer), "%li", va_arg(*vargs, long));
517 }
518 else if (longlongflag) {
519 len = snprintf(buffer, sizeof(buffer), "%lli", va_arg(*vargs, long long));
520 }
521 else if (size_tflag) {
522 len = snprintf(buffer, sizeof(buffer), "%zi", va_arg(*vargs, HPy_ssize_t));
523 }
524 else {
525 len = snprintf(buffer, sizeof(buffer), "%i", va_arg(*vargs, int));
526 }
527 }
528 assert(len >= 0);
529 len = MIN((HPy_ssize_t) sizeof(buffer), len);
530
531 int negative = (buffer[0] == '-');
532 len -= negative;
533
534 precision = MAX(precision, len);
535 width = MAX(width, precision + negative);
536
537 HPy_ssize_t arglen = MAX(precision, width);
538 if (!StrWriter_EnsureSpace(writer, arglen))
539 return NULL;
540
541 if (width > precision) {
542 if (negative && zeropad) {
543 StrWriter_WriteCharRaw(writer, '-');
544 }
545
546 fill = width - precision - negative;
547 char fillchar = zeropad ? '0' : ' ';
548 StrWriter_WriteCharNRaw(writer, fillchar, fill);
549
550 if (negative && !zeropad) {
551 StrWriter_WriteCharRaw(writer, '-');
552 }
553 }
554 if (precision > len) {
555 fill = precision - len;
556 StrWriter_WriteCharNRaw(writer, '0', fill);
557 }
558
559 StrWriter_WriteRaw(writer, &buffer[negative], len);
560 break;
561 }
562
563 case 'p':
564 {
565 if (precision != -1 || width != -1) {
566 HPyErr_SetString(ctx, ctx->h_SystemError,
567 "formatting unit '%p' does not support width nor precision");
568 return NULL;
569 }
570
571 char number[MAX_LONG_LONG_CHARS];
572
573 len = snprintf(number, sizeof(number), "%p", va_arg(*vargs, void*));
574 len = MIN((HPy_ssize_t) sizeof(number), len);
575 assert(len >= 0);
576
577 /* %p is ill-defined: ensure leading 0x. */
578 if (number[1] == 'X')
579 number[1] = 'x';
580 else if (number[1] != 'x') {
581 memmove(number + 2, number,
582 strlen(number) + 1);
583 number[0] = '0';
584 number[1] = 'x';
585 len += 2;
586 }
587
588 if (!StrWriter_Write(writer, number, len))
589 return NULL;
590 break;
591 }
592
593 case 's':
594 {
595 /* UTF-8 */
596 const char *s = va_arg(*vargs, const char*);
597 if (!s) {
598 HPyErr_SetString(ctx, ctx->h_SystemError, "null c string passed as value for formatting unit '%s'");
599 return NULL;
600 }
601 if (!StrWriter_WriteWithWidth(writer, s, (HPy_ssize_t) strlen(s), width, precision))
602 return NULL;
603 break;
604 }
605
606 case 'U':
607 {
608 HPy h = va_arg(*vargs, HPy);
609 if (HPy_IsNull(h)) {
610 HPyErr_SetString(ctx, ctx->h_SystemError, "HPy_NULL passed as value for formatting unit '%U'");
611 return NULL;
612 }
613 if (!StrWriter_DupAndWriteUnicode(ctx, writer, h, width, precision))
614 return NULL;
615 break;
616 }
617
618 case 'V':
619 {
620 HPy h = va_arg(*vargs, HPy);
621 const char *str = va_arg(*vargs, const char *);
622 if (!HPy_IsNull(h)) {
623 assert(HPyUnicode_Check(ctx, h));
624 if (!StrWriter_DupAndWriteUnicode(ctx, writer, h, width, precision))
625 return NULL;
626 } else {
627 assert(str != NULL);
628 if (!StrWriter_WriteWithWidth(writer, str, (HPy_ssize_t) strlen(str), width, precision))
629 return NULL;
630 }
631 break;
632 }
633
634 case 'S':
635 {
636 if (!StrWriter_WriteFunResult(ctx, writer, HPy_Str, va_arg(*vargs, HPy), width, precision))
637 return NULL;
638 break;
639 }
640
641 case 'R':
642 {
643 if (!StrWriter_WriteFunResult(ctx, writer, HPy_Repr, va_arg(*vargs, HPy), width, precision))
644 return NULL;
645 break;
646 }
647
648 case 'A':
649 {
650 if (!StrWriter_WriteFunResult(ctx, writer, HPy_ASCII, va_arg(*vargs, HPy), width, precision))
651 return NULL;
652 break;
653 }
654
655 default:
656 HPyErr_SetString(ctx, ctx->h_SystemError, "invalid format string");
657 return NULL;
658 }
659
660 f++;
661 return f;
662 }
663
664 HPyAPI_HELPER HPy
665 HPyUnicode_FromFormatV(HPyContext *ctx, const char *format, va_list vargs)
666 {
667 va_list vargs2;
668 const char *f;
669 StrWriter writer;
670
671 StrWriter_Init(&writer, (HPy_ssize_t) (strlen(format) + 100));
672
673 // Copy varags to be able to pass a reference to a subfunction.
674 va_copy(vargs2, vargs);
675
676 for (f = format; *f; ) {
677 if (*f == '%') {
678 f = unicode_fromformat_arg(ctx, &writer, f, &vargs2);
679 if (f == NULL) {
680 StrWriter_Close(&writer);
681 goto end;
682 }
683 }
684 else {
685 const char *p;
686 HPy_ssize_t len;
687
688 p = f;
689 do
690 {
691 if ((unsigned char)*p > 127) {
692 HPyErr_Format(ctx, ctx->h_ValueError,
693 "expected an ASCII-encoded format "
694 "string, got a non-ASCII byte: 0x%02x",
695 (unsigned char)*p);
696 StrWriter_Close(&writer);
697 goto end;
698 }
699 p++;
700 }
701 while (*p != '\0' && *p != '%');
702 len = p - f;
703
704 if (!StrWriter_Write(&writer, f, len)) {
705 StrWriter_Close(&writer);
706 goto end;
707 }
708
709 f = p;
710 }
711 }
712 end:
713 va_end(vargs2);
714 return StrWriter_ToUnicode(ctx, &writer);
715 }
716
717 HPyAPI_HELPER HPy
718 HPyUnicode_FromFormat(HPyContext *ctx, const char *fmt, ...)
719 {
720 va_list vargs;
721 va_start(vargs, fmt);
722 HPy ret = HPyUnicode_FromFormatV(ctx, fmt, vargs);
723 va_end(vargs);
724 return ret;
725 }
726
727 HPyAPI_HELPER HPy
728 HPyErr_Format(HPyContext *ctx, HPy h_type, const char *fmt, ...)
729 {
730 va_list vargs;
731 va_start(vargs, fmt);
732 HPy h_str = HPyUnicode_FromFormatV(ctx, fmt, vargs);
733 va_end(vargs);
734 HPyErr_SetObject(ctx, h_type, h_str);
735 HPy_Close(ctx, h_str);
736 return HPy_NULL;
737 }