Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 8314:f94d5ae642a9 legacy-trunk
New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Adapted from code submitted by Just van Rossum.
PySys_WriteStdout(format, ...)
PySys_WriteStderr(format, ...)
The first function writes to sys.stdout; the second to sys.stderr. When
there is a problem, they write to the real (C level) stdout or stderr;
no exceptions are raised (but a pending exception may be cleared when a
new exception is caught).
Both take a printf-style format string as their first argument followed
by a variable length argument list determined by the format string.
*** WARNING ***
The format should limit the total size of the formatted output string to
1000 bytes. In particular, this means that no unrestricted "%s" formats
should occur; these should be limited using "%.<N>s where <N> is a
decimal number calculated so that <N> plus the maximum size of other
formatted text does not exceed 1000 bytes. Also watch out for "%f",
which can print hundreds of digits for very large numbers.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 12 May 1998 14:59:24 +0000 |
| parents | c7454ca7d3ac |
| children | 6e8b0e754a7f |
comparison
equal
deleted
inserted
replaced
| 8313:c62360aa5ea4 | 8314:f94d5ae642a9 |
|---|---|
| 484 Py_FatalError("sys.path.insert(0) failed"); | 484 Py_FatalError("sys.path.insert(0) failed"); |
| 485 Py_DECREF(a); | 485 Py_DECREF(a); |
| 486 } | 486 } |
| 487 Py_DECREF(av); | 487 Py_DECREF(av); |
| 488 } | 488 } |
| 489 | |
| 490 | |
| 491 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. | |
| 492 Adapted from code submitted by Just van Rossum. | |
| 493 | |
| 494 PySys_WriteStdout(format, ...) | |
| 495 PySys_WriteStderr(format, ...) | |
| 496 | |
| 497 The first function writes to sys.stdout; the second to sys.stderr. When | |
| 498 there is a problem, they write to the real (C level) stdout or stderr; | |
| 499 no exceptions are raised (but a pending exception may be cleared when a | |
| 500 new exception is caught). | |
| 501 | |
| 502 Both take a printf-style format string as their first argument followed | |
| 503 by a variable length argument list determined by the format string. | |
| 504 | |
| 505 *** WARNING *** | |
| 506 | |
| 507 The format should limit the total size of the formatted output string to | |
| 508 1000 bytes. In particular, this means that no unrestricted "%s" formats | |
| 509 should occur; these should be limited using "%.<N>s where <N> is a | |
| 510 decimal number calculated so that <N> plus the maximum size of other | |
| 511 formatted text does not exceed 1000 bytes. Also watch out for "%f", | |
| 512 which can print hundreds of digits for very large numbers. | |
| 513 | |
| 514 */ | |
| 515 | |
| 516 static void | |
| 517 mywrite(name, fp, format, va) | |
| 518 char *name; | |
| 519 FILE *fp; | |
| 520 const char *format; | |
| 521 va_list va; | |
| 522 { | |
| 523 PyObject *file; | |
| 524 | |
| 525 file = PySys_GetObject(name); | |
| 526 if (file == NULL || PyFile_AsFile(file) == fp) | |
| 527 vfprintf(fp, format, va); | |
| 528 else { | |
| 529 char buffer[1001]; | |
| 530 vsprintf(buffer, format, va); | |
| 531 if (PyFile_WriteString(buffer, file) != 0) { | |
| 532 PyErr_Clear(); | |
| 533 fputs(buffer, fp); | |
| 534 } | |
| 535 } | |
| 536 } | |
| 537 | |
| 538 void | |
| 539 #ifdef HAVE_STDARG_PROTOTYPES | |
| 540 PySys_WriteStdout(const char *format, ...) | |
| 541 #else | |
| 542 PySys_WriteStdout(va_alist) | |
| 543 va_dcl | |
| 544 #endif | |
| 545 { | |
| 546 va_list va; | |
| 547 | |
| 548 #ifdef HAVE_STDARG_PROTOTYPES | |
| 549 va_start(va, format); | |
| 550 #else | |
| 551 char *format; | |
| 552 va_start(va); | |
| 553 format = va_arg(va, char *); | |
| 554 #endif | |
| 555 mywrite("stdout", stdout, format, va); | |
| 556 va_end(va); | |
| 557 } | |
| 558 | |
| 559 void | |
| 560 #ifdef HAVE_STDARG_PROTOTYPES | |
| 561 PySys_WriteStderr(const char *format, ...) | |
| 562 #else | |
| 563 PySys_WriteStderr(va_alist) | |
| 564 va_dcl | |
| 565 #endif | |
| 566 { | |
| 567 va_list va; | |
| 568 | |
| 569 #ifdef HAVE_STDARG_PROTOTYPES | |
| 570 va_start(va, format); | |
| 571 #else | |
| 572 char *format; | |
| 573 va_start(va); | |
| 574 format = va_arg(va, char *); | |
| 575 #endif | |
| 576 mywrite("stderr", stderr, format, va); | |
| 577 va_end(va); | |
| 578 } |
