annotate Objects/stringobject.c @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 21fb1767e185
children 80cfd4caa726
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
76189
a11a2bbd8241 fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents: 75135
diff changeset
1265 #ifdef Py_DEBUG
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1266 assert(_Py_HashSecret_Initialized);
76189
a11a2bbd8241 fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents: 75135
diff changeset
1267 #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
1268 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
1269 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
1270 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
1271 /*
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1272 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
1273 (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
1274 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1275 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
1276 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
1277 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
1278 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1279 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
1280 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
1281 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
1282 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
1283 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
1284 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
1285 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
1286 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
1287 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
1288 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
1289 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1290 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1291
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1292 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
1293 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
1294 {
75133
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 (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
1296 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
1297 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
1298 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
1299 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
1300 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
1301 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
1302 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1303 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
1304 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
1305 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
1306 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
1307 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
1308
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1309 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
1310 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
1311 &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
1312 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
1313 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1314
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1315 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
1316 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
1317 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1318 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
1319 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
1320 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
1321 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
1322 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
1323 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1324 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
1325 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
1326 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
1327 slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1328 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1329 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1330 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
1331 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
1332 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
1333 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
1334
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1335 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
1336 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
1337 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
1338 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1339
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1340 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
1341 slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1342 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
1343 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
1344 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1345 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1346 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1347 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
1348 "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
1349 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
1350 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
1351 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1352 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1353
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1354 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
1355 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
1356 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1357 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
1358 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
1359 "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
1360 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
1361 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1362 *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
1363 return Py_SIZE(self);
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1364 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1365
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1366 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
1367 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
1368 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1369 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
1370 "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
1371 return -1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1372 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1373
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1374 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
1375 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
1376 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1377 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
1378 *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
1379 return 1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1380 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1381
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1382 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
1383 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
1384 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1385 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
1386 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
1387 "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
1388 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
1389 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1390 *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
1391 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
1392 }
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1393
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 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
1395 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
1396 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1397 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
1398 (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
1399 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
1400 }
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
1401
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1402 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
1403 (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
1404 (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
1405 (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
1406 (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
1407 (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
1408 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
1409 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
1410 (objobjproc)string_contains /*sq_contains*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1411 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1412
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1413 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
1414 (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
1415 (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
1416 0,
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1417 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1418
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1419 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
1420 (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
1421 (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
1422 (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
1423 (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
1424 (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
1425 0, /* XXX */
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1426 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1427
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1428
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1429
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1430 #define LEFTSTRIP 0
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1431 #define RIGHTSTRIP 1
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1432 #define BOTHSTRIP 2
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1433
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1434 /* Arrays indexed by above */
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1435 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
1436
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1437 #define STRIPNAME(i) (stripformat[i]+3)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1438
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1439 PyDoc_STRVAR(split__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1440 "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
1441 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1442 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
1443 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
1444 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
1445 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
1446 from the result.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1447
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1448 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
1449 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
1450 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1451 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
1452 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
1453 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
1454 PyObject *subobj = Py_None;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1455
75133
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 (!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
1457 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
1458 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
1459 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
1460 if (subobj == Py_None)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1461 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
1462 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
1463 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
1464 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
1465 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1466 #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
1467 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
1468 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
1469 #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
1470 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
1471 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
1472
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1473 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
1474 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1475
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1476 PyDoc_STRVAR(partition__doc__,
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1477 "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
1478 \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 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
1480 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
1481 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
1482
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1483 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
1484 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
1485 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1486 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
1487 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
1488
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1489 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
1490 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
1491 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
1492 }
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38332
diff changeset
1493 #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
1494 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
1495 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
1496 #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
1497 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
1498 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
1499
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1500 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
1501 (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
1502 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
1503 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
1504 );
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1505 }
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1506
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1507 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
1508 "S.rpartition(sep) -> (head, sep, tail)\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1509 \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 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
1511 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
1512 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
1513
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1514 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
1515 string_rpartition(PyStringObject *self, PyObject *sep_obj)
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1516 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1517 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
1518 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
1519
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1520 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
1521 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
1522 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
1523 }
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1524 #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
1525 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
1526 return PyUnicode_RPartition((PyObject *) self, sep_obj);
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1527 #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
1528 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
1529 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
1530
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1531 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
1532 (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
1533 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
1534 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
1535 );
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1536 }
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1537
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1538 PyDoc_STRVAR(rsplit__doc__,
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1539 "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
1540 \n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1541 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
1542 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
1543 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
1544 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
1545 is a separator.");
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1546
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1547 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
1548 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
1549 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1550 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
1551 Py_ssize_t maxsplit = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1552 const char *s = PyString_AS_STRING(self), *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1553 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
1554
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 (!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
1556 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
1557 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
1558 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
1559 if (subobj == Py_None)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1560 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
1561 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
1562 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
1563 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
1564 }
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1565 #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
1566 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
1567 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
1568 #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
1569 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
1570 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
1571
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1572 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
1573 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1574
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1575
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1576 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
1577 "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
1578 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1579 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
1580 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
1581
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1582 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
1583 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
1584 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1585 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
1586 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
1587 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
1588 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
1589 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
1590 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
1591 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
1592 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
1593
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1594 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
1595 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
1596 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
1597 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1598
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1599 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
1600 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
1601 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
1602 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
1603 }
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 (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
1605 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
1606 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
1607 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
1608 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
1609 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
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1612
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1613 /* 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
1614 * 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
1615 * 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
1616 * 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
1617 * 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
1618 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1619 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
1620 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
1621 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
1622 if (!PyString_Check(item)){
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1623 #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
1624 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
1625 /* 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
1626 * 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
1627 * 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
1628 * 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
1629 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1630 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
1631 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
1632 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
1633 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
1634 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1635 #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
1636 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
1637 "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
1638 " %.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
1639 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
1640 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
1641 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
1642 }
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 += 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
1644 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
1645 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
1646 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
1647 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
1648 "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
1649 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
1650 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
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1653
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1654 /* 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
1655 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
1656 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
1657 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
1658 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
1659 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1660
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1661 /* 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
1662 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
1663 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
1664 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
1665 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
1666 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
1667 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
1668 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
1669 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
1670 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
1671 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
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1674
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1675 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
1676 return res;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1677 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1678
18130
8eea2efb34cd Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1679 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
1680 _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
1681 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1682 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
1683 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
1684 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
1685 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1686
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
1687 /* 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
1688 #define ADJUST_INDICES(start, end, len) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1689 if (end > len) \
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 else if (end < 0) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1692 end += len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1693 if (end < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1694 end = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1695 } \
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 += len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1698 if (start < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1699 start = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1700 }
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1701
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1702 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
1703 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
1704 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1705 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
1706 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
1707 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
1708 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
1709
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
1710 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
1711 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
1712 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
1713
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1714 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
1715 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
1716 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
1717 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1718 #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
1719 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
1720 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
1721 (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
1722 #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
1723 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
1724 /* 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
1725 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
1726 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
1727
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1728 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
1729 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
1730 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
1731 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
1732 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1733 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
1734 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
1735 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
1736 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1737
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1738
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1739 PyDoc_STRVAR(find__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1740 "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
1741 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1742 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
1743 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
1744 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
1745 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1746 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1747
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1748 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
1749 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
1750 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1751 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
1752 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
1753 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
1754 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
1755 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1756
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1757
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1758 PyDoc_STRVAR(index__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1759 "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
1760 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1761 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
1762
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1763 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
1764 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
1765 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1766 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
1767 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
1768 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
1769 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
1770 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
1771 "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
1772 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
1773 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1774 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
1775 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1776
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1777
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1778 PyDoc_STRVAR(rfind__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1779 "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
1780 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1781 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
1782 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
1783 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
1784 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1785 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1786
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1787 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
1788 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
1789 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1790 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
1791 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
1792 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
1793 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
1794 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1795
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1796
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1797 PyDoc_STRVAR(rindex__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1798 "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
1799 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1800 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
1801
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1802 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
1803 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
1804 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1805 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
1806 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
1807 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
1808 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
1809 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
1810 "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
1811 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
1812 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1813 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
1814 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1815
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1816
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1817 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
1818 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
1819 {
75133
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 *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
1821 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
1822 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
1823 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
1824 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
1825
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1826 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
1827 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
1828 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
1829 i++;
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1832
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1833 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
1834 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
1835 do {
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 } 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
1838 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1839 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1840
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1841 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
1842 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
1843 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
1844 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1845 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1846 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
1847 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1848
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1849
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1850 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
1851 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
1852 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1853 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
1854 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
1855
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1856 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
1857 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
1858 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
1859 i++;
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1862
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1863 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
1864 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
1865 do {
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 } 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
1868 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1869 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1870
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1871 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
1872 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
1873 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
1874 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1875 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1876 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
1877 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1878
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1879
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1880 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
1881 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
1882 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1883 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
1884
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1885 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
1886 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
1887
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1888 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
1889 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
1890 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
1891 #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
1892 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
1893 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
1894 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
1895 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
1896 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
1897 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
1898 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
1899 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
1900 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
1901 }
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1902 #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
1903 PyErr_Format(PyExc_TypeError,
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1904 #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
1905 "%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
1906 #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
1907 "%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
1908 #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
1909 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
1910 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
1911 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1912
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1913 return do_strip(self, striptype);
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1914 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1915
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1916
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1917 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
1918 "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
1919 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1920 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
1921 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
1922 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
1923 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
1924
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1925 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
1926 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
1927 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1928 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
1929 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
1930 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1931 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
1932 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1933
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1934
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1935 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
1936 "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
1937 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1938 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
1939 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
1940 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
1941
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1942 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
1943 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
1944 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1945 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
1946 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
1947 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1948 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
1949 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1950
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1951
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1952 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
1953 "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
1954 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1955 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
1956 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
1957 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
1958
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1959 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
1960 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
1961 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1962 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
1963 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
1964 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1965 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
1966 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1967
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1968
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1969 PyDoc_STRVAR(lower__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1970 "S.lower() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1971 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1972 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
1973
38294
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1974 /* _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
1975 #ifndef _tolower
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1976 #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
1977 #endif
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1978
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1979 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
1980 string_lower(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1981 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1982 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
1983 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
1984 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
1985
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1986 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
1987 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
1988 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
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 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
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 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
1993
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1994 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
1995 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
1996 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
1997 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
1998 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1999
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2000 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2001 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2002
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2003 PyDoc_STRVAR(upper__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2004 "S.upper() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2005 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2006 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
2007
38294
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2008 #ifndef _toupper
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2009 #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
2010 #endif
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2011
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2012 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
2013 string_upper(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2014 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2015 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
2016 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
2017 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
2018
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2019 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
2020 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
2021 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
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 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
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 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
2026
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2027 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
2028 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
2029 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
2030 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
2031 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2032
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2033 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2034 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2035
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2036 PyDoc_STRVAR(title__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2037 "S.title() -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2038 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2039 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
2040 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
2041
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2042 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
2043 string_title(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2044 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2045 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
2046 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
2047 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
2048 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
2049
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2050 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
2051 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
2052 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
2053 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
2054 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
2055 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
2056 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
2057 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
2058 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
2059 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
2060 } 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
2061 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
2062 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
2063 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
2064 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2065 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
2066 *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
2067 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2068 return newobj;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2069 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2070
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2071 PyDoc_STRVAR(capitalize__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2072 "S.capitalize() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2073 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2074 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
2075 capitalized.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2076
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2077 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
2078 string_capitalize(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2079 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2080 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
2081 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
2082 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
2083
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2084 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
2085 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
2086 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
2087 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
2088 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
2089 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
2090 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
2091 *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
2092 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2093 *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
2094 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
2095 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2096 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
2097 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
2098 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
2099 *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
2100 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2101 *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
2102 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
2103 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2104 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2105 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2106
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2107
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2108 PyDoc_STRVAR(count__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2109 "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
2110 \n\
38236
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2111 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
2112 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
2113 as in slice notation.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2114
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2115 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
2116 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
2117 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2118 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
2119 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
2120 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
2121 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
2122
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
2123 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
2124 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
2125
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2126 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
2127 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
2128 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
2129 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2130 #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
2131 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
2132 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
2133 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
2134 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
2135 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
2136 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2137 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
2138 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2139 #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
2140 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
2141 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
2142
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2143 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
2144
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2145 return PyInt_FromSsize_t(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2146 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
2147 );
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2148 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2149
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2150 PyDoc_STRVAR(swapcase__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2151 "S.swapcase() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2152 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2153 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
2154 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
2155
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2156 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
2157 string_swapcase(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2158 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2159 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
2160 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
2161 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
2162
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2163 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
2164 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
2165 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
2166 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
2167 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
2168 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
2169 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
2170 *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
2171 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2172 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
2173 *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
2174 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2175 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2176 *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
2177 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
2178 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2179 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2180 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2181
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2182
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2183 PyDoc_STRVAR(translate__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2184 "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
2185 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2186 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
2187 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
2188 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
2189 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
2190 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
2191 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
2192
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2193 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
2194 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
2195 {
75133
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 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
2197 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
2198 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
2199 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
2200 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
2201 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
2202 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
2203 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
2204 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
2205
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2206 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
2207 &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
2208 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
2209
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2210 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
2211 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
2212 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
2213 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2214 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
2215 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
2216 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
2217 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2218 #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
2219 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
2220 /* 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
2221 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
2222 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
2223 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
2224 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
2225 "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
2226 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
2227 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2228 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
2229 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2230 #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
2231 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
2232 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
2233
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2234 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
2235 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
2236 "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
2237 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
2238 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2239
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2240 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
2241 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
2242 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
2243 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
2244 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2245 #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
2246 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
2247 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
2248 "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
2249 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
2250 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2251 #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
2252 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
2253 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
2254 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2255 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2256 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
2257 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
2258 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2259
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2260 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
2261 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
2262 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
2263 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
2264 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
2265 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
2266
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2267 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
2268 /* 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
2269 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
2270 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
2271 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
2272 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
2273 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2274 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
2275 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
2276 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
2277 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
2278 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
2279 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2280
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2281 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
2282 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
2283 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
2284 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2285 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
2286 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
2287 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2288
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2289 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
2290 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
2291
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2292 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
2293 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
2294 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
2295 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
2296 continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2297 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
2298 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2299 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
2300 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
2301 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
2302 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
2303 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2304 /* Fix the size of the resulting string */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2305 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
2306 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
2307 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2308 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2309
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2310
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2311 /* 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
2312
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2313 #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
2314 ((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
2315
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2316 /* 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
2317 /* 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
2318 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
2319 return_self(PyStringObject *self)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2320 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2321 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
2322 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
2323 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
2324 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2325 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
2326 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
2327 PyString_GET_SIZE(self));
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2328 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2329
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
2330 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
2331 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
2332 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2333 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
2334 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
2335 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
2336
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2337 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
2338 count++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2339 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
2340 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2341 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
2342 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2343 return count;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2344 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2345
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2346
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
2347 /* 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
2348
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2349 /* 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
2350 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
2351 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
2352 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
2353 Py_ssize_t maxcount)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2354 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2355 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
2356 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
2357 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
2358 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
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 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
2361
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2362 /* 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
2363 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
2364 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
2365 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
2366
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2367 /* 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
2368 /* 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
2369 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
2370 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
2371 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
2372 "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
2373 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
2374 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2375 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
2376 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
2377 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
2378 "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
2379 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
2380 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2381
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2382 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
2383 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
2384 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
2385
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2386 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
2387 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
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 /* 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
2390
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2391 /* 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
2392 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
2393 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
2394 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
2395
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2396 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
2397 *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
2398 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
2399 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
2400 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2401
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2402 /* 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
2403 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
2404
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2405 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2406 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2407
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2408 /* 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
2409 /* 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
2410 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
2411 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
2412 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
2413 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2414 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
2415 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
2416 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
2417 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
2418 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
2419
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2420 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
2421 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
2422
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2423 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
2424 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
2425 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
2426 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2427
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2428 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
2429 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
2430
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2431 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
2432 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
2433 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
2434 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
2435
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2436 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
2437 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
2438 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
2439 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
2440 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
2441 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2442 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
2443 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
2444 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
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 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
2447
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2448 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2449 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2450
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2451 /* 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
2452
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2453 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
2454 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
2455 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
2456 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
2457 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
2458 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
2459 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
2460 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
2461 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
2462
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2463 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
2464 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
2465
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2466 count = stringlib_count(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2467 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2468 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
2469
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2470 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
2471 /* 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
2472 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
2473 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2474
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2475 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
2476 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
2477
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2478 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
2479 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
2480 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
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 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
2483
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2484 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
2485 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
2486 while (count-- > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2487 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2488 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2489 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
2490 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
2491 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2492 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
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 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
2495
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2496 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
2497 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
2498 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2499 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
2500 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2501 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2502
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2503 /* 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
2504 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
2505 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
2506 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
2507 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2508 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2509 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
2510 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
2511 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
2512
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2513 /* 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
2514 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
2515 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
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 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
2518
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2519 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
2520 /* 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
2521 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
2522 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2523
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2524 /* 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
2525 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
2526 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
2527 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
2528 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
2529 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
2530
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2531 /* 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
2532 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
2533 *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
2534 start++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2535 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
2536
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2537 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
2538 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
2539 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
2540 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2541 *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
2542 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
2543 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2544
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2545 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2546 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2547
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2548 /* 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
2549 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
2550 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
2551 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
2552 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
2553 Py_ssize_t maxcount)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2554 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2555 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
2556 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
2557 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
2558 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
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 /* 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
2561
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2562 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
2563 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
2564
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2565 offset = stringlib_find(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2566 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2567 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
2568 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
2569 /* 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
2570 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
2571 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2572
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2573 /* 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
2574 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
2575 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
2576 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
2577 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
2578 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
2579
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2580 /* 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
2581 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
2582 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
2583 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
2584 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
2585
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2586 while ( --maxcount > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2587 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2588 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2589 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
2590 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
2591 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2592 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
2593 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
2594 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2595
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2596 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2597 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2598
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2599 /* 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
2600 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
2601 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
2602 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
2603 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
2604 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2605 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2606 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
2607 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
2608 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
2609 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
2610 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
2611
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2612 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
2613 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
2614
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2615 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
2616 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
2617 /* 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
2618 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
2619 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2620
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2621 /* 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
2622 /* 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
2623 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
2624 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
2625 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
2626 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
2627 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2628 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
2629 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
2630 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
2631 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
2632 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2633
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2634 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
2635 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
2636 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
2637 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
2638
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2639 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
2640 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
2641 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
2642 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
2643 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
2644 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2645
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2646 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
2647 /* 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
2648 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
2649 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
2650 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
2651 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2652 /* 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
2653 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
2654 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
2655 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
2656 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
2657 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
2658 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2659 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2660 /* 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
2661 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
2662
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2663 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2664 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2665
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2666 /* 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
2667 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
2668 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
2669 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
2670 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
2671 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
2672 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
2673 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
2674 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
2675 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
2676 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
2677
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2678 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
2679 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
2680
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2681 count = stringlib_count(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2682 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2683 maxcount);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2684
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2685 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
2686 /* 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
2687 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
2688 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2689
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2690 /* 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
2691 /* 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
2692 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
2693 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
2694 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
2695 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
2696 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2697 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
2698 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
2699 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
2700 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
2701 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2702
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2703 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
2704 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
2705 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
2706 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
2707
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2708 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
2709 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
2710 while (count-- > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2711 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2712 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2713 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
2714 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
2715 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2716 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
2717 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
2718 /* 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
2719 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
2720 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
2721 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
2722 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2723 /* 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
2724 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
2725 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
2726 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
2727 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
2728 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
2729 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2730 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2731 /* 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
2732 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
2733
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2734 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2735 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2736
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2737
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2738 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
2739 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
2740 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
2741 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
2742 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2743 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2744 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
2745 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
2746 } 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
2747 /* 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
2748 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
2749 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2750
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2751 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
2752 (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
2753 /* 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
2754 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
2755 }
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 /* 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
2758
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2759 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
2760 /* 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
2761 /* >>> "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
2762 /* '.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
2763 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
2764 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2765
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2766 /* 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
2767 /* 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
2768 /* 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
2769 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
2770 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
2771 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2772
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 (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
2774 /* 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
2775 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
2776 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
2777 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
2778 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2779 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
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 }
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 /* 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
2784
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2785 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
2786 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
2787 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
2788 self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2789 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
2790 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
2791 maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2792 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2793 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
2794 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
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2797
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2798 /* 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
2799 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
2800 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
2801 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
2802 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2803 /* 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
2804 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
2805 }
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2806 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2807
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2808 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
2809 "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
2810 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2811 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
2812 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
2813 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
2814
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2815 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
2816 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
2817 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2818 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
2819 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
2820 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
2821 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
2822
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2823 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
2824 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
2825
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2826 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
2827 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
2828 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
2829 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2830 #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
2831 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
2832 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
2833 from, to, count);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2834 #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
2835 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
2836 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
2837
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2838 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
2839 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
2840 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
2841 }
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2842 #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
2843 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
2844 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
2845 from, to, count);
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2846 #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
2847 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
2848 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
2849
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2850 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
2851 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
2852 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
2853 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2854
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2855 /** End DALKE **/
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2856
38738
0a8a392e1fae Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
2857 /* 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
2858 * 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
2859 * -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
2860 */
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2861 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
2862 _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
2863 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
2864 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2865 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
2866 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
2867 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
2868 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
2869
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2870 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
2871 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
2872 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
2873 }
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2874 #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
2875 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
2876 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
2877 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
2878 #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
2879 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
2880 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
2881 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
2882
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2883 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
2884
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 (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
2886 /* startswith */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2887 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
2888 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
2889 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2890 /* endswith */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2891 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
2892 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
2893
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2894 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
2895 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
2896 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2897 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
2898 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
2899 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
2900 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2901
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2902
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2903 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
2904 "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
2905 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2906 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
2907 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
2908 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
2909 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
2910
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2911 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
2912 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
2913 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2914 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
2915 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
2916 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
2917 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
2918
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
2919 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
2920 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
2921 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
2922 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
2923 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
2924 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
2925 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
2926 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
2927 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
2928 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
2929 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
2930 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
2931 }
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 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
2934 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2935 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
2936 if (result == -1) {
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2937 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
2938 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
2939 "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
2940 return NULL;
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2941 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2942 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2943 return PyBool_FromLong(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2944 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2945
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2946
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2947 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
2948 "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
2949 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2950 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
2951 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
2952 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
2953 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
2954
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2955 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
2956 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
2957 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2958 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
2959 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
2960 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
2961 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
2962
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
2963 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
2964 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
2965 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
2966 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
2967 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
2968 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
2969 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
2970 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
2971 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
2972 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
2973 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
2974 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
2975 }
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 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
2978 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2979 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
2980 if (result == -1) {
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2981 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
2982 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
2983 "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
2984 return NULL;
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2985 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2986 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2987 return PyBool_FromLong(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2988 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2989
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2990
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2991 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
2992 "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
2993 \n\
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2994 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
2995 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
2996 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
2997 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
2998 '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
2999 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
3000
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3001 static PyObject *
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3002 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
3003 {
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3004 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
3005 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
3006 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3007 PyObject *v;
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
3008
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3009 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3010 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
3011 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
3012 v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3013 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3014 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
3015 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
3016 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3017 "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
3018 "(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
3019 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
3020 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3021 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3022 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3023 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3024
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3025 onError:
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3026 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
3027 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3028
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3029
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3030 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
3031 "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
3032 \n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3033 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
3034 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
3035 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
3036 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
3037 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
3038 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
3039
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3040 static PyObject *
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3041 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
3042 {
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3043 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
3044 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
3045 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3046 PyObject *v;
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
3047
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3048 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3049 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
3050 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
3051 v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3052 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3053 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
3054 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
3055 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3056 "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
3057 "(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
3058 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
3059 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3060 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3061 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3062 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3063
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3064 onError:
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3065 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
3066 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3067
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3068
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3069 PyDoc_STRVAR(expandtabs__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3070 "S.expandtabs([tabsize]) -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3071 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3072 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
3073 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
3074
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3075 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
3076 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
3077 {
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3078 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
3079 char *q;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3080 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
3081 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3082 int tabsize = 8;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3083
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3084 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
3085 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3086
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
3087 /* 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
3088 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
3089 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
3090 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
3091 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
3092 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
3093 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
3094 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
3095 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
3096 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
3097 j += incr;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3098 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3099 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3100 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3101 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
3102 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
3103 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 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
3105 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
3106 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
3107 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
3108 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
3109 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3110 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3111
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3112 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
3113 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
3114
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3115 /* 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
3116 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
3117 if (!u)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3118 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3119
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3120 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
3121 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
3122 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
3123
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3124 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
3125 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
3126 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
3127 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
3128 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
3129 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
3130 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
3131 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
3132 *q++ = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3133 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3134 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3135 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3136 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3137 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
3138 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
3139 *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
3140 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3141 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
3142 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
3143 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3144
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3145 return u;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3146
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3147 overflow2:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3148 Py_DECREF(u);
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3149 overflow1:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3150 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
3151 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3152 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3153
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
3154 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
3155 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
3156 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3157 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3158
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3159 if (left < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3160 left = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3161 if (right < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3162 right = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3163
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3164 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
3165 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3166 return (PyObject *)self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3167 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3168
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3169 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
3170 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
3171 if (u) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3172 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
3173 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
3174 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
3175 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
3176 PyString_GET_SIZE(self));
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3177 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
3178 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
3179 fill, right);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3180 }
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 return u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3183 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3184
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3185 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
3186 "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
3187 "\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
3188 "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
3189 "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
3190
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3191 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
3192 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
3193 {
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
3194 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
3195 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3196
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
3197 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
3198 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3199
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3200 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
3201 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3202 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3203 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3204
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3205 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
3206 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3207
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3208
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3209 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
3210 "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
3211 "\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
3212 "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
3213 "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
3214
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3215 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
3216 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
3217 {
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
3218 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
3219 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3220
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
3221 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
3222 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3223
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3224 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
3225 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3226 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3227 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3228
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3229 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
3230 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3231
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3232
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3233 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
3234 "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
3235 "\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3236 "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
3237 "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
3238
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3239 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
3240 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
3241 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
3242 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
3243 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
3244 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3245
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
3246 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
3247 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3248
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3249 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
3250 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3251 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3252 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3253
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3254 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
3255 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
3256
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3257 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
3258 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3259
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3260 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
3261 "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
3262 "\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
3263 "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
3264 "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
3265
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 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
3267 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
3268 {
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
3269 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
3270 PyObject *s;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3271 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
3272 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
3273
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
3274 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
3275 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
3276
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3277 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
3278 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
3279 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
3280 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
3281 }
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3282 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
3283 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
3284 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
3285 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
3286 );
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
3287 }
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
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3289 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
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 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
3292
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 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
3294 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
3295
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3296 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
3297 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
3298 /* 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
3299 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
3300 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
3301 }
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 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
3304 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3305
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3306 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
3307 "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
3308 \n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3309 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
3310 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
3311
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3312 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
3313 string_isspace(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3314 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3315 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
3316 = (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
3317 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
3318
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3319 /* 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
3320 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
3321 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
3322 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3323
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3324 /* 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
3325 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
3326 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3327
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3328 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
3329 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
3330 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
3331 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3332 }
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
3333 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3334 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3335
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3336
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3337 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
3338 "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
3339 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3340 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
3341 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
3342
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3343 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
3344 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
3345 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3346 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
3347 = (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
3348 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
3349
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3350 /* 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
3351 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
3352 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
3353 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
3354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3355 /* 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
3356 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
3357 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
3358
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3359 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
3360 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
3361 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
3362 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
3363 }
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
3364 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
3365 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3366
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3367
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3368 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
3369 "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
3370 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3371 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
3372 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
3373
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3374 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
3375 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
3376 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3377 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
3378 = (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
3379 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
3380
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3381 /* 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
3382 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
3383 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
3384 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
3385
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3386 /* 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
3387 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
3388 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
3389
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3390 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
3391 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
3392 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
3393 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
3394 }
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
3395 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
3396 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3397
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3398
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3399 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
3400 "S.isdigit() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3401 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3402 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
3403 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
3404
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3405 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
3406 string_isdigit(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3407 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3408 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
3409 = (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
3410 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
3411
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3412 /* 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
3413 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
3414 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
3415 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3416
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3417 /* 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
3418 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
3419 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3420
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3421 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
3422 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
3423 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
3424 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3425 }
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
3426 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3427 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3428
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3429
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3430 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
3431 "S.islower() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3432 \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
3433 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
3434 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
3435
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3436 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
3437 string_islower(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3438 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3439 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
3440 = (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
3441 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
3442 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3443
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3444 /* 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
3445 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
3446 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
3447
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3448 /* 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
3449 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
3450 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3451
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3452 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
3453 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3454 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
3455 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
3456 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
3457 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
3458 cased = 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3459 }
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
3460 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3461 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3462
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3463
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3464 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
3465 "S.isupper() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3466 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3467 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
3468 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
3469
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3470 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
3471 string_isupper(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3472 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3473 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
3474 = (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
3475 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
3476 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3477
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3478 /* 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
3479 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
3480 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
3481
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3482 /* 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
3483 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
3484 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3485
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3486 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
3487 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3488 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
3489 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
3490 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
3491 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
3492 cased = 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3493 }
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
3494 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3495 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3496
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3497
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3498 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
3499 "S.istitle() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3500 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3501 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
3502 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
3503 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
3504 otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3505
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3506 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
3507 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
3508 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3509 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
3510 = (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
3511 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
3512 int cased, previous_is_cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3513
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3514 /* 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
3515 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
3516 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
3517
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3518 /* 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
3519 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
3520 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3521
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3522 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
3523 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3524 previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3525 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
3526 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
3527
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3528 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
3529 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
3530 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
3531 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
3532 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
3533 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3534 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
3535 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
3536 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
3537 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
3538 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
3539 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3540 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3541 previous_is_cased = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3542 }
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
3543 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3544 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3545
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3546
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3547 PyDoc_STRVAR(splitlines__doc__,
77292
2fbee0b741f7 Improve tooltips for splitlines() by showing that the default for keepends is False.
Raymond Hettinger <python@rcn.com>
parents: 76189
diff changeset
3548 "S.splitlines(keepends=False) -> list of strings\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3549 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3550 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
3551 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
3552 is given and true.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3553
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3554 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
3555 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
3556 {
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3557 int keepends = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3558
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3559 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
3560 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3561
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
3562 return stringlib_splitlines(
62208
ba2f2d08504b Issue #8930: fix some C code indentation
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
3563 (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
3564 keepends
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3565 );
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3566 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3567
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3568 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
3569 "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
3570
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3571 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
3572 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
3573 {
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 Py_ssize_t res;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3575 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
3576 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
3577 }
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3578
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3579 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
3580 string_getnewargs(PyStringObject *v)
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3581 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3582 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
3583 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3584
45381
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 #include "stringlib/string_format.h"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3587
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3588 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
3589 "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
3590 \n\
66044
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3591 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
3592 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
3593
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3594 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
3595 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
3596 {
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 *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
3598 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
3599 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
3600
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 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
3602 /* 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
3603 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
3604 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
3605 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
3606 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
3607 "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
3608 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
3609 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3610 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
3611 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
3612 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
3613 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
3614
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3615 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
3616 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
3617 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
3618 done:
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3619 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
3620 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
3621 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3622
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3623 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
3624 "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
3625 \n\
66044
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3626 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
3627
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3628
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3629 static PyMethodDef
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3630 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
3631 /* 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
3632 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
3633 {"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
3634 {"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
3635 {"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
3636 {"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
3637 {"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
3638 {"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
3639 {"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
3640 {"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
3641 {"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
3642 {"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
3643 {"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
3644 {"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
3645 {"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
3646 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
3647 {"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
3648 {"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
3649 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
3650 {"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
3651 {"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
3652 {"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
3653 {"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
3654 {"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
3655 {"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
3656 {"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
3657 {"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
3658 {"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
3659 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
3660 {"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
3661 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
3662 {"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
3663 {"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
3664 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
3665 {"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
3666 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
3667 {"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
3668 {"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
3669 {"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
3670 {"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
3671 {"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
3672 {"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
3673 {"__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
3674 {"_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
3675 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3676 {"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
3677 {"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
3678 {"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
3679 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
3680 {"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
3681 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
3682 {"__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
3683 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
3684 {"__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
3685 {NULL, NULL} /* sentinel */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3686 };
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3687
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24184
diff changeset
3688 static PyObject *
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3689 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
3690
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3691 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3692 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
3693 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3694 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
3695 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
3696
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 (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
3698 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
3699 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
3700 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
3701 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
3702 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
3703 return PyObject_Str(x);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3704 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3705
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3706 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3707 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
3708 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3709 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
3710 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
3711
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3712 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
3713 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
3714 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
3715 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
3716 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
3717 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
3718 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
3719 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
3720 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
3721 ((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
3722 ((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
3723 ((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
3724 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3725 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
3726 return pnew;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3727 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3728
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
3729 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
3730 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
3731 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3732 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
3733 "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
3734 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
3735 }
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3736
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
3737 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
3738 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
3739 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3740 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
3741 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
3742 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
3743 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3744 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
3745 }
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
3746
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3747 PyDoc_STRVAR(basestring_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3748 "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
3749
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
3750 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
3751 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
3752 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
3753 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
3754 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
3755 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
3756 };
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
3757
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
3758
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
3759 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
3760 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
3761 "basestring",
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,
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,
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_dealloc */
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_print */
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_getattr */
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_setattr */
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_compare */
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_repr */
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_number */
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_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
3772 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
3773 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
3774 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
3775 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
3776 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
3777 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
3778 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
3779 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
3780 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
3781 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
3782 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
3783 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
3784 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
3785 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
3786 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
3787 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
3788 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
3789 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
3790 &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
3791 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
3792 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
3793 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
3794 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
3795 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
3796 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
3797 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
3798 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
3799 };
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3800
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3801 PyDoc_STRVAR(string_doc,
79590
3b484f53f91b Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78792
diff changeset
3802 "str(object='') -> string\n\
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3803 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3804 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
3805 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
3806
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3807 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
3808 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
3809 "str",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3810 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
3811 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
3812 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
3813 (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
3814 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
3815 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
3816 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
3817 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
3818 &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
3819 &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
3820 &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
3821 (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
3822 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
3823 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
3824 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
3825 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
3826 &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
3827 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
3828 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
3829 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
3830 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
3831 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
3832 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
3833 (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
3834 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
3835 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
3836 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
3837 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
3838 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
3839 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
3840 &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
3841 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
3842 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
3843 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
3844 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
3845 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
3846 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
3847 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
3848 PyObject_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3849 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3850
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3851 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
3852 PyString_Concat(register PyObject **pv, register PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3853 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3854 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
3855 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
3856 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3857 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
3858 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
3859 *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
3860 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3861 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3862 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
3863 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
3864 *pv = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3865 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3866
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3867 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
3868 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
3869 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3870 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
3871 Py_XDECREF(w);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3872 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3873
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3874
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3875 /* The following function breaks the notion that strings are immutable:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3876 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
3877 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
3878 as creating a new string object and destroying the old one, only
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3879 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
3880 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
3881 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
3882 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
3883 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
3884 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
3885 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
3886 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
3887 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3888
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3889 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
3890 _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3891 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3892 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
3893 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
3894 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
3895 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
3896 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
3897 *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
3898 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
3899 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
3900 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
3901 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3902 /* 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
3903 _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
3904 _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
3905 *pv = (PyObject *)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3906 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
3907 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
3908 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
3909 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
3910 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
3911 }
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_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
3913 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
3914 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
3915 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
3916 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
3917 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3918 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3919
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3920 /* Helpers for formatstring */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3921
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
3922 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
3923 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
3924 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3925 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
3926 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
3927 (*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
3928 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
3929 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
3930 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3931 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
3932 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3933 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
3934 "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
3935 return NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3936 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3937
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
3938 /* 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
3939 * 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
3940 * 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
3941 * 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
3942 * 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
3943 * 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
3944 */
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3945 #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
3946 #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
3947 #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
3948 #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
3949 #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
3950
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3951 /* 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
3952
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3953 static PyObject *
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3954 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
3955 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3956 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3957 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
3958 double x;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3959
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3960 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
3961 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
3962 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
3963 "not %.200s", Py_TYPE(v)->tp_name);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3964 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
3965 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3966
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3967 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
3968 prec = 6;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3969
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3970 p = PyOS_double_to_string(x, type, prec,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3971 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3972
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3973 if (p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3974 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3975 result = PyString_FromStringAndSize(p, strlen(p));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3976 PyMem_Free(p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3977 return result;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3978 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3979
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3980 /* _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
3981 * 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
3982 * 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
3983 * 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
3984 * . *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
3985 * *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
3986 * 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
3987 * 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
3988 * "-"? ("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
3989 * "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
3990 * 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
3991 * 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
3992 * 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
3993 * 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
3994 * 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
3995 * 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
3996 * 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
3997 *
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 * 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
3999 * 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
4000 */
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
4001 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
4002 _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
4003 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
4004 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4005 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
4006 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
4007 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
4008 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
4009 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
4010 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
4011 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
4012 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
4013
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4014 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
4015 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
4016 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
4017 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
4018 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4019 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
4020 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
4021 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4022 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
4023 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
4024 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
4025 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
4026 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4027 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4028 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
4029 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4030 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
4031 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
4032
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4033 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
4034 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
4035 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
4036 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
4037 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4038
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4039 /* 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
4040 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
4041 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
4042 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
4043 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4044 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
4045 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
4046 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
4047 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
4048 }
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 = (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
4050 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
4051 --len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4052 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
4053 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4054 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
4055 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
4056 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
4057 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
4058
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4059 /* 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
4060 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
4061 /* 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
4062 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
4063 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
4064 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
4065 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
4066 /* 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
4067 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
4068 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
4069 --numdigits;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4070 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4071 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4072 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
4073 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
4074 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
4075 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
4076 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
4077 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
4078 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4079 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4080 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
4081 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
4082 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
4083 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
4084 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
4085 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4086 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
4087 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
4088 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4089
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4090 /* 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
4091 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
4092 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
4093 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
4094 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
4095 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
4096 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
4097 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
4098 }
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 = 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
4100 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
4101 *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
4102 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
4103 *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
4104 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
4105 *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
4106 *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
4107 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
4108 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
4109 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
4110 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
4111 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4112
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4113 /* 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
4114 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
4115 /* 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
4116 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
4117 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
4118 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
4119 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
4120 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4121 *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
4122 *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
4123 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
4124 }
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
4125
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
4126 Py_LOCAL_INLINE(int)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4127 formatint(char *buf, size_t buflen, int flags,
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4128 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
4129 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4130 /* 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
4131 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
4132 + 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
4133 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
4134 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
4135 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
4136
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4137 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
4138 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
4139 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
4140 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
4141 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
4142 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4143 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
4144 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
4145 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4146 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
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 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4149 sign = "";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4150 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
4151 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
4152
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4153 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
4154 (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
4155 /* 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
4156 * 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
4157 * - 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
4158 * 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
4159 * %#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
4160 * 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
4161 * - 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
4162 * 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
4163 * (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
4164 * - 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
4165 * 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
4166 * 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
4167 *
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4168 * 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
4169 * 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
4170 * 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
4171 *
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4172 * 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
4173 * 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
4174 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4175 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
4176 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
4177 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4178 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4179 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
4180 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
4181 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
4182 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4183
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4184 /* 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
4185 * 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
4186 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4187 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
4188 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
4189 "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
4190 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
4191 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4192 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
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 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4195 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
4196 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
4197 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4198
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
4199 Py_LOCAL_INLINE(int)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4200 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
4201 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4202 /* 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
4203 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
4204 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
4205 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
4206 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4207 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4208 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
4209 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
4210 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4211 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
4212 return 1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4213 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4214
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4215 /* 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
4216
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
4217 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
4218 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
4219 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
4220 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
4221 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
4222 */
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4223 #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
4224
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4225 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
4226 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
4227 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4228 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
4229 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
4230 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
4231 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
4232 PyObject *result, *orig_args;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4233 #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
4234 PyObject *v, *w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4235 #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
4236 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
4237 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
4238 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
4239 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
4240 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4241 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
4242 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
4243 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
4244 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
4245 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
4246 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
4247 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
4248 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
4249 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
4250 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
4251 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
4252 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4253 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4254 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
4255 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
4256 }
78792
2801bf875a24 use the stricter PyMapping_Check (closes #15801)
Benjamin Peterson <benjamin@python.org>
parents: 77292
diff changeset
4257 if (PyMapping_Check(args) && !PyTuple_Check(args) &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4258 !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
4259 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
4260 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
4261 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
4262 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
4263 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
4264 reslen += rescnt;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4265 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
4266 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
4267 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
4268 + 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
4269 --rescnt;
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 *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
4272 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4273 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4274 /* 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
4275 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
4276 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
4277 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
4278 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
4279 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
4280 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
4281 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
4282 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
4283 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
4284 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
4285 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
4286 char formatbuf[FORMATBUFLEN];
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4287 /* For format{int,char}() */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4288 #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
4289 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
4290 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
4291 #endif
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4292
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4293 fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4294 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
4295 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
4296 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
4297 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
4298 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
4299
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4300 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
4301 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
4302 "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
4303 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
4304 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4305 ++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 --fmtcnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4307 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
4308 /* 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
4309 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
4310 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 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
4313 ++pcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4314 fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4315 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4316 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
4317 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
4318 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
4319 "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
4320 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
4321 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4322 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
4323 keylen);
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 (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
4325 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
4326 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
4327 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
4328 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
4329 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4330 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
4331 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
4332 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
4333 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
4334 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4335 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
4336 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
4337 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
4338 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4339 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
4340 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
4341 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
4342 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
4343 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
4344 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
4345 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
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 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4348 }
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 (c == '*') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4350 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
4351 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
4352 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
4353 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
4354 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
4355 "* 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
4356 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
4357 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4358 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
4359 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
4360 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
4361 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
4362 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4363 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
4364 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
4365 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4366 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
4367 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
4368 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
4369 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
4370 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
4371 break;
80014
21fb1767e185 Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 79590
diff changeset
4372 if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4373 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
4374 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
4375 "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
4376 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
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 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
4379 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4380 }
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 (c == '.') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4382 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
4383 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
4384 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
4385 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
4386 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
4387 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
4388 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
4389 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
4390 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
4391 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
4392 "* 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
4393 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
4394 }
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 = 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
4396 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
4397 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
4398 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
4399 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
4400 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4401 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
4402 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
4403 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
4404 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
4405 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
4406 break;
80014
21fb1767e185 Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 79590
diff changeset
4407 if (prec > (INT_MAX - ((int)c - '0')) / 10) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4408 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
4409 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
4410 "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
4411 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
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 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
4414 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4415 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4416 } /* prec */
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 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
4419 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
4420 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
4421 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4422 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4423 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
4424 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
4425 "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
4426 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
4427 }
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 (c != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4429 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
4430 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
4431 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
4432 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4433 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
4434 fill = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4435 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
4436 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4437 pbuf = "%";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4438 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
4439 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4440 case 's':
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4441 #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
4442 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
4443 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
4444 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
4445 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
4446 }
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 #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
4448 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
4449 #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
4450 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
4451 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
4452 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
4453 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
4454 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
4455 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4456 #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
4457 /* 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
4458 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
4459 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
4460 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
4461 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
4462 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
4463 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
4464 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
4465 "%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
4466 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
4467 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
4468 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4469 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
4470 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
4471 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
4472 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
4473 break;
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 'i':
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 'd':
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 'u':
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 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4478 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
4479 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
4480 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
4481 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
4482 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
4483 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
4484 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
4485
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4486 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
4487 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
4488 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
4489 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4490 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4491 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
4492 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
4493 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4494 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
4495 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
4496 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
4497 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
4498 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
4499 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
4500 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
4501 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
4502 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
4503 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
4504 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
4505 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4506 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
4507 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
4508
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4509 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
4510 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
4511 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
4512 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
4513 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
4514 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
4515 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
4516 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
4517 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4518 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4519 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
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 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4522 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4523 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
4524 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
4525 "%%%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
4526 "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
4527 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
4528 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4529 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
4530 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
4531 break;
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 'e':
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 'E':
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 'f':
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 'F':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4536 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
4537 case 'G':
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4538 temp = formatfloat(v, flags, prec, c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4539 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
4540 goto error;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4541 pbuf = PyString_AS_STRING(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4542 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
4543 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
4544 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
4545 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
4546 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4547 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
4548 #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
4549 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
4550 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
4551 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
4552 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
4553 }
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4554 #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
4555 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
4556 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
4557 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
4558 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
4559 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4560 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4561 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
4562 "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
4563 "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
4564 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
4565 (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
4566 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
4567 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
4568 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4569 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
4570 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
4571 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
4572 len--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4573 }
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_SIGN)
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 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
4577 sign = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4578 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4579 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
4580 }
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 (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
4582 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
4583 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
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 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
4586 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
4587 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
4588 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
4589 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
4590 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
4591 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4592 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
4593 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
4594 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
4595 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4596 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
4597 + 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
4598 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4599 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
4600 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
4601 *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
4602 rescnt--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4603 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
4604 width--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4605 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4606 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
4607 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
4608 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
4609 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
4610 *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
4611 *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
4612 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4613 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
4614 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
4615 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
4616 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
4617 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
4618 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4619 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
4620 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4621 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4622 *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
4623 } 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
4624 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4625 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
4626 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
4627 *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
4628 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
4629 (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
4630 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
4631 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
4632 *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
4633 *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
4634 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4635 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4636 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
4637 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
4638 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
4639 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
4640 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4641 *res++ = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4642 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4643 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
4644 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
4645 "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
4646 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
4647 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
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 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
4650 } /* '%' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4651 } /* 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
4652 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
4653 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
4654 "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
4655 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
4656 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4657 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
4658 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
4659 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4660 if (_PyString_Resize(&result, reslen - rescnt))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4661 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
4662 return result;
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4663
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4664 #ifdef Py_USING_UNICODE
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4665 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
4666 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
4667 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
4668 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
4669 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4670 /* 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
4671 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
4672 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
4673 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
4674 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
4675 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
4676 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
4677 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
4678 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
4679 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
4680 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
4681 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4682 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
4683 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4684 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
4685 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
4686 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4687 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
4688 /* 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
4689 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
4690 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
4691 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
4692 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
4693 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
4694 (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
4695 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
4696 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
4697 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
4698 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
4699 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
4700 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
4701 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
4702 /* 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
4703 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
4704 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
4705 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
4706 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
4707 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
4708 return w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4709 #endif /* Py_USING_UNICODE */
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4710
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4711 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
4712 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
4713 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
4714 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
4715 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4716 return NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4717 }
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4718
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4719 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4720 PyString_InternInPlace(PyObject **p)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4721 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4722 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
4723 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
4724 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
4725 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
4726 /* 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
4727 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
4728 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
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 (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
4731 return;
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 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
4734 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
4735 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
4736 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4737 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4738 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4739 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
4740 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
4741 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
4742 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
4743 *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
4744 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4745 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4746
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4747 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
4748 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
4749 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4750 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4751 /* 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
4752 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
4753 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
4754 PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4755 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4756
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4757 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4758 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
4759 {
75133
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_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
4761 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
4762 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
4763 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
4764 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4765 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4766
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4767
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4768 PyObject *
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4769 PyString_InternFromString(const char *cp)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4770 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4771 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
4772 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
4773 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
4774 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
4775 return s;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4776 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4777
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4778 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4779 PyString_Fini(void)
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4780 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4781 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
4782 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
4783 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
4784 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
4785 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4786 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
4787 nullstring = NULL;
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4788 }
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4789
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4790 void _Py_ReleaseInternedStrings(void)
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4791 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4792 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
4793 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
4794 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
4795 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
4796
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4797 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
4798 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4799 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
4800 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
4801 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
4802 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4803 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4804
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4805 /* 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
4806 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
4807 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
4808 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
4809
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 = 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
4811 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
4812 n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4813 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
4814 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
4815 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
4816 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
4817 /* 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
4818 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4819 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
4820 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
4821 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
4822 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4823 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
4824 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
4825 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
4826 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4827 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4828 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
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 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
4831 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4832 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
4833 "%" 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
4834 "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
4835 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
4836 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
4837 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
4838 interned = NULL;
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4839 }