annotate Python/mysnprintf.c @ 20491:149a65fa427c legacy-trunk

Fix computation of stack depth for classdef and closures. Also minor tweaks to internal routines. Use PyCF_MASK instead of explicit list of flags. For the MAKE_CLOSURE opcode, the number of items popped off the stack depends on both the oparg and the number of free variables for the code object. Fix the code so it accounts for the free variables. In com_classdef(), record an extra pop to account for the STORE call after the BUILD_CLASS. Get rid of some commented out debugging code in com_push() and com_pop(). Factor string resize logic into helper routine com_check_size(). In com_addbyte(), remove redudant if statement after assert. (They test the same condition.) In several routines, use string macros instead of string functions.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Wed, 17 Oct 2001 13:22:22 +0000
parents 2bc648932d44
children 6b6a9cae256e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18908
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2 #include "Python.h"
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
3
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
4 /* snprintf() emulation for platforms which don't have it (yet).
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
5
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
6 Return value
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
7
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
8 The number of characters printed (not including the trailing
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
9 `\0' used to end output to strings) or a negative number in
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
10 case of an error.
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
11
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
12 PyOS_snprintf and PyOS_vsnprintf do not write more than size
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
13 bytes (including the trailing '\0').
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
14
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
15 If the output would have been truncated, they return the number
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
16 of characters (excluding the trailing '\0') which would have
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
17 been written to the final string if enough space had been
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
18 available. This is inline with the C99 standard.
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
19
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
20 */
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
21
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
22 #include <ctype.h>
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
23
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
24 #ifndef HAVE_SNPRINTF
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
25
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
26 static
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
27 int myvsnprintf(char *str, size_t size, const char *format, va_list va)
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
28 {
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
29 char *buffer = PyMem_Malloc(size + 512);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
30 int len;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
31
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
32 if (buffer == NULL)
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
33 return -1;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
34 len = vsprintf(buffer, format, va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
35 if (len < 0) {
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
36 PyMem_Free(buffer);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
37 return len;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
38 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
39 len++;
18916
2bc648932d44 Add mysnprintf.c to Windows build, + squash compiler wngs in mysnprintf.c.
Tim Peters <tim.peters@gmail.com>
parents: 18908
diff changeset
40 assert(len >= 0);
2bc648932d44 Add mysnprintf.c to Windows build, + squash compiler wngs in mysnprintf.c.
Tim Peters <tim.peters@gmail.com>
parents: 18908
diff changeset
41 if ((size_t)len > size + 512)
18908
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
42 Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
18916
2bc648932d44 Add mysnprintf.c to Windows build, + squash compiler wngs in mysnprintf.c.
Tim Peters <tim.peters@gmail.com>
parents: 18908
diff changeset
43 if ((size_t)len > size) {
18908
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
44 PyMem_Free(buffer);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
45 return len - 1;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
46 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
47 memcpy(str, buffer, len);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
48 PyMem_Free(buffer);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
49 return len - 1;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
50 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
51
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
52 int PyOS_snprintf(char *str, size_t size, const char *format, ...)
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
53 {
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
54 int rc;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
55 va_list va;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
56
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
57 va_start(va, format);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
58 rc = myvsnprintf(str, size, format, va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
59 va_end(va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
60 return rc;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
61 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
62
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
63 int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
64 {
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
65 return myvsnprintf(str, size, format, va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
66 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
67
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
68 #else
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
69
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
70 /* Make sure that a C API is included in the lib */
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
71
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
72 #ifdef PyOS_snprintf
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
73 # undef PyOS_snprintf
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
74 #endif
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
75
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
76 int PyOS_snprintf(char *str, size_t size, const char *format, ...)
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
77 {
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
78 int rc;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
79 va_list va;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
80
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
81 va_start(va, format);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
82 rc = vsnprintf(str, size, format, va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
83 va_end(va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
84 return rc;
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
85 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
86
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
87 #ifdef PyOS_vsnprintf
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
88 # undef PyOS_vsnprintf
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
89 #endif
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
90
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
91 int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
92 {
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
93 return vsnprintf(str, size, format, va);
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
94 }
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
95
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
96 #endif
79c676f9b34a This patch turns the Python API mismatch notice into a standard
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
97