blob: 9c35f64484f051815ea8d97f3816e58b11519d56 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001#include "Python.h"
2
3#define DEFAULT_ENCODING "utf-8"
4#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
5#define MIN_EXPANSION 6
6
7#ifdef Py_UNICODE_WIDE
8#define MAX_EXPANSION (2 * MIN_EXPANSION)
9#else
10#define MAX_EXPANSION MIN_EXPANSION
11#endif
12
13static Py_ssize_t
14ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars)
15{
16 Py_UNICODE x;
17 output[chars++] = '\\';
18 switch (c) {
19 case '\\': output[chars++] = (char)c; break;
20 case '"': output[chars++] = (char)c; break;
21 case '\b': output[chars++] = 'b'; break;
22 case '\f': output[chars++] = 'f'; break;
23 case '\n': output[chars++] = 'n'; break;
24 case '\r': output[chars++] = 'r'; break;
25 case '\t': output[chars++] = 't'; break;
26 default:
27#ifdef Py_UNICODE_WIDE
28 if (c >= 0x10000) {
29 /* UTF-16 surrogate pair */
30 Py_UNICODE v = c - 0x10000;
31 c = 0xd800 | ((v >> 10) & 0x3ff);
32 output[chars++] = 'u';
33 x = (c & 0xf000) >> 12;
34 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
35 x = (c & 0x0f00) >> 8;
36 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
37 x = (c & 0x00f0) >> 4;
38 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
39 x = (c & 0x000f);
40 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
41 c = 0xdc00 | (v & 0x3ff);
42 output[chars++] = '\\';
43 }
44#endif
45 output[chars++] = 'u';
46 x = (c & 0xf000) >> 12;
47 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
48 x = (c & 0x0f00) >> 8;
49 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
50 x = (c & 0x00f0) >> 4;
51 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
52 x = (c & 0x000f);
53 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10);
54 }
55 return chars;
56}
57
58static PyObject *
59ascii_escape_unicode(PyObject *pystr)
60{
61 Py_ssize_t i;
62 Py_ssize_t input_chars;
63 Py_ssize_t output_size;
64 Py_ssize_t chars;
65 PyObject *rval;
66 char *output;
67 Py_UNICODE *input_unicode;
68
69 input_chars = PyUnicode_GET_SIZE(pystr);
70 input_unicode = PyUnicode_AS_UNICODE(pystr);
71 /* One char input can be up to 6 chars output, estimate 4 of these */
72 output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
Christian Heimes72b710a2008-05-26 13:28:38 +000073 rval = PyBytes_FromStringAndSize(NULL, output_size);
Christian Heimes90540002008-05-08 14:29:10 +000074 if (rval == NULL) {
75 return NULL;
76 }
Christian Heimes72b710a2008-05-26 13:28:38 +000077 output = PyBytes_AS_STRING(rval);
Christian Heimes90540002008-05-08 14:29:10 +000078 chars = 0;
79 output[chars++] = '"';
80 for (i = 0; i < input_chars; i++) {
81 Py_UNICODE c = input_unicode[i];
82 if (S_CHAR(c)) {
83 output[chars++] = (char)c;
84 }
85 else {
86 chars = ascii_escape_char(c, output, chars);
87 }
88 if (output_size - chars < (1 + MAX_EXPANSION)) {
89 /* There's more than four, so let's resize by a lot */
90 output_size *= 2;
91 /* This is an upper bound */
92 if (output_size > 2 + (input_chars * MAX_EXPANSION)) {
93 output_size = 2 + (input_chars * MAX_EXPANSION);
94 }
Christian Heimes72b710a2008-05-26 13:28:38 +000095 if (_PyBytes_Resize(&rval, output_size) == -1) {
Christian Heimes90540002008-05-08 14:29:10 +000096 return NULL;
97 }
Christian Heimes72b710a2008-05-26 13:28:38 +000098 output = PyBytes_AS_STRING(rval);
Christian Heimes90540002008-05-08 14:29:10 +000099 }
100 }
101 output[chars++] = '"';
Christian Heimes72b710a2008-05-26 13:28:38 +0000102 if (_PyBytes_Resize(&rval, chars) == -1) {
Christian Heimes90540002008-05-08 14:29:10 +0000103 return NULL;
104 }
105 return rval;
106}
107
108static PyObject *
109ascii_escape_str(PyObject *pystr)
110{
111 Py_ssize_t i;
112 Py_ssize_t input_chars;
113 Py_ssize_t output_size;
114 Py_ssize_t chars;
115 PyObject *rval;
116 char *output;
117 char *input_str;
118
Christian Heimes72b710a2008-05-26 13:28:38 +0000119 input_chars = PyBytes_GET_SIZE(pystr);
120 input_str = PyBytes_AS_STRING(pystr);
Christian Heimes90540002008-05-08 14:29:10 +0000121 /* One char input can be up to 6 chars output, estimate 4 of these */
122 output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
Christian Heimes72b710a2008-05-26 13:28:38 +0000123 rval = PyBytes_FromStringAndSize(NULL, output_size);
Christian Heimes90540002008-05-08 14:29:10 +0000124 if (rval == NULL) {
125 return NULL;
126 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000127 output = PyBytes_AS_STRING(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000128 chars = 0;
129 output[chars++] = '"';
130 for (i = 0; i < input_chars; i++) {
131 Py_UNICODE c = (Py_UNICODE)input_str[i];
132 if (S_CHAR(c)) {
133 output[chars++] = (char)c;
134 }
135 else if (c > 0x7F) {
136 /* We hit a non-ASCII character, bail to unicode mode */
137 PyObject *uni;
138 Py_DECREF(rval);
139 uni = PyUnicode_DecodeUTF8(input_str, input_chars, "strict");
140 if (uni == NULL) {
141 return NULL;
142 }
143 rval = ascii_escape_unicode(uni);
144 Py_DECREF(uni);
145 return rval;
146 }
147 else {
148 chars = ascii_escape_char(c, output, chars);
149 }
150 /* An ASCII char can't possibly expand to a surrogate! */
151 if (output_size - chars < (1 + MIN_EXPANSION)) {
152 /* There's more than four, so let's resize by a lot */
153 output_size *= 2;
154 if (output_size > 2 + (input_chars * MIN_EXPANSION)) {
155 output_size = 2 + (input_chars * MIN_EXPANSION);
156 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000157 if (_PyBytes_Resize(&rval, output_size) == -1) {
Christian Heimes90540002008-05-08 14:29:10 +0000158 return NULL;
159 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000160 output = PyBytes_AS_STRING(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000161 }
162 }
163 output[chars++] = '"';
Christian Heimes72b710a2008-05-26 13:28:38 +0000164 if (_PyBytes_Resize(&rval, chars) == -1) {
Christian Heimes90540002008-05-08 14:29:10 +0000165 return NULL;
166 }
167 return rval;
168}
169
170void
171raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
172{
173 static PyObject *errmsg_fn = NULL;
174 PyObject *pymsg;
175 if (errmsg_fn == NULL) {
176 PyObject *decoder = PyImport_ImportModule("json.decoder");
177 if (decoder == NULL)
178 return;
179 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
180 if (errmsg_fn == NULL)
181 return;
Benjamin Petersona13d4752008-10-16 21:17:24 +0000182 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000183 }
184 pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000185 if (pymsg) {
186 PyErr_SetObject(PyExc_ValueError, pymsg);
187 Py_DECREF(pymsg);
188 }
Christian Heimes90540002008-05-08 14:29:10 +0000189/*
190
191def linecol(doc, pos):
192 lineno = doc.count('\n', 0, pos) + 1
193 if lineno == 1:
194 colno = pos
195 else:
196 colno = pos - doc.rindex('\n', 0, pos)
197 return lineno, colno
198
199def errmsg(msg, doc, pos, end=None):
200 lineno, colno = linecol(doc, pos)
201 if end is None:
202 return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
203 endlineno, endcolno = linecol(doc, end)
204 return '%s: line %d column %d - line %d column %d (char %d - %d)' % (
205 msg, lineno, colno, endlineno, endcolno, pos, end)
206
207*/
208}
209
210static PyObject *
211join_list_unicode(PyObject *lst)
212{
213 static PyObject *ustr = NULL;
214 static PyObject *joinstr = NULL;
215 if (ustr == NULL) {
216 Py_UNICODE c = 0;
217 ustr = PyUnicode_FromUnicode(&c, 0);
218 }
219 if (joinstr == NULL) {
220 joinstr = PyUnicode_InternFromString("join");
221 }
222 if (joinstr == NULL || ustr == NULL) {
223 return NULL;
224 }
225 return PyObject_CallMethodObjArgs(ustr, joinstr, lst, NULL);
226}
227
228static PyObject *
229scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict)
230{
231 PyObject *rval;
Christian Heimes72b710a2008-05-26 13:28:38 +0000232 Py_ssize_t len = PyBytes_GET_SIZE(pystr);
Christian Heimes90540002008-05-08 14:29:10 +0000233 Py_ssize_t begin = end - 1;
234 Py_ssize_t next = begin;
Christian Heimes72b710a2008-05-26 13:28:38 +0000235 char *buf = PyBytes_AS_STRING(pystr);
Christian Heimes90540002008-05-08 14:29:10 +0000236 Py_buffer info;
237 PyObject *chunks = PyList_New(0);
238 if (chunks == NULL) {
239 goto bail;
240 }
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000241 if (end < 0 || len <= end) {
242 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
243 goto bail;
244 }
Christian Heimes90540002008-05-08 14:29:10 +0000245 while (1) {
246 /* Find the end of the string or the next escape */
247 Py_UNICODE c = 0;
248 PyObject *chunk = NULL;
249 for (next = end; next < len; next++) {
250 c = buf[next];
251 if (c == '"' || c == '\\') {
252 break;
253 }
254 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000255 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000256 goto bail;
257 }
258 }
259 if (!(c == '"' || c == '\\')) {
260 raise_errmsg("Unterminated string starting at", pystr, begin);
261 goto bail;
262 }
263 /* Pick up this chunk if it's not zero length */
264 if (next != end) {
Amaury Forgeot d'Arccb0cdce2008-05-08 20:56:43 +0000265 PyObject *strchunk;
Martin v. Löwis423be952008-08-13 15:53:07 +0000266 if (PyBuffer_FillInfo(&info, NULL, &buf[end], next - end, 1, 0) < 0) {
Christian Heimes90540002008-05-08 14:29:10 +0000267 goto bail;
268 }
Antoine Pitrouee58fa42008-08-19 18:22:14 +0000269 strchunk = PyMemoryView_FromBuffer(&info);
Christian Heimes90540002008-05-08 14:29:10 +0000270 if (strchunk == NULL) {
271 goto bail;
272 }
273 chunk = PyUnicode_FromEncodedObject(strchunk, encoding, NULL);
274 Py_DECREF(strchunk);
275 if (chunk == NULL) {
276 goto bail;
277 }
278 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000279 Py_DECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000280 goto bail;
281 }
282 Py_DECREF(chunk);
283 }
284 next++;
285 if (c == '"') {
286 end = next;
287 break;
288 }
289 if (next == len) {
290 raise_errmsg("Unterminated string starting at", pystr, begin);
291 goto bail;
292 }
293 c = buf[next];
294 if (c != 'u') {
295 /* Non-unicode backslash escapes */
296 end = next + 1;
297 switch (c) {
298 case '"': break;
299 case '\\': break;
300 case '/': break;
301 case 'b': c = '\b'; break;
302 case 'f': c = '\f'; break;
303 case 'n': c = '\n'; break;
304 case 'r': c = '\r'; break;
305 case 't': c = '\t'; break;
306 default: c = 0;
307 }
308 if (c == 0) {
309 raise_errmsg("Invalid \\escape", pystr, end - 2);
310 goto bail;
311 }
312 }
313 else {
314 c = 0;
315 next++;
316 end = next + 4;
317 if (end >= len) {
318 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
319 goto bail;
320 }
321 /* Decode 4 hex digits */
322 for (; next < end; next++) {
323 Py_ssize_t shl = (end - next - 1) << 2;
324 Py_UNICODE digit = buf[next];
325 switch (digit) {
326 case '0': case '1': case '2': case '3': case '4':
327 case '5': case '6': case '7': case '8': case '9':
328 c |= (digit - '0') << shl; break;
329 case 'a': case 'b': case 'c': case 'd': case 'e':
330 case 'f':
331 c |= (digit - 'a' + 10) << shl; break;
332 case 'A': case 'B': case 'C': case 'D': case 'E':
333 case 'F':
334 c |= (digit - 'A' + 10) << shl; break;
335 default:
336 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
337 goto bail;
338 }
339 }
340#ifdef Py_UNICODE_WIDE
341 /* Surrogate pair */
342 if (c >= 0xd800 && c <= 0xdbff) {
343 Py_UNICODE c2 = 0;
344 if (end + 6 >= len) {
345 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
346 end - 5);
347 }
348 if (buf[next++] != '\\' || buf[next++] != 'u') {
349 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
350 end - 5);
351 }
352 end += 6;
353 /* Decode 4 hex digits */
354 for (; next < end; next++) {
355 Py_ssize_t shl = (end - next - 1) << 2;
356 Py_UNICODE digit = buf[next];
357 switch (digit) {
358 case '0': case '1': case '2': case '3': case '4':
359 case '5': case '6': case '7': case '8': case '9':
360 c2 |= (digit - '0') << shl; break;
361 case 'a': case 'b': case 'c': case 'd': case 'e':
362 case 'f':
363 c2 |= (digit - 'a' + 10) << shl; break;
364 case 'A': case 'B': case 'C': case 'D': case 'E':
365 case 'F':
366 c2 |= (digit - 'A' + 10) << shl; break;
367 default:
368 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
369 goto bail;
370 }
371 }
372 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
373 }
374#endif
375 }
376 chunk = PyUnicode_FromUnicode(&c, 1);
377 if (chunk == NULL) {
378 goto bail;
379 }
380 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000381 Py_DECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000382 goto bail;
383 }
384 Py_DECREF(chunk);
385 }
386
387 rval = join_list_unicode(chunks);
388 if (rval == NULL) {
389 goto bail;
390 }
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000391 Py_CLEAR(chunks);
Christian Heimes90540002008-05-08 14:29:10 +0000392 return Py_BuildValue("(Nn)", rval, end);
393bail:
394 Py_XDECREF(chunks);
395 return NULL;
396}
397
398
399static PyObject *
400scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict)
401{
402 PyObject *rval;
403 Py_ssize_t len = PyUnicode_GET_SIZE(pystr);
404 Py_ssize_t begin = end - 1;
405 Py_ssize_t next = begin;
406 const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr);
407 PyObject *chunks = PyList_New(0);
408 if (chunks == NULL) {
409 goto bail;
410 }
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000411 if (end < 0 || len <= end) {
412 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
413 goto bail;
414 }
Christian Heimes90540002008-05-08 14:29:10 +0000415 while (1) {
416 /* Find the end of the string or the next escape */
417 Py_UNICODE c = 0;
418 PyObject *chunk = NULL;
419 for (next = end; next < len; next++) {
420 c = buf[next];
421 if (c == '"' || c == '\\') {
422 break;
423 }
424 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000425 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000426 goto bail;
427 }
428 }
429 if (!(c == '"' || c == '\\')) {
430 raise_errmsg("Unterminated string starting at", pystr, begin);
431 goto bail;
432 }
433 /* Pick up this chunk if it's not zero length */
434 if (next != end) {
435 chunk = PyUnicode_FromUnicode(&buf[end], next - end);
436 if (chunk == NULL) {
437 goto bail;
438 }
439 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000440 Py_DECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000441 goto bail;
442 }
443 Py_DECREF(chunk);
444 }
445 next++;
446 if (c == '"') {
447 end = next;
448 break;
449 }
450 if (next == len) {
451 raise_errmsg("Unterminated string starting at", pystr, begin);
452 goto bail;
453 }
454 c = buf[next];
455 if (c != 'u') {
456 /* Non-unicode backslash escapes */
457 end = next + 1;
458 switch (c) {
459 case '"': break;
460 case '\\': break;
461 case '/': break;
462 case 'b': c = '\b'; break;
463 case 'f': c = '\f'; break;
464 case 'n': c = '\n'; break;
465 case 'r': c = '\r'; break;
466 case 't': c = '\t'; break;
467 default: c = 0;
468 }
469 if (c == 0) {
470 raise_errmsg("Invalid \\escape", pystr, end - 2);
471 goto bail;
472 }
473 }
474 else {
475 c = 0;
476 next++;
477 end = next + 4;
478 if (end >= len) {
479 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
480 goto bail;
481 }
482 /* Decode 4 hex digits */
483 for (; next < end; next++) {
484 Py_ssize_t shl = (end - next - 1) << 2;
485 Py_UNICODE digit = buf[next];
486 switch (digit) {
487 case '0': case '1': case '2': case '3': case '4':
488 case '5': case '6': case '7': case '8': case '9':
489 c |= (digit - '0') << shl; break;
490 case 'a': case 'b': case 'c': case 'd': case 'e':
491 case 'f':
492 c |= (digit - 'a' + 10) << shl; break;
493 case 'A': case 'B': case 'C': case 'D': case 'E':
494 case 'F':
495 c |= (digit - 'A' + 10) << shl; break;
496 default:
497 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
498 goto bail;
499 }
500 }
501#ifdef Py_UNICODE_WIDE
502 /* Surrogate pair */
503 if (c >= 0xd800 && c <= 0xdbff) {
504 Py_UNICODE c2 = 0;
505 if (end + 6 >= len) {
506 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
507 end - 5);
508 }
509 if (buf[next++] != '\\' || buf[next++] != 'u') {
510 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
511 end - 5);
512 }
513 end += 6;
514 /* Decode 4 hex digits */
515 for (; next < end; next++) {
516 Py_ssize_t shl = (end - next - 1) << 2;
517 Py_UNICODE digit = buf[next];
518 switch (digit) {
519 case '0': case '1': case '2': case '3': case '4':
520 case '5': case '6': case '7': case '8': case '9':
521 c2 |= (digit - '0') << shl; break;
522 case 'a': case 'b': case 'c': case 'd': case 'e':
523 case 'f':
524 c2 |= (digit - 'a' + 10) << shl; break;
525 case 'A': case 'B': case 'C': case 'D': case 'E':
526 case 'F':
527 c2 |= (digit - 'A' + 10) << shl; break;
528 default:
529 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
530 goto bail;
531 }
532 }
533 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
534 }
535#endif
536 }
537 chunk = PyUnicode_FromUnicode(&c, 1);
538 if (chunk == NULL) {
539 goto bail;
540 }
541 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000542 Py_DECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000543 goto bail;
544 }
545 Py_DECREF(chunk);
546 }
547
548 rval = join_list_unicode(chunks);
549 if (rval == NULL) {
550 goto bail;
551 }
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000552 Py_CLEAR(chunks);
Christian Heimes90540002008-05-08 14:29:10 +0000553 return Py_BuildValue("(Nn)", rval, end);
554bail:
555 Py_XDECREF(chunks);
556 return NULL;
557}
558
559PyDoc_STRVAR(pydoc_scanstring,
560"scanstring(str_or_bytes, end, encoding) -> (bytes, end)\n");
561
562static PyObject *
563py_scanstring(PyObject* self, PyObject *args)
564{
565 PyObject *pystr;
566 Py_ssize_t end;
567 char *encoding = NULL;
568 int strict = 0;
569 if (!PyArg_ParseTuple(args, "On|zi:scanstring", &pystr, &end, &encoding, &strict)) {
570 return NULL;
571 }
572 if (encoding == NULL) {
573 encoding = DEFAULT_ENCODING;
574 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000575 if (PyBytes_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000576 return scanstring_str(pystr, end, encoding, strict);
577 }
578 else if (PyUnicode_Check(pystr)) {
579 return scanstring_unicode(pystr, end, strict);
580 }
581 else {
582 PyErr_Format(PyExc_TypeError,
583 "first argument must be a string or bytes, not %.80s",
584 Py_TYPE(pystr)->tp_name);
585 return NULL;
586 }
587}
588
589PyDoc_STRVAR(pydoc_encode_basestring_ascii,
590"encode_basestring_ascii(str_or_bytes) -> bytes\n");
591
592static PyObject *
593py_encode_basestring_ascii(PyObject* self, PyObject *pystr)
594{
595 PyObject *rval;
596 /* METH_O */
Christian Heimes72b710a2008-05-26 13:28:38 +0000597 if (PyBytes_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000598 rval = ascii_escape_str(pystr);
599 }
600 else if (PyUnicode_Check(pystr)) {
601 rval = ascii_escape_unicode(pystr);
602 }
603 else {
604 PyErr_Format(PyExc_TypeError,
605 "first argument must be a string or unicode, not %.80s",
606 Py_TYPE(pystr)->tp_name);
607 return NULL;
608 }
Benjamin Petersona13d4752008-10-16 21:17:24 +0000609 if (rval != NULL && PyBytes_Check(rval)) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000610 PyObject *urval = PyUnicode_DecodeASCII(PyBytes_AS_STRING(rval), PyBytes_GET_SIZE(rval), NULL);
Christian Heimes90540002008-05-08 14:29:10 +0000611 Py_DECREF(rval);
612 return urval;
613 }
614 return rval;
615}
616
617static PyMethodDef json_methods[] = {
618 {"encode_basestring_ascii", (PyCFunction)py_encode_basestring_ascii,
619 METH_O, pydoc_encode_basestring_ascii},
620 {"scanstring", (PyCFunction)py_scanstring, METH_VARARGS,
621 pydoc_scanstring},
622 {NULL, NULL, 0, NULL}
623};
624
625PyDoc_STRVAR(module_doc,
626"json speedups\n");
627
Martin v. Löwis1a214512008-06-11 05:26:20 +0000628static struct PyModuleDef jsonmodule = {
629 PyModuleDef_HEAD_INIT,
630 "_json",
631 module_doc,
632 -1,
633 json_methods,
634 NULL,
635 NULL,
636 NULL,
637 NULL
638};
639
640PyObject*
641PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +0000642{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000643 return PyModule_Create(&jsonmodule);
Christian Heimes90540002008-05-08 14:29:10 +0000644}