blob: 0068bda9d2dfb73af65b91c0fd585ef0b12d3e28 [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)) {
279 goto bail;
280 }
281 Py_DECREF(chunk);
282 }
283 next++;
284 if (c == '"') {
285 end = next;
286 break;
287 }
288 if (next == len) {
289 raise_errmsg("Unterminated string starting at", pystr, begin);
290 goto bail;
291 }
292 c = buf[next];
293 if (c != 'u') {
294 /* Non-unicode backslash escapes */
295 end = next + 1;
296 switch (c) {
297 case '"': break;
298 case '\\': break;
299 case '/': break;
300 case 'b': c = '\b'; break;
301 case 'f': c = '\f'; break;
302 case 'n': c = '\n'; break;
303 case 'r': c = '\r'; break;
304 case 't': c = '\t'; break;
305 default: c = 0;
306 }
307 if (c == 0) {
308 raise_errmsg("Invalid \\escape", pystr, end - 2);
309 goto bail;
310 }
311 }
312 else {
313 c = 0;
314 next++;
315 end = next + 4;
316 if (end >= len) {
317 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
318 goto bail;
319 }
320 /* Decode 4 hex digits */
321 for (; next < end; next++) {
322 Py_ssize_t shl = (end - next - 1) << 2;
323 Py_UNICODE digit = buf[next];
324 switch (digit) {
325 case '0': case '1': case '2': case '3': case '4':
326 case '5': case '6': case '7': case '8': case '9':
327 c |= (digit - '0') << shl; break;
328 case 'a': case 'b': case 'c': case 'd': case 'e':
329 case 'f':
330 c |= (digit - 'a' + 10) << shl; break;
331 case 'A': case 'B': case 'C': case 'D': case 'E':
332 case 'F':
333 c |= (digit - 'A' + 10) << shl; break;
334 default:
335 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
336 goto bail;
337 }
338 }
339#ifdef Py_UNICODE_WIDE
340 /* Surrogate pair */
341 if (c >= 0xd800 && c <= 0xdbff) {
342 Py_UNICODE c2 = 0;
343 if (end + 6 >= len) {
344 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
345 end - 5);
346 }
347 if (buf[next++] != '\\' || buf[next++] != 'u') {
348 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
349 end - 5);
350 }
351 end += 6;
352 /* Decode 4 hex digits */
353 for (; next < end; next++) {
354 Py_ssize_t shl = (end - next - 1) << 2;
355 Py_UNICODE digit = buf[next];
356 switch (digit) {
357 case '0': case '1': case '2': case '3': case '4':
358 case '5': case '6': case '7': case '8': case '9':
359 c2 |= (digit - '0') << shl; break;
360 case 'a': case 'b': case 'c': case 'd': case 'e':
361 case 'f':
362 c2 |= (digit - 'a' + 10) << shl; break;
363 case 'A': case 'B': case 'C': case 'D': case 'E':
364 case 'F':
365 c2 |= (digit - 'A' + 10) << shl; break;
366 default:
367 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
368 goto bail;
369 }
370 }
371 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
372 }
373#endif
374 }
375 chunk = PyUnicode_FromUnicode(&c, 1);
376 if (chunk == NULL) {
377 goto bail;
378 }
379 if (PyList_Append(chunks, chunk)) {
380 goto bail;
381 }
382 Py_DECREF(chunk);
383 }
384
385 rval = join_list_unicode(chunks);
386 if (rval == NULL) {
387 goto bail;
388 }
389 Py_DECREF(chunks);
390 chunks = NULL;
391 return Py_BuildValue("(Nn)", rval, end);
392bail:
393 Py_XDECREF(chunks);
394 return NULL;
395}
396
397
398static PyObject *
399scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict)
400{
401 PyObject *rval;
402 Py_ssize_t len = PyUnicode_GET_SIZE(pystr);
403 Py_ssize_t begin = end - 1;
404 Py_ssize_t next = begin;
405 const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr);
406 PyObject *chunks = PyList_New(0);
407 if (chunks == NULL) {
408 goto bail;
409 }
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000410 if (end < 0 || len <= end) {
411 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
412 goto bail;
413 }
Christian Heimes90540002008-05-08 14:29:10 +0000414 while (1) {
415 /* Find the end of the string or the next escape */
416 Py_UNICODE c = 0;
417 PyObject *chunk = NULL;
418 for (next = end; next < len; next++) {
419 c = buf[next];
420 if (c == '"' || c == '\\') {
421 break;
422 }
423 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000424 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000425 goto bail;
426 }
427 }
428 if (!(c == '"' || c == '\\')) {
429 raise_errmsg("Unterminated string starting at", pystr, begin);
430 goto bail;
431 }
432 /* Pick up this chunk if it's not zero length */
433 if (next != end) {
434 chunk = PyUnicode_FromUnicode(&buf[end], next - end);
435 if (chunk == NULL) {
436 goto bail;
437 }
438 if (PyList_Append(chunks, chunk)) {
439 goto bail;
440 }
441 Py_DECREF(chunk);
442 }
443 next++;
444 if (c == '"') {
445 end = next;
446 break;
447 }
448 if (next == len) {
449 raise_errmsg("Unterminated string starting at", pystr, begin);
450 goto bail;
451 }
452 c = buf[next];
453 if (c != 'u') {
454 /* Non-unicode backslash escapes */
455 end = next + 1;
456 switch (c) {
457 case '"': break;
458 case '\\': break;
459 case '/': break;
460 case 'b': c = '\b'; break;
461 case 'f': c = '\f'; break;
462 case 'n': c = '\n'; break;
463 case 'r': c = '\r'; break;
464 case 't': c = '\t'; break;
465 default: c = 0;
466 }
467 if (c == 0) {
468 raise_errmsg("Invalid \\escape", pystr, end - 2);
469 goto bail;
470 }
471 }
472 else {
473 c = 0;
474 next++;
475 end = next + 4;
476 if (end >= len) {
477 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
478 goto bail;
479 }
480 /* Decode 4 hex digits */
481 for (; next < end; next++) {
482 Py_ssize_t shl = (end - next - 1) << 2;
483 Py_UNICODE digit = buf[next];
484 switch (digit) {
485 case '0': case '1': case '2': case '3': case '4':
486 case '5': case '6': case '7': case '8': case '9':
487 c |= (digit - '0') << shl; break;
488 case 'a': case 'b': case 'c': case 'd': case 'e':
489 case 'f':
490 c |= (digit - 'a' + 10) << shl; break;
491 case 'A': case 'B': case 'C': case 'D': case 'E':
492 case 'F':
493 c |= (digit - 'A' + 10) << shl; break;
494 default:
495 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
496 goto bail;
497 }
498 }
499#ifdef Py_UNICODE_WIDE
500 /* Surrogate pair */
501 if (c >= 0xd800 && c <= 0xdbff) {
502 Py_UNICODE c2 = 0;
503 if (end + 6 >= len) {
504 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
505 end - 5);
506 }
507 if (buf[next++] != '\\' || buf[next++] != 'u') {
508 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr,
509 end - 5);
510 }
511 end += 6;
512 /* Decode 4 hex digits */
513 for (; next < end; next++) {
514 Py_ssize_t shl = (end - next - 1) << 2;
515 Py_UNICODE digit = buf[next];
516 switch (digit) {
517 case '0': case '1': case '2': case '3': case '4':
518 case '5': case '6': case '7': case '8': case '9':
519 c2 |= (digit - '0') << shl; break;
520 case 'a': case 'b': case 'c': case 'd': case 'e':
521 case 'f':
522 c2 |= (digit - 'a' + 10) << shl; break;
523 case 'A': case 'B': case 'C': case 'D': case 'E':
524 case 'F':
525 c2 |= (digit - 'A' + 10) << shl; break;
526 default:
527 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
528 goto bail;
529 }
530 }
531 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
532 }
533#endif
534 }
535 chunk = PyUnicode_FromUnicode(&c, 1);
536 if (chunk == NULL) {
537 goto bail;
538 }
539 if (PyList_Append(chunks, chunk)) {
540 goto bail;
541 }
542 Py_DECREF(chunk);
543 }
544
545 rval = join_list_unicode(chunks);
546 if (rval == NULL) {
547 goto bail;
548 }
549 Py_DECREF(chunks);
550 chunks = NULL;
551 return Py_BuildValue("(Nn)", rval, end);
552bail:
553 Py_XDECREF(chunks);
554 return NULL;
555}
556
557PyDoc_STRVAR(pydoc_scanstring,
558"scanstring(str_or_bytes, end, encoding) -> (bytes, end)\n");
559
560static PyObject *
561py_scanstring(PyObject* self, PyObject *args)
562{
563 PyObject *pystr;
564 Py_ssize_t end;
565 char *encoding = NULL;
566 int strict = 0;
567 if (!PyArg_ParseTuple(args, "On|zi:scanstring", &pystr, &end, &encoding, &strict)) {
568 return NULL;
569 }
570 if (encoding == NULL) {
571 encoding = DEFAULT_ENCODING;
572 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000573 if (PyBytes_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000574 return scanstring_str(pystr, end, encoding, strict);
575 }
576 else if (PyUnicode_Check(pystr)) {
577 return scanstring_unicode(pystr, end, strict);
578 }
579 else {
580 PyErr_Format(PyExc_TypeError,
581 "first argument must be a string or bytes, not %.80s",
582 Py_TYPE(pystr)->tp_name);
583 return NULL;
584 }
585}
586
587PyDoc_STRVAR(pydoc_encode_basestring_ascii,
588"encode_basestring_ascii(str_or_bytes) -> bytes\n");
589
590static PyObject *
591py_encode_basestring_ascii(PyObject* self, PyObject *pystr)
592{
593 PyObject *rval;
594 /* METH_O */
Christian Heimes72b710a2008-05-26 13:28:38 +0000595 if (PyBytes_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000596 rval = ascii_escape_str(pystr);
597 }
598 else if (PyUnicode_Check(pystr)) {
599 rval = ascii_escape_unicode(pystr);
600 }
601 else {
602 PyErr_Format(PyExc_TypeError,
603 "first argument must be a string or unicode, not %.80s",
604 Py_TYPE(pystr)->tp_name);
605 return NULL;
606 }
Benjamin Petersona13d4752008-10-16 21:17:24 +0000607 if (rval != NULL && PyBytes_Check(rval)) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000608 PyObject *urval = PyUnicode_DecodeASCII(PyBytes_AS_STRING(rval), PyBytes_GET_SIZE(rval), NULL);
Christian Heimes90540002008-05-08 14:29:10 +0000609 Py_DECREF(rval);
610 return urval;
611 }
612 return rval;
613}
614
615static PyMethodDef json_methods[] = {
616 {"encode_basestring_ascii", (PyCFunction)py_encode_basestring_ascii,
617 METH_O, pydoc_encode_basestring_ascii},
618 {"scanstring", (PyCFunction)py_scanstring, METH_VARARGS,
619 pydoc_scanstring},
620 {NULL, NULL, 0, NULL}
621};
622
623PyDoc_STRVAR(module_doc,
624"json speedups\n");
625
Martin v. Löwis1a214512008-06-11 05:26:20 +0000626static struct PyModuleDef jsonmodule = {
627 PyModuleDef_HEAD_INIT,
628 "_json",
629 module_doc,
630 -1,
631 json_methods,
632 NULL,
633 NULL,
634 NULL,
635 NULL
636};
637
638PyObject*
639PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +0000640{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000641 return PyModule_Create(&jsonmodule);
Christian Heimes90540002008-05-08 14:29:10 +0000642}