annotate Objects/stringobject.c @ 75339:fc89e09ca2fc 2.7

#10713: Improve documentation for \b and \B and add a few tests. Initial patch and tests by Martin Pool.
author Ezio Melotti <ezio.melotti@gmail.com>
date Wed, 29 Feb 2012 11:40:00 +0200
parents 40b684215b4f
children a11a2bbd8241
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1 /* String (str/bytes) object implementation */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37678
diff changeset
3 #define PY_SSIZE_T_CLEAN
38376
a16948e9915e needforspeed: use Py_LOCAL on a few more locals in stringobject.c
Fredrik Lundh <fredrik@pythonware.com>
parents: 38372
diff changeset
4
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
5 #include "Python.h"
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
6 #include <ctype.h>
50036
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
7 #include <stddef.h>
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
8
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
9 #ifdef COUNT_ALLOCS
50627
f0e0c3536e22 Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 50036
diff changeset
10 Py_ssize_t null_strings, one_strings;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
11 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
12
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
13 static PyStringObject *characters[UCHAR_MAX + 1];
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
14 static PyStringObject *nullstring;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
15
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
16 /* This dictionary holds all interned strings. Note that references to
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
17 strings in this dictionary are *not* counted in the string's ob_refcnt.
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
18 When the interned string reaches a refcnt of 0 the string deallocation
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
19 function will delete the reference from this dictionary.
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
20
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
21 Another way to look at this is that to say that the actual reference
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
22 count of a string is: s->ob_refcnt + (s->ob_sstate?2:0)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
23 */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
24 static PyObject *interned;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
25
50036
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
26 /* PyStringObject_SIZE gives the basic size of a string; any memory allocation
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
27 for a string of length n should request PyStringObject_SIZE + n bytes.
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
28
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
29 Using PyStringObject_SIZE instead of sizeof(PyStringObject) saves
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
30 3 bytes per string allocation on a typical system.
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
31 */
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
32 #define PyStringObject_SIZE (offsetof(PyStringObject, ob_sval) + 1)
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
33
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
34 /*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
35 For PyString_FromString(), the parameter `str' points to a null-terminated
21737
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
36 string containing exactly `size' bytes.
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
37
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
38 For PyString_FromStringAndSize(), the parameter the parameter `str' is
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
39 either NULL or else points to a string containing at least `size' bytes.
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
40 For PyString_FromStringAndSize(), the string in the `str' parameter does
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
41 not have to be null-terminated. (Therefore it is safe to construct a
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
42 substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
43 If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1'
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
44 bytes (setting the last byte to the null terminating character) and you can
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
45 fill in the data yourself. If `str' is non-NULL then the resulting
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
46 PyString object must be treated as immutable and you must not fill in nor
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
47 alter the data yourself, since the strings may be shared.
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
48
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
49 The PyObject member `op->ob_size', which denotes the number of "extra
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
50 items" in a variable-size object, will contain the number of bytes
68907
44749e501982 Issue #11634: Remove misleading paragraph from a comment
Eli Bendersky <eliben@gmail.com>
parents: 68503
diff changeset
51 allocated for string data, not counting the null terminating character.
44749e501982 Issue #11634: Remove misleading paragraph from a comment
Eli Bendersky <eliben@gmail.com>
parents: 68503
diff changeset
52 It is therefore equal to the `size' parameter (for
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
53 PyString_FromStringAndSize()) or the length of the string in the `str'
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
54 parameter (for PyString_FromString()).
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
55 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
56 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
57 PyString_FromStringAndSize(const char *str, Py_ssize_t size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
59 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
60 if (size < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
61 PyErr_SetString(PyExc_SystemError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
62 "Negative size passed to PyString_FromStringAndSize");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
63 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
64 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
65 if (size == 0 && (op = nullstring) != NULL) {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
66 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
67 null_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
68 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
69 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
70 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
71 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
72 if (size == 1 && str != NULL &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
73 (op = characters[*str & UCHAR_MAX]) != NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
74 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
75 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
76 one_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
77 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
78 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
79 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
80 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
81
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
82 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
83 PyErr_SetString(PyExc_OverflowError, "string is too large");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
84 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
85 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
86
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
87 /* Inline PyObject_NewVar */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
88 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
89 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
90 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
91 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
92 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
93 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
94 if (str != NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
95 Py_MEMCPY(op->ob_sval, str, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
96 op->ob_sval[size] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
97 /* share short strings */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
98 if (size == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
99 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
100 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
101 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
102 nullstring = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
103 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
104 } else if (size == 1 && str != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
105 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
106 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
107 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
108 characters[*str & UCHAR_MAX] = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
109 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
110 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
111 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
114 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
115 PyString_FromString(const char *str)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
117 register size_t size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
118 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
119
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
120 assert(str != NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
121 size = strlen(str);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
122 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
123 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
124 "string is too long for a Python string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
125 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
126 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
127 if (size == 0 && (op = nullstring) != NULL) {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
128 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
129 null_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
130 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
131 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
132 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
133 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
134 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
135 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
136 one_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
137 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
138 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
139 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
140 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
141
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
142 /* Inline PyObject_NewVar */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
143 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
144 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
145 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
146 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
147 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
148 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
149 Py_MEMCPY(op->ob_sval, str, size+1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
150 /* share short strings */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
151 if (size == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
152 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
153 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
154 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
155 nullstring = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
156 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
157 } else if (size == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
158 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
159 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
160 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
161 characters[*str & UCHAR_MAX] = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
162 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
163 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
164 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
167 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
168 PyString_FromFormatV(const char *format, va_list vargs)
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
169 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
170 va_list count;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
171 Py_ssize_t n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
172 const char* f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
173 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
174 PyObject* string;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
175
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
176 #ifdef VA_LIST_IS_ARRAY
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
177 Py_MEMCPY(count, vargs, sizeof(va_list));
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
178 #else
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
179 #ifdef __va_copy
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
180 __va_copy(count, vargs);
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
181 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
182 count = vargs;
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
183 #endif
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
184 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
185 /* step 1: figure out how large a buffer we need */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
186 for (f = format; *f; f++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
187 if (*f == '%') {
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
188 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
189 int longlongflag = 0;
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
190 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
191 const char* p = f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
192 while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
193 ;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
194
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
195 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
196 * they don't affect the amount of space we reserve.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
197 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
198 if (*f == 'l') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
199 if (f[1] == 'd' || f[1] == 'u') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
200 ++f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
201 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
202 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
203 else if (f[1] == 'l' &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
204 (f[2] == 'd' || f[2] == 'u')) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
205 longlongflag = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
206 f += 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
207 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
208 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
210 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
211 ++f;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
212 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
213
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
214 switch (*f) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
215 case 'c':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
216 (void)va_arg(count, int);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
217 /* fall through... */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
218 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
219 n++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
220 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
221 case 'd': case 'u': case 'i': case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
222 (void) va_arg(count, int);
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
223 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
224 /* Need at most
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
225 ceil(log10(256)*SIZEOF_LONG_LONG) digits,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
226 plus 1 for the sign. 53/22 is an upper
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
227 bound for log10(256). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
228 if (longlongflag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
229 n += 2 + (SIZEOF_LONG_LONG*53-1) / 22;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
230 else
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
231 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
232 /* 20 bytes is enough to hold a 64-bit
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
233 integer. Decimal takes the most
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
234 space. This isn't enough for
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
235 octal. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
236 n += 20;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
237
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
238 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
239 case 's':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
240 s = va_arg(count, char*);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
241 n += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
242 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
243 case 'p':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
244 (void) va_arg(count, int);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
245 /* maximum 64-bit pointer representation:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
246 * 0xffffffffffffffff
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
247 * so 19 characters is enough.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
248 * XXX I count 18 -- what's the extra for?
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
249 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
250 n += 19;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
251 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
252 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
253 /* if we stumble upon an unknown
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
254 formatting code, copy the rest of
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
255 the format string to the output
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
256 string. (we cannot just skip the
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
257 code, since there's no way to know
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
258 what's in the argument list) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
259 n += strlen(p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
260 goto expand;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
261 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
262 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
263 n++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
264 }
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
265 expand:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
266 /* step 2: fill the buffer */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
267 /* Since we've analyzed how much space we need for the worst case,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
268 use sprintf directly instead of the slower PyOS_snprintf. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
269 string = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
270 if (!string)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
271 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
272
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
273 s = PyString_AsString(string);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
274
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
275 for (f = format; *f; f++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
276 if (*f == '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
277 const char* p = f++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
278 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
279 int longflag = 0;
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
280 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
281 int longlongflag = 0;
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
282 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
283 int size_tflag = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
284 /* parse the width.precision part (we're only
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
285 interested in the precision value, if any) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
286 n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
287 while (isdigit(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
288 n = (n*10) + *f++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
289 if (*f == '.') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
290 f++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
291 n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
292 while (isdigit(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
293 n = (n*10) + *f++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
294 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
295 while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
296 f++;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
297 /* Handle %ld, %lu, %lld and %llu. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
298 if (*f == 'l') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
299 if (f[1] == 'd' || f[1] == 'u') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
300 longflag = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
301 ++f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
302 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
303 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
304 else if (f[1] == 'l' &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
305 (f[2] == 'd' || f[2] == 'u')) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
306 longlongflag = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
307 f += 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
308 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
309 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
310 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
311 /* handle the size_t flag. */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
312 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
313 size_tflag = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
314 ++f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
315 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
316
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
317 switch (*f) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
318 case 'c':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
319 *s++ = va_arg(vargs, int);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
320 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
321 case 'd':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
322 if (longflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
323 sprintf(s, "%ld", va_arg(vargs, long));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
324 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
325 else if (longlongflag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
326 sprintf(s, "%" PY_FORMAT_LONG_LONG "d",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
327 va_arg(vargs, PY_LONG_LONG));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
328 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
329 else if (size_tflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
330 sprintf(s, "%" PY_FORMAT_SIZE_T "d",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
331 va_arg(vargs, Py_ssize_t));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
332 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
333 sprintf(s, "%d", va_arg(vargs, int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
334 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
335 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
336 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
337 if (longflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
338 sprintf(s, "%lu",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
339 va_arg(vargs, unsigned long));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
340 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
341 else if (longlongflag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
342 sprintf(s, "%" PY_FORMAT_LONG_LONG "u",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
343 va_arg(vargs, PY_LONG_LONG));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
344 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
345 else if (size_tflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
346 sprintf(s, "%" PY_FORMAT_SIZE_T "u",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
347 va_arg(vargs, size_t));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
348 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
349 sprintf(s, "%u",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
350 va_arg(vargs, unsigned int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
351 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
352 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
353 case 'i':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
354 sprintf(s, "%i", va_arg(vargs, int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
355 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
356 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
357 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
358 sprintf(s, "%x", va_arg(vargs, int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
359 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
360 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
361 case 's':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
362 p = va_arg(vargs, char*);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
363 i = strlen(p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
364 if (n > 0 && i > n)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
365 i = n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
366 Py_MEMCPY(s, p, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
367 s += i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
368 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
369 case 'p':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
370 sprintf(s, "%p", va_arg(vargs, void*));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
371 /* %p is ill-defined: ensure leading 0x. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
372 if (s[1] == 'X')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
373 s[1] = 'x';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
374 else if (s[1] != 'x') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
375 memmove(s+2, s, strlen(s)+1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
376 s[0] = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
377 s[1] = 'x';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
378 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
379 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
380 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
381 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
382 *s++ = '%';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
383 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
384 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
385 strcpy(s, p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
386 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
387 goto end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
388 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
389 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
390 *s++ = *f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
391 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
392
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
393 end:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
394 if (_PyString_Resize(&string, s - PyString_AS_STRING(string)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
395 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
396 return string;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
397 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
398
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
399 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
400 PyString_FromFormat(const char *format, ...)
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
401 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
402 PyObject* ret;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
403 va_list vargs;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
404
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
405 #ifdef HAVE_STDARG_PROTOTYPES
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
406 va_start(vargs, format);
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
407 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
408 va_start(vargs);
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
409 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
410 ret = PyString_FromFormatV(format, vargs);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
411 va_end(vargs);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
412 return ret;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
413 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
414
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
415
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
416 PyObject *PyString_Decode(const char *s,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
417 Py_ssize_t size,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
418 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
419 const char *errors)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
420 {
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
421 PyObject *v, *str;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
422
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
423 str = PyString_FromStringAndSize(s, size);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
424 if (str == NULL)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
425 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
426 v = PyString_AsDecodedString(str, encoding, errors);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
427 Py_DECREF(str);
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
428 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
429 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
430
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
431 PyObject *PyString_AsDecodedObject(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
432 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
433 const char *errors)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
434 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
435 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
436
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
437 if (!PyString_Check(str)) {
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
438 PyErr_BadArgument();
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
439 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
440 }
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
441
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
442 if (encoding == NULL) {
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
443 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
444 encoding = PyUnicode_GetDefaultEncoding();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
445 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
446 PyErr_SetString(PyExc_ValueError, "no encoding specified");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
447 goto onError;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
448 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
449 }
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
450
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
451 /* Decode via the codec registry */
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
452 v = PyCodec_Decode(str, encoding, errors);
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
453 if (v == NULL)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
454 goto onError;
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
455
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
456 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
457
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
458 onError:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
459 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
460 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
461
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
462 PyObject *PyString_AsDecodedString(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
463 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
464 const char *errors)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
465 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
466 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
467
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
468 v = PyString_AsDecodedObject(str, encoding, errors);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
469 if (v == NULL)
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
470 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
471
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
472 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
473 /* Convert Unicode to a string using the default encoding */
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
474 if (PyUnicode_Check(v)) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
475 PyObject *temp = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
476 v = PyUnicode_AsEncodedString(v, NULL, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
477 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
478 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
479 goto onError;
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
480 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
481 #endif
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
482 if (!PyString_Check(v)) {
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
483 PyErr_Format(PyExc_TypeError,
13561
ea732719b3fd Fix typo in error message
Andrew M. Kuchling <amk@amk.ca>
parents: 13544
diff changeset
484 "decoder did not return a string object (type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
485 Py_TYPE(v)->tp_name);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
486 Py_DECREF(v);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
487 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
488 }
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
489
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
490 return v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
491
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
492 onError:
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
493 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
494 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
495
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
496 PyObject *PyString_Encode(const char *s,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
497 Py_ssize_t size,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
498 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
499 const char *errors)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
500 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
501 PyObject *v, *str;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
502
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
503 str = PyString_FromStringAndSize(s, size);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
504 if (str == NULL)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
505 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
506 v = PyString_AsEncodedString(str, encoding, errors);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
507 Py_DECREF(str);
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
508 return v;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
509 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
510
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
511 PyObject *PyString_AsEncodedObject(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
512 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
513 const char *errors)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
514 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
515 PyObject *v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
516
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
517 if (!PyString_Check(str)) {
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
518 PyErr_BadArgument();
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
519 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
520 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
521
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
522 if (encoding == NULL) {
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
523 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
524 encoding = PyUnicode_GetDefaultEncoding();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
525 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
526 PyErr_SetString(PyExc_ValueError, "no encoding specified");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
527 goto onError;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
528 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
529 }
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
530
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
531 /* Encode via the codec registry */
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
532 v = PyCodec_Encode(str, encoding, errors);
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
533 if (v == NULL)
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
534 goto onError;
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
535
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
536 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
537
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
538 onError:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
539 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
540 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
541
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
542 PyObject *PyString_AsEncodedString(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
543 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
544 const char *errors)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
545 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
546 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
547
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
548 v = PyString_AsEncodedObject(str, encoding, errors);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
549 if (v == NULL)
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
550 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
551
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
552 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
553 /* Convert Unicode to a string using the default encoding */
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
554 if (PyUnicode_Check(v)) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
555 PyObject *temp = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
556 v = PyUnicode_AsEncodedString(v, NULL, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
557 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
558 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
559 goto onError;
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
560 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
561 #endif
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
562 if (!PyString_Check(v)) {
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
563 PyErr_Format(PyExc_TypeError,
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
564 "encoder did not return a string object (type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
565 Py_TYPE(v)->tp_name);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
566 Py_DECREF(v);
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
567 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
568 }
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
569
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
570 return v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
571
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
572 onError:
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
573 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
574 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
575
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
576 static void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
577 string_dealloc(PyObject *op)
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
578 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
579 switch (PyString_CHECK_INTERNED(op)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
580 case SSTATE_NOT_INTERNED:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
581 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
582
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
583 case SSTATE_INTERNED_MORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
584 /* revive dead object temporarily for DelItem */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
585 Py_REFCNT(op) = 3;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
586 if (PyDict_DelItem(interned, op) != 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
587 Py_FatalError(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
588 "deletion of interned string failed");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
589 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
590
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
591 case SSTATE_INTERNED_IMMORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
592 Py_FatalError("Immortal interned string died.");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
593
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
594 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
595 Py_FatalError("Inconsistent interned string state.");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
596 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
597 Py_TYPE(op)->tp_free(op);
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
598 }
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
599
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
600 /* Unescape a backslash-escaped string. If unicode is non-zero,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
601 the string is a u-literal. If recode_encoding is non-zero,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
602 the string is UTF-8 encoded and should be re-encoded in the
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
603 specified encoding. */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
604
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
605 PyObject *PyString_DecodeEscape(const char *s,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
606 Py_ssize_t len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
607 const char *errors,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
608 Py_ssize_t unicode,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
609 const char *recode_encoding)
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
610 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
611 int c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
612 char *p, *buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
613 const char *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
614 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
615 Py_ssize_t newlen = recode_encoding ? 4*len:len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
616 v = PyString_FromStringAndSize((char *)NULL, newlen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
617 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
618 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
619 p = buf = PyString_AsString(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
620 end = s + len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
621 while (s < end) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
622 if (*s != '\\') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
623 non_esc:
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
624 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
625 if (recode_encoding && (*s & 0x80)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
626 PyObject *u, *w;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
627 char *r;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
628 const char* t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
629 Py_ssize_t rn;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
630 t = s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
631 /* Decode non-ASCII bytes as UTF-8. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
632 while (t < end && (*t & 0x80)) t++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
633 u = PyUnicode_DecodeUTF8(s, t - s, errors);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
634 if(!u) goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
635
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
636 /* Recode them in target encoding. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
637 w = PyUnicode_AsEncodedString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
638 u, recode_encoding, errors);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
639 Py_DECREF(u);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
640 if (!w) goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
641
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
642 /* Append bytes to output buffer. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
643 assert(PyString_Check(w));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
644 r = PyString_AS_STRING(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
645 rn = PyString_GET_SIZE(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
646 Py_MEMCPY(p, r, rn);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
647 p += rn;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
648 Py_DECREF(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
649 s = t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
650 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
651 *p++ = *s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
652 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
653 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
654 *p++ = *s++;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
655 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
656 continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
657 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
658 s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
659 if (s==end) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
660 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
661 "Trailing \\ in string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
662 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
663 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
664 switch (*s++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
665 /* XXX This assumes ASCII! */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
666 case '\n': break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
667 case '\\': *p++ = '\\'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
668 case '\'': *p++ = '\''; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
669 case '\"': *p++ = '\"'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
670 case 'b': *p++ = '\b'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
671 case 'f': *p++ = '\014'; break; /* FF */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
672 case 't': *p++ = '\t'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
673 case 'n': *p++ = '\n'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
674 case 'r': *p++ = '\r'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
675 case 'v': *p++ = '\013'; break; /* VT */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
676 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
677 case '0': case '1': case '2': case '3':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
678 case '4': case '5': case '6': case '7':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
679 c = s[-1] - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
680 if (s < end && '0' <= *s && *s <= '7') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
681 c = (c<<3) + *s++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
682 if (s < end && '0' <= *s && *s <= '7')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
683 c = (c<<3) + *s++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
684 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
685 *p++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
686 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
687 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
688 if (s+1 < end &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
689 isxdigit(Py_CHARMASK(s[0])) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
690 isxdigit(Py_CHARMASK(s[1])))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
691 {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
692 unsigned int x = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
693 c = Py_CHARMASK(*s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
694 s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
695 if (isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
696 x = c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
697 else if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
698 x = 10 + c - 'a';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
699 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
700 x = 10 + c - 'A';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
701 x = x << 4;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
702 c = Py_CHARMASK(*s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
703 s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
704 if (isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
705 x += c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
706 else if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
707 x += 10 + c - 'a';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
708 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
709 x += 10 + c - 'A';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
710 *p++ = x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
711 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
712 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
713 if (!errors || strcmp(errors, "strict") == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
714 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
715 "invalid \\x escape");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
716 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
717 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
718 if (strcmp(errors, "replace") == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
719 *p++ = '?';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
720 } else if (strcmp(errors, "ignore") == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
721 /* do nothing */;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
722 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
723 PyErr_Format(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
724 "decoding error; "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
725 "unknown error handling code: %.400s",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
726 errors);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
727 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
728 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
729 #ifndef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
730 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
731 case 'U':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
732 case 'N':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
733 if (unicode) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
734 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
735 "Unicode escapes not legal "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
736 "when Unicode disabled");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
737 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
738 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
739 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
740 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
741 *p++ = '\\';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
742 s--;
68503
ee5350f1d0c2 #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
743 goto non_esc; /* an arbitrary number of unescaped
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
744 UTF-8 bytes may follow. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
745 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
746 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
747 if (p-buf < newlen && _PyString_Resize(&v, p - buf))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
748 goto failed;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
749 return v;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
750 failed:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
751 Py_DECREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
752 return NULL;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
753 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
754
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
755 /* -------------------------------------------------------------------- */
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
756 /* object api */
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
757
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
758 static Py_ssize_t
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
759 string_getsize(register PyObject *op)
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
760 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
761 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
762 Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
763 if (PyString_AsStringAndSize(op, &s, &len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
764 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
765 return len;
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
766 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
767
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
768 static /*const*/ char *
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
769 string_getbuffer(register PyObject *op)
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
770 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
771 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
772 Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
773 if (PyString_AsStringAndSize(op, &s, &len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
774 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
775 return s;
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
776 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
777
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
778 Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
779 PyString_Size(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
781 if (!PyString_Check(op))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
782 return string_getsize(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
783 return Py_SIZE(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 /*const*/ char *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
787 PyString_AsString(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
789 if (!PyString_Check(op))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
790 return string_getbuffer(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
791 return ((PyStringObject *)op) -> ob_sval;
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
792 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
793
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
794 int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
795 PyString_AsStringAndSize(register PyObject *obj,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
796 register char **s,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
797 register Py_ssize_t *len)
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
798 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
799 if (s == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
800 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
801 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
802 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
803
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
804 if (!PyString_Check(obj)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
805 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
806 if (PyUnicode_Check(obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
807 obj = _PyUnicode_AsDefaultEncodedString(obj, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
808 if (obj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
809 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
810 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
811 else
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
812 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
813 {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
814 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
815 "expected string or Unicode object, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
816 "%.200s found", Py_TYPE(obj)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
817 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
818 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
819 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
820
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
821 *s = PyString_AS_STRING(obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
822 if (len != NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
823 *len = PyString_GET_SIZE(obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
824 else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
825 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
826 "expected string without null bytes");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
827 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
828 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
829 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
831
38303
17eaa8b57e05 needforspeed: use "fastsearch" for count. this results in a 3x speedup
Fredrik Lundh <fredrik@pythonware.com>
parents: 38302
diff changeset
832 /* -------------------------------------------------------------------- */
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
833 /* Methods */
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38376
diff changeset
834
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
835 #include "stringlib/stringdefs.h"
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38376
diff changeset
836 #include "stringlib/fastsearch.h"
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
837
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
838 #include "stringlib/count.h"
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
839 #include "stringlib/find.h"
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
840 #include "stringlib/partition.h"
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
841 #include "stringlib/split.h"
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38376
diff changeset
842
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
843 #define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46871
diff changeset
844 #include "stringlib/localeutil.h"
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46871
diff changeset
845
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46871
diff changeset
846
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
847
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
848 static int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
849 string_print(PyStringObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
851 Py_ssize_t i, str_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
852 char c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
853 int quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
854
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
855 /* XXX Ought to check for interrupts when writing long strings */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
856 if (! PyString_CheckExact(op)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
857 int ret;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
858 /* A str subclass may have its own __str__ method. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
859 op = (PyStringObject *) PyObject_Str((PyObject *)op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
860 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
861 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
862 ret = string_print(op, fp, flags);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
863 Py_DECREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
864 return ret;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
865 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
866 if (flags & Py_PRINT_RAW) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
867 char *data = op->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
868 Py_ssize_t size = Py_SIZE(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
869 Py_BEGIN_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
870 while (size > INT_MAX) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
871 /* Very long strings cannot be written atomically.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
872 * But don't write exactly INT_MAX bytes at a time
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
873 * to avoid memory aligment issues.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
874 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
875 const int chunk_size = INT_MAX & ~0x3FFF;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
876 fwrite(data, 1, chunk_size, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
877 data += chunk_size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
878 size -= chunk_size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
879 }
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
880 #ifdef __VMS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
881 if (size) fwrite(data, (int)size, 1, fp);
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
882 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
883 fwrite(data, 1, (int)size, fp);
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
884 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
885 Py_END_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
886 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
887 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
888
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
889 /* figure out which quote to use; single is preferred */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
890 quote = '\'';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
891 if (memchr(op->ob_sval, '\'', Py_SIZE(op)) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
892 !memchr(op->ob_sval, '"', Py_SIZE(op)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
893 quote = '"';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
894
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
895 str_len = Py_SIZE(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
896 Py_BEGIN_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
897 fputc(quote, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
898 for (i = 0; i < str_len; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
899 /* Since strings are immutable and the caller should have a
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
900 reference, accessing the interal buffer should not be an issue
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
901 with the GIL released. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
902 c = op->ob_sval[i];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
903 if (c == quote || c == '\\')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
904 fprintf(fp, "\\%c", c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
905 else if (c == '\t')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
906 fprintf(fp, "\\t");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
907 else if (c == '\n')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
908 fprintf(fp, "\\n");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
909 else if (c == '\r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
910 fprintf(fp, "\\r");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
911 else if (c < ' ' || c >= 0x7f)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
912 fprintf(fp, "\\x%02x", c & 0xff);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
913 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
914 fputc(c, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
915 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
916 fputc(quote, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
917 Py_END_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
918 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
919 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
920
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
921 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
922 PyString_Repr(PyObject *obj, int smartquotes)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
923 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
924 register PyStringObject* op = (PyStringObject*) obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
925 size_t newsize = 2 + 4 * Py_SIZE(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
926 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
927 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_SIZE(op)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
928 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
929 "string is too large to make repr");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
930 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
931 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
932 v = PyString_FromStringAndSize((char *)NULL, newsize);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
933 if (v == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
934 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
935 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
936 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
937 register Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
938 register char c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
939 register char *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
940 int quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
941
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
942 /* figure out which quote to use; single is preferred */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
943 quote = '\'';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
944 if (smartquotes &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
945 memchr(op->ob_sval, '\'', Py_SIZE(op)) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
946 !memchr(op->ob_sval, '"', Py_SIZE(op)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
947 quote = '"';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
948
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
949 p = PyString_AS_STRING(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
950 *p++ = quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
951 for (i = 0; i < Py_SIZE(op); i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
952 /* There's at least enough room for a hex escape
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
953 and a closing quote. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
954 assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
955 c = op->ob_sval[i];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
956 if (c == quote || c == '\\')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
957 *p++ = '\\', *p++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
958 else if (c == '\t')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
959 *p++ = '\\', *p++ = 't';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
960 else if (c == '\n')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
961 *p++ = '\\', *p++ = 'n';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
962 else if (c == '\r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
963 *p++ = '\\', *p++ = 'r';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
964 else if (c < ' ' || c >= 0x7f) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
965 /* For performance, we don't want to call
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
966 PyOS_snprintf here (extra layers of
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
967 function call). */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
968 sprintf(p, "\\x%02x", c & 0xff);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
969 p += 4;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
970 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
971 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
972 *p++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
973 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
974 assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
975 *p++ = quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
976 *p = '\0';
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
977 if (_PyString_Resize(&v, (p - PyString_AS_STRING(v))))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
978 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
979 return v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
980 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
982
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
983 static PyObject *
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
984 string_repr(PyObject *op)
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
985 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
986 return PyString_Repr(op, 1);
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
987 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
988
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
989 static PyObject *
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
990 string_str(PyObject *s)
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
991 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
992 assert(PyString_Check(s));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
993 if (PyString_CheckExact(s)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
994 Py_INCREF(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
995 return s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
996 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
997 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
998 /* Subtype -- return genuine string with the same value. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
999 PyStringObject *t = (PyStringObject *) s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1000 return PyString_FromStringAndSize(t->ob_sval, Py_SIZE(t));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1001 }
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
1002 }
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
1003
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1004 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1005 string_length(PyStringObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1007 return Py_SIZE(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1010 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1011 string_concat(register PyStringObject *a, register PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1013 register Py_ssize_t size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1014 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1015 if (!PyString_Check(bb)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1016 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1017 if (PyUnicode_Check(bb))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1018 return PyUnicode_Concat((PyObject *)a, bb);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1019 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1020 if (PyByteArray_Check(bb))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1021 return PyByteArray_Concat((PyObject *)a, bb);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1022 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1023 "cannot concatenate 'str' and '%.200s' objects",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1024 Py_TYPE(bb)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1025 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1026 }
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1027 #define b ((PyStringObject *)bb)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1028 /* Optimize cases with empty left or right operand */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1029 if ((Py_SIZE(a) == 0 || Py_SIZE(b) == 0) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1030 PyString_CheckExact(a) && PyString_CheckExact(b)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1031 if (Py_SIZE(a) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1032 Py_INCREF(bb);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1033 return bb;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1034 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1035 Py_INCREF(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1036 return (PyObject *)a;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1037 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1038 size = Py_SIZE(a) + Py_SIZE(b);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1039 /* Check that string sizes are not negative, to prevent an
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1040 overflow in cases where we are passed incorrectly-created
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1041 strings with negative lengths (due to a bug in other code).
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1042 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1043 if (Py_SIZE(a) < 0 || Py_SIZE(b) < 0 ||
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1044 Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1045 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1046 "strings are too large to concat");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1047 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1048 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1049
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1050 /* Inline PyObject_NewVar */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1051 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1052 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1053 "strings are too large to concat");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1054 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1055 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1056 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1057 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1058 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1059 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1060 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1061 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1062 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1063 Py_MEMCPY(op->ob_sval + Py_SIZE(a), b->ob_sval, Py_SIZE(b));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1064 op->ob_sval[size] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1065 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1069 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1070 string_repeat(register PyStringObject *a, register Py_ssize_t n)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1072 register Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1073 register Py_ssize_t j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1074 register Py_ssize_t size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1075 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1076 size_t nbytes;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1077 if (n < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1078 n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1079 /* watch out for overflows: the size can overflow int,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1080 * and the # of bytes needed can overflow size_t
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1081 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1082 size = Py_SIZE(a) * n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1083 if (n && size / n != Py_SIZE(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1084 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1085 "repeated string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1086 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1087 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1088 if (size == Py_SIZE(a) && PyString_CheckExact(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1089 Py_INCREF(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1090 return (PyObject *)a;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1091 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1092 nbytes = (size_t)size;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1093 if (nbytes + PyStringObject_SIZE <= nbytes) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1094 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1095 "repeated string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1096 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1097 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1098 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + nbytes);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1099 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1100 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1101 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1102 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1103 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1104 op->ob_sval[size] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1105 if (Py_SIZE(a) == 1 && n > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1106 memset(op->ob_sval, a->ob_sval[0] , n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1107 return (PyObject *) op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1108 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1109 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1110 if (i < size) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1111 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1112 i = Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1113 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1114 while (i < size) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1115 j = (i <= size-i) ? i : size-i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1116 Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1117 i += j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1118 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1119 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1124 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1125 string_slice(register PyStringObject *a, register Py_ssize_t i,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1126 register Py_ssize_t j)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1127 /* j -- may be negative! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1129 if (i < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1130 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1131 if (j < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1132 j = 0; /* Avoid signed/unsigned bug in next line */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1133 if (j > Py_SIZE(a))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1134 j = Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1135 if (i == 0 && j == Py_SIZE(a) && PyString_CheckExact(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1136 /* It's the same as a */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1137 Py_INCREF(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1138 return (PyObject *)a;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1139 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1140 if (j < i)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1141 j = i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1142 return PyString_FromStringAndSize(a->ob_sval + i, j-i);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1145 static int
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
1146 string_contains(PyObject *str_obj, PyObject *sub_obj)
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1147 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1148 if (!PyString_CheckExact(sub_obj)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1149 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1150 if (PyUnicode_Check(sub_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1151 return PyUnicode_Contains(str_obj, sub_obj);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1152 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1153 if (!PyString_Check(sub_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1154 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1155 "'in <string>' requires string as left operand, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1156 "not %.200s", Py_TYPE(sub_obj)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1157 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1158 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1159 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1160
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1161 return stringlib_contains_obj(str_obj, sub_obj);
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1162 }
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1163
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1164 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1165 string_item(PyStringObject *a, register Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1167 char pchar;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1168 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1169 if (i < 0 || i >= Py_SIZE(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1170 PyErr_SetString(PyExc_IndexError, "string index out of range");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1171 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1172 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1173 pchar = a->ob_sval[i];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1174 v = (PyObject *)characters[pchar & UCHAR_MAX];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1175 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1176 v = PyString_FromStringAndSize(&pchar, 1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1177 else {
17712
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1178 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1179 one_strings++;
17712
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1180 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1181 Py_INCREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1182 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1183 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1186 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1187 string_richcompare(PyStringObject *a, PyStringObject *b, int op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1189 int c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1190 Py_ssize_t len_a, len_b;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1191 Py_ssize_t min_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1192 PyObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1193
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1194 /* Make sure both arguments are strings. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1195 if (!(PyString_Check(a) && PyString_Check(b))) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1196 result = Py_NotImplemented;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1197 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1198 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1199 if (a == b) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1200 switch (op) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1201 case Py_EQ:case Py_LE:case Py_GE:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1202 result = Py_True;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1203 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1204 case Py_NE:case Py_LT:case Py_GT:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1205 result = Py_False;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1206 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1207 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1208 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1209 if (op == Py_EQ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1210 /* Supporting Py_NE here as well does not save
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1211 much time, since Py_NE is rarely used. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1212 if (Py_SIZE(a) == Py_SIZE(b)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1213 && (a->ob_sval[0] == b->ob_sval[0]
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1214 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1215 result = Py_True;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1216 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1217 result = Py_False;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1218 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1219 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1220 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1221 len_a = Py_SIZE(a); len_b = Py_SIZE(b);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1222 min_len = (len_a < len_b) ? len_a : len_b;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1223 if (min_len > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1224 c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1225 if (c==0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1226 c = memcmp(a->ob_sval, b->ob_sval, min_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1227 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1228 c = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1229 if (c == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1230 c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1231 switch (op) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1232 case Py_LT: c = c < 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1233 case Py_LE: c = c <= 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1234 case Py_EQ: assert(0); break; /* unreachable */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1235 case Py_NE: c = c != 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1236 case Py_GT: c = c > 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1237 case Py_GE: c = c >= 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1238 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1239 result = Py_NotImplemented;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1240 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1241 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1242 result = c ? Py_True : Py_False;
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1243 out:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1244 Py_INCREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1245 return result;
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1246 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1247
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1248 int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1249 _PyString_Eq(PyObject *o1, PyObject *o2)
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1250 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1251 PyStringObject *a = (PyStringObject*) o1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1252 PyStringObject *b = (PyStringObject*) o2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1253 return Py_SIZE(a) == Py_SIZE(b)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1254 && *a->ob_sval == *b->ob_sval
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1255 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1258 static long
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1259 string_hash(PyStringObject *a)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1260 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1261 register Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1262 register unsigned char *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1263 register long x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1264
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1265 assert(_Py_HashSecret_Initialized);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1266 if (a->ob_shash != -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1267 return a->ob_shash;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1268 len = Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1269 /*
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1270 We make the hash of the empty string be 0, rather than using
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1271 (prefix ^ suffix), since this slightly obfuscates the hash secret
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1272 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1273 if (len == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1274 a->ob_shash = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1275 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1276 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1277 p = (unsigned char *) a->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1278 x = _Py_HashSecret.prefix;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1279 x ^= *p << 7;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1280 while (--len >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1281 x = (1000003*x) ^ *p++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1282 x ^= Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1283 x ^= _Py_HashSecret.suffix;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1284 if (x == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1285 x = -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1286 a->ob_shash = x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1287 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1288 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1289
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1290 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1291 string_subscript(PyStringObject* self, PyObject* item)
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1292 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1293 if (PyIndex_Check(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1294 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1295 if (i == -1 && PyErr_Occurred())
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1296 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1297 if (i < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1298 i += PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1299 return string_item(self, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1300 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1301 else if (PySlice_Check(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1302 Py_ssize_t start, stop, step, slicelength, cur, i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1303 char* source_buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1304 char* result_buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1305 PyObject* result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1306
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1307 if (PySlice_GetIndicesEx((PySliceObject*)item,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1308 PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1309 &start, &stop, &step, &slicelength) < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1310 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1311 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1312
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1313 if (slicelength <= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1314 return PyString_FromStringAndSize("", 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1315 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1316 else if (start == 0 && step == 1 &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1317 slicelength == PyString_GET_SIZE(self) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1318 PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1319 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1320 return (PyObject *)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1321 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1322 else if (step == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1323 return PyString_FromStringAndSize(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1324 PyString_AS_STRING(self) + start,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1325 slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1326 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1327 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1328 source_buf = PyString_AsString((PyObject*)self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1329 result_buf = (char *)PyMem_Malloc(slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1330 if (result_buf == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1331 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1332
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1333 for (cur = start, i = 0; i < slicelength;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1334 cur += step, i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1335 result_buf[i] = source_buf[cur];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1336 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1337
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1338 result = PyString_FromStringAndSize(result_buf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1339 slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1340 PyMem_Free(result_buf);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1341 return result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1342 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1343 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1344 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1345 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1346 "string indices must be integers, not %.200s",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1347 Py_TYPE(item)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1348 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1349 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1350 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1351
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1352 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1353 string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1354 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1355 if ( index != 0 ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1356 PyErr_SetString(PyExc_SystemError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1357 "accessing non-existent string segment");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1358 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1359 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1360 *ptr = (void *)self->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1361 return Py_SIZE(self);
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1362 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1363
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1364 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1365 string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1366 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1367 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1368 "Cannot use string as modifiable buffer");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1369 return -1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1370 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1371
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1372 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1373 string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1374 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1375 if ( lenp )
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1376 *lenp = Py_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1377 return 1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1378 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1379
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1380 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1381 string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr)
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1382 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1383 if ( index != 0 ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1384 PyErr_SetString(PyExc_SystemError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1385 "accessing non-existent string segment");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1386 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1387 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1388 *ptr = self->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1389 return Py_SIZE(self);
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1390 }
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1391
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1392 static int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1393 string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags)
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1394 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1395 return PyBuffer_FillInfo(view, (PyObject*)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1396 (void *)self->ob_sval, Py_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1397 1, flags);
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1398 }
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1399
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1400 static PySequenceMethods string_as_sequence = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1401 (lenfunc)string_length, /*sq_length*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1402 (binaryfunc)string_concat, /*sq_concat*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1403 (ssizeargfunc)string_repeat, /*sq_repeat*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1404 (ssizeargfunc)string_item, /*sq_item*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1405 (ssizessizeargfunc)string_slice, /*sq_slice*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1406 0, /*sq_ass_item*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1407 0, /*sq_ass_slice*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1408 (objobjproc)string_contains /*sq_contains*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1411 static PyMappingMethods string_as_mapping = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1412 (lenfunc)string_length,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1413 (binaryfunc)string_subscript,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1414 0,
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1415 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1416
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1417 static PyBufferProcs string_as_buffer = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1418 (readbufferproc)string_buffer_getreadbuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1419 (writebufferproc)string_buffer_getwritebuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1420 (segcountproc)string_buffer_getsegcount,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1421 (charbufferproc)string_buffer_getcharbuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1422 (getbufferproc)string_buffer_getbuffer,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1423 0, /* XXX */
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1424 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1425
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1426
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1427
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1428 #define LEFTSTRIP 0
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1429 #define RIGHTSTRIP 1
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1430 #define BOTHSTRIP 2
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1431
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1432 /* Arrays indexed by above */
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1433 static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1434
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1435 #define STRIPNAME(i) (stripformat[i]+3)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1436
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1437 PyDoc_STRVAR(split__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1438 "S.split([sep [,maxsplit]]) -> list of strings\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1439 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1440 Return a list of the words in the string S, using sep as the\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1441 delimiter string. If maxsplit is given, at most maxsplit\n\
24561
b25c31e008c8 SF 582071 clarified the .split() method's docstring to note that sep=None
Raymond Hettinger <python@rcn.com>
parents: 24415
diff changeset
1442 splits are done. If sep is not specified or is None, any\n\
46908
ed2c1888e143 #2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents: 46898
diff changeset
1443 whitespace string is a separator and empty strings are removed\n\
ed2c1888e143 #2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents: 46898
diff changeset
1444 from the result.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1445
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1446 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1447 string_split(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1448 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1449 Py_ssize_t len = PyString_GET_SIZE(self), n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1450 Py_ssize_t maxsplit = -1;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1451 const char *s = PyString_AS_STRING(self), *sub;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1452 PyObject *subobj = Py_None;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1453
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1454 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1455 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1456 if (maxsplit < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1457 maxsplit = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1458 if (subobj == Py_None)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1459 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1460 if (PyString_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1461 sub = PyString_AS_STRING(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1462 n = PyString_GET_SIZE(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1463 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1464 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1465 else if (PyUnicode_Check(subobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1466 return PyUnicode_Split((PyObject *)self, subobj, maxsplit);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1467 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1468 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1469 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1470
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1471 return stringlib_split((PyObject*) self, s, len, sub, n, maxsplit);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1472 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1473
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1474 PyDoc_STRVAR(partition__doc__,
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1475 "S.partition(sep) -> (head, sep, tail)\n\
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1476 \n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1477 Search for the separator sep in S, and return the part before it,\n\
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1478 the separator itself, and the part after it. If the separator is not\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1479 found, return S and two empty strings.");
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1480
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1481 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1482 string_partition(PyStringObject *self, PyObject *sep_obj)
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1483 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1484 const char *sep;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1485 Py_ssize_t sep_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1486
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1487 if (PyString_Check(sep_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1488 sep = PyString_AS_STRING(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1489 sep_len = PyString_GET_SIZE(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1490 }
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38332
diff changeset
1491 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1492 else if (PyUnicode_Check(sep_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1493 return PyUnicode_Partition((PyObject *) self, sep_obj);
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1494 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1495 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1496 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1497
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1498 return stringlib_partition(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1499 (PyObject*) self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1500 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1501 sep_obj, sep, sep_len
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1502 );
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1503 }
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1504
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1505 PyDoc_STRVAR(rpartition__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1506 "S.rpartition(sep) -> (head, sep, tail)\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1507 \n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1508 Search for the separator sep in S, starting at the end of S, and return\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1509 the part before it, the separator itself, and the part after it. If the\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1510 separator is not found, return two empty strings and S.");
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1511
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1512 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1513 string_rpartition(PyStringObject *self, PyObject *sep_obj)
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1514 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1515 const char *sep;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1516 Py_ssize_t sep_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1517
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1518 if (PyString_Check(sep_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1519 sep = PyString_AS_STRING(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1520 sep_len = PyString_GET_SIZE(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1521 }
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1522 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1523 else if (PyUnicode_Check(sep_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1524 return PyUnicode_RPartition((PyObject *) self, sep_obj);
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1525 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1526 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1527 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1528
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1529 return stringlib_rpartition(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1530 (PyObject*) self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1531 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1532 sep_obj, sep, sep_len
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1533 );
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1534 }
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1535
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1536 PyDoc_STRVAR(rsplit__doc__,
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1537 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1538 \n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1539 Return a list of the words in the string S, using sep as the\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1540 delimiter string, starting at the end of the string and working\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1541 to the front. If maxsplit is given, at most maxsplit splits are\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1542 done. If sep is not specified or is None, any whitespace string\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1543 is a separator.");
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1544
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1545 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1546 string_rsplit(PyStringObject *self, PyObject *args)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1547 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1548 Py_ssize_t len = PyString_GET_SIZE(self), n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1549 Py_ssize_t maxsplit = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1550 const char *s = PyString_AS_STRING(self), *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1551 PyObject *subobj = Py_None;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1552
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1553 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1554 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1555 if (maxsplit < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1556 maxsplit = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1557 if (subobj == Py_None)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1558 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1559 if (PyString_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1560 sub = PyString_AS_STRING(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1561 n = PyString_GET_SIZE(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1562 }
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1563 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1564 else if (PyUnicode_Check(subobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1565 return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1566 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1567 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1568 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1569
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1570 return stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1571 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1572
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1573
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1574 PyDoc_STRVAR(join__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1575 "S.join(iterable) -> string\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1576 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1577 Return a string which is the concatenation of the strings in the\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1578 iterable. The separator between elements is S.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1579
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1580 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1581 string_join(PyStringObject *self, PyObject *orig)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1582 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1583 char *sep = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1584 const Py_ssize_t seplen = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1585 PyObject *res = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1586 char *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1587 Py_ssize_t seqlen = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1588 size_t sz = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1589 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1590 PyObject *seq, *item;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1591
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1592 seq = PySequence_Fast(orig, "");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1593 if (seq == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1594 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1595 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1596
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1597 seqlen = PySequence_Size(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1598 if (seqlen == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1599 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1600 return PyString_FromString("");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1601 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1602 if (seqlen == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1603 item = PySequence_Fast_GET_ITEM(seq, 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1604 if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1605 Py_INCREF(item);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1606 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1607 return item;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1608 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1609 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1610
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1611 /* There are at least two things to join, or else we have a subclass
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1612 * of the builtin types in the sequence.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1613 * Do a pre-pass to figure out the total amount of space we'll
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1614 * need (sz), see whether any argument is absurd, and defer to
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1615 * the Unicode join if appropriate.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1616 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1617 for (i = 0; i < seqlen; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1618 const size_t old_sz = sz;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1619 item = PySequence_Fast_GET_ITEM(seq, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1620 if (!PyString_Check(item)){
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1621 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1622 if (PyUnicode_Check(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1623 /* Defer to Unicode join.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1624 * CAUTION: There's no gurantee that the
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1625 * original sequence can be iterated over
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1626 * again, so we must pass seq here.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1627 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1628 PyObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1629 result = PyUnicode_Join((PyObject *)self, seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1630 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1631 return result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1632 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1633 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1634 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1635 "sequence item %zd: expected string,"
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1636 " %.80s found",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1637 i, Py_TYPE(item)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1638 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1639 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1640 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1641 sz += PyString_GET_SIZE(item);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1642 if (i != 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1643 sz += seplen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1644 if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1645 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1646 "join() result is too long for a Python string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1647 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1648 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1649 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1650 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1651
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1652 /* Allocate result space. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1653 res = PyString_FromStringAndSize((char*)NULL, sz);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1654 if (res == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1655 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1656 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1657 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1658
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1659 /* Catenate everything. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1660 p = PyString_AS_STRING(res);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1661 for (i = 0; i < seqlen; ++i) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1662 size_t n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1663 item = PySequence_Fast_GET_ITEM(seq, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1664 n = PyString_GET_SIZE(item);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1665 Py_MEMCPY(p, PyString_AS_STRING(item), n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1666 p += n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1667 if (i < seqlen - 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1668 Py_MEMCPY(p, sep, seplen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1669 p += seplen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1670 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1671 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1672
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1673 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1674 return res;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1675 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1676
18130
8eea2efb34cd Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1677 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1678 _PyString_Join(PyObject *sep, PyObject *x)
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1679 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1680 assert(sep != NULL && PyString_Check(sep));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1681 assert(x != NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1682 return string_join((PyStringObject *)sep, x);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1683 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1684
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
1685 /* helper macro to fixup start/end slice values */
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
1686 #define ADJUST_INDICES(start, end, len) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1687 if (end > len) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1688 end = len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1689 else if (end < 0) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1690 end += len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1691 if (end < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1692 end = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1693 } \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1694 if (start < 0) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1695 start += len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1696 if (start < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1697 start = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1698 }
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1699
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1700 Py_LOCAL_INLINE(Py_ssize_t)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1701 string_find_internal(PyStringObject *self, PyObject *args, int dir)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1702 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1703 PyObject *subobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1704 const char *sub;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1705 Py_ssize_t sub_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1706 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
1707
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
1708 if (!stringlib_parse_args_finds("find/rfind/index/rindex",
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
1709 args, &subobj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1710 return -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1711
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1712 if (PyString_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1713 sub = PyString_AS_STRING(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1714 sub_len = PyString_GET_SIZE(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1715 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1716 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1717 else if (PyUnicode_Check(subobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1718 return PyUnicode_Find(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1719 (PyObject *)self, subobj, start, end, dir);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1720 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1721 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1722 /* XXX - the "expected a character buffer object" is pretty
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1723 confusing for a non-expert. remap to something else ? */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1724 return -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1725
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1726 if (dir > 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1727 return stringlib_find_slice(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1728 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1729 sub, sub_len, start, end);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1730 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1731 return stringlib_rfind_slice(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1732 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1733 sub, sub_len, start, end);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1734 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1735
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1736
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1737 PyDoc_STRVAR(find__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1738 "S.find(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1739 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1740 Return the lowest index in S where substring sub is found,\n\
71529
e266415cf42c merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents: 70947
diff changeset
1741 such that sub is contained within S[start:end]. Optional\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1742 arguments start and end are interpreted as in slice notation.\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1743 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1744 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1745
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1746 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1747 string_find(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1748 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1749 Py_ssize_t result = string_find_internal(self, args, +1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1750 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1751 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1752 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1753 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1754
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1755
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1756 PyDoc_STRVAR(index__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1757 "S.index(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1758 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1759 Like S.find() but raise ValueError when the substring is not found.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1760
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1761 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1762 string_index(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1763 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1764 Py_ssize_t result = string_find_internal(self, args, +1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1765 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1766 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1767 if (result == -1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1768 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1769 "substring not found");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1770 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1771 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1772 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1773 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1774
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1775
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1776 PyDoc_STRVAR(rfind__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1777 "S.rfind(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1778 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1779 Return the highest index in S where substring sub is found,\n\
71529
e266415cf42c merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents: 70947
diff changeset
1780 such that sub is contained within S[start:end]. Optional\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1781 arguments start and end are interpreted as in slice notation.\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1782 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1783 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1784
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1785 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1786 string_rfind(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1787 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1788 Py_ssize_t result = string_find_internal(self, args, -1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1789 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1790 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1791 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1792 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1793
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1794
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1795 PyDoc_STRVAR(rindex__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1796 "S.rindex(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1797 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1798 Like S.rfind() but raise ValueError when the substring is not found.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1799
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1800 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1801 string_rindex(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1802 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1803 Py_ssize_t result = string_find_internal(self, args, -1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1804 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1805 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1806 if (result == -1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1807 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1808 "substring not found");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1809 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1810 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1811 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1812 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1813
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1814
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1815 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1816 do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1817 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1818 char *s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1819 Py_ssize_t len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1820 char *sep = PyString_AS_STRING(sepobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1821 Py_ssize_t seplen = PyString_GET_SIZE(sepobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1822 Py_ssize_t i, j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1823
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1824 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1825 if (striptype != RIGHTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1826 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1827 i++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1828 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1829 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1830
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1831 j = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1832 if (striptype != LEFTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1833 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1834 j--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1835 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1836 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1837 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1838
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1839 if (i == 0 && j == len && PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1840 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1841 return (PyObject*)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1842 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1843 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1844 return PyString_FromStringAndSize(s+i, j-i);
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1845 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1846
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1847
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1848 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1849 do_strip(PyStringObject *self, int striptype)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1850 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1851 char *s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1852 Py_ssize_t len = PyString_GET_SIZE(self), i, j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1853
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1854 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1855 if (striptype != RIGHTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1856 while (i < len && isspace(Py_CHARMASK(s[i]))) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1857 i++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1858 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1859 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1860
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1861 j = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1862 if (striptype != LEFTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1863 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1864 j--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1865 } while (j >= i && isspace(Py_CHARMASK(s[j])));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1866 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1867 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1868
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1869 if (i == 0 && j == len && PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1870 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1871 return (PyObject*)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1872 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1873 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1874 return PyString_FromStringAndSize(s+i, j-i);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1875 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1876
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1877
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1878 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1879 do_argstrip(PyStringObject *self, int striptype, PyObject *args)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1880 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1881 PyObject *sep = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1882
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1883 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1884 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1885
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1886 if (sep != NULL && sep != Py_None) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1887 if (PyString_Check(sep))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1888 return do_xstrip(self, striptype, sep);
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1889 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1890 else if (PyUnicode_Check(sep)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1891 PyObject *uniself = PyUnicode_FromObject((PyObject *)self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1892 PyObject *res;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1893 if (uniself==NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1894 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1895 res = _PyUnicode_XStrip((PyUnicodeObject *)uniself,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1896 striptype, sep);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1897 Py_DECREF(uniself);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1898 return res;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1899 }
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1900 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1901 PyErr_Format(PyExc_TypeError,
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1902 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1903 "%s arg must be None, str or unicode",
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1904 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1905 "%s arg must be None or str",
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1906 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1907 STRIPNAME(striptype));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1908 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1909 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1910
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1911 return do_strip(self, striptype);
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1912 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1913
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1914
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1915 PyDoc_STRVAR(strip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1916 "S.strip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1917 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1918 Return a copy of the string S with leading and trailing\n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1919 whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1920 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1921 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1922
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1923 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1924 string_strip(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1925 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1926 if (PyTuple_GET_SIZE(args) == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1927 return do_strip(self, BOTHSTRIP); /* Common case */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1928 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1929 return do_argstrip(self, BOTHSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1930 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1931
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1932
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1933 PyDoc_STRVAR(lstrip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1934 "S.lstrip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1935 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1936 Return a copy of the string S with leading whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1937 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1938 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1939
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1940 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1941 string_lstrip(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1942 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1943 if (PyTuple_GET_SIZE(args) == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1944 return do_strip(self, LEFTSTRIP); /* Common case */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1945 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1946 return do_argstrip(self, LEFTSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1947 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1948
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1949
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1950 PyDoc_STRVAR(rstrip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1951 "S.rstrip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1952 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1953 Return a copy of the string S with trailing whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1954 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1955 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1956
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1957 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1958 string_rstrip(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1959 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1960 if (PyTuple_GET_SIZE(args) == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1961 return do_strip(self, RIGHTSTRIP); /* Common case */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1962 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1963 return do_argstrip(self, RIGHTSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1964 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1965
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1966
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1967 PyDoc_STRVAR(lower__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1968 "S.lower() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1969 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1970 Return a copy of the string S converted to lowercase.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1971
38294
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1972 /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1973 #ifndef _tolower
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1974 #define _tolower tolower
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1975 #endif
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1976
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1977 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1978 string_lower(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1979 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1980 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1981 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1982 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1983
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1984 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1985 if (!newobj)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1986 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1987
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1988 s = PyString_AS_STRING(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1989
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1990 Py_MEMCPY(s, PyString_AS_STRING(self), n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1991
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1992 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1993 int c = Py_CHARMASK(s[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1994 if (isupper(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1995 s[i] = _tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1996 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1997
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1998 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1999 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2000
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2001 PyDoc_STRVAR(upper__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2002 "S.upper() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2003 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2004 Return a copy of the string S converted to uppercase.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2005
38294
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2006 #ifndef _toupper
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2007 #define _toupper toupper
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2008 #endif
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2009
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2010 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2011 string_upper(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2012 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2013 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2014 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2015 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2016
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2017 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2018 if (!newobj)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2019 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2020
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2021 s = PyString_AS_STRING(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2022
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2023 Py_MEMCPY(s, PyString_AS_STRING(self), n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2024
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2025 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2026 int c = Py_CHARMASK(s[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2027 if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2028 s[i] = _toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2029 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2030
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2031 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2032 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2033
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2034 PyDoc_STRVAR(title__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2035 "S.title() -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2036 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2037 Return a titlecased version of S, i.e. words start with uppercase\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2038 characters, all remaining cased characters have lowercase.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2039
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2040 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2041 string_title(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2042 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2043 char *s = PyString_AS_STRING(self), *s_new;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2044 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2045 int previous_is_cased = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2046 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2047
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2048 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2049 if (newobj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2050 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2051 s_new = PyString_AsString(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2052 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2053 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2054 if (islower(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2055 if (!previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2056 c = toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2057 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2058 } else if (isupper(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2059 if (previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2060 c = tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2061 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2062 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2063 previous_is_cased = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2064 *s_new++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2065 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2066 return newobj;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2067 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2068
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2069 PyDoc_STRVAR(capitalize__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2070 "S.capitalize() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2071 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2072 Return a copy of the string S with only its first character\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2073 capitalized.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2074
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2075 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2076 string_capitalize(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2077 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2078 char *s = PyString_AS_STRING(self), *s_new;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2079 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2080 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2081
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2082 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2083 if (newobj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2084 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2085 s_new = PyString_AsString(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2086 if (0 < n) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2087 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2088 if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2089 *s_new = toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2090 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2091 *s_new = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2092 s_new++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2093 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2094 for (i = 1; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2095 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2096 if (isupper(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2097 *s_new = tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2098 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2099 *s_new = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2100 s_new++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2101 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2102 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2103 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2104
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2105
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2106 PyDoc_STRVAR(count__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2107 "S.count(sub[, start[, end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2108 \n\
38236
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2109 Return the number of non-overlapping occurrences of substring sub in\n\
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2110 string S[start:end]. Optional arguments start and end are interpreted\n\
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2111 as in slice notation.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2112
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2113 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2114 string_count(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2115 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2116 PyObject *sub_obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2117 const char *str = PyString_AS_STRING(self), *sub;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2118 Py_ssize_t sub_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2119 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2120
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
2121 if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2122 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2123
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2124 if (PyString_Check(sub_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2125 sub = PyString_AS_STRING(sub_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2126 sub_len = PyString_GET_SIZE(sub_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2127 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2128 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2129 else if (PyUnicode_Check(sub_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2130 Py_ssize_t count;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2131 count = PyUnicode_Count((PyObject *)self, sub_obj, start, end);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2132 if (count == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2133 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2134 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2135 return PyInt_FromSsize_t(count);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2136 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2137 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2138 else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2139 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2140
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2141 ADJUST_INDICES(start, end, PyString_GET_SIZE(self));
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2142
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2143 return PyInt_FromSsize_t(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2144 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2145 );
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2146 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2147
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2148 PyDoc_STRVAR(swapcase__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2149 "S.swapcase() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2150 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2151 Return a copy of the string S with uppercase characters\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2152 converted to lowercase and vice versa.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2153
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2154 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2155 string_swapcase(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2156 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2157 char *s = PyString_AS_STRING(self), *s_new;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2158 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2159 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2160
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2161 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2162 if (newobj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2163 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2164 s_new = PyString_AsString(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2165 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2166 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2167 if (islower(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2168 *s_new = toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2169 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2170 else if (isupper(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2171 *s_new = tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2172 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2173 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2174 *s_new = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2175 s_new++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2176 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2177 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2178 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2179
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2180
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2181 PyDoc_STRVAR(translate__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2182 "S.translate(table [,deletechars]) -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2183 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2184 Return a copy of the string S, where all characters occurring\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2185 in the optional argument deletechars are removed, and the\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2186 remaining characters have been mapped through the given\n\
70947
60b3a5615c4e Issue #12164: Document (in docstring) that str.translate accepts None for the first argument.
Mark Dickinson <mdickinson@enthought.com>
parents: 69562
diff changeset
2187 translation table, which must be a string of length 256 or None.\n\
60b3a5615c4e Issue #12164: Document (in docstring) that str.translate accepts None for the first argument.
Mark Dickinson <mdickinson@enthought.com>
parents: 69562
diff changeset
2188 If the table argument is None, no translation is applied and\n\
60b3a5615c4e Issue #12164: Document (in docstring) that str.translate accepts None for the first argument.
Mark Dickinson <mdickinson@enthought.com>
parents: 69562
diff changeset
2189 the operation simply removes the characters in deletechars.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2190
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2191 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2192 string_translate(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2193 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2194 register char *input, *output;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2195 const char *table;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2196 register Py_ssize_t i, c, changed = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2197 PyObject *input_obj = (PyObject*)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2198 const char *output_start, *del_table=NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2199 Py_ssize_t inlen, tablen, dellen = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2200 PyObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2201 int trans_table[256];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2202 PyObject *tableobj, *delobj = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2203
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2204 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2205 &tableobj, &delobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2206 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2207
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2208 if (PyString_Check(tableobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2209 table = PyString_AS_STRING(tableobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2210 tablen = PyString_GET_SIZE(tableobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2211 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2212 else if (tableobj == Py_None) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2213 table = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2214 tablen = 256;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2215 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2216 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2217 else if (PyUnicode_Check(tableobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2218 /* Unicode .translate() does not support the deletechars
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2219 parameter; instead a mapping to None will cause characters
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2220 to be deleted. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2221 if (delobj != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2222 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2223 "deletions are implemented differently for unicode");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2224 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2225 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2226 return PyUnicode_Translate((PyObject *)self, tableobj, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2227 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2228 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2229 else if (PyObject_AsCharBuffer(tableobj, &table, &tablen))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2230 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2231
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2232 if (tablen != 256) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2233 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2234 "translation table must be 256 characters long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2235 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2236 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2237
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2238 if (delobj != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2239 if (PyString_Check(delobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2240 del_table = PyString_AS_STRING(delobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2241 dellen = PyString_GET_SIZE(delobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2242 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2243 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2244 else if (PyUnicode_Check(delobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2245 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2246 "deletions are implemented differently for unicode");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2247 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2248 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2249 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2250 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2251 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2252 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2253 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2254 del_table = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2255 dellen = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2256 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2257
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2258 inlen = PyString_GET_SIZE(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2259 result = PyString_FromStringAndSize((char *)NULL, inlen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2260 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2261 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2262 output_start = output = PyString_AsString(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2263 input = PyString_AS_STRING(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2264
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2265 if (dellen == 0 && table != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2266 /* If no deletions are required, use faster code */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2267 for (i = inlen; --i >= 0; ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2268 c = Py_CHARMASK(*input++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2269 if (Py_CHARMASK((*output++ = table[c])) != c)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2270 changed = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2271 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2272 if (changed || !PyString_CheckExact(input_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2273 return result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2274 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2275 Py_INCREF(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2276 return input_obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2277 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2278
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2279 if (table == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2280 for (i = 0; i < 256; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2281 trans_table[i] = Py_CHARMASK(i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2282 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2283 for (i = 0; i < 256; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2284 trans_table[i] = Py_CHARMASK(table[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2285 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2286
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2287 for (i = 0; i < dellen; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2288 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2289
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2290 for (i = inlen; --i >= 0; ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2291 c = Py_CHARMASK(*input++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2292 if (trans_table[c] != -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2293 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2294 continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2295 changed = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2296 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2297 if (!changed && PyString_CheckExact(input_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2298 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2299 Py_INCREF(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2300 return input_obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2301 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2302 /* Fix the size of the resulting string */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2303 if (inlen > 0 && _PyString_Resize(&result, output - output_start))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2304 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2305 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2306 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2307
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2308
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2309 /* find and count characters and substrings */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2310
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2311 #define findchar(target, target_len, c) \
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2312 ((char *)memchr((const void *)(target), c, target_len))
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2313
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2314 /* String ops must return a string. */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2315 /* If the object is subclass of string, create a copy */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2316 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2317 return_self(PyStringObject *self)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2318 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2319 if (PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2320 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2321 return self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2322 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2323 return (PyStringObject *)PyString_FromStringAndSize(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2324 PyString_AS_STRING(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2325 PyString_GET_SIZE(self));
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2326 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2327
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
2328 Py_LOCAL_INLINE(Py_ssize_t)
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38738
diff changeset
2329 countchar(const char *target, int target_len, char c, Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2330 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2331 Py_ssize_t count=0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2332 const char *start=target;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2333 const char *end=target+target_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2334
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2335 while ( (start=findchar(start, end-start, c)) != NULL ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2336 count++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2337 if (count >= maxcount)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2338 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2339 start += 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2340 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2341 return count;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2342 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2343
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2344
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
2345 /* Algorithms for different cases of string replacement */
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2346
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2347 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2348 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2349 replace_interleave(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2350 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2351 Py_ssize_t maxcount)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2352 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2353 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2354 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2355 Py_ssize_t count, i, product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2356 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2357
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2358 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2359
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2360 /* 1 at the end plus 1 after every character */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2361 count = self_len+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2362 if (maxcount < count)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2363 count = maxcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2364
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2365 /* Check for overflow */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2366 /* result_len = count * to_len + self_len; */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2367 product = count * to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2368 if (product / to_len != count) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2369 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2370 "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2371 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2372 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2373 result_len = product + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2374 if (result_len < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2375 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2376 "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2377 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2378 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2379
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2380 if (! (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2381 PyString_FromStringAndSize(NULL, result_len)) )
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2382 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2383
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2384 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2385 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2386
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2387 /* TODO: special case single character, which doesn't need memcpy */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2388
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2389 /* Lay the first one down (guaranteed this will occur) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2390 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2391 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2392 count -= 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2393
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2394 for (i=0; i<count; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2395 *result_s++ = *self_s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2396 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2397 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2398 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2399
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2400 /* Copy the rest of the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2401 Py_MEMCPY(result_s, self_s, self_len-i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2402
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2403 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2404 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2405
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2406 /* Special case for deleting a single character */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2407 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2408 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2409 replace_delete_single_character(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2410 char from_c, Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2411 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2412 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2413 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2414 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2415 Py_ssize_t count;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2416 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2417
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2418 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2419 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2420
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2421 count = countchar(self_s, self_len, from_c, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2422 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2423 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2424 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2425
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2426 result_len = self_len - count; /* from_len == 1 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2427 assert(result_len>=0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2428
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2429 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2430 PyString_FromStringAndSize(NULL, result_len)) == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2431 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2432 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2433
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2434 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2435 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2436 while (count-- > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2437 next = findchar(start, end-start, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2438 if (next == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2439 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2440 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2441 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2442 start = next+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2443 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2444 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2445
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2446 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2447 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2448
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2449 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2450
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2451 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2452 replace_delete_substring(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2453 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2454 Py_ssize_t maxcount) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2455 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2456 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2457 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2458 Py_ssize_t count, offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2459 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2460
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2461 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2462 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2463
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2464 count = stringlib_count(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2465 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2466 maxcount);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2467
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2468 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2469 /* no matches */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2470 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2471 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2472
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2473 result_len = self_len - (count * from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2474 assert (result_len>=0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2475
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2476 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2477 PyString_FromStringAndSize(NULL, result_len)) == NULL )
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2478 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2479
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2480 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2481
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2482 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2483 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2484 while (count-- > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2485 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2486 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2487 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2488 if (offset == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2489 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2490 next = start + offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2491
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2492 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2493
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2494 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2495 start = next+from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2496 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2497 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2498 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2499 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2500
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2501 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2502 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2503 replace_single_character_in_place(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2504 char from_c, char to_c,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2505 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2506 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2507 char *self_s, *result_s, *start, *end, *next;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2508 Py_ssize_t self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2509 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2510
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2511 /* The result string will be the same size */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2512 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2513 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2514
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2515 next = findchar(self_s, self_len, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2516
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2517 if (next == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2518 /* No matches; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2519 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2520 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2521
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2522 /* Need to make a new string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2523 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2524 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2525 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2526 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2527 Py_MEMCPY(result_s, self_s, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2528
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2529 /* change everything in-place, starting with this one */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2530 start = result_s + (next-self_s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2531 *start = to_c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2532 start++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2533 end = result_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2534
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2535 while (--maxcount > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2536 next = findchar(start, end-start, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2537 if (next == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2538 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2539 *next = to_c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2540 start = next+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2541 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2542
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2543 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2544 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2545
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2546 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2547 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2548 replace_substring_in_place(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2549 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2550 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2551 Py_ssize_t maxcount)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2552 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2553 char *result_s, *start, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2554 char *self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2555 Py_ssize_t self_len, offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2556 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2557
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2558 /* The result string will be the same size */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2559
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2560 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2561 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2562
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2563 offset = stringlib_find(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2564 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2565 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2566 if (offset == -1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2567 /* No matches; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2568 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2569 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2570
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2571 /* Need to make a new string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2572 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2573 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2574 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2575 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2576 Py_MEMCPY(result_s, self_s, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2577
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2578 /* change everything in-place, starting with this one */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2579 start = result_s + offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2580 Py_MEMCPY(start, to_s, from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2581 start += from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2582 end = result_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2583
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2584 while ( --maxcount > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2585 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2586 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2587 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2588 if (offset==-1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2589 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2590 Py_MEMCPY(start+offset, to_s, from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2591 start += offset+from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2592 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2593
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2594 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2595 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2596
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2597 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2598 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2599 replace_single_character(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2600 char from_c,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2601 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2602 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2603 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2604 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2605 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2606 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2607 Py_ssize_t count, product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2608 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2609
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2610 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2611 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2612
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2613 count = countchar(self_s, self_len, from_c, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2614 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2615 /* no matches, return unchanged */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2616 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2617 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2618
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2619 /* use the difference between current and new, hence the "-1" */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2620 /* result_len = self_len + count * (to_len-1) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2621 product = count * (to_len-1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2622 if (product / (to_len-1) != count) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2623 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2624 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2625 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2626 result_len = self_len + product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2627 if (result_len < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2628 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2629 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2630 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2631
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2632 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2633 PyString_FromStringAndSize(NULL, result_len)) == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2634 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2635 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2636
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2637 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2638 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2639 while (count-- > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2640 next = findchar(start, end-start, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2641 if (next == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2642 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2643
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2644 if (next == start) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2645 /* replace with the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2646 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2647 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2648 start += 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2649 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2650 /* copy the unchanged old then the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2651 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2652 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2653 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2654 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2655 start = next+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2656 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2657 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2658 /* Copy the remainder of the remaining string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2659 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2660
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2661 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2662 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2663
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2664 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2665 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2666 replace_substring(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2667 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2668 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2669 Py_ssize_t maxcount) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2670 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2671 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2672 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2673 Py_ssize_t count, offset, product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2674 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2675
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2676 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2677 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2678
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2679 count = stringlib_count(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2680 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2681 maxcount);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2682
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2683 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2684 /* no matches, return unchanged */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2685 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2686 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2687
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2688 /* Check for overflow */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2689 /* result_len = self_len + count * (to_len-from_len) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2690 product = count * (to_len-from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2691 if (product / (to_len-from_len) != count) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2692 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2693 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2694 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2695 result_len = self_len + product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2696 if (result_len < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2697 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2698 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2699 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2700
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2701 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2702 PyString_FromStringAndSize(NULL, result_len)) == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2703 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2704 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2705
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2706 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2707 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2708 while (count-- > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2709 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2710 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2711 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2712 if (offset == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2713 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2714 next = start+offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2715 if (next == start) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2716 /* replace with the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2717 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2718 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2719 start += from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2720 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2721 /* copy the unchanged old then the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2722 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2723 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2724 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2725 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2726 start = next+from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2727 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2728 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2729 /* Copy the remainder of the remaining string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2730 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2731
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2732 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2733 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2734
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2735
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2736 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2737 replace(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2738 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2739 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2740 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2741 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2742 if (maxcount < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2743 maxcount = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2744 } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2745 /* nothing to do; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2746 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2747 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2748
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2749 if (maxcount == 0 ||
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2750 (from_len == 0 && to_len == 0)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2751 /* nothing to do; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2752 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2753 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2754
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2755 /* Handle zero-length special cases */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2756
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2757 if (from_len == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2758 /* insert the 'to' string everywhere. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2759 /* >>> "Python".replace("", ".") */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2760 /* '.P.y.t.h.o.n.' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2761 return replace_interleave(self, to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2762 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2763
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2764 /* Except for "".replace("", "A") == "A" there is no way beyond this */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2765 /* point for an empty self string to generate a non-empty string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2766 /* Special case so the remaining code always gets a non-empty string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2767 if (PyString_GET_SIZE(self) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2768 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2769 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2770
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2771 if (to_len == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2772 /* delete all occurances of 'from' string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2773 if (from_len == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2774 return replace_delete_single_character(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2775 self, from_s[0], maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2776 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2777 return replace_delete_substring(self, from_s, from_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2778 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2779 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2780
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2781 /* Handle special case where both strings have the same length */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2782
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2783 if (from_len == to_len) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2784 if (from_len == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2785 return replace_single_character_in_place(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2786 self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2787 from_s[0],
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2788 to_s[0],
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2789 maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2790 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2791 return replace_substring_in_place(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2792 self, from_s, from_len, to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2793 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2794 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2795
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2796 /* Otherwise use the more generic algorithms */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2797 if (from_len == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2798 return replace_single_character(self, from_s[0],
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2799 to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2800 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2801 /* len('from')>=2, len('to')>=1 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2802 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2803 }
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2804 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2805
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2806 PyDoc_STRVAR(replace__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2807 "S.replace(old, new[, count]) -> string\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2808 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2809 Return a copy of string S with all occurrences of substring\n\
30751
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2810 old replaced by new. If the optional argument count is\n\
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2811 given, only the first count occurrences are replaced.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2812
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2813 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2814 string_replace(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2815 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2816 Py_ssize_t count = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2817 PyObject *from, *to;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2818 const char *from_s, *to_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2819 Py_ssize_t from_len, to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2820
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2821 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2822 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2823
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2824 if (PyString_Check(from)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2825 from_s = PyString_AS_STRING(from);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2826 from_len = PyString_GET_SIZE(from);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2827 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2828 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2829 if (PyUnicode_Check(from))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2830 return PyUnicode_Replace((PyObject *)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2831 from, to, count);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2832 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2833 else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2834 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2835
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2836 if (PyString_Check(to)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2837 to_s = PyString_AS_STRING(to);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2838 to_len = PyString_GET_SIZE(to);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2839 }
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2840 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2841 else if (PyUnicode_Check(to))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2842 return PyUnicode_Replace((PyObject *)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2843 from, to, count);
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2844 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2845 else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2846 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2847
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2848 return (PyObject *)replace((PyStringObject *) self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2849 from_s, from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2850 to_s, to_len, count);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2851 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2852
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2853 /** End DALKE **/
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2854
38738
0a8a392e1fae Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
2855 /* Matches the end (direction >= 0) or start (direction < 0) of self
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2856 * against substr, using the start and end arguments. Returns
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2857 * -1 on error, 0 if not found and 1 if found.
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2858 */
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2859 Py_LOCAL(int)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2860 _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2861 Py_ssize_t end, int direction)
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2862 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2863 Py_ssize_t len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2864 Py_ssize_t slen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2865 const char* sub;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2866 const char* str;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2867
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2868 if (PyString_Check(substr)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2869 sub = PyString_AS_STRING(substr);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2870 slen = PyString_GET_SIZE(substr);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2871 }
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2872 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2873 else if (PyUnicode_Check(substr))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2874 return PyUnicode_Tailmatch((PyObject *)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2875 substr, start, end, direction);
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2876 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2877 else if (PyObject_AsCharBuffer(substr, &sub, &slen))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2878 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2879 str = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2880
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2881 ADJUST_INDICES(start, end, len);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2882
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2883 if (direction < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2884 /* startswith */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2885 if (start+slen > len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2886 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2887 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2888 /* endswith */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2889 if (end-start < slen || start > len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2890 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2891
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2892 if (end-slen > start)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2893 start = end - slen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2894 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2895 if (end-start >= slen)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2896 return ! memcmp(str+start, sub, slen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2897 return 0;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2898 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2899
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2900
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2901 PyDoc_STRVAR(startswith__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2902 "S.startswith(prefix[, start[, end]]) -> bool\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2903 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2904 Return True if S starts with the specified prefix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2905 With optional start, test S beginning at that position.\n\
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2906 With optional end, stop comparing S at that position.\n\
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2907 prefix can also be a tuple of strings to try.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2908
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2909 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2910 string_startswith(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2911 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2912 Py_ssize_t start = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2913 Py_ssize_t end = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2914 PyObject *subobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2915 int result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2916
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
2917 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2918 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2919 if (PyTuple_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2920 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2921 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2922 result = _string_tailmatch(self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2923 PyTuple_GET_ITEM(subobj, i),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2924 start, end, -1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2925 if (result == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2926 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2927 else if (result) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2928 Py_RETURN_TRUE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2929 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2930 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2931 Py_RETURN_FALSE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2932 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2933 result = _string_tailmatch(self, subobj, start, end, -1);
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2934 if (result == -1) {
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2935 if (PyErr_ExceptionMatches(PyExc_TypeError))
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2936 PyErr_Format(PyExc_TypeError, "startswith first arg must be str, "
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2937 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2938 return NULL;
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2939 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2940 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2941 return PyBool_FromLong(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2942 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2943
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2944
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2945 PyDoc_STRVAR(endswith__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2946 "S.endswith(suffix[, start[, end]]) -> bool\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2947 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2948 Return True if S ends with the specified suffix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2949 With optional start, test S beginning at that position.\n\
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2950 With optional end, stop comparing S at that position.\n\
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2951 suffix can also be a tuple of strings to try.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2952
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2953 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2954 string_endswith(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2955 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2956 Py_ssize_t start = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2957 Py_ssize_t end = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2958 PyObject *subobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2959 int result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2960
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
2961 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2962 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2963 if (PyTuple_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2964 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2965 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2966 result = _string_tailmatch(self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2967 PyTuple_GET_ITEM(subobj, i),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2968 start, end, +1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2969 if (result == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2970 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2971 else if (result) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2972 Py_RETURN_TRUE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2973 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2974 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2975 Py_RETURN_FALSE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2976 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2977 result = _string_tailmatch(self, subobj, start, end, +1);
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2978 if (result == -1) {
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2979 if (PyErr_ExceptionMatches(PyExc_TypeError))
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2980 PyErr_Format(PyExc_TypeError, "endswith first arg must be str, "
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2981 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2982 return NULL;
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2983 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2984 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2985 return PyBool_FromLong(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2986 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2987
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2988
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2989 PyDoc_STRVAR(encode__doc__,
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2990 "S.encode([encoding[,errors]]) -> object\n\
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2991 \n\
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2992 Encodes S using the codec registered for encoding. encoding defaults\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2993 to the default encoding. errors may be given to set a different error\n\
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2994 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2995 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2996 'xmlcharrefreplace' as well as any other name registered with\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2997 codecs.register_error that is able to handle UnicodeEncodeErrors.");
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2998
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2999 static PyObject *
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3000 string_encode(PyStringObject *self, PyObject *args, PyObject *kwargs)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3001 {
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3002 static char *kwlist[] = {"encoding", "errors", 0};
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3003 char *encoding = NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3004 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3005 PyObject *v;
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
3006
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3007 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3008 kwlist, &encoding, &errors))
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3009 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3010 v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3011 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3012 goto onError;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3013 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3014 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3015 "encoder did not return a string/unicode object "
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3016 "(type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
3017 Py_TYPE(v)->tp_name);
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3018 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3019 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3020 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3021 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3022
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3023 onError:
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3024 return NULL;
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3025 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3026
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3027
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3028 PyDoc_STRVAR(decode__doc__,
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3029 "S.decode([encoding[,errors]]) -> object\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3030 \n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3031 Decodes S using the codec registered for encoding. encoding defaults\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3032 to the default encoding. errors may be given to set a different error\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3033 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3034 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3035 as well as any other name registered with codecs.register_error that is\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3036 able to handle UnicodeDecodeErrors.");
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3037
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3038 static PyObject *
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3039 string_decode(PyStringObject *self, PyObject *args, PyObject *kwargs)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3040 {
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3041 static char *kwlist[] = {"encoding", "errors", 0};
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3042 char *encoding = NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3043 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3044 PyObject *v;
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
3045
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3046 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3047 kwlist, &encoding, &errors))
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3048 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3049 v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3050 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3051 goto onError;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3052 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3053 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3054 "decoder did not return a string/unicode object "
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3055 "(type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
3056 Py_TYPE(v)->tp_name);
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3057 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3058 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3059 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3060 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3061
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3062 onError:
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3063 return NULL;
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3064 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3065
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3066
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3067 PyDoc_STRVAR(expandtabs__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3068 "S.expandtabs([tabsize]) -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3069 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3070 Return a copy of S where all tab characters are expanded using spaces.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3071 If tabsize is not given, a tab size of 8 characters is assumed.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3072
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3073 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3074 string_expandtabs(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3075 {
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3076 const char *e, *p, *qe;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3077 char *q;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3078 Py_ssize_t i, j, incr;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3079 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3080 int tabsize = 8;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3081
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3082 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3083 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3084
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
3085 /* First pass: determine size of output string */
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3086 i = 0; /* chars up to and including most recent \n or \r */
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3087 j = 0; /* chars since most recent \n or \r (use in tab calculations) */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3088 e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); /* end of input */
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3089 for (p = PyString_AS_STRING(self); p < e; p++)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3090 if (*p == '\t') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3091 if (tabsize > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3092 incr = tabsize - (j % tabsize);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3093 if (j > PY_SSIZE_T_MAX - incr)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3094 goto overflow1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3095 j += incr;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3096 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3097 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3098 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3099 if (j > PY_SSIZE_T_MAX - 1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3100 goto overflow1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3101 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3102 if (*p == '\n' || *p == '\r') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3103 if (i > PY_SSIZE_T_MAX - j)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3104 goto overflow1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3105 i += j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3106 j = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3107 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3108 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3109
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3110 if (i > PY_SSIZE_T_MAX - j)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3111 goto overflow1;
42004
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41440
diff changeset
3112
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3113 /* Second pass: create output string and fill it */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3114 u = PyString_FromStringAndSize(NULL, i + j);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3115 if (!u)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3116 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3117
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3118 j = 0; /* same as in first pass */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3119 q = PyString_AS_STRING(u); /* next output char */
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3120 qe = PyString_AS_STRING(u) + PyString_GET_SIZE(u); /* end of output */
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3121
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3122 for (p = PyString_AS_STRING(self); p < e; p++)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3123 if (*p == '\t') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3124 if (tabsize > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3125 i = tabsize - (j % tabsize);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3126 j += i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3127 while (i--) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3128 if (q >= qe)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3129 goto overflow2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3130 *q++ = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3131 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3132 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3133 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3134 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3135 if (q >= qe)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3136 goto overflow2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3137 *q++ = *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3138 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3139 if (*p == '\n' || *p == '\r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3140 j = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3141 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3142
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3143 return u;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3144
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3145 overflow2:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3146 Py_DECREF(u);
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3147 overflow1:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3148 PyErr_SetString(PyExc_OverflowError, "new string is too long");
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3149 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3150 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3151
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
3152 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3153 pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3154 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3155 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3156
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3157 if (left < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3158 left = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3159 if (right < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3160 right = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3161
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3162 if (left == 0 && right == 0 && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3163 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3164 return (PyObject *)self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3165 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3166
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3167 u = PyString_FromStringAndSize(NULL,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3168 left + PyString_GET_SIZE(self) + right);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3169 if (u) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3170 if (left)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3171 memset(PyString_AS_STRING(u), fill, left);
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3172 Py_MEMCPY(PyString_AS_STRING(u) + left,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3173 PyString_AS_STRING(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3174 PyString_GET_SIZE(self));
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3175 if (right)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3176 memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self),
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3177 fill, right);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3178 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3179
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3180 return u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3181 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3182
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3183 PyDoc_STRVAR(ljust__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3184 "S.ljust(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
3185 "\n"
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3186 "Return S left-justified in a string of length width. Padding is\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3187 "done using the specified fill character (default is a space).");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3188
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3189 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3190 string_ljust(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3191 {
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3192 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3193 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3194
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3195 if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3196 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3197
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3198 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3199 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3200 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3201 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3202
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3203 return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3204 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3205
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3206
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3207 PyDoc_STRVAR(rjust__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3208 "S.rjust(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
3209 "\n"
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3210 "Return S right-justified in a string of length width. Padding is\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3211 "done using the specified fill character (default is a space)");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3212
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3213 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3214 string_rjust(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3215 {
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3216 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3217 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3218
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3219 if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3220 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3221
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3222 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3223 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3224 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3225 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3226
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3227 return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3228 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3229
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3230
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3231 PyDoc_STRVAR(center__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3232 "S.center(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
3233 "\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3234 "Return S centered in a string of length width. Padding is\n"
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3235 "done using the specified fill character (default is a space)");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3236
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3237 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3238 string_center(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3239 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
3240 Py_ssize_t marg, left;
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3241 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3242 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3243
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3244 if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3245 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3246
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3247 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3248 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3249 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3250 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3251
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3252 marg = width - PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3253 left = marg / 2 + (marg & width & 1);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3254
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3255 return pad(self, left, marg - left, fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3256 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3257
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3258 PyDoc_STRVAR(zfill__doc__,
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3259 "S.zfill(width) -> string\n"
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3260 "\n"
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3261 "Pad a numeric string S with zeros on the left, to fill a field\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3262 "of the specified width. The string S is never truncated.");
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3263
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3264 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3265 string_zfill(PyStringObject *self, PyObject *args)
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3266 {
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
3267 Py_ssize_t fill;
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3268 PyObject *s;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3269 char *p;
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3270 Py_ssize_t width;
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3271
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3272 if (!PyArg_ParseTuple(args, "n:zfill", &width))
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3273 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3274
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3275 if (PyString_GET_SIZE(self) >= width) {
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3276 if (PyString_CheckExact(self)) {
23051
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3277 Py_INCREF(self);
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3278 return (PyObject*) self;
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3279 }
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3280 else
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3281 return PyString_FromStringAndSize(
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3282 PyString_AS_STRING(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3283 PyString_GET_SIZE(self)
23051
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3284 );
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3285 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3286
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3287 fill = width - PyString_GET_SIZE(self);
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3288
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3289 s = pad(self, fill, 0, '0');
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3290
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3291 if (s == NULL)
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3292 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3293
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3294 p = PyString_AS_STRING(s);
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3295 if (p[fill] == '+' || p[fill] == '-') {
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3296 /* move sign to beginning of string */
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3297 p[0] = p[fill];
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3298 p[fill] = '0';
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3299 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3300
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3301 return (PyObject*) s;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3302 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3303
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3304 PyDoc_STRVAR(isspace__doc__,
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3305 "S.isspace() -> bool\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3306 \n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3307 Return True if all characters in S are whitespace\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3308 and there is at least one character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3309
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3310 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3311 string_isspace(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3312 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3313 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3314 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3315 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3316
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3317 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3318 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3319 isspace(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3320 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3321
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3322 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3323 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3324 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3325
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3326 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3327 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3328 if (!isspace(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3329 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3330 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3331 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3332 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3333
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3334
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3335 PyDoc_STRVAR(isalpha__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3336 "S.isalpha() -> bool\n\
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3337 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3338 Return True if all characters in S are alphabetic\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3339 and there is at least one character in S, False otherwise.");
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3340
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3341 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3342 string_isalpha(PyStringObject *self)
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3343 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3344 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3345 = (unsigned char *) PyString_AS_STRING(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3346 register const unsigned char *e;
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3347
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3348 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3349 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3350 isalpha(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3351 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3352
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3353 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3354 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3355 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3356
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3357 e = p + PyString_GET_SIZE(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3358 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3359 if (!isalpha(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3360 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3361 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3362 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3363 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3364
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3365
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3366 PyDoc_STRVAR(isalnum__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3367 "S.isalnum() -> bool\n\
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3368 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3369 Return True if all characters in S are alphanumeric\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3370 and there is at least one character in S, False otherwise.");
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3371
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3372 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3373 string_isalnum(PyStringObject *self)
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3374 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3375 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3376 = (unsigned char *) PyString_AS_STRING(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3377 register const unsigned char *e;
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3378
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3379 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3380 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3381 isalnum(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3382 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3383
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3384 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3385 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3386 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3387
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3388 e = p + PyString_GET_SIZE(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3389 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3390 if (!isalnum(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3391 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3392 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3393 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3394 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3395
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3396
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3397 PyDoc_STRVAR(isdigit__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3398 "S.isdigit() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3399 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3400 Return True if all characters in S are digits\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3401 and there is at least one character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3402
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3403 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3404 string_isdigit(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3405 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3406 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3407 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3408 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3409
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3410 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3411 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3412 isdigit(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3413 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3414
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3415 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3416 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3417 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3418
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3419 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3420 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3421 if (!isdigit(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3422 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3423 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3424 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3425 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3426
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3427
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3428 PyDoc_STRVAR(islower__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3429 "S.islower() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3430 \n\
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3431 Return True if all cased characters in S are lowercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3432 at least one cased character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3433
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3434 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3435 string_islower(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3436 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3437 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3438 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3439 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3440 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3441
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3442 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3443 if (PyString_GET_SIZE(self) == 1)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3444 return PyBool_FromLong(islower(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3445
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3446 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3447 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3448 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3449
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3450 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3451 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3452 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3453 if (isupper(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3454 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3455 else if (!cased && islower(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3456 cased = 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3457 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3458 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3459 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3460
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3461
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3462 PyDoc_STRVAR(isupper__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3463 "S.isupper() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3464 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3465 Return True if all cased characters in S are uppercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3466 at least one cased character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3467
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3468 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3469 string_isupper(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3470 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3471 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3472 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3473 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3474 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3475
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3476 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3477 if (PyString_GET_SIZE(self) == 1)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3478 return PyBool_FromLong(isupper(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3479
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3480 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3481 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3482 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3483
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3484 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3485 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3486 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3487 if (islower(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3488 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3489 else if (!cased && isupper(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3490 cased = 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3491 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3492 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3493 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3494
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3495
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3496 PyDoc_STRVAR(istitle__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3497 "S.istitle() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3498 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3499 Return True if S is a titlecased string and there is at least one\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3500 character in S, i.e. uppercase characters may only follow uncased\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3501 characters and lowercase characters only cased ones. Return False\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3502 otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3503
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3504 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3505 string_istitle(PyStringObject *self, PyObject *uncased)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3506 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3507 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3508 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3509 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3510 int cased, previous_is_cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3511
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3512 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3513 if (PyString_GET_SIZE(self) == 1)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3514 return PyBool_FromLong(isupper(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3515
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3516 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3517 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3518 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3519
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3520 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3521 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3522 previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3523 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3524 register const unsigned char ch = *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3525
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3526 if (isupper(ch)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3527 if (previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3528 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3529 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3530 cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3531 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3532 else if (islower(ch)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3533 if (!previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3534 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3535 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3536 cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3537 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3538 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3539 previous_is_cased = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3540 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3541 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3542 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3543
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3544
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3545 PyDoc_STRVAR(splitlines__doc__,
20415
621c7703e8b7 Remove extra "]" in splitlines() docstring.
Fred Drake <fdrake@acm.org>
parents: 20277
diff changeset
3546 "S.splitlines([keepends]) -> list of strings\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3547 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3548 Return a list of the lines in S, breaking at line boundaries.\n\
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3549 Line breaks are not included in the resulting list unless keepends\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3550 is given and true.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3551
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3552 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3553 string_splitlines(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3554 {
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3555 int keepends = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3556
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3557 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3558 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3559
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
3560 return stringlib_splitlines(
62208
ba2f2d08504b Issue #8930: fix some C code indentation
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
3561 (PyObject*) self, PyString_AS_STRING(self), PyString_GET_SIZE(self),
ba2f2d08504b Issue #8930: fix some C code indentation
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
3562 keepends
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3563 );
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3564 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3565
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3566 PyDoc_STRVAR(sizeof__doc__,
47421
a65f095b27f3 Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents: 47419
diff changeset
3567 "S.__sizeof__() -> size of S in memory, in bytes");
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3568
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3569 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3570 string_sizeof(PyStringObject *v)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3571 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3572 Py_ssize_t res;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3573 res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3574 return PyInt_FromSsize_t(res);
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3575 }
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3576
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3577 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3578 string_getnewargs(PyStringObject *v)
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3579 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3580 return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v));
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3581 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3582
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3583
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3584 #include "stringlib/string_format.h"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3585
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3586 PyDoc_STRVAR(format__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3587 "S.format(*args, **kwargs) -> string\n\
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3588 \n\
66044
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3589 Return a formatted version of S, using substitutions from args and kwargs.\n\
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3590 The substitutions are identified by braces ('{' and '}').");
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3591
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3592 static PyObject *
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3593 string__format__(PyObject* self, PyObject* args)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3594 {
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3595 PyObject *format_spec;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3596 PyObject *result = NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3597 PyObject *tmp = NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3598
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3599 /* If 2.x, convert format_spec to the same type as value */
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3600 /* This is to allow things like u''.format('') */
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3601 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3602 goto done;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3603 if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) {
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3604 PyErr_Format(PyExc_TypeError, "__format__ arg must be str "
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3605 "or unicode, not %s", Py_TYPE(format_spec)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3606 goto done;
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3607 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3608 tmp = PyObject_Str(format_spec);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3609 if (tmp == NULL)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3610 goto done;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3611 format_spec = tmp;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3612
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3613 result = _PyBytes_FormatAdvanced(self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3614 PyString_AS_STRING(format_spec),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3615 PyString_GET_SIZE(format_spec));
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3616 done:
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3617 Py_XDECREF(tmp);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3618 return result;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3619 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3620
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3621 PyDoc_STRVAR(p_format__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3622 "S.__format__(format_spec) -> string\n\
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3623 \n\
66044
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3624 Return a formatted version of S as described by format_spec.");
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3625
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3626
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3627 static PyMethodDef
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3628 string_methods[] = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3629 /* Counterparts of the obsolete stropmodule functions; except
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3630 string.maketrans(). */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3631 {"join", (PyCFunction)string_join, METH_O, join__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3632 {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3633 {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3634 {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3635 {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3636 {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3637 {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3638 {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3639 {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3640 {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3641 {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3642 {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3643 {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3644 capitalize__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3645 {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3646 {"endswith", (PyCFunction)string_endswith, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3647 endswith__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3648 {"partition", (PyCFunction)string_partition, METH_O, partition__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3649 {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3650 {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3651 {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3652 {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3653 {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3654 {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3655 {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3656 {"rpartition", (PyCFunction)string_rpartition, METH_O,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3657 rpartition__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3658 {"startswith", (PyCFunction)string_startswith, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3659 startswith__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3660 {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3661 {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3662 swapcase__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3663 {"translate", (PyCFunction)string_translate, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3664 translate__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3665 {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3666 {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3667 {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3668 {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3669 {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3670 {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3671 {"__format__", (PyCFunction) string__format__, METH_VARARGS, p_format__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3672 {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3673 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3674 {"encode", (PyCFunction)string_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3675 {"decode", (PyCFunction)string_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__},
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3676 {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3677 expandtabs__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3678 {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3679 splitlines__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3680 {"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3681 sizeof__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3682 {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3683 {NULL, NULL} /* sentinel */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3684 };
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3685
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24184
diff changeset
3686 static PyObject *
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3687 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3688
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3689 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3690 string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3691 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3692 PyObject *x = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3693 static char *kwlist[] = {"object", 0};
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3694
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3695 if (type != &PyString_Type)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3696 return str_subtype_new(type, args, kwds);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3697 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3698 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3699 if (x == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3700 return PyString_FromString("");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3701 return PyObject_Str(x);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3702 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3703
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3704 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3705 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3706 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3707 PyObject *tmp, *pnew;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3708 Py_ssize_t n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3709
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3710 assert(PyType_IsSubtype(type, &PyString_Type));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3711 tmp = string_new(&PyString_Type, args, kwds);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3712 if (tmp == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3713 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3714 assert(PyString_CheckExact(tmp));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3715 n = PyString_GET_SIZE(tmp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3716 pnew = type->tp_alloc(type, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3717 if (pnew != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3718 Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3719 ((PyStringObject *)pnew)->ob_shash =
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3720 ((PyStringObject *)tmp)->ob_shash;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3721 ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3722 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3723 Py_DECREF(tmp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3724 return pnew;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3725 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3726
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3727 static PyObject *
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3728 basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3729 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3730 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3731 "The basestring type cannot be instantiated");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3732 return NULL;
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3733 }
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3734
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3735 static PyObject *
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3736 string_mod(PyObject *v, PyObject *w)
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3737 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3738 if (!PyString_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3739 Py_INCREF(Py_NotImplemented);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3740 return Py_NotImplemented;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3741 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3742 return PyString_Format(v, w);
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3743 }
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3744
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3745 PyDoc_STRVAR(basestring_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3746 "Type basestring cannot be instantiated; it is the base for str and unicode.");
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3747
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3748 static PyNumberMethods string_as_number = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3749 0, /*nb_add*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3750 0, /*nb_subtract*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3751 0, /*nb_multiply*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3752 0, /*nb_divide*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3753 string_mod, /*nb_remainder*/
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3754 };
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3755
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3756
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3757 PyTypeObject PyBaseString_Type = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3758 PyVarObject_HEAD_INIT(&PyType_Type, 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3759 "basestring",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3760 0,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3761 0,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3762 0, /* tp_dealloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3763 0, /* tp_print */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3764 0, /* tp_getattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3765 0, /* tp_setattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3766 0, /* tp_compare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3767 0, /* tp_repr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3768 0, /* tp_as_number */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3769 0, /* tp_as_sequence */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3770 0, /* tp_as_mapping */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3771 0, /* tp_hash */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3772 0, /* tp_call */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3773 0, /* tp_str */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3774 0, /* tp_getattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3775 0, /* tp_setattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3776 0, /* tp_as_buffer */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3777 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3778 basestring_doc, /* tp_doc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3779 0, /* tp_traverse */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3780 0, /* tp_clear */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3781 0, /* tp_richcompare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3782 0, /* tp_weaklistoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3783 0, /* tp_iter */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3784 0, /* tp_iternext */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3785 0, /* tp_methods */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3786 0, /* tp_members */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3787 0, /* tp_getset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3788 &PyBaseObject_Type, /* tp_base */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3789 0, /* tp_dict */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3790 0, /* tp_descr_get */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3791 0, /* tp_descr_set */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3792 0, /* tp_dictoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3793 0, /* tp_init */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3794 0, /* tp_alloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3795 basestring_new, /* tp_new */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3796 0, /* tp_free */
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3797 };
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3798
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3799 PyDoc_STRVAR(string_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3800 "str(object) -> string\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3801 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3802 Return a nice string representation of the object.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3803 If the argument is a string, the return value is the same object.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3804
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3805 PyTypeObject PyString_Type = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3806 PyVarObject_HEAD_INIT(&PyType_Type, 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3807 "str",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3808 PyStringObject_SIZE,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3809 sizeof(char),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3810 string_dealloc, /* tp_dealloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3811 (printfunc)string_print, /* tp_print */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3812 0, /* tp_getattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3813 0, /* tp_setattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3814 0, /* tp_compare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3815 string_repr, /* tp_repr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3816 &string_as_number, /* tp_as_number */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3817 &string_as_sequence, /* tp_as_sequence */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3818 &string_as_mapping, /* tp_as_mapping */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3819 (hashfunc)string_hash, /* tp_hash */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3820 0, /* tp_call */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3821 string_str, /* tp_str */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3822 PyObject_GenericGetAttr, /* tp_getattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3823 0, /* tp_setattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3824 &string_as_buffer, /* tp_as_buffer */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3825 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3826 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_STRING_SUBCLASS |
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3827 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3828 string_doc, /* tp_doc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3829 0, /* tp_traverse */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3830 0, /* tp_clear */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3831 (richcmpfunc)string_richcompare, /* tp_richcompare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3832 0, /* tp_weaklistoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3833 0, /* tp_iter */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3834 0, /* tp_iternext */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3835 string_methods, /* tp_methods */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3836 0, /* tp_members */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3837 0, /* tp_getset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3838 &PyBaseString_Type, /* tp_base */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3839 0, /* tp_dict */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3840 0, /* tp_descr_get */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3841 0, /* tp_descr_set */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3842 0, /* tp_dictoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3843 0, /* tp_init */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3844 0, /* tp_alloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3845 string_new, /* tp_new */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3846 PyObject_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3847 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3848
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3849 void
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3850 PyString_Concat(register PyObject **pv, register PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3851 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3852 register PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3853 if (*pv == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3854 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3855 if (w == NULL || !PyString_Check(*pv)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3856 Py_DECREF(*pv);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3857 *pv = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3858 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3859 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3860 v = string_concat((PyStringObject *) *pv, w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3861 Py_DECREF(*pv);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3862 *pv = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3863 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3864
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3865 void
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3866 PyString_ConcatAndDel(register PyObject **pv, register PyObject *w)
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3867 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3868 PyString_Concat(pv, w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3869 Py_XDECREF(w);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3870 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3871
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3872
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3873 /* The following function breaks the notion that strings are immutable:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3874 it changes the size of a string. We get away with this only if there
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3875 is only one module referencing the object. You can also think of it
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3876 as creating a new string object and destroying the old one, only
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3877 more efficiently. In any case, don't use this if the string may
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3878 already be known to some other part of the code...
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3879 Note that if there's not enough memory to resize the string, the original
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3880 string object at *pv is deallocated, *pv is set to NULL, an "out of
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3881 memory" exception is set, and -1 is returned. Else (on success) 0 is
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3882 returned, and the value in *pv may or may not be the same as on input.
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3883 As always, an extra byte is allocated for a trailing \0 byte (newsize
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3884 does *not* include that), and a trailing \0 byte is stored.
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3885 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3886
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3887 int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3888 _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3889 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3890 register PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3891 register PyStringObject *sv;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3892 v = *pv;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3893 if (!PyString_Check(v) || Py_REFCNT(v) != 1 || newsize < 0 ||
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3894 PyString_CHECK_INTERNED(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3895 *pv = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3896 Py_DECREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3897 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3898 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3899 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3900 /* XXX UNREF/NEWREF interface should be more symmetrical */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3901 _Py_DEC_REFTOTAL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3902 _Py_ForgetReference(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3903 *pv = (PyObject *)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3904 PyObject_REALLOC((char *)v, PyStringObject_SIZE + newsize);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3905 if (*pv == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3906 PyObject_Del(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3907 PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3908 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3909 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3910 _Py_NewReference(*pv);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3911 sv = (PyStringObject *) *pv;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3912 Py_SIZE(sv) = newsize;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3913 sv->ob_sval[newsize] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3914 sv->ob_shash = -1; /* invalidate cached hash value */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3915 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3916 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3917
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3918 /* Helpers for formatstring */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3919
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
3920 Py_LOCAL_INLINE(PyObject *)
36528
76ce880dce49 Use Py_ssize_t in helper function between Py_ssize_t-using functions.
Thomas Wouters <thomas@python.org>
parents: 36519
diff changeset
3921 getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3922 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3923 Py_ssize_t argidx = *p_argidx;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3924 if (argidx < arglen) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3925 (*p_argidx)++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3926 if (arglen < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3927 return args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3928 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3929 return PyTuple_GetItem(args, argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3930 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3931 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3932 "not enough arguments for format string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3933 return NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3934 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3935
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3936 /* Format codes
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3937 * F_LJUST '-'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3938 * F_SIGN '+'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3939 * F_BLANK ' '
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3940 * F_ALT '#'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3941 * F_ZERO '0'
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3942 */
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3943 #define F_LJUST (1<<0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3944 #define F_SIGN (1<<1)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3945 #define F_BLANK (1<<2)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3946 #define F_ALT (1<<3)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3947 #define F_ZERO (1<<4)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3948
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3949 /* Returns a new reference to a PyString object, or NULL on failure. */
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3950
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3951 static PyObject *
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3952 formatfloat(PyObject *v, int flags, int prec, int type)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3953 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3954 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3955 PyObject *result;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3956 double x;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3957
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3958 x = PyFloat_AsDouble(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3959 if (x == -1.0 && PyErr_Occurred()) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3960 PyErr_Format(PyExc_TypeError, "float argument required, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3961 "not %.200s", Py_TYPE(v)->tp_name);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3962 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3963 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3964
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3965 if (prec < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3966 prec = 6;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3967
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3968 p = PyOS_double_to_string(x, type, prec,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3969 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3970
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3971 if (p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3972 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3973 result = PyString_FromStringAndSize(p, strlen(p));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3974 PyMem_Free(p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3975 return result;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3976 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3977
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3978 /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3979 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3980 * Python's regular ints.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3981 * Return value: a new PyString*, or NULL if error.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3982 * . *pbuf is set to point into it,
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3983 * *plen set to the # of chars following that.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3984 * Caller must decref it when done using pbuf.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3985 * The string starting at *pbuf is of the form
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3986 * "-"? ("0x" | "0X")? digit+
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3987 * "0x"/"0X" are present only for x and X conversions, with F_ALT
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3988 * set in flags. The case of hex digits will be correct,
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3989 * There will be at least prec digits, zero-filled on the left if
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3990 * necessary to get that many.
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3991 * val object to be converted
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3992 * flags bitmask of format flags; only F_ALT is looked at
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3993 * prec minimum number of digits; 0-fill on left if needed
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3994 * type a character in [duoxX]; u acts the same as d
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3995 *
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3996 * CAUTION: o, x and X conversions on regular ints can never
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3997 * produce a '-' sign, but can for Python's unbounded ints.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3998 */
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3999 PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
4000 _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4001 char **pbuf, int *plen)
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4002 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4003 PyObject *result = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4004 char *buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4005 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4006 int sign; /* 1 if '-', else 0 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4007 int len; /* number of characters */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4008 Py_ssize_t llen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4009 int numdigits; /* len == numnondigits + numdigits */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4010 int numnondigits = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4011
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4012 switch (type) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4013 case 'd':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4014 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4015 result = Py_TYPE(val)->tp_str(val);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4016 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4017 case 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4018 result = Py_TYPE(val)->tp_as_number->nb_oct(val);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4019 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4020 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4021 case 'X':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4022 numnondigits = 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4023 result = Py_TYPE(val)->tp_as_number->nb_hex(val);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4024 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4025 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4026 assert(!"'type' not in [duoxX]");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4027 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4028 if (!result)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4029 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4030
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4031 buf = PyString_AsString(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4032 if (!buf) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4033 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4034 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4035 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4036
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4037 /* To modify the string in-place, there can only be one reference. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4038 if (Py_REFCNT(result) != 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4039 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4040 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4041 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4042 llen = PyString_Size(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4043 if (llen > INT_MAX) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4044 PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4045 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4046 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4047 len = (int)llen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4048 if (buf[len-1] == 'L') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4049 --len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4050 buf[len] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4051 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4052 sign = buf[0] == '-';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4053 numnondigits += sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4054 numdigits = len - numnondigits;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4055 assert(numdigits > 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4056
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4057 /* Get rid of base marker unless F_ALT */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4058 if ((flags & F_ALT) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4059 /* Need to skip 0x, 0X or 0. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4060 int skipped = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4061 switch (type) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4062 case 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4063 assert(buf[sign] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4064 /* If 0 is only digit, leave it alone. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4065 if (numdigits > 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4066 skipped = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4067 --numdigits;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4068 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4069 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4070 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4071 case 'X':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4072 assert(buf[sign] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4073 assert(buf[sign + 1] == 'x');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4074 skipped = 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4075 numnondigits -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4076 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4077 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4078 if (skipped) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4079 buf += skipped;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4080 len -= skipped;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4081 if (sign)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4082 buf[0] = '-';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4083 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4084 assert(len == numnondigits + numdigits);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4085 assert(numdigits > 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4086 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4087
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4088 /* Fill with leading zeroes to meet minimum width. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4089 if (prec > numdigits) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4090 PyObject *r1 = PyString_FromStringAndSize(NULL,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4091 numnondigits + prec);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4092 char *b1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4093 if (!r1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4094 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4095 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4096 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4097 b1 = PyString_AS_STRING(r1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4098 for (i = 0; i < numnondigits; ++i)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4099 *b1++ = *buf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4100 for (i = 0; i < prec - numdigits; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4101 *b1++ = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4102 for (i = 0; i < numdigits; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4103 *b1++ = *buf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4104 *b1 = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4105 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4106 result = r1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4107 buf = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4108 len = numnondigits + prec;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4109 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4110
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4111 /* Fix up case for hex conversions. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4112 if (type == 'X') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4113 /* Need to convert all lower case letters to upper case.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4114 and need to convert 0x to 0X (and -0x to -0X). */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4115 for (i = 0; i < len; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4116 if (buf[i] >= 'a' && buf[i] <= 'x')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4117 buf[i] -= 'a'-'A';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4118 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4119 *pbuf = buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4120 *plen = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4121 return result;
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4122 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4123
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
4124 Py_LOCAL_INLINE(int)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4125 formatint(char *buf, size_t buflen, int flags,
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4126 int prec, int type, PyObject *v)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4127 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4128 /* fmt = '%#.' + `prec` + 'l' + `type`
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4129 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4130 + 1 + 1 = 24 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4131 char fmt[64]; /* plenty big enough! */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4132 char *sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4133 long x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4134
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4135 x = PyInt_AsLong(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4136 if (x == -1 && PyErr_Occurred()) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4137 PyErr_Format(PyExc_TypeError, "int argument required, not %.200s",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4138 Py_TYPE(v)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4139 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4140 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4141 if (x < 0 && type == 'u') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4142 type = 'd';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4143 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4144 if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4145 sign = "-";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4146 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4147 sign = "";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4148 if (prec < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4149 prec = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4150
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4151 if ((flags & F_ALT) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4152 (type == 'x' || type == 'X')) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4153 /* When converting under %#x or %#X, there are a number
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4154 * of issues that cause pain:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4155 * - when 0 is being converted, the C standard leaves off
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4156 * the '0x' or '0X', which is inconsistent with other
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4157 * %#x/%#X conversions and inconsistent with Python's
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4158 * hex() function
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4159 * - there are platforms that violate the standard and
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4160 * convert 0 with the '0x' or '0X'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4161 * (Metrowerks, Compaq Tru64)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4162 * - there are platforms that give '0x' when converting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4163 * under %#X, but convert 0 in accordance with the
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4164 * standard (OS/2 EMX)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4165 *
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4166 * We can achieve the desired consistency by inserting our
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4167 * own '0x' or '0X' prefix, and substituting %x/%X in place
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4168 * of %#x/%#X.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4169 *
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4170 * Note that this is the same approach as used in
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4171 * formatint() in unicodeobject.c
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4172 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4173 PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4174 sign, type, prec, type);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4175 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4176 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4177 PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4178 sign, (flags&F_ALT) ? "#" : "",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4179 prec, type);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4180 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4181
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4182 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4183 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4184 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4185 if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4186 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4187 "formatted integer is too long (precision too large?)");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4188 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4189 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4190 if (sign[0])
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4191 PyOS_snprintf(buf, buflen, fmt, -x);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4192 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4193 PyOS_snprintf(buf, buflen, fmt, x);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4194 return (int)strlen(buf);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4195 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4196
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
4197 Py_LOCAL_INLINE(int)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4198 formatchar(char *buf, size_t buflen, PyObject *v)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4199 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4200 /* presume that the buffer is at least 2 characters long */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4201 if (PyString_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4202 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4203 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4204 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4205 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4206 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4207 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4208 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4209 buf[1] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4210 return 1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4211 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4212
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4213 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4214
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
4215 FORMATBUFLEN is the length of the buffer in which the ints &
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4216 chars are formatted. XXX This is a magic number. Each formatting
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4217 routine does bounds checking to ensure no overflow, but a better
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4218 solution may be to malloc a buffer of appropriate size for each
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4219 format. For now, the current solution is sufficient.
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4220 */
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4221 #define FORMATBUFLEN (size_t)120
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4222
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4223 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
4224 PyString_Format(PyObject *format, PyObject *args)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4225 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4226 char *fmt, *res;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4227 Py_ssize_t arglen, argidx;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4228 Py_ssize_t reslen, rescnt, fmtcnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4229 int args_owned = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4230 PyObject *result, *orig_args;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4231 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4232 PyObject *v, *w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4233 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4234 PyObject *dict = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4235 if (format == NULL || !PyString_Check(format) || args == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4236 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4237 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4238 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4239 orig_args = args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4240 fmt = PyString_AS_STRING(format);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4241 fmtcnt = PyString_GET_SIZE(format);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4242 reslen = rescnt = fmtcnt + 100;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4243 result = PyString_FromStringAndSize((char *)NULL, reslen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4244 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4245 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4246 res = PyString_AsString(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4247 if (PyTuple_Check(args)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4248 arglen = PyTuple_GET_SIZE(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4249 argidx = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4250 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4251 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4252 arglen = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4253 argidx = -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4254 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4255 if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4256 !PyObject_TypeCheck(args, &PyBaseString_Type))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4257 dict = args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4258 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4259 if (*fmt != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4260 if (--rescnt < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4261 rescnt = fmtcnt + 100;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4262 reslen += rescnt;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4263 if (_PyString_Resize(&result, reslen))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4264 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4265 res = PyString_AS_STRING(result)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4266 + reslen - rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4267 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4268 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4269 *res++ = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4270 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4271 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4272 /* Got a format specifier */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4273 int flags = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4274 Py_ssize_t width = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4275 int prec = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4276 int c = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4277 int fill;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4278 int isnumok;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4279 PyObject *v = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4280 PyObject *temp = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4281 char *pbuf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4282 int sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4283 Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4284 char formatbuf[FORMATBUFLEN];
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4285 /* For format{int,char}() */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4286 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4287 char *fmt_start = fmt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4288 Py_ssize_t argidx_start = argidx;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4289 #endif
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4290
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4291 fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4292 if (*fmt == '(') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4293 char *keystart;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4294 Py_ssize_t keylen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4295 PyObject *key;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4296 int pcount = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4297
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4298 if (dict == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4299 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4300 "format requires a mapping");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4301 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4302 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4303 ++fmt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4304 --fmtcnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4305 keystart = fmt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4306 /* Skip over balanced parentheses */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4307 while (pcount > 0 && --fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4308 if (*fmt == ')')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4309 --pcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4310 else if (*fmt == '(')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4311 ++pcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4312 fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4313 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4314 keylen = fmt - keystart - 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4315 if (fmtcnt < 0 || pcount > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4316 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4317 "incomplete format key");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4318 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4319 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4320 key = PyString_FromStringAndSize(keystart,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4321 keylen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4322 if (key == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4323 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4324 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4325 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4326 args_owned = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4327 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4328 args = PyObject_GetItem(dict, key);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4329 Py_DECREF(key);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4330 if (args == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4331 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4332 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4333 args_owned = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4334 arglen = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4335 argidx = -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4336 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4337 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4338 switch (c = *fmt++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4339 case '-': flags |= F_LJUST; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4340 case '+': flags |= F_SIGN; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4341 case ' ': flags |= F_BLANK; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4342 case '#': flags |= F_ALT; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4343 case '0': flags |= F_ZERO; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4344 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4345 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4346 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4347 if (c == '*') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4348 v = getnextarg(args, arglen, &argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4349 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4350 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4351 if (!PyInt_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4352 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4353 "* wants int");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4354 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4355 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4356 width = PyInt_AsLong(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4357 if (width < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4358 flags |= F_LJUST;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4359 width = -width;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4360 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4361 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4362 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4363 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4364 else if (c >= 0 && isdigit(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4365 width = c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4366 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4367 c = Py_CHARMASK(*fmt++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4368 if (!isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4369 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4370 if ((width*10) / 10 != width) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4371 PyErr_SetString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4372 PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4373 "width too big");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4374 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4375 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4376 width = width*10 + (c - '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4377 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4378 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4379 if (c == '.') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4380 prec = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4381 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4382 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4383 if (c == '*') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4384 v = getnextarg(args, arglen, &argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4385 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4386 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4387 if (!PyInt_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4388 PyErr_SetString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4389 PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4390 "* wants int");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4391 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4392 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4393 prec = PyInt_AsLong(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4394 if (prec < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4395 prec = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4396 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4397 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4398 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4399 else if (c >= 0 && isdigit(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4400 prec = c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4401 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4402 c = Py_CHARMASK(*fmt++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4403 if (!isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4404 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4405 if ((prec*10) / 10 != prec) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4406 PyErr_SetString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4407 PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4408 "prec too big");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4409 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4410 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4411 prec = prec*10 + (c - '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4412 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4413 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4414 } /* prec */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4415 if (fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4416 if (c == 'h' || c == 'l' || c == 'L') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4417 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4418 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4419 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4420 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4421 if (fmtcnt < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4422 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4423 "incomplete format");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4424 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4425 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4426 if (c != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4427 v = getnextarg(args, arglen, &argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4428 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4429 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4430 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4431 sign = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4432 fill = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4433 switch (c) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4434 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4435 pbuf = "%";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4436 len = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4437 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4438 case 's':
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4439 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4440 if (PyUnicode_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4441 fmt = fmt_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4442 argidx = argidx_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4443 goto unicode;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4444 }
35665
dc5d706283d6 Fix PyString_Format so that the "%s" format works again when Unicode is not
Georg Brandl <georg@python.org>
parents: 35503
diff changeset
4445 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4446 temp = _PyObject_Str(v);
35665
dc5d706283d6 Fix PyString_Format so that the "%s" format works again when Unicode is not
Georg Brandl <georg@python.org>
parents: 35503
diff changeset
4447 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4448 if (temp != NULL && PyUnicode_Check(temp)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4449 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4450 fmt = fmt_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4451 argidx = argidx_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4452 goto unicode;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4453 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4454 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4455 /* Fall through */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4456 case 'r':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4457 if (c == 'r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4458 temp = PyObject_Repr(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4459 if (temp == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4460 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4461 if (!PyString_Check(temp)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4462 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4463 "%s argument has non-string str()");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4464 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4465 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4466 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4467 pbuf = PyString_AS_STRING(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4468 len = PyString_GET_SIZE(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4469 if (prec >= 0 && len > prec)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4470 len = prec;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4471 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4472 case 'i':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4473 case 'd':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4474 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4475 case 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4476 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4477 case 'X':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4478 if (c == 'i')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4479 c = 'd';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4480 isnumok = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4481 if (PyNumber_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4482 PyObject *iobj=NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4483
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4484 if (PyInt_Check(v) || (PyLong_Check(v))) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4485 iobj = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4486 Py_INCREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4487 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4488 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4489 iobj = PyNumber_Int(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4490 if (iobj==NULL) iobj = PyNumber_Long(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4491 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4492 if (iobj!=NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4493 if (PyInt_Check(iobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4494 isnumok = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4495 pbuf = formatbuf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4496 len = formatint(pbuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4497 sizeof(formatbuf),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4498 flags, prec, c, iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4499 Py_DECREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4500 if (len < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4501 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4502 sign = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4503 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4504 else if (PyLong_Check(iobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4505 int ilen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4506
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4507 isnumok = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4508 temp = _PyString_FormatLong(iobj, flags,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4509 prec, c, &pbuf, &ilen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4510 Py_DECREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4511 len = ilen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4512 if (!temp)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4513 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4514 sign = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4515 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4516 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4517 Py_DECREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4518 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4519 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4520 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4521 if (!isnumok) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4522 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4523 "%%%c format: a number is required, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4524 "not %.200s", c, Py_TYPE(v)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4525 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4526 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4527 if (flags & F_ZERO)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4528 fill = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4529 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4530 case 'e':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4531 case 'E':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4532 case 'f':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4533 case 'F':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4534 case 'g':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4535 case 'G':
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4536 temp = formatfloat(v, flags, prec, c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4537 if (temp == NULL)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4538 goto error;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4539 pbuf = PyString_AS_STRING(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4540 len = PyString_GET_SIZE(temp);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4541 sign = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4542 if (flags & F_ZERO)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4543 fill = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4544 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4545 case 'c':
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4546 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4547 if (PyUnicode_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4548 fmt = fmt_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4549 argidx = argidx_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4550 goto unicode;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4551 }
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4552 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4553 pbuf = formatbuf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4554 len = formatchar(pbuf, sizeof(formatbuf), v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4555 if (len < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4556 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4557 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4558 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4559 PyErr_Format(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4560 "unsupported format character '%c' (0x%x) "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4561 "at index %zd",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4562 c, c,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4563 (Py_ssize_t)(fmt - 1 -
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4564 PyString_AsString(format)));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4565 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4566 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4567 if (sign) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4568 if (*pbuf == '-' || *pbuf == '+') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4569 sign = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4570 len--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4571 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4572 else if (flags & F_SIGN)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4573 sign = '+';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4574 else if (flags & F_BLANK)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4575 sign = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4576 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4577 sign = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4578 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4579 if (width < len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4580 width = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4581 if (rescnt - (sign != 0) < width) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4582 reslen -= rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4583 rescnt = width + fmtcnt + 100;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4584 reslen += rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4585 if (reslen < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4586 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4587 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4588 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4589 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4590 if (_PyString_Resize(&result, reslen)) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4591 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4592 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4593 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4594 res = PyString_AS_STRING(result)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4595 + reslen - rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4596 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4597 if (sign) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4598 if (fill != ' ')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4599 *res++ = sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4600 rescnt--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4601 if (width > len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4602 width--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4603 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4604 if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4605 assert(pbuf[0] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4606 assert(pbuf[1] == c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4607 if (fill != ' ') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4608 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4609 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4610 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4611 rescnt -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4612 width -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4613 if (width < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4614 width = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4615 len -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4616 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4617 if (width > len && !(flags & F_LJUST)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4618 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4619 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4620 *res++ = fill;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4621 } while (--width > len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4622 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4623 if (fill == ' ') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4624 if (sign)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4625 *res++ = sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4626 if ((flags & F_ALT) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4627 (c == 'x' || c == 'X')) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4628 assert(pbuf[0] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4629 assert(pbuf[1] == c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4630 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4631 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4632 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4633 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4634 Py_MEMCPY(res, pbuf, len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4635 res += len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4636 rescnt -= len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4637 while (--width >= len) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4638 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4639 *res++ = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4640 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4641 if (dict && (argidx < arglen) && c != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4642 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4643 "not all arguments converted during string formatting");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4644 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4645 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4646 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4647 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4648 } /* '%' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4649 } /* until end */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4650 if (argidx < arglen && !dict) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4651 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4652 "not all arguments converted during string formatting");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4653 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4654 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4655 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4656 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4657 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4658 if (_PyString_Resize(&result, reslen - rescnt))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4659 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4660 return result;
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4661
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4662 #ifdef Py_USING_UNICODE
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4663 unicode:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4664 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4665 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4666 args_owned = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4667 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4668 /* Fiddle args right (remove the first argidx arguments) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4669 if (PyTuple_Check(orig_args) && argidx > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4670 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4671 Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4672 v = PyTuple_New(n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4673 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4674 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4675 while (--n >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4676 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4677 Py_INCREF(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4678 PyTuple_SET_ITEM(v, n, w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4679 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4680 args = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4681 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4682 Py_INCREF(orig_args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4683 args = orig_args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4684 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4685 args_owned = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4686 /* Take what we have of the result and let the Unicode formatting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4687 function format the rest of the input. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4688 rescnt = res - PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4689 if (_PyString_Resize(&result, rescnt))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4690 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4691 fmtcnt = PyString_GET_SIZE(format) - \
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4692 (fmt - PyString_AS_STRING(format));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4693 format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4694 if (format == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4695 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4696 v = PyUnicode_Format(format, args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4697 Py_DECREF(format);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4698 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4699 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4700 /* Paste what we have (result) to what the Unicode formatting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4701 function returned (v) and return the result (or error) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4702 w = PyUnicode_Concat(result, v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4703 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4704 Py_DECREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4705 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4706 return w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4707 #endif /* Py_USING_UNICODE */
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4708
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4709 error:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4710 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4711 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4712 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4713 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4714 return NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4715 }
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4716
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4717 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4718 PyString_InternInPlace(PyObject **p)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4719 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4720 register PyStringObject *s = (PyStringObject *)(*p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4721 PyObject *t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4722 if (s == NULL || !PyString_Check(s))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4723 Py_FatalError("PyString_InternInPlace: strings only please!");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4724 /* If it's a string subclass, we don't really know what putting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4725 it in the interned dict might do. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4726 if (!PyString_CheckExact(s))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4727 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4728 if (PyString_CHECK_INTERNED(s))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4729 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4730 if (interned == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4731 interned = PyDict_New();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4732 if (interned == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4733 PyErr_Clear(); /* Don't leave an exception */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4734 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4735 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4736 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4737 t = PyDict_GetItem(interned, (PyObject *)s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4738 if (t) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4739 Py_INCREF(t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4740 Py_DECREF(*p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4741 *p = t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4742 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4743 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4744
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4745 if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4746 PyErr_Clear();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4747 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4748 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4749 /* The two references in interned are not counted by refcnt.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4750 The string deallocator will take care of this */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4751 Py_REFCNT(s) -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4752 PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4753 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4754
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4755 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4756 PyString_InternImmortal(PyObject **p)
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4757 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4758 PyString_InternInPlace(p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4759 if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4760 PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4761 Py_INCREF(*p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4762 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4763 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4764
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4765
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4766 PyObject *
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4767 PyString_InternFromString(const char *cp)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4768 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4769 PyObject *s = PyString_FromString(cp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4770 if (s == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4771 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4772 PyString_InternInPlace(&s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4773 return s;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4774 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4775
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4776 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4777 PyString_Fini(void)
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4778 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4779 int i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4780 for (i = 0; i < UCHAR_MAX + 1; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4781 Py_XDECREF(characters[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4782 characters[i] = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4783 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4784 Py_XDECREF(nullstring);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4785 nullstring = NULL;
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4786 }
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4787
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4788 void _Py_ReleaseInternedStrings(void)
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4789 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4790 PyObject *keys;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4791 PyStringObject *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4792 Py_ssize_t i, n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4793 Py_ssize_t immortal_size = 0, mortal_size = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4794
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4795 if (interned == NULL || !PyDict_Check(interned))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4796 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4797 keys = PyDict_Keys(interned);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4798 if (keys == NULL || !PyList_Check(keys)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4799 PyErr_Clear();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4800 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4801 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4802
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4803 /* Since _Py_ReleaseInternedStrings() is intended to help a leak
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4804 detector, interned strings are not forcibly deallocated; rather, we
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4805 give them their stolen references back, and then clear and DECREF
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4806 the interned dict. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4807
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4808 n = PyList_GET_SIZE(keys);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4809 fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4810 n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4811 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4812 s = (PyStringObject *) PyList_GET_ITEM(keys, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4813 switch (s->ob_sstate) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4814 case SSTATE_NOT_INTERNED:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4815 /* XXX Shouldn't happen */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4816 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4817 case SSTATE_INTERNED_IMMORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4818 Py_REFCNT(s) += 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4819 immortal_size += Py_SIZE(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4820 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4821 case SSTATE_INTERNED_MORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4822 Py_REFCNT(s) += 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4823 mortal_size += Py_SIZE(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4824 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4825 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4826 Py_FatalError("Inconsistent interned string state.");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4827 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4828 s->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4829 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4830 fprintf(stderr, "total size of all interned strings: "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4831 "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4832 "mortal/immortal\n", mortal_size, immortal_size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4833 Py_DECREF(keys);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4834 PyDict_Clear(interned);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4835 Py_DECREF(interned);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4836 interned = NULL;
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4837 }