Speed up cPickle's internal output machinery - Code Review (original) (raw)

OLD

NEW

1 #include "Python.h"

1 #include "Python.h"

2 #include "cStringIO.h"

2 #include "cStringIO.h"

3 #include "structmember.h"

3 #include "structmember.h"

4

4

5 PyDoc_STRVAR(cPickle_module_documentation,

5 PyDoc_STRVAR(cPickle_module_documentation,

6 "C implementation and optimization of the Python pickle module.");

6 "C implementation and optimization of the Python pickle module.");

7

7

8 #ifndef Py_eval_input

8 #ifndef Py_eval_input

9 #include <graminit.h>

9 #include <graminit.h>

10 #define Py_eval_input eval_input

10 #define Py_eval_input eval_input

11 #endif /* Py_eval_input */

11 #endif /* Py_eval_input */

12

12

13 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))

13 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))

14

14

15 #define WRITE_BUF_SIZE 256

16

17 /* Bump this when new opcodes are added to the pickle protocol. */

15 /* Bump this when new opcodes are added to the pickle protocol. */

18 #define HIGHEST_PROTOCOL 2

16 #define HIGHEST_PROTOCOL 2

19

17

20 /*

18 /*

21 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since

19 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since

22 * all headers have already been included here, we can safely redefine it.

20 * all headers have already been included here, we can safely redefine it.

23 */

21 */

24 #ifdef UNICODE

22 #ifdef UNICODE

25 # undef UNICODE

23 # undef UNICODE

26 #endif

24 #endif

(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

147 static void

145 static void

148 Pdata_dealloc(Pdata *self)

146 Pdata_dealloc(Pdata *self)

149 {

147 {

150 int i;

148 int i;

151 PyObject **p;

149 PyObject **p;

152

150

153 for (i = self->length, p = self->data; --i >= 0; p++) {

151 for (i = self->length, p = self->data; --i >= 0; p++) {

154 Py_DECREF(*p);

152 Py_DECREF(*p);

155 }

153 }

156 if (self->data)

154 if (self->data)

157 » » free(self->data);

155 » » PyMem_FREE(self->data);

158 PyObject_Del(self);

156 PyObject_Del(self);

159 }

157 }

160

158

161 static PyTypeObject PdataType = {

159 static PyTypeObject PdataType = {

162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,

160 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,

163 (destructor)Pdata_dealloc,

161 (destructor)Pdata_dealloc,

164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""

162 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""

165 };

163 };

166

164

167 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)

165 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)

168

166

169 static PyObject *

167 static PyObject *

170 Pdata_New(void)

168 Pdata_New(void)

171 {

169 {

172 Pdata *self;

170 Pdata *self;

173

171

174 if (!(self = PyObject_New(Pdata, &PdataType)))

172 if (!(self = PyObject_New(Pdata, &PdataType)))

175 return NULL;

173 return NULL;

176 self->size = 8;

174 self->size = 8;

177 self->length = 0;

175 self->length = 0;

178 » self->data = malloc(self->size * sizeof(PyObject*));

176 » self->data = PyMem_NEW(PyObject *, self->size);

179 if (self->data)

177 if (self->data)

180 return (PyObject*)self;

178 return (PyObject*)self;

181 Py_DECREF(self);

179 Py_DECREF(self);

182 return PyErr_NoMemory();

180 return PyErr_NoMemory();

183 }

181 }

184

182

185 static int

183 static int

186 stackUnderflow(void)

184 stackUnderflow(void)

187 {

185 {

188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");

186 PyErr_SetString(UnpicklingError, "unpickling stack underflow");

(...skipping 19 matching lines...) Expand all Loading...

208 }

206 }

209 self->length = clearto;

207 self->length = clearto;

210

208

211 return 0;

209 return 0;

212 }

210 }

213

211

214 static int

212 static int

215 Pdata_grow(Pdata *self)

213 Pdata_grow(Pdata *self)

216 {

214 {

217 int bigger;

215 int bigger;

218 size_t nbytes;

219 PyObject **tmp;

216 PyObject **tmp;

220

217

221 bigger = self->size << 1;

218 bigger = self->size << 1;

222 if (bigger <= 0) /* was 0, or new value overflows */

219 if (bigger <= 0) /* was 0, or new value overflows */

223 goto nomemory;

220 goto nomemory;

224 if ((int)(size_t)bigger != bigger)

221 if ((int)(size_t)bigger != bigger)

225 goto nomemory;

222 goto nomemory;

226 » nbytes = (size_t)bigger * sizeof(PyObject *);

223 » tmp = self->data;

227 » if (nbytes / sizeof(PyObject *) != (size_t)bigger)

224 » PyMem_RESIZE(tmp, PyObject *, bigger);

228 » » goto nomemory;

229 » tmp = realloc(self->data, nbytes);

230 if (tmp == NULL)

225 if (tmp == NULL)

231 goto nomemory;

226 goto nomemory;

232 self->data = tmp;

227 self->data = tmp;

233 self->size = bigger;

228 self->size = bigger;

234 return 0;

229 return 0;

235

230

236 nomemory:

231 nomemory:

237 PyErr_NoMemory();

232 PyErr_NoMemory();

238 return -1;

233 return -1;

239 }

234 }

(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

325

320

326 #define FREE_ARG_TUP(self) { \

321 #define FREE_ARG_TUP(self) { \

327 if (Py_REFCNT(self->arg) > 1) { \

322 if (Py_REFCNT(self->arg) > 1) { \

328 Py_DECREF(self->arg); \

323 Py_DECREF(self->arg); \

329 self->arg=NULL; \

324 self->arg=NULL; \

330 } \

325 } \

331 }

326 }

332

327

333 typedef struct Picklerobject {

328 typedef struct Picklerobject {

334 PyObject_HEAD

329 PyObject_HEAD

335 FILE *fp;

336 PyObject *write;

337 PyObject *file;

330 PyObject *file;

338 PyObject *memo;

331 PyObject *memo;

339 PyObject *arg;

332 PyObject *arg;

340 PyObject *pers_func;

333 PyObject *pers_func;

341 PyObject *inst_pers_func;

334 PyObject *inst_pers_func;

342

335

343 /* pickle protocol number, >= 0 */

336 /* pickle protocol number, >= 0 */

344 int proto;

337 int proto;

345

338

346 /* bool, true if proto > 0 */

339 /* bool, true if proto > 0 */

347 int bin;

340 int bin;

348

341

342 /* Write into a local buffer before flushing out to file. output_len

343 tracks the current size; when output_len >= max_output_len, we

344 PyMem_RESIZE. */

345 Py_ssize_t max_output_len;

346 Py_ssize_t output_len;

347 char *output_buffer;

348

349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */

349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */

350 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);

351 char *write_buf;

352 int buf_size;

353 PyObject *dispatch_table;

350 PyObject *dispatch_table;

354 int fast_container; /* count nested container dumps */

351 int fast_container; /* count nested container dumps */

355 PyObject *fast_memo;

352 PyObject *fast_memo;

356 } Picklerobject;

353 } Picklerobject;

357

354

358 #ifndef PY_CPICKLE_FAST_LIMIT

355 #ifndef PY_CPICKLE_FAST_LIMIT

359 #define PY_CPICKLE_FAST_LIMIT 50

356 #define PY_CPICKLE_FAST_LIMIT 50

360 #endif

357 #endif

361

358

362 static PyTypeObject Picklertype;

359 static PyTypeObject Picklertype;

(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

418 else {

415 else {

419 PyErr_SetObject(ErrType,Py_None);

416 PyErr_SetObject(ErrType,Py_None);

420 return NULL;

417 return NULL;

421 }

418 }

422 PyErr_SetObject(ErrType,retval);

419 PyErr_SetObject(ErrType,retval);

423 Py_DECREF(retval);

420 Py_DECREF(retval);

424 return NULL;

421 return NULL;

425 }

422 }

426

423

427 static int

424 static int

428 write_file(Picklerobject *self, const char *s, Py_ssize_t n)

425 _Pickler_Write(Picklerobject *self, const char *s, Py_ssize_t n)

429 {

426 {

430 » size_t nbyteswritten;

427 » Py_ssize_t i;

428 » char *buffer;

431

429

432 » if (s == NULL) {

430 » if (self->output_len + n > self->max_output_len) {

433 » » return 0;

431 » » self->max_output_len = (self->output_len + n) * 2;

432 » » buffer = self->output_buffer;

433 » » PyMem_RESIZE(buffer, char, self->max_output_len);

434 » » if (buffer == NULL) {

435 » » » PyErr_NoMemory();

436 » » » return -1;

437 » » }

438 » » self->output_buffer = buffer;

439 » }

440 » /* This is faster than memcpy for the char *s we're copying. */

441 » for (i = 0; i < n; i++) {

442 » » self->output_buffer[self->output_len + i] = s[i];

434 }

443 }

444 self->output_len += n;

445 return n;

446 }

435

447

436 » if (n > INT_MAX) {

448 static PyObject *

437 » » /* String too large */

449 _Pickler_GetString(Picklerobject *self)

438 » » return -1;

450 {

439 » }

451 » return PyString_FromStringAndSize(self->output_buffer,

440

452 » » » » » self->output_len);

441 » PyFile_IncUseCount((PyFileObject *)self->file);

442 » Py_BEGIN_ALLOW_THREADS

443 » nbyteswritten = fwrite(s, sizeof(char), n, self->fp);

444 » Py_END_ALLOW_THREADS

445 » PyFile_DecUseCount((PyFileObject *)self->file);

446 » if (nbyteswritten != (size_t)n) {

447 » » PyErr_SetFromErrno(PyExc_IOError);

448 » » return -1;

449 » }

450

451 » return (int)n;

452 }

453 }

453

454

454 static int

455 static int

455 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)

456 _Pickler_FlushToFile(Picklerobject *self)

456 {

457 {

457 » if (s == NULL) {

458 » PyObject *output, *ret;

458 » » return 0;

459 » }

460

459

461 » if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {

460 » assert(self->file != NULL);

461

462 » output = _Pickler_GetString(self);

463 » if (output == NULL)

462 return -1;

464 return -1;

463 }

464

465

465 » return (int)n;

466 » ret = PyObject_CallMethod(self->file, "write", "O", output);

467 » Py_DECREF(output);

468 » Py_XDECREF(ret);

469 » return (ret == NULL) ? -1 : 0;

466 }

470 }

467

471

468 static int

469 write_none(Picklerobject *self, const char *s, Py_ssize_t n)

470 {

471 if (s == NULL) return 0;

472 if (n > INT_MAX) return -1;

473 return (int)n;

474 }

475

476 static int

477 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)

478 {

479 PyObject *py_str = 0, *junk = 0;

480 int n;

481

482 if (_n > INT_MAX)

483 return -1;

484 n = (int)_n;

485 if (s == NULL) {

486 if (!( self->buf_size )) return 0;

487 py_str = PyString_FromStringAndSize(self->write_buf,

488 self->buf_size);

489 if (!py_str)

490 return -1;

491 }

492 else {

493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {

494 if (write_other(self, NULL, 0) < 0)

495 return -1;

496 }

497

498 if (n > WRITE_BUF_SIZE) {

499 if (!( py_str =

500 PyString_FromStringAndSize(s, n)))

501 return -1;

502 }

503 else {

504 memcpy(self->write_buf + self->buf_size, s, n);

505 self->buf_size += n;

506 return n;

507 }

508 }

509

510 if (self->write) {

511 /* object with write method */

512 ARG_TUP(self, py_str);

513 if (self->arg) {

514 junk = PyObject_Call(self->write, self->arg, NULL);

515 FREE_ARG_TUP(self);

516 }

517 if (junk) Py_DECREF(junk);

518 else return -1;

519 }

520 else

521 PDATA_PUSH(self->file, py_str, -1);

522

523 self->buf_size = 0;

524 return n;

525 }

526

527

528 static Py_ssize_t

472 static Py_ssize_t

529 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)

473 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)

530 {

474 {

531 size_t nbytesread;

475 size_t nbytesread;

532

476

533 if (self->buf_size == 0) {

477 if (self->buf_size == 0) {

534 int size;

478 int size;

535

479

536 size = ((n < 32) ? 32 : n);

480 size = ((n < 32) ? 32 : n);

537 if (!( self->buf = (char *)malloc(size))) {

481 if (!( self->buf = (char *)malloc(size))) {

(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

689 return str_size;

633 return str_size;

690 }

634 }

691

635

692 /* Copy the first n bytes from s into newly malloc'ed memory, plus a

636 /* Copy the first n bytes from s into newly malloc'ed memory, plus a

693 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.

637 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.

694 * The caller is responsible for free()'ing the return value.

638 * The caller is responsible for free()'ing the return value.

695 */

639 */

696 static char *

640 static char *

697 pystrndup(const char *s, int n)

641 pystrndup(const char *s, int n)

698 {

642 {

699 » char *r = (char *)malloc(n+1);

643 » char *r = PyMem_NEW(char, n+1);

700 if (r == NULL)

644 if (r == NULL)

701 return (char*)PyErr_NoMemory();

645 return (char*)PyErr_NoMemory();

702 memcpy(r, s, n);

646 memcpy(r, s, n);

703 r[n] = 0;

647 r[n] = 0;

704 return r;

648 return r;

705 }

649 }

706

650

707

651

708 static int

652 static int

709 get(Picklerobject *self, PyObject *id)

653 get(Picklerobject *self, PyObject *id)

(...skipping 15 matching lines...) Expand all Loading...

725 PyErr_SetString(PicklingError, "no int where int expected in mem o");

669 PyErr_SetString(PicklingError, "no int where int expected in mem o");

726 return -1;

670 return -1;

727 }

671 }

728 c_value = PyInt_AS_LONG((PyIntObject*)value);

672 c_value = PyInt_AS_LONG((PyIntObject*)value);

729

673

730 if (!self->bin) {

674 if (!self->bin) {

731 s[0] = GET;

675 s[0] = GET;

732 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);

676 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);

733 len = strlen(s);

677 len = strlen(s);

734 }

678 }

735 else if (Pdata_Check(self->file)) {

736 if (write_other(self, NULL, 0) < 0) return -1;

737 PDATA_APPEND(self->file, mv, -1);

738 return 0;

739 }

740 else {

679 else {

741 if (c_value < 256) {

680 if (c_value < 256) {

742 s[0] = BINGET;

681 s[0] = BINGET;

743 s[1] = (int)(c_value & 0xff);

682 s[1] = (int)(c_value & 0xff);

744 len = 2;

683 len = 2;

745 }

684 }

746 else {

685 else {

747 s[0] = LONG_BINGET;

686 s[0] = LONG_BINGET;

748 s[1] = (int)(c_value & 0xff);

687 s[1] = (int)(c_value & 0xff);

749 s[2] = (int)((c_value >> 8) & 0xff);

688 s[2] = (int)((c_value >> 8) & 0xff);

750 s[3] = (int)((c_value >> 16) & 0xff);

689 s[3] = (int)((c_value >> 16) & 0xff);

751 s[4] = (int)((c_value >> 24) & 0xff);

690 s[4] = (int)((c_value >> 24) & 0xff);

752 len = 5;

691 len = 5;

753 }

692 }

754 }

693 }

755

694

756 » if (self->write_func(self, s, len) < 0)

695 » if (_Pickler_Write(self, s, len) < 0)

757 return -1;

696 return -1;

758

697

759 return 0;

698 return 0;

760 }

699 }

761

700

762

701

763 static int

702 static int

764 put(Picklerobject *self, PyObject *ob)

703 put(Picklerobject *self, PyObject *ob)

765 {

704 {

766 if (Py_REFCNT(ob) < 2 || self->fast)

705 if (Py_REFCNT(ob) < 2 || self->fast)

(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

808 Py_INCREF(ob);

747 Py_INCREF(ob);

809

748

810 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)

749 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)

811 goto finally;

750 goto finally;

812

751

813 if (!self->bin) {

752 if (!self->bin) {

814 c_str[0] = PUT;

753 c_str[0] = PUT;

815 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);

754 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);

816 len = strlen(c_str);

755 len = strlen(c_str);

817 }

756 }

818 else if (Pdata_Check(self->file)) {

819 if (write_other(self, NULL, 0) < 0) return -1;

820 PDATA_APPEND(self->file, memo_len, -1);

821 res=0; /* Job well done ;) */

822 goto finally;

823 }

824 else {

757 else {

825 if (p >= 256) {

758 if (p >= 256) {

826 c_str[0] = LONG_BINPUT;

759 c_str[0] = LONG_BINPUT;

827 c_str[1] = (int)(p & 0xff);

760 c_str[1] = (int)(p & 0xff);

828 c_str[2] = (int)((p >> 8) & 0xff);

761 c_str[2] = (int)((p >> 8) & 0xff);

829 c_str[3] = (int)((p >> 16) & 0xff);

762 c_str[3] = (int)((p >> 16) & 0xff);

830 c_str[4] = (int)((p >> 24) & 0xff);

763 c_str[4] = (int)((p >> 24) & 0xff);

831 len = 5;

764 len = 5;

832 }

765 }

833 else {

766 else {

834 c_str[0] = BINPUT;

767 c_str[0] = BINPUT;

835 c_str[1] = p;

768 c_str[1] = p;

836 len = 2;

769 len = 2;

837 }

770 }

838 }

771 }

839

772

840 » if (self->write_func(self, c_str, len) < 0)

773 » if (_Pickler_Write(self, c_str, len) < 0)

841 goto finally;

774 goto finally;

842

775

843 res = 0;

776 res = 0;

844

777

845 finally:

778 finally:

846 Py_XDECREF(py_ob_id);

779 Py_XDECREF(py_ob_id);

847 Py_XDECREF(memo_len);

780 Py_XDECREF(memo_len);

848 Py_XDECREF(t);

781 Py_XDECREF(t);

849

782

850 return res;

783 return res;

(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

954 }

887 }

955 Py_DECREF(key);

888 Py_DECREF(key);

956 }

889 }

957 return 1;

890 return 1;

958 }

891 }

959

892

960 static int

893 static int

961 save_none(Picklerobject *self, PyObject *args)

894 save_none(Picklerobject *self, PyObject *args)

962 {

895 {

963 static char none = NONE;

896 static char none = NONE;

964 » if (self->write_func(self, &none, 1) < 0)

897 » if (_Pickler_Write(self, &none, 1) < 0)

965 return -1;

898 return -1;

966

899

967 return 0;

900 return 0;

968 }

901 }

969

902

970 static int

903 static int

971 save_bool(Picklerobject *self, PyObject *args)

904 save_bool(Picklerobject *self, PyObject *args)

972 {

905 {

973 static const char *buf[2] = {FALSE, TRUE};

906 static const char *buf[2] = {FALSE, TRUE};

974 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};

907 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};

975 long l = PyInt_AS_LONG((PyIntObject *)args);

908 long l = PyInt_AS_LONG((PyIntObject *)args);

976

909

977 if (self->proto >= 2) {

910 if (self->proto >= 2) {

978 char opcode = l ? NEWTRUE : NEWFALSE;

911 char opcode = l ? NEWTRUE : NEWFALSE;

979 » » if (self->write_func(self, &opcode, 1) < 0)

912 » » if (_Pickler_Write(self, &opcode, 1) < 0)

980 return -1;

913 return -1;

981 }

914 }

982 » else if (self->write_func(self, buf[l], len[l]) < 0)

915 » else if (_Pickler_Write(self, buf[l], len[l]) < 0)

983 return -1;

916 return -1;

984 return 0;

917 return 0;

985 }

918 }

986

919

987 static int

920 static int

988 save_int(Picklerobject *self, PyObject *args)

921 save_int(Picklerobject *self, PyObject *args)

989 {

922 {

990 char c_str[32];

923 char c_str[32];

991 long l = PyInt_AS_LONG((PyIntObject *)args);

924 long l = PyInt_AS_LONG((PyIntObject *)args);

992 int len = 0;

925 int len = 0;

993

926

994 if (!self->bin

927 if (!self->bin

995 #if SIZEOF_LONG > 4

928 #if SIZEOF_LONG > 4

996 || l > 0x7fffffffL

929 || l > 0x7fffffffL

997 || l < -0x80000000L

930 || l < -0x80000000L

998 #endif

931 #endif

999 ) {

932 ) {

1000 /* Text-mode pickle, or long too big to fit in the 4-byte

933 /* Text-mode pickle, or long too big to fit in the 4-byte

1001 * signed BININT format: store as a string.

934 * signed BININT format: store as a string.

1002 */

935 */

1003 c_str[0] = INT;

936 c_str[0] = INT;

1004 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);

937 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);

1005 » » if (self->write_func(self, c_str, strlen(c_str)) < 0)

938 » » if (_Pickler_Write(self, c_str, strlen(c_str)) < 0)

1006 return -1;

939 return -1;

1007 }

940 }

1008 else {

941 else {

1009 /* Binary pickle and l fits in a signed 4-byte int. */

942 /* Binary pickle and l fits in a signed 4-byte int. */

1010 c_str[1] = (int)( l & 0xff);

943 c_str[1] = (int)( l & 0xff);

1011 c_str[2] = (int)((l >> 8) & 0xff);

944 c_str[2] = (int)((l >> 8) & 0xff);

1012 c_str[3] = (int)((l >> 16) & 0xff);

945 c_str[3] = (int)((l >> 16) & 0xff);

1013 c_str[4] = (int)((l >> 24) & 0xff);

946 c_str[4] = (int)((l >> 24) & 0xff);

1014

947

1015 if ((c_str[4] == 0) && (c_str[3] == 0)) {

948 if ((c_str[4] == 0) && (c_str[3] == 0)) {

1016 if (c_str[2] == 0) {

949 if (c_str[2] == 0) {

1017 c_str[0] = BININT1;

950 c_str[0] = BININT1;

1018 len = 2;

951 len = 2;

1019 }

952 }

1020 else {

953 else {

1021 c_str[0] = BININT2;

954 c_str[0] = BININT2;

1022 len = 3;

955 len = 3;

1023 }

956 }

1024 }

957 }

1025 else {

958 else {

1026 c_str[0] = BININT;

959 c_str[0] = BININT;

1027 len = 5;

960 len = 5;

1028 }

961 }

1029

962

1030 » » if (self->write_func(self, c_str, len) < 0)

963 » » if (_Pickler_Write(self, c_str, len) < 0)

1031 return -1;

964 return -1;

1032 }

965 }

1033

966

1034 return 0;

967 return 0;

1035 }

968 }

1036

969

1037

970

1038 static int

971 static int

1039 save_long(Picklerobject *self, PyObject *args)

972 save_long(Picklerobject *self, PyObject *args)

1040 {

973 {

1041 Py_ssize_t size;

974 Py_ssize_t size;

1042 int res = -1;

975 int res = -1;

1043 PyObject *repr = NULL;

976 PyObject *repr = NULL;

1044

977

1045 static char l = LONG;

978 static char l = LONG;

1046

979

1047 if (self->proto >= 2) {

980 if (self->proto >= 2) {

1048 /* Linear-time pickling. */

981 /* Linear-time pickling. */

1049 size_t nbits;

982 size_t nbits;

1050 size_t nbytes;

983 size_t nbytes;

1051 unsigned char *pdata;

984 unsigned char *pdata;

1052 char c_str[5];

985 char c_str[5];

1053 int i;

986 int i;

1054 int sign = _PyLong_Sign(args);

987 int sign = _PyLong_Sign(args);

1055

988

1056 if (sign == 0) {

989 if (sign == 0) {

1057 /* It's 0 -- an empty bytestring. */

990 /* It's 0 -- an empty bytestring. */

1058 c_str[0] = LONG1;

991 c_str[0] = LONG1;

1059 c_str[1] = 0;

992 c_str[1] = 0;

1060 » » » i = self->write_func(self, c_str, 2);

993 » » » i = _Pickler_Write(self, c_str, 2);

1061 if (i < 0) goto finally;

994 if (i < 0) goto finally;

1062 res = 0;

995 res = 0;

1063 goto finally;

996 goto finally;

1064 }

997 }

1065 nbits = _PyLong_NumBits(args);

998 nbits = _PyLong_NumBits(args);

1066 if (nbits == (size_t)-1 && PyErr_Occurred())

999 if (nbits == (size_t)-1 && PyErr_Occurred())

1067 goto finally;

1000 goto finally;

1068 /* How many bytes do we need? There are nbits >> 3 full

1001 /* How many bytes do we need? There are nbits >> 3 full

1069 * bytes of data, and nbits & 7 leftover bits. If there

1002 * bytes of data, and nbits & 7 leftover bits. If there

1070 * are any leftover bits, then we clearly need another

1003 * are any leftover bits, then we clearly need another

(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1107 }

1040 }

1108 else {

1041 else {

1109 c_str[0] = LONG4;

1042 c_str[0] = LONG4;

1110 size = (int)nbytes;

1043 size = (int)nbytes;

1111 for (i = 1; i < 5; i++) {

1044 for (i = 1; i < 5; i++) {

1112 c_str[i] = (char)(size & 0xff);

1045 c_str[i] = (char)(size & 0xff);

1113 size >>= 8;

1046 size >>= 8;

1114 }

1047 }

1115 size = 5;

1048 size = 5;

1116 }

1049 }

1117 » » i = self->write_func(self, c_str, size);

1050 » » i = _Pickler_Write(self, c_str, size);

1118 if (i < 0) goto finally;

1051 if (i < 0) goto finally;

1119 » » i = self->write_func(self, (char *)pdata, (int)nbytes);

1052 » » i = _Pickler_Write(self, (char *)pdata, (int)nbytes);

1120 if (i < 0) goto finally;

1053 if (i < 0) goto finally;

1121 res = 0;

1054 res = 0;

1122 goto finally;

1055 goto finally;

1123 }

1056 }

1124

1057

1125 /* proto < 2: write the repr and newline. This is quadratic-time

1058 /* proto < 2: write the repr and newline. This is quadratic-time

1126 * (in the number of digits), in both directions.

1059 * (in the number of digits), in both directions.

1127 */

1060 */

1128 if (!( repr = PyObject_Repr(args)))

1061 if (!( repr = PyObject_Repr(args)))

1129 goto finally;

1062 goto finally;

1130

1063

1131 if ((size = PyString_Size(repr)) < 0)

1064 if ((size = PyString_Size(repr)) < 0)

1132 goto finally;

1065 goto finally;

1133

1066

1134 » if (self->write_func(self, &l, 1) < 0)

1067 » if (_Pickler_Write(self, &l, 1) < 0)

1135 goto finally;

1068 goto finally;

1136

1069

1137 » if (self->write_func(self,

1070 » if (_Pickler_Write(self,

1138 » » » PyString_AS_STRING((PyStringObject *)repr),

1071 » » » PyString_AS_STRING((PyStringObject *)repr),

1139 » » » » » » size) < 0)

1072 » » » » » size) < 0)

1140 goto finally;

1073 goto finally;

1141

1074

1142 » if (self->write_func(self, "\n", 1) < 0)

1075 » if (_Pickler_Write(self, "\n", 1) < 0)

1143 goto finally;

1076 goto finally;

1144

1077

1145 res = 0;

1078 res = 0;

1146

1079

1147 finally:

1080 finally:

1148 Py_XDECREF(repr);

1081 Py_XDECREF(repr);

1149 return res;

1082 return res;

1150 }

1083 }

1151

1084

1152

1085

1153 static int

1086 static int

1154 save_float(Picklerobject *self, PyObject *args)

1087 save_float(Picklerobject *self, PyObject *args)

1155 {

1088 {

1156 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);

1089 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);

1157

1090

1158 if (self->bin) {

1091 if (self->bin) {

1159 char str[9];

1092 char str[9];

1160 str[0] = BINFLOAT;

1093 str[0] = BINFLOAT;

1161 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)

1094 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)

1162 return -1;

1095 return -1;

1163 » » if (self->write_func(self, str, 9) < 0)

1096 » » if (_Pickler_Write(self, str, 9) < 0)

1164 return -1;

1097 return -1;

1165 }

1098 }

1166 else {

1099 else {

1167 char c_str[250];

1100 char c_str[250];

1168 c_str[0] = FLOAT;

1101 c_str[0] = FLOAT;

1169 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);

1102 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);

1170 /* Extend the formatted string with a newline character */

1103 /* Extend the formatted string with a newline character */

1171 strcat(c_str, "\n");

1104 strcat(c_str, "\n");

1172

1105

1173 » » if (self->write_func(self, c_str, strlen(c_str)) < 0)

1106 » » if (_Pickler_Write(self, c_str, strlen(c_str)) < 0)

1174 return -1;

1107 return -1;

1175 }

1108 }

1176

1109

1177 return 0;

1110 return 0;

1178 }

1111 }

1179

1112

1180

1113

1181 static int

1114 static int

1182 save_string(Picklerobject *self, PyObject *args, int doput)

1115 save_string(Picklerobject *self, PyObject *args, int doput)

1183 {

1116 {

1184 int size, len;

1117 int size, len;

1185 PyObject *repr=0;

1118 PyObject *repr=0;

1186

1119

1187 if ((size = PyString_Size(args)) < 0)

1120 if ((size = PyString_Size(args)) < 0)

1188 return -1;

1121 return -1;

1189

1122

1190 if (!self->bin) {

1123 if (!self->bin) {

1191 char *repr_str;

1124 char *repr_str;

1192

1125

1193 static char string = STRING;

1126 static char string = STRING;

1194

1127

1195 if (!( repr = PyObject_Repr(args)))

1128 if (!( repr = PyObject_Repr(args)))

1196 return -1;

1129 return -1;

1197

1130

1198 if ((len = PyString_Size(repr)) < 0)

1131 if ((len = PyString_Size(repr)) < 0)

1199 goto err;

1132 goto err;

1200 repr_str = PyString_AS_STRING((PyStringObject *)repr);

1133 repr_str = PyString_AS_STRING((PyStringObject *)repr);

1201

1134

1202 » » if (self->write_func(self, &string, 1) < 0)

1135 » » if (_Pickler_Write(self, &string, 1) < 0)

1203 goto err;

1136 goto err;

1204

1137

1205 » » if (self->write_func(self, repr_str, len) < 0)

1138 » » if (_Pickler_Write(self, repr_str, len) < 0)

1206 goto err;

1139 goto err;

1207

1140

1208 » » if (self->write_func(self, "\n", 1) < 0)

1141 » » if (_Pickler_Write(self, "\n", 1) < 0)

1209 goto err;

1142 goto err;

1210

1143

1211 Py_XDECREF(repr);

1144 Py_XDECREF(repr);

1212 }

1145 }

1213 else {

1146 else {

1214 int i;

1147 int i;

1215 char c_str[5];

1148 char c_str[5];

1216

1149

1217 if ((size = PyString_Size(args)) < 0)

1150 if ((size = PyString_Size(args)) < 0)

1218 return -1;

1151 return -1;

1219

1152

1220 if (size < 256) {

1153 if (size < 256) {

1221 c_str[0] = SHORT_BINSTRING;

1154 c_str[0] = SHORT_BINSTRING;

1222 c_str[1] = size;

1155 c_str[1] = size;

1223 len = 2;

1156 len = 2;

1224 }

1157 }

1225 else if (size <= INT_MAX) {

1158 else if (size <= INT_MAX) {

1226 c_str[0] = BINSTRING;

1159 c_str[0] = BINSTRING;

1227 for (i = 1; i < 5; i++)

1160 for (i = 1; i < 5; i++)

1228 c_str[i] = (int)(size >> ((i - 1) * 8));

1161 c_str[i] = (int)(size >> ((i - 1) * 8));

1229 len = 5;

1162 len = 5;

1230 }

1163 }

1231 else

1164 else

1232 return -1; /* string too large */

1165 return -1; /* string too large */

1233

1166

1234 » » if (self->write_func(self, c_str, len) < 0)

1167 » » if (_Pickler_Write(self, c_str, len) < 0)

1235 return -1;

1168 return -1;

1236

1169

1237 » » if (size > 128 && Pdata_Check(self->file)) {

1170 » » if (_Pickler_Write(self,

1238 » » » if (write_other(self, NULL, 0) < 0) return -1;

1171 » » » » PyString_AS_STRING(

1239 » » » PDATA_APPEND(self->file, args, -1);

1172 » » » » » (PyStringObject *)args),

1240 » » }

1173 » » » » size) < 0)

1241 » » else {

1174 » » » return -1;

1242 » » » if (self->write_func(self,

1243 » » » » » PyString_AS_STRING(

1244 » » » » » » (PyStringObject *)args),

1245 » » » » » size) < 0)

1246 » » » » return -1;

1247 » » }

1248 }

1175 }

1249

1176

1250 if (doput)

1177 if (doput)

1251 if (put(self, args) < 0)

1178 if (put(self, args) < 0)

1252 return -1;

1179 return -1;

1253

1180

1254 return 0;

1181 return 0;

1255

1182

1256 err:

1183 err:

1257 Py_XDECREF(repr);

1184 Py_XDECREF(repr);

(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1363

1290

1364 repr = modified_EncodeRawUnicodeEscape(

1291 repr = modified_EncodeRawUnicodeEscape(

1365 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));

1292 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));

1366 if (!repr)

1293 if (!repr)

1367 return -1;

1294 return -1;

1368

1295

1369 if ((len = PyString_Size(repr)) < 0)

1296 if ((len = PyString_Size(repr)) < 0)

1370 goto err;

1297 goto err;

1371 repr_str = PyString_AS_STRING((PyStringObject *)repr);

1298 repr_str = PyString_AS_STRING((PyStringObject *)repr);

1372

1299

1373 » » if (self->write_func(self, &string, 1) < 0)

1300 » » if (_Pickler_Write(self, &string, 1) < 0)

1374 goto err;

1301 goto err;

1375

1302

1376 » » if (self->write_func(self, repr_str, len) < 0)

1303 » » if (_Pickler_Write(self, repr_str, len) < 0)

1377 goto err;

1304 goto err;

1378

1305

1379 » » if (self->write_func(self, "\n", 1) < 0)

1306 » » if (_Pickler_Write(self, "\n", 1) < 0)

1380 goto err;

1307 goto err;

1381

1308

1382 Py_XDECREF(repr);

1309 Py_XDECREF(repr);

1383 }

1310 }

1384 else {

1311 else {

1385 int i;

1312 int i;

1386 char c_str[5];

1313 char c_str[5];

1387

1314

1388 if (!( repr = PyUnicode_AsUTF8String(args)))

1315 if (!( repr = PyUnicode_AsUTF8String(args)))

1389 return -1;

1316 return -1;

1390

1317

1391 if ((size = PyString_Size(repr)) < 0)

1318 if ((size = PyString_Size(repr)) < 0)

1392 goto err;

1319 goto err;

1393 if (size > INT_MAX)

1320 if (size > INT_MAX)

1394 return -1; /* string too large */

1321 return -1; /* string too large */

1395

1322

1396 c_str[0] = BINUNICODE;

1323 c_str[0] = BINUNICODE;

1397 for (i = 1; i < 5; i++)

1324 for (i = 1; i < 5; i++)

1398 c_str[i] = (int)(size >> ((i - 1) * 8));

1325 c_str[i] = (int)(size >> ((i - 1) * 8));

1399 len = 5;

1326 len = 5;

1400

1327

1401 » » if (self->write_func(self, c_str, len) < 0)

1328 » » if (_Pickler_Write(self, c_str, len) < 0)

1402 goto err;

1329 goto err;

1403

1330

1404 » » if (size > 128 && Pdata_Check(self->file)) {

1331 » » if (_Pickler_Write(self, PyString_AS_STRING(repr), size) < 0)

1405 » » » if (write_other(self, NULL, 0) < 0)

1332 » » » goto err;

1406 » » » » goto err;

1407 » » » PDATA_APPEND(self->file, repr, -1);

1408 » » }

1409 » » else {

1410 » » » if (self->write_func(self, PyString_AS_STRING(repr),

1411 » » » » » size) < 0)

1412 » » » » goto err;

1413 » » }

1414

1333

1415 Py_DECREF(repr);

1334 Py_DECREF(repr);

1416 }

1335 }

1417

1336

1418 if (doput)

1337 if (doput)

1419 if (put(self, args) < 0)

1338 if (put(self, args) < 0)

1420 return -1;

1339 return -1;

1421

1340

1422 return 0;

1341 return 0;

1423

1342

(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1476

1395

1477 if (self->proto) {

1396 if (self->proto) {

1478 c_str[0] = EMPTY_TUPLE;

1397 c_str[0] = EMPTY_TUPLE;

1479 len = 1;

1398 len = 1;

1480 }

1399 }

1481 else {

1400 else {

1482 c_str[0] = MARK;

1401 c_str[0] = MARK;

1483 c_str[1] = TUPLE;

1402 c_str[1] = TUPLE;

1484 len = 2;

1403 len = 2;

1485 }

1404 }

1486 » » if (self->write_func(self, c_str, len) >= 0)

1405 » » if (_Pickler_Write(self, c_str, len) >= 0)

1487 res = 0;

1406 res = 0;

1488 /* Don't memoize an empty tuple. */

1407 /* Don't memoize an empty tuple. */

1489 goto finally;

1408 goto finally;

1490 }

1409 }

1491

1410

1492 /* A non-empty tuple. */

1411 /* A non-empty tuple. */

1493

1412

1494 /* id(tuple) isn't in the memo now. If it shows up there after

1413 /* id(tuple) isn't in the memo now. If it shows up there after

1495 * saving the tuple elements, the tuple must be recursive, in

1414 * saving the tuple elements, the tuple must be recursive, in

1496 * which case we'll pop everything we put on the stack, and fetch

1415 * which case we'll pop everything we put on the stack, and fetch

1497 * its value from the memo.

1416 * its value from the memo.

1498 */

1417 */

1499 py_tuple_id = PyLong_FromVoidPtr(args);

1418 py_tuple_id = PyLong_FromVoidPtr(args);

1500 if (py_tuple_id == NULL)

1419 if (py_tuple_id == NULL)

1501 goto finally;

1420 goto finally;

1502

1421

1503 if (len <= 3 && self->proto >= 2) {

1422 if (len <= 3 && self->proto >= 2) {

1504 /* Use TUPLE{1,2,3} opcodes. */

1423 /* Use TUPLE{1,2,3} opcodes. */

1505 if (store_tuple_elements(self, args, len) < 0)

1424 if (store_tuple_elements(self, args, len) < 0)

1506 goto finally;

1425 goto finally;

1507 if (PyDict_GetItem(self->memo, py_tuple_id)) {

1426 if (PyDict_GetItem(self->memo, py_tuple_id)) {

1508 /* pop the len elements */

1427 /* pop the len elements */

1509 for (i = 0; i < len; ++i)

1428 for (i = 0; i < len; ++i)

1510 » » » » if (self->write_func(self, &pop, 1) < 0)

1429 » » » » if (_Pickler_Write(self, &pop, 1) < 0)

1511 goto finally;

1430 goto finally;

1512 /* fetch from memo */

1431 /* fetch from memo */

1513 if (get(self, py_tuple_id) < 0)

1432 if (get(self, py_tuple_id) < 0)

1514 goto finally;

1433 goto finally;

1515 res = 0;

1434 res = 0;

1516 goto finally;

1435 goto finally;

1517 }

1436 }

1518 /* Not recursive. */

1437 /* Not recursive. */

1519 » » if (self->write_func(self, len2opcode + len, 1) < 0)

1438 » » if (_Pickler_Write(self, len2opcode + len, 1) < 0)

1520 goto finally;

1439 goto finally;

1521 goto memoize;

1440 goto memoize;

1522 }

1441 }

1523

1442

1524 /* proto < 2 and len > 0, or proto >= 2 and len > 3.

1443 /* proto < 2 and len > 0, or proto >= 2 and len > 3.

1525 * Generate MARK elt1 elt2 ... TUPLE

1444 * Generate MARK elt1 elt2 ... TUPLE

1526 */

1445 */

1527 » if (self->write_func(self, &MARKv, 1) < 0)

1446 » if (_Pickler_Write(self, &MARKv, 1) < 0)

1528 goto finally;

1447 goto finally;

1529

1448

1530 if (store_tuple_elements(self, args, len) < 0)

1449 if (store_tuple_elements(self, args, len) < 0)

1531 goto finally;

1450 goto finally;

1532

1451

1533 if (PyDict_GetItem(self->memo, py_tuple_id)) {

1452 if (PyDict_GetItem(self->memo, py_tuple_id)) {

1534 /* pop the stack stuff we pushed */

1453 /* pop the stack stuff we pushed */

1535 if (self->bin) {

1454 if (self->bin) {

1536 » » » if (self->write_func(self, &pop_mark, 1) < 0)

1455 » » » if (_Pickler_Write(self, &pop_mark, 1) < 0)

1537 goto finally;

1456 goto finally;

1538 }

1457 }

1539 else {

1458 else {

1540 /* Note that we pop one more than len, to remove

1459 /* Note that we pop one more than len, to remove

1541 * the MARK too.

1460 * the MARK too.

1542 */

1461 */

1543 for (i = 0; i <= len; i++)

1462 for (i = 0; i <= len; i++)

1544 » » » » if (self->write_func(self, &pop, 1) < 0)

1463 » » » » if (_Pickler_Write(self, &pop, 1) < 0)

1545 goto finally;

1464 goto finally;

1546 }

1465 }

1547 /* fetch from memo */

1466 /* fetch from memo */

1548 if (get(self, py_tuple_id) >= 0)

1467 if (get(self, py_tuple_id) >= 0)

1549 res = 0;

1468 res = 0;

1550 goto finally;

1469 goto finally;

1551 }

1470 }

1552

1471

1553 /* Not recursive. */

1472 /* Not recursive. */

1554 » if (self->write_func(self, &tuple, 1) < 0)

1473 » if (_Pickler_Write(self, &tuple, 1) < 0)

1555 goto finally;

1474 goto finally;

1556

1475

1557 memoize:

1476 memoize:

1558 if (put(self, args) >= 0)

1477 if (put(self, args) >= 0)

1559 res = 0;

1478 res = 0;

1560

1479

1561 finally:

1480 finally:

1562 Py_XDECREF(py_tuple_id);

1481 Py_XDECREF(py_tuple_id);

1563 return res;

1482 return res;

1564 }

1483 }

(...skipping 22 matching lines...) Expand all Loading...

1587 obj = PyIter_Next(iter);

1506 obj = PyIter_Next(iter);

1588 if (obj == NULL) {

1507 if (obj == NULL) {

1589 if (PyErr_Occurred())

1508 if (PyErr_Occurred())

1590 return -1;

1509 return -1;

1591 break;

1510 break;

1592 }

1511 }

1593 i = save(self, obj, 0);

1512 i = save(self, obj, 0);

1594 Py_DECREF(obj);

1513 Py_DECREF(obj);

1595 if (i < 0)

1514 if (i < 0)

1596 return -1;

1515 return -1;

1597 » » » if (self->write_func(self, &append, 1) < 0)

1516 » » » if (_Pickler_Write(self, &append, 1) < 0)

1598 return -1;

1517 return -1;

1599 }

1518 }

1600 return 0;

1519 return 0;

1601 }

1520 }

1602

1521

1603 /* proto > 0: write in batches of BATCHSIZE. */

1522 /* proto > 0: write in batches of BATCHSIZE. */

1604 do {

1523 do {

1605 /* Get first item */

1524 /* Get first item */

1606 firstitem = PyIter_Next(iter);

1525 firstitem = PyIter_Next(iter);

1607 if (firstitem == NULL) {

1526 if (firstitem == NULL) {

1608 if (PyErr_Occurred())

1527 if (PyErr_Occurred())

1609 goto BatchFailed;

1528 goto BatchFailed;

1610

1529

1611 /* nothing more to add */

1530 /* nothing more to add */

1612 break;

1531 break;

1613 }

1532 }

1614

1533

1615 /* Try to get a second item */

1534 /* Try to get a second item */

1616 obj = PyIter_Next(iter);

1535 obj = PyIter_Next(iter);

1617 if (obj == NULL) {

1536 if (obj == NULL) {

1618 if (PyErr_Occurred())

1537 if (PyErr_Occurred())

1619 goto BatchFailed;

1538 goto BatchFailed;

1620

1539

1621 /* Only one item to write */

1540 /* Only one item to write */

1622 if (save(self, firstitem, 0) < 0)

1541 if (save(self, firstitem, 0) < 0)

1623 goto BatchFailed;

1542 goto BatchFailed;

1624 » » » if (self->write_func(self, &append, 1) < 0)

1543 » » » if (_Pickler_Write(self, &append, 1) < 0)

1625 goto BatchFailed;

1544 goto BatchFailed;

1626 Py_CLEAR(firstitem);

1545 Py_CLEAR(firstitem);

1627 break;

1546 break;

1628 }

1547 }

1629

1548

1630 /* More than one item to write */

1549 /* More than one item to write */

1631

1550

1632 /* Pump out MARK, items, APPENDS. */

1551 /* Pump out MARK, items, APPENDS. */

1633 » » if (self->write_func(self, &MARKv, 1) < 0)

1552 » » if (_Pickler_Write(self, &MARKv, 1) < 0)

1634 goto BatchFailed;

1553 goto BatchFailed;

1635 ················

1554 ················

1636 if (save(self, firstitem, 0) < 0)

1555 if (save(self, firstitem, 0) < 0)

1637 goto BatchFailed;

1556 goto BatchFailed;

1638 Py_CLEAR(firstitem);

1557 Py_CLEAR(firstitem);

1639 n = 1;

1558 n = 1;

1640 ················

1559 ················

1641 /* Fetch and save up to BATCHSIZE items */

1560 /* Fetch and save up to BATCHSIZE items */

1642 while (obj) {

1561 while (obj) {

1643 if (save(self, obj, 0) < 0)

1562 if (save(self, obj, 0) < 0)

1644 goto BatchFailed;

1563 goto BatchFailed;

1645 Py_CLEAR(obj);

1564 Py_CLEAR(obj);

1646 n += 1;

1565 n += 1;

1647 ························

1566 ························

1648 if (n == BATCHSIZE)

1567 if (n == BATCHSIZE)

1649 break;

1568 break;

1650

1569

1651 obj = PyIter_Next(iter);

1570 obj = PyIter_Next(iter);

1652 if (obj == NULL) {

1571 if (obj == NULL) {

1653 if (PyErr_Occurred())

1572 if (PyErr_Occurred())

1654 goto BatchFailed;

1573 goto BatchFailed;

1655 break;

1574 break;

1656 }

1575 }

1657 }

1576 }

1658

1577

1659 » » if (self->write_func(self, &appends, 1) < 0)

1578 » » if (_Pickler_Write(self, &appends, 1) < 0)

1660 goto BatchFailed;

1579 goto BatchFailed;

1661

1580

1662 } while (n == BATCHSIZE);

1581 } while (n == BATCHSIZE);

1663 return 0;

1582 return 0;

1664

1583

1665 BatchFailed:

1584 BatchFailed:

1666 Py_XDECREF(firstitem);

1585 Py_XDECREF(firstitem);

1667 Py_XDECREF(obj);

1586 Py_XDECREF(obj);

1668 return -1;

1587 return -1;

1669 }

1588 }

(...skipping 13 matching lines...) Expand all Loading...

1683 if (self->bin) {

1602 if (self->bin) {

1684 s[0] = EMPTY_LIST;

1603 s[0] = EMPTY_LIST;

1685 len = 1;

1604 len = 1;

1686 }

1605 }

1687 else {

1606 else {

1688 s[0] = MARK;

1607 s[0] = MARK;

1689 s[1] = LIST;

1608 s[1] = LIST;

1690 len = 2;

1609 len = 2;

1691 }

1610 }

1692

1611

1693 » if (self->write_func(self, s, len) < 0)

1612 » if (_Pickler_Write(self, s, len) < 0)

1694 goto finally;

1613 goto finally;

1695

1614

1696 /* Get list length, and bow out early if empty. */

1615 /* Get list length, and bow out early if empty. */

1697 if ((len = PyList_Size(args)) < 0)

1616 if ((len = PyList_Size(args)) < 0)

1698 goto finally;

1617 goto finally;

1699

1618

1700 /* Memoize. */

1619 /* Memoize. */

1701 if (len == 0) {

1620 if (len == 0) {

1702 if (put(self, args) >= 0)

1621 if (put(self, args) >= 0)

1703 res = 0;

1622 res = 0;

(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1762 PyErr_SetString(PyExc_TypeError, "dict items "

1681 PyErr_SetString(PyExc_TypeError, "dict items "

1763 "iterator must return 2-tuples");

1682 "iterator must return 2-tuples");

1764 return -1;

1683 return -1;

1765 }

1684 }

1766 i = save(self, PyTuple_GET_ITEM(p, 0), 0);

1685 i = save(self, PyTuple_GET_ITEM(p, 0), 0);

1767 if (i >= 0)

1686 if (i >= 0)

1768 i = save(self, PyTuple_GET_ITEM(p, 1), 0);

1687 i = save(self, PyTuple_GET_ITEM(p, 1), 0);

1769 Py_DECREF(p);

1688 Py_DECREF(p);

1770 if (i < 0)

1689 if (i < 0)

1771 return -1;

1690 return -1;

1772 » » » if (self->write_func(self, &setitem, 1) < 0)

1691 » » » if (_Pickler_Write(self, &setitem, 1) < 0)

1773 return -1;

1692 return -1;

1774 }

1693 }

1775 return 0;

1694 return 0;

1776 }

1695 }

1777

1696

1778 /* proto > 0: write in batches of BATCHSIZE. */

1697 /* proto > 0: write in batches of BATCHSIZE. */

1779 do {

1698 do {

1780 /* Get first item */

1699 /* Get first item */

1781 firstitem = PyIter_Next(iter);

1700 firstitem = PyIter_Next(iter);

1782 if (firstitem == NULL) {

1701 if (firstitem == NULL) {

(...skipping 13 matching lines...) Expand all Loading...

1796 p = PyIter_Next(iter);

1715 p = PyIter_Next(iter);

1797 if (p == NULL) {

1716 if (p == NULL) {

1798 if (PyErr_Occurred())

1717 if (PyErr_Occurred())

1799 goto BatchFailed;

1718 goto BatchFailed;

1800

1719

1801 /* Only one item to write */

1720 /* Only one item to write */

1802 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)

1721 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)

1803 goto BatchFailed;

1722 goto BatchFailed;

1804 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)

1723 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)

1805 goto BatchFailed;

1724 goto BatchFailed;

1806 » » » if (self->write_func(self, &setitem, 1) < 0)

1725 » » » if (_Pickler_Write(self, &setitem, 1) < 0)

1807 goto BatchFailed;

1726 goto BatchFailed;

1808 Py_CLEAR(firstitem);

1727 Py_CLEAR(firstitem);

1809 break;

1728 break;

1810 }

1729 }

1811

1730

1812 /* More than one item to write */

1731 /* More than one item to write */

1813

1732

1814 /* Pump out MARK, items, SETITEMS. */

1733 /* Pump out MARK, items, SETITEMS. */

1815 » » if (self->write_func(self, &MARKv, 1) < 0)

1734 » » if (_Pickler_Write(self, &MARKv, 1) < 0)

1816 goto BatchFailed;

1735 goto BatchFailed;

1817

1736

1818 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)

1737 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)

1819 goto BatchFailed;

1738 goto BatchFailed;

1820 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)

1739 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)

1821 goto BatchFailed;

1740 goto BatchFailed;

1822 Py_CLEAR(firstitem);

1741 Py_CLEAR(firstitem);

1823 n = 1;

1742 n = 1;

1824

1743

1825 /* Fetch and save up to BATCHSIZE items */

1744 /* Fetch and save up to BATCHSIZE items */

(...skipping 14 matching lines...) Expand all Loading...

1840 break;

1759 break;

1841

1760

1842 p = PyIter_Next(iter);

1761 p = PyIter_Next(iter);

1843 if (p == NULL) {

1762 if (p == NULL) {

1844 if (PyErr_Occurred())

1763 if (PyErr_Occurred())

1845 goto BatchFailed;

1764 goto BatchFailed;

1846 break;

1765 break;

1847 }

1766 }

1848 }

1767 }

1849

1768

1850 » » if (self->write_func(self, &setitems, 1) < 0)

1769 » » if (_Pickler_Write(self, &setitems, 1) < 0)

1851 goto BatchFailed;

1770 goto BatchFailed;

1852

1771

1853 } while (n == BATCHSIZE);

1772 } while (n == BATCHSIZE);

1854 return 0;

1773 return 0;

1855

1774

1856 BatchFailed:

1775 BatchFailed:

1857 Py_XDECREF(firstitem);

1776 Py_XDECREF(firstitem);

1858 Py_XDECREF(p);

1777 Py_XDECREF(p);

1859 return -1;

1778 return -1;

1860 }

1779 }

(...skipping 13 matching lines...) Expand all Loading...

1874 if (self->bin) {

1793 if (self->bin) {

1875 s[0] = EMPTY_DICT;

1794 s[0] = EMPTY_DICT;

1876 len = 1;

1795 len = 1;

1877 }

1796 }

1878 else {

1797 else {

1879 s[0] = MARK;

1798 s[0] = MARK;

1880 s[1] = DICT;

1799 s[1] = DICT;

1881 len = 2;

1800 len = 2;

1882 }

1801 }

1883

1802

1884 » if (self->write_func(self, s, len) < 0)

1803 » if (_Pickler_Write(self, s, len) < 0)

1885 goto finally;

1804 goto finally;

1886

1805

1887 /* Get dict size, and bow out early if empty. */

1806 /* Get dict size, and bow out early if empty. */

1888 if ((len = PyDict_Size(args)) < 0)

1807 if ((len = PyDict_Size(args)) < 0)

1889 goto finally;

1808 goto finally;

1890

1809

1891 if (len == 0) {

1810 if (len == 0) {

1892 if (put(self, args) >= 0)

1811 if (put(self, args) >= 0)

1893 res = 0;

1812 res = 0;

1894 goto finally;

1813 goto finally;

(...skipping 26 matching lines...) Expand all Loading...

1921 PyObject *class = 0, *module = 0, *name = 0, *state = 0,

1840 PyObject *class = 0, *module = 0, *name = 0, *state = 0,

1922 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;

1841 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;

1923 char *module_str, *name_str;

1842 char *module_str, *name_str;

1924 int module_size, name_size, res = -1;

1843 int module_size, name_size, res = -1;

1925

1844

1926 static char inst = INST, obj = OBJ, build = BUILD;

1845 static char inst = INST, obj = OBJ, build = BUILD;

1927

1846

1928 if (self->fast && !fast_save_enter(self, args))

1847 if (self->fast && !fast_save_enter(self, args))

1929 goto finally;

1848 goto finally;

1930

1849

1931 » if (self->write_func(self, &MARKv, 1) < 0)

1850 » if (_Pickler_Write(self, &MARKv, 1) < 0)

1932 goto finally;

1851 goto finally;

1933

1852

1934 if (!( class = PyObject_GetAttr(args, __class___str)))

1853 if (!( class = PyObject_GetAttr(args, __class___str)))

1935 goto finally;

1854 goto finally;

1936

1855

1937 if (self->bin) {

1856 if (self->bin) {

1938 if (save(self, class, 0) < 0)

1857 if (save(self, class, 0) < 0)

1939 goto finally;

1858 goto finally;

1940 }

1859 }

1941

1860

(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1979 goto finally;

1898 goto finally;

1980

1899

1981

1900

1982 if ((module_size = PyString_Size(module)) < 0 ||

1901 if ((module_size = PyString_Size(module)) < 0 ||

1983 (name_size = PyString_Size(name)) < 0)

1902 (name_size = PyString_Size(name)) < 0)

1984 goto finally;

1903 goto finally;

1985

1904

1986 module_str = PyString_AS_STRING((PyStringObject *)module);

1905 module_str = PyString_AS_STRING((PyStringObject *)module);

1987 name_str = PyString_AS_STRING((PyStringObject *)name);

1906 name_str = PyString_AS_STRING((PyStringObject *)name);

1988

1907

1989 » » if (self->write_func(self, &inst, 1) < 0)

1908 » » if (_Pickler_Write(self, &inst, 1) < 0)

1990 goto finally;

1909 goto finally;

1991

1910

1992 » » if (self->write_func(self, module_str, module_size) < 0)

1911 » » if (_Pickler_Write(self, module_str, module_size) < 0)

1993 goto finally;

1912 goto finally;

1994

1913

1995 » » if (self->write_func(self, "\n", 1) < 0)

1914 » » if (_Pickler_Write(self, "\n", 1) < 0)

1996 goto finally;

1915 goto finally;

1997

1916

1998 » » if (self->write_func(self, name_str, name_size) < 0)

1917 » » if (_Pickler_Write(self, name_str, name_size) < 0)

1999 goto finally;

1918 goto finally;

2000

1919

2001 » » if (self->write_func(self, "\n", 1) < 0)

1920 » » if (_Pickler_Write(self, "\n", 1) < 0)

2002 goto finally;

1921 goto finally;

2003 }

1922 }

2004 » else if (self->write_func(self, &obj, 1) < 0) {

1923 » else if (_Pickler_Write(self, &obj, 1) < 0) {

2005 goto finally;

1924 goto finally;

2006 }

1925 }

2007

1926

2008 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {

1927 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {

2009 state = PyObject_Call(getstate_func, empty_tuple, NULL);

1928 state = PyObject_Call(getstate_func, empty_tuple, NULL);

2010 if (!state)

1929 if (!state)

2011 goto finally;

1930 goto finally;

2012 }

1931 }

2013 else {

1932 else {

2014 if (PyErr_ExceptionMatches(PyExc_AttributeError))

1933 if (PyErr_ExceptionMatches(PyExc_AttributeError))

(...skipping 16 matching lines...) Expand all Loading...

2031 goto finally;

1950 goto finally;

2032 }

1951 }

2033 else {

1952 else {

2034 if (put(self, args) < 0)

1953 if (put(self, args) < 0)

2035 goto finally;

1954 goto finally;

2036 }

1955 }

2037

1956

2038 if (save(self, state, 0) < 0)

1957 if (save(self, state, 0) < 0)

2039 goto finally;

1958 goto finally;

2040

1959

2041 » if (self->write_func(self, &build, 1) < 0)

1960 » if (_Pickler_Write(self, &build, 1) < 0)

2042 goto finally;

1961 goto finally;

2043

1962

2044 res = 0;

1963 res = 0;

2045

1964

2046 finally:

1965 finally:

2047 if (self->fast && !fast_save_leave(self, args))

1966 if (self->fast && !fast_save_leave(self, args))

2048 res = -1;

1967 res = -1;

2049

1968

2050 Py_XDECREF(module);

1969 Py_XDECREF(module);

2051 Py_XDECREF(class);

1970 Py_XDECREF(class);

(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2158 }

2077 }

2159 else {

2078 else {

2160 c_str[0] = EXT4;

2079 c_str[0] = EXT4;

2161 c_str[1] = (char)(code & 0xff);

2080 c_str[1] = (char)(code & 0xff);

2162 c_str[2] = (char)((code >> 8) & 0xff);

2081 c_str[2] = (char)((code >> 8) & 0xff);

2163 c_str[3] = (char)((code >> 16) & 0xff);

2082 c_str[3] = (char)((code >> 16) & 0xff);

2164 c_str[4] = (char)((code >> 24) & 0xff);

2083 c_str[4] = (char)((code >> 24) & 0xff);

2165 n = 5;

2084 n = 5;

2166 }

2085 }

2167

2086

2168 » » if (self->write_func(self, c_str, n) >= 0)

2087 » » if (_Pickler_Write(self, c_str, n) >= 0)

2169 res = 0;

2088 res = 0;

2170 goto finally; /* and don't memoize */

2089 goto finally; /* and don't memoize */

2171 }

2090 }

2172

2091

2173 gen_global:

2092 gen_global:

2174 » if (self->write_func(self, &global, 1) < 0)

2093 » if (_Pickler_Write(self, &global, 1) < 0)

2175 goto finally;

2094 goto finally;

2176

2095

2177 » if (self->write_func(self, module_str, module_size) < 0)

2096 » if (_Pickler_Write(self, module_str, module_size) < 0)

2178 goto finally;

2097 goto finally;

2179

2098

2180 » if (self->write_func(self, "\n", 1) < 0)

2099 » if (_Pickler_Write(self, "\n", 1) < 0)

2181 goto finally;

2100 goto finally;

2182

2101

2183 » if (self->write_func(self, name_str, name_size) < 0)

2102 » if (_Pickler_Write(self, name_str, name_size) < 0)

2184 goto finally;

2103 goto finally;

2185

2104

2186 » if (self->write_func(self, "\n", 1) < 0)

2105 » if (_Pickler_Write(self, "\n", 1) < 0)

2187 goto finally;

2106 goto finally;

2188

2107

2189 if (put(self, args) < 0)

2108 if (put(self, args) < 0)

2190 goto finally;

2109 goto finally;

2191

2110

2192 res = 0;

2111 res = 0;

2193

2112

2194 finally:

2113 finally:

2195 Py_XDECREF(module);

2114 Py_XDECREF(module);

2196 Py_XDECREF(global_name);

2115 Py_XDECREF(global_name);

(...skipping 19 matching lines...) Expand all Loading...

2216 if (! pid) return -1;

2135 if (! pid) return -1;

2217

2136

2218 if (pid != Py_None) {

2137 if (pid != Py_None) {

2219 if (!self->bin) {

2138 if (!self->bin) {

2220 if (!PyString_Check(pid)) {

2139 if (!PyString_Check(pid)) {

2221 PyErr_SetString(PicklingError,

2140 PyErr_SetString(PicklingError,

2222 "persistent id must be string");

2141 "persistent id must be string");

2223 goto finally;

2142 goto finally;

2224 }

2143 }

2225

2144

2226 » » » if (self->write_func(self, &persid, 1) < 0)

2145 » » » if (_Pickler_Write(self, &persid, 1) < 0)

2227 goto finally;

2146 goto finally;

2228

2147

2229 if ((size = PyString_Size(pid)) < 0)

2148 if ((size = PyString_Size(pid)) < 0)

2230 goto finally;

2149 goto finally;

2231

2150

2232 » » » if (self->write_func(self,

2151 » » » if (_Pickler_Write(self,

2233 » » » » » PyString_AS_STRING(

2152 » » » » » PyString_AS_STRING(

2234 » » » » » » (PyStringObject *)pid),

2153 » » » » » » (PyStringObject *)pid),

2235 » » » » » size) < 0)

2154 » » » » » size) < 0)

2236 goto finally;

2155 goto finally;

2237

2156

2238 » » » if (self->write_func(self, "\n", 1) < 0)

2157 » » » if (_Pickler_Write(self, "\n", 1) < 0)

2239 goto finally;

2158 goto finally;

2240

2159

2241 res = 1;

2160 res = 1;

2242 goto finally;

2161 goto finally;

2243 }

2162 }

2244 else if (save(self, pid, 1) >= 0) {

2163 else if (save(self, pid, 1) >= 0) {

2245 » » » if (self->write_func(self, &binpersid, 1) < 0)

2164 » » » if (_Pickler_Write(self, &binpersid, 1) < 0)

2246 res = -1;

2165 res = -1;

2247 else

2166 else

2248 res = 1;

2167 res = 1;

2249 }

2168 }

2250

2169

2251 goto finally;

2170 goto finally;

2252 }

2171 }

2253

2172

2254 res = 0;

2173 res = 0;

2255

2174

(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2394 PyObject *temp = PyTuple_GET_ITEM(argtup, i);

2313 PyObject *temp = PyTuple_GET_ITEM(argtup, i);

2395 Py_INCREF(temp);

2314 Py_INCREF(temp);

2396 PyTuple_SET_ITEM(newargtup, i-1, temp);

2315 PyTuple_SET_ITEM(newargtup, i-1, temp);

2397 }

2316 }

2398 i = save(self, newargtup, 0);

2317 i = save(self, newargtup, 0);

2399 Py_DECREF(newargtup);

2318 Py_DECREF(newargtup);

2400 if (i < 0)

2319 if (i < 0)

2401 return -1;

2320 return -1;

2402

2321

2403 /* Add NEWOBJ opcode. */

2322 /* Add NEWOBJ opcode. */

2404 » » if (self->write_func(self, &newobj, 1) < 0)

2323 » » if (_Pickler_Write(self, &newobj, 1) < 0)

2405 return -1;

2324 return -1;

2406 }

2325 }

2407 else {

2326 else {

2408 /* Not using NEWOBJ. */

2327 /* Not using NEWOBJ. */

2409 if (save(self, callable, 0) < 0 ||

2328 if (save(self, callable, 0) < 0 ||

2410 save(self, argtup, 0) < 0 ||

2329 save(self, argtup, 0) < 0 ||

2411 » » self->write_func(self, &reduce, 1) < 0)

2330 » » _Pickler_Write(self, &reduce, 1) < 0)

2412 return -1;

2331 return -1;

2413 }

2332 }

2414

2333

2415 /* Memoize. */

2334 /* Memoize. */

2416 /* XXX How can ob be NULL? */

2335 /* XXX How can ob be NULL? */

2417 if (ob != NULL) {

2336 if (ob != NULL) {

2418 if (state && !PyDict_Check(state)) {

2337 if (state && !PyDict_Check(state)) {

2419 if (put2(self, ob) < 0)

2338 if (put2(self, ob) < 0)

2420 return -1;

2339 return -1;

2421 }

2340 }

2422 else if (put(self, ob) < 0)

2341 else if (put(self, ob) < 0)

2423 return -1;

2342 return -1;

2424 }

2343 }

2425

2344

2426

2345

2427 if (listitems && batch_list(self, listitems) < 0)

2346 if (listitems && batch_list(self, listitems) < 0)

2428 return -1;

2347 return -1;

2429

2348

2430 if (dictitems && batch_dict(self, dictitems) < 0)

2349 if (dictitems && batch_dict(self, dictitems) < 0)

2431 return -1;

2350 return -1;

2432

2351

2433 if (state) {

2352 if (state) {

2434 if (save(self, state, 0) < 0 ||

2353 if (save(self, state, 0) < 0 ||

2435 » » self->write_func(self, &build, 1) < 0)

2354 » » _Pickler_Write(self, &build, 1) < 0)

2436 return -1;

2355 return -1;

2437 }

2356 }

2438

2357

2439 return 0;

2358 return 0;

2440 }

2359 }

2441

2360

2442 static int

2361 static int

2443 save(Picklerobject *self, PyObject *args, int pers_save)

2362 save(Picklerobject *self, PyObject *args, int pers_save)

2444 {

2363 {

2445 PyTypeObject *type;

2364 PyTypeObject *type;

(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2694 dump(Picklerobject *self, PyObject *args)

2613 dump(Picklerobject *self, PyObject *args)

2695 {

2614 {

2696 static char stop = STOP;

2615 static char stop = STOP;

2697

2616

2698 if (self->proto >= 2) {

2617 if (self->proto >= 2) {

2699 char bytes[2];

2618 char bytes[2];

2700

2619

2701 bytes[0] = PROTO;

2620 bytes[0] = PROTO;

2702 assert(self->proto >= 0 && self->proto < 256);

2621 assert(self->proto >= 0 && self->proto < 256);

2703 bytes[1] = (char)self->proto;

2622 bytes[1] = (char)self->proto;

2704 » » if (self->write_func(self, bytes, 2) < 0)

2623 » » if (_Pickler_Write(self, bytes, 2) < 0)

2705 return -1;

2624 return -1;

2706 }

2625 }

2707

2626

2708 if (save(self, args, 0) < 0)

2627 if (save(self, args, 0) < 0)

2709 return -1;

2628 return -1;

2710

2629

2711 » if (self->write_func(self, &stop, 1) < 0)

2630 » if (_Pickler_Write(self, &stop, 1) < 0)

2712 return -1;

2631 return -1;

2713

2632

2714 » if (self->write_func(self, NULL, 0) < 0)

2633 » if (_Pickler_Write(self, NULL, 0) < 0)

2715 return -1;

2634 return -1;

2716

2635

2717 return 0;

2636 return 0;

2718 }

2637 }

2719

2638

2639 static int

2640 _Pickler_ClearBuffer(Picklerobject *self)

2641 {

2642 memset(self->output_buffer, 0, sizeof(char) * self->output_len);

2643 self->output_len = 0;

2644 return 0;

2645 }

2646

2720 static PyObject *

2647 static PyObject *

2721 Pickle_clear_memo(Picklerobject *self, PyObject *args)

2648 Pickler_clear_memo(Picklerobject *self, PyObject *args)

2722 {

2649 {

2723 if (self->memo)

2650 if (self->memo)

2724 PyDict_Clear(self->memo);

2651 PyDict_Clear(self->memo);

2725 Py_INCREF(Py_None);

2652 Py_INCREF(Py_None);

2726 return Py_None;

2653 return Py_None;

2727 }

2654 }

2728

2655

2729 static PyObject *

2656 static PyObject *

2730 Pickle_getvalue(Picklerobject *self, PyObject *args)

2657 Pickler_getvalue(Picklerobject *self, PyObject *args)

2731 {

2658 {

2732 » int l, i, rsize, ssize, clear=1, lm;

2659 » int clear = 1;

2733 » long ik;

2660 » PyObject *value;

2734 » PyObject *k, *r;

2735 » char *s, *p, *have_get;

2736 » Pdata *data;

2737

2661

2738 /* Can be called by Python code or C code */

2662 /* Can be called by Python code or C code */

2739 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))

2663 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))

2740 return NULL;

2664 return NULL;

2741

2665

2742 » /* Check to make sure we are based on a list */

2666 » if (self->file != NULL) {

2743 » if (! Pdata_Check(self->file)) {

2667 » » PyErr_SetString(

2744 » » PyErr_SetString(PicklingError,

2668 » » » PicklingError,

2745 » » » » "Attempt to getvalue() a non-list-based pickler" );

2669 » » » "Attempt to getvalue() a stream-based pickler");

2746 return NULL;

2670 return NULL;

2747 }

2671 }

2748

2672

2749 » /* flush write buffer */

2673 » value = _Pickler_GetString(self);

2750 » if (write_other(self, NULL, 0) < 0) return NULL;

2674 » if (value == NULL)

2675 » » return NULL;

2751

2676

2752 » data=(Pdata*)self->file;

2677 » if (clear)

2753 » l=data->length;

2678 » » self->output_len = 0;

2754

2679 » return value;

2755 » /* set up an array to hold get/put status */

2756 » lm = PyDict_Size(self->memo);

2757 » if (lm < 0) return NULL;

2758 » lm++;

2759 » have_get = malloc(lm);

2760 » if (have_get == NULL) return PyErr_NoMemory();

2761 » memset(have_get, 0, lm);

2762

2763 » /* Scan for gets. */

2764 » for (rsize = 0, i = l; --i >= 0; ) {

2765 » » k = data->data[i];

2766

2767 » » if (PyString_Check(k))

2768 » » » rsize += PyString_GET_SIZE(k);

2769

2770 » » else if (PyInt_Check(k)) { /* put */

2771 » » » ik = PyInt_AS_LONG((PyIntObject*)k);

2772 » » » if (ik >= lm || ik == 0) {

2773 » » » » PyErr_SetString(PicklingError,

2774 » » » » » » "Invalid get data");

2775 » » » » goto err;

2776 » » » }

2777 » » » if (have_get[ik]) /* with matching get */

2778 » » » » rsize += ik < 256 ? 2 : 5;

2779 » » }

2780

2781 » » else if (! (PyTuple_Check(k) &&

2782 » » » PyTuple_GET_SIZE(k) == 2 &&

2783 » » » PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))

2784 » » » ) {

2785 » » » PyErr_SetString(PicklingError,

2786 » » » » » "Unexpected data in internal list");

2787 » » » goto err;

2788 » » }

2789

2790 » » else { /* put */

2791 » » » ik = PyInt_AS_LONG((PyIntObject *)k);

2792 » » » if (ik >= lm || ik == 0) {

2793 » » » » PyErr_SetString(PicklingError,

2794 » » » » » » "Invalid get data");

2795 » » » » return NULL;

2796 » » » }

2797 » » » have_get[ik] = 1;

2798 » » » rsize += ik < 256 ? 2 : 5;

2799 » » }

2800 » }

2801

2802 » /* Now generate the result */

2803 » r = PyString_FromStringAndSize(NULL, rsize);

2804 » if (r == NULL) goto err;

2805 » s = PyString_AS_STRING((PyStringObject *)r);

2806

2807 » for (i = 0; i < l; i++) {

2808 » » k = data->data[i];

2809

2810 » » if (PyString_Check(k)) {

2811 » » » ssize = PyString_GET_SIZE(k);

2812 » » » if (ssize) {

2813 » » » » p=PyString_AS_STRING((PyStringObject *)k);

2814 » » » » while (--ssize >= 0)

2815 » » » » » *s++ = *p++;

2816 » » » }

2817 » » }

2818

2819 » » else if (PyTuple_Check(k)) { /* get */

2820 » » » ik = PyInt_AS_LONG((PyIntObject *)

2821 » » » » » PyTuple_GET_ITEM(k, 0));

2822 » » » if (ik < 256) {

2823 » » » » *s++ = BINGET;

2824 » » » » *s++ = (int)(ik & 0xff);

2825 » » » }

2826 » » » else {

2827 » » » » *s++ = LONG_BINGET;

2828 » » » » *s++ = (int)(ik & 0xff);

2829 » » » » *s++ = (int)((ik >> 8) & 0xff);

2830 » » » » *s++ = (int)((ik >> 16) & 0xff);

2831 » » » » *s++ = (int)((ik >> 24) & 0xff);

2832 » » » }

2833 » » }

2834

2835 » » else { /* put */

2836 » » » ik = PyInt_AS_LONG((PyIntObject*)k);

2837

2838 » » » if (have_get[ik]) { /* with matching get */

2839 » » » » if (ik < 256) {

2840 » » » » » *s++ = BINPUT;

2841 » » » » » *s++ = (int)(ik & 0xff);

2842 » » » » }

2843 » » » » else {

2844 » » » » » *s++ = LONG_BINPUT;

2845 » » » » » *s++ = (int)(ik & 0xff);

2846 » » » » » *s++ = (int)((ik >> 8) & 0xff);

2847 » » » » » *s++ = (int)((ik >> 16) & 0xff);

2848 » » » » » *s++ = (int)((ik >> 24) & 0xff);

2849 » » » » }

2850 » » » }

2851 » » }

2852 » }

2853

2854 » if (clear) {

2855 » » PyDict_Clear(self->memo);

2856 » » Pdata_clear(data, 0);

2857 » }

2858

2859 » free(have_get);

2860 » return r;

2861 err:

2862 » free(have_get);

2863 » return NULL;

2864 }

2680 }

2865

2681

2866 static PyObject *

2682 static PyObject *

2867 Pickler_dump(Picklerobject *self, PyObject *args)

2683 Pickler_dump(Picklerobject *self, PyObject *args)

2868 {

2684 {

2869 PyObject *ob;

2685 PyObject *ob;

2870 int get=0;

2686 int get=0;

2871

2687

2872 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))

2688 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))

2873 return NULL;

2689 return NULL;

2874

2690

2691 if (_Pickler_ClearBuffer(self) < 0)

2692 return NULL;

2875 if (dump(self, ob) < 0)

2693 if (dump(self, ob) < 0)

2876 return NULL;

2694 return NULL;

2877

2695

2878 » if (get) return Pickle_getvalue(self, NULL);

2696 » if (self->file && _Pickler_FlushToFile(self) < 0)

2697 » » return NULL;

2698

2699 » if (get)

2700 » » return Pickler_getvalue(self, NULL);

2879

2701

2880 /* XXX Why does dump() return self? */

2702 /* XXX Why does dump() return self? */

2881 Py_INCREF(self);

2703 Py_INCREF(self);

2882 return (PyObject*)self;

2704 return (PyObject*)self;

2883 }

2705 }

2884

2706

2885

2707

2886 static struct PyMethodDef Pickler_methods[] =

2708 static struct PyMethodDef Pickler_methods[] =

2887 {

2709 {

2888 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,

2710 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,

2889 PyDoc_STR("dump(object) -- "

2711 PyDoc_STR("dump(object) -- "

2890 "Write an object in pickle format to the object's pickle stream")},

2712 "Write an object in pickle format to the object's pickle stream")},

2891 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,

2713 {"clear_memo", (PyCFunction)Pickler_clear_memo, METH_NOARGS,

2892 PyDoc_STR("clear_memo() -- Clear the picklers memo")},

2714 PyDoc_STR("clear_memo() -- Clear the picklers memo")},

2893 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,

2715 {"getvalue", (PyCFunction)Pickler_getvalue, METH_VARARGS,

2894 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},

2716 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},

2895 {NULL, NULL} /* sentinel */

2717 {NULL, NULL} /* sentinel */

2896 };

2718 };

2897

2719

2898

2720

2899 static Picklerobject *

2721 static Picklerobject *

2900 newPicklerobject(PyObject *file, int proto)

2722 newPicklerobject(PyObject *file, int proto)

2901 {

2723 {

2902 Picklerobject *self;

2724 Picklerobject *self;

2903

2725

2904 if (proto < 0)

2726 if (proto < 0)

2905 proto = HIGHEST_PROTOCOL;

2727 proto = HIGHEST_PROTOCOL;

2906 if (proto > HIGHEST_PROTOCOL) {

2728 if (proto > HIGHEST_PROTOCOL) {

2907 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "

2729 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "

2908 "the highest available protocol is %d",

2730 "the highest available protocol is %d",

2909 proto, HIGHEST_PROTOCOL);

2731 proto, HIGHEST_PROTOCOL);

2910 return NULL;

2732 return NULL;

2911 }

2733 }

2912

2734

2913 self = PyObject_GC_New(Picklerobject, &Picklertype);

2735 self = PyObject_GC_New(Picklerobject, &Picklertype);

2914 if (self == NULL)

2736 if (self == NULL)

2915 return NULL;

2737 return NULL;

2916 self->proto = proto;

2738 self->proto = proto;

2917 self->bin = proto > 0;

2739 self->bin = proto > 0;

2918 self->fp = NULL;

2919 self->write = NULL;

2920 self->memo = NULL;

2740 self->memo = NULL;

2921 self->arg = NULL;

2741 self->arg = NULL;

2922 self->pers_func = NULL;

2742 self->pers_func = NULL;

2923 self->inst_pers_func = NULL;

2743 self->inst_pers_func = NULL;

2924 self->write_buf = NULL;

2925 self->fast = 0;

2744 self->fast = 0;

2926 self->fast_container = 0;

2745 self->fast_container = 0;

2927 self->fast_memo = NULL;

2746 self->fast_memo = NULL;

2928 self->buf_size = 0;

2929 self->dispatch_table = NULL;

2747 self->dispatch_table = NULL;

2930

2748

2931 » self->file = NULL;

2749 » /* Use this buffer internally, flushing to file only when

2932 » if (file)

2750 » necessary. */

2933 » » Py_INCREF(file);

2751 » self->max_output_len = 4096;

2934 » else {

2752 » self->output_buffer = PyMem_NEW(char, 4096);

2935 » » file = Pdata_New();

2753 » self->output_len = 0;

2936 » » if (file == NULL)

2937 » » » goto err;

2938 » }

2939 self->file = file;

2754 self->file = file;

2755 Py_XINCREF(self->file);

2940

2756

2941 if (!( self->memo = PyDict_New()))

2757 if (!( self->memo = PyDict_New()))

2942 goto err;

2758 goto err;

2943

2759

2944 if (PyFile_Check(file)) {

2945 self->fp = PyFile_AsFile(file);

2946 if (self->fp == NULL) {

2947 PyErr_SetString(PyExc_ValueError,

2948 "I/O operation on closed file");

2949 goto err;

2950 }

2951 self->write_func = write_file;

2952 }

2953 else if (PycStringIO_OutputCheck(file)) {

2954 self->write_func = write_cStringIO;

2955 }

2956 else if (file == Py_None) {

2957 self->write_func = write_none;

2958 }

2959 else {

2960 self->write_func = write_other;

2961

2962 if (! Pdata_Check(file)) {

2963 self->write = PyObject_GetAttr(file, write_str);

2964 if (!self->write) {

2965 PyErr_Clear();

2966 PyErr_SetString(PyExc_TypeError,

2967 "argument must have 'write' "

2968 "attribute");

2969 goto err;

2970 }

2971 }

2972

2973 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);

2974 if (self->write_buf == NULL) {

2975 PyErr_NoMemory();

2976 goto err;

2977 }

2978 }

2979

2980 if (PyEval_GetRestricted()) {

2760 if (PyEval_GetRestricted()) {

2981 /* Restricted execution, get private tables */

2761 /* Restricted execution, get private tables */

2982 PyObject *m = PyImport_Import(copyreg_str);

2762 PyObject *m = PyImport_Import(copyreg_str);

2983

2763

2984 if (m == NULL)

2764 if (m == NULL)

2985 goto err;

2765 goto err;

2986 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);

2766 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);

2987 Py_DECREF(m);

2767 Py_DECREF(m);

2988 if (self->dispatch_table == NULL)

2768 if (self->dispatch_table == NULL)

2989 goto err;

2769 goto err;

(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3024 return NULL;

2804 return NULL;

3025 }

2805 }

3026 return (PyObject *)newPicklerobject(file, proto);

2806 return (PyObject *)newPicklerobject(file, proto);

3027 }

2807 }

3028

2808

3029

2809

3030 static void

2810 static void

3031 Pickler_dealloc(Picklerobject *self)

2811 Pickler_dealloc(Picklerobject *self)

3032 {

2812 {

3033 PyObject_GC_UnTrack(self);

2813 PyObject_GC_UnTrack(self);

3034 Py_XDECREF(self->write);

3035 Py_XDECREF(self->memo);

2814 Py_XDECREF(self->memo);

3036 Py_XDECREF(self->fast_memo);

2815 Py_XDECREF(self->fast_memo);

3037 Py_XDECREF(self->arg);

2816 Py_XDECREF(self->arg);

3038 Py_XDECREF(self->file);

2817 Py_XDECREF(self->file);

3039 Py_XDECREF(self->pers_func);

2818 Py_XDECREF(self->pers_func);

3040 Py_XDECREF(self->inst_pers_func);

2819 Py_XDECREF(self->inst_pers_func);

3041 Py_XDECREF(self->dispatch_table);

2820 Py_XDECREF(self->dispatch_table);

3042 » PyMem_Free(self->write_buf);

2821 » if (self->output_buffer != NULL)

2822 » » PyMem_FREE(self->output_buffer);

2823 » self->output_buffer = NULL;

3043 Py_TYPE(self)->tp_free((PyObject *)self);

2824 Py_TYPE(self)->tp_free((PyObject *)self);

3044 }

2825 }

3045

2826

3046 static int

2827 static int

3047 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)

2828 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)

3048 {

2829 {

3049 Py_VISIT(self->write);

3050 Py_VISIT(self->memo);

2830 Py_VISIT(self->memo);

3051 Py_VISIT(self->fast_memo);

2831 Py_VISIT(self->fast_memo);

3052 Py_VISIT(self->arg);

2832 Py_VISIT(self->arg);

3053 Py_VISIT(self->file);

2833 Py_VISIT(self->file);

3054 Py_VISIT(self->pers_func);

2834 Py_VISIT(self->pers_func);

3055 Py_VISIT(self->inst_pers_func);

2835 Py_VISIT(self->inst_pers_func);

3056 Py_VISIT(self->dispatch_table);

2836 Py_VISIT(self->dispatch_table);

3057 return 0;

2837 return 0;

3058 }

2838 }

3059

2839

3060 static int

2840 static int

3061 Pickler_clear(Picklerobject *self)

2841 Pickler_clear(Picklerobject *self)

3062 {

2842 {

3063 Py_CLEAR(self->write);

3064 Py_CLEAR(self->memo);

2843 Py_CLEAR(self->memo);

3065 Py_CLEAR(self->fast_memo);

2844 Py_CLEAR(self->fast_memo);

3066 Py_CLEAR(self->arg);

2845 Py_CLEAR(self->arg);

3067 Py_CLEAR(self->file);

2846 Py_CLEAR(self->file);

3068 Py_CLEAR(self->pers_func);

2847 Py_CLEAR(self->pers_func);

3069 Py_CLEAR(self->inst_pers_func);

2848 Py_CLEAR(self->inst_pers_func);

3070 Py_CLEAR(self->dispatch_table);

2849 Py_CLEAR(self->dispatch_table);

2850 if (self->output_buffer != NULL)

2851 PyMem_FREE(self->output_buffer);

2852 self->output_buffer = NULL;

3071 return 0;

2853 return 0;

3072 }

2854 }

3073

2855

3074 static PyObject *

2856 static PyObject *

3075 Pickler_get_pers_func(Picklerobject *p)

2857 Pickler_get_pers_func(Picklerobject *p)

3076 {

2858 {

3077 if (p->pers_func == NULL)

2859 if (p->pers_func == NULL)

3078 PyErr_SetString(PyExc_AttributeError, "persistent_id");

2860 PyErr_SetString(PyExc_AttributeError, "persistent_id");

3079 else

2861 else

3080 Py_INCREF(p->pers_func);

2862 Py_INCREF(p->pers_func);

(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3158 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},

2940 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},

3159 {"PicklingError", (getter)Pickler_get_error, NULL},

2941 {"PicklingError", (getter)Pickler_get_error, NULL},

3160 {NULL}

2942 {NULL}

3161 };

2943 };

3162

2944

3163 PyDoc_STRVAR(Picklertype__doc__,

2945 PyDoc_STRVAR(Picklertype__doc__,

3164 "Objects that know how to pickle objects\n");

2946 "Objects that know how to pickle objects\n");

3165

2947

3166 static PyTypeObject Picklertype = {

2948 static PyTypeObject Picklertype = {

3167 PyVarObject_HEAD_INIT(NULL, 0)

2949 PyVarObject_HEAD_INIT(NULL, 0)

3168 "cPickle.Pickler", /*tp_name*/

2950 "cPickle.Pickler", » /*tp_name*/

3169 sizeof(Picklerobject), /*tp_basicsize*/

2951 sizeof(Picklerobject), /*tp_basicsize*/

3170 0,

2952 0,

3171 (destructor)Pickler_dealloc, /* tp_dealloc */

2953 (destructor)Pickler_dealloc, /* tp_dealloc */

3172 0, /* tp_print */

2954 0, /* tp_print */

3173 0,» » » » » /* tp_getattr */

2955 0,» » » » » /* tp_getattr */

3174 0,» » » » » /* tp_setattr */

2956 0,» » » » » /* tp_setattr */

3175 0, /* tp_compare */

2957 0, /* tp_compare */

3176 0,» » » » » /* tp_repr */

2958 0,» » » » » /* tp_repr */

3177 0, /* tp_as_number */

2959 0, /* tp_as_number */

3178 0, /* tp_as_sequence */

2960 0, /* tp_as_sequence */

3179 0, /* tp_as_mapping */

2961 0, /* tp_as_mapping */

3180 0, /* tp_hash */

2962 0, /* tp_hash */

3181 0, /* tp_call */

2963 0, /* tp_call */

3182 0, /* tp_str */

2964 0, /* tp_str */

3183 PyObject_GenericGetAttr, /* tp_getattro */

2965 PyObject_GenericGetAttr, /* tp_getattro */

3184 PyObject_GenericSetAttr, /* tp_setattro */

2966 PyObject_GenericSetAttr, /* tp_setattro */

3185 0, /* tp_as_buffer */

2967 0, /* tp_as_buffer */

3186 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,

2968 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,

(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3282 }

3064 }

3283 else {

3065 else {

3284 if (len == 3 && (l == 0 || l == 1)) {

3066 if (len == 3 && (l == 0 || l == 1)) {

3285 if (!( py_int = PyBool_FromLong(l))) goto finally;

3067 if (!( py_int = PyBool_FromLong(l))) goto finally;

3286 }

3068 }

3287 else {

3069 else {

3288 if (!( py_int = PyInt_FromLong(l))) goto finally;

3070 if (!( py_int = PyInt_FromLong(l))) goto finally;

3289 }

3071 }

3290 }

3072 }

3291

3073

3292 » free(s);

3074 » PyMem_FREE(s);

3293 PDATA_PUSH(self->stack, py_int, -1);

3075 PDATA_PUSH(self->stack, py_int, -1);

3294 return 0;

3076 return 0;

3295

3077

3296 finally:

3078 finally:

3297 » free(s);

3079 » PyMem_FREE(s);

3298

3080

3299 return res;

3081 return res;

3300 }

3082 }

3301

3083

3302 static int

3084 static int

3303 load_bool(Unpicklerobject *self, PyObject *boolean)

3085 load_bool(Unpicklerobject *self, PyObject *boolean)

3304 {

3086 {

3305 assert(boolean == Py_True || boolean == Py_False);

3087 assert(boolean == Py_True || boolean == Py_False);

3306 PDATA_APPEND(self->stack, boolean, -1);

3088 PDATA_APPEND(self->stack, boolean, -1);

3307 return 0;

3089 return 0;

(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3393 char *end, *s;

3175 char *end, *s;

3394 int len, res = -1;

3176 int len, res = -1;

3395

3177

3396 if ((len = self->readline_func(self, &s)) < 0) return -1;

3178 if ((len = self->readline_func(self, &s)) < 0) return -1;

3397 if (len < 2) return bad_readline();

3179 if (len < 2) return bad_readline();

3398 if (!( s=pystrndup(s,len))) return -1;

3180 if (!( s=pystrndup(s,len))) return -1;

3399

3181

3400 if (!( l = PyLong_FromString(s, &end, 0)))

3182 if (!( l = PyLong_FromString(s, &end, 0)))

3401 goto finally;

3183 goto finally;

3402

3184

3403 » free(s);

3185 » PyMem_FREE(s);

3404 PDATA_PUSH(self->stack, l, -1);

3186 PDATA_PUSH(self->stack, l, -1);

3405 return 0;

3187 return 0;

3406

3188

3407 finally:

3189 finally:

3408 » free(s);

3190 » PyMem_FREE(s);

3409

3191

3410 return res;

3192 return res;

3411 }

3193 }

3412

3194

3413 /* 'size' bytes contain the # of bytes of little-endian 256's-complement

3195 /* 'size' bytes contain the # of bytes of little-endian 256's-complement

3414 * data following.

3196 * data following.

3415 */

3197 */

3416 static int

3198 static int

3417 load_counted_long(Unpicklerobject *self, int size)

3199 load_counted_long(Unpicklerobject *self, int size)

3418 {

3200 {

(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3468 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||

3250 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||

3469 (endptr[0] != '\n') || (endptr[1] != '\0')) {

3251 (endptr[0] != '\n') || (endptr[1] != '\0')) {

3470 PyErr_SetString(PyExc_ValueError,

3252 PyErr_SetString(PyExc_ValueError,

3471 "could not convert string to float");

3253 "could not convert string to float");

3472 goto finally;

3254 goto finally;

3473 }

3255 }

3474

3256

3475 if (!( py_float = PyFloat_FromDouble(d)))

3257 if (!( py_float = PyFloat_FromDouble(d)))

3476 goto finally;

3258 goto finally;

3477

3259

3478 » free(s);

3260 » PyMem_FREE(s);

3479 PDATA_PUSH(self->stack, py_float, -1);

3261 PDATA_PUSH(self->stack, py_float, -1);

3480 return 0;

3262 return 0;

3481

3263

3482 finally:

3264 finally:

3483 » free(s);

3265 » PyMem_FREE(s);

3484

3266

3485 return res;

3267 return res;

3486 }

3268 }

3487

3269

3488 static int

3270 static int

3489 load_binfloat(Unpicklerobject *self)

3271 load_binfloat(Unpicklerobject *self)

3490 {

3272 {

3491 PyObject *py_float;

3273 PyObject *py_float;

3492 double x;

3274 double x;

3493 char *p;

3275 char *p;

(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3528 len -= 2;

3310 len -= 2;

3529 } else if(s[0]=='\'' && s[len-1]=='\''){

3311 } else if(s[0]=='\'' && s[len-1]=='\''){

3530 s[len-1] = '\0';

3312 s[len-1] = '\0';

3531 p = s + 1 ;

3313 p = s + 1 ;

3532 len -= 2;

3314 len -= 2;

3533 } else

3315 } else

3534 goto insecure;

3316 goto insecure;

3535 /********************************************/

3317 /********************************************/

3536

3318

3537 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);

3319 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);

3538 » free(s);

3320 » PyMem_FREE(s);

3539 if (str) {

3321 if (str) {

3540 PDATA_PUSH(self->stack, str, -1);

3322 PDATA_PUSH(self->stack, str, -1);

3541 res = 0;

3323 res = 0;

3542 }

3324 }

3543 return res;

3325 return res;

3544

3326

3545 insecure:

3327 insecure:

3546 » free(s);

3328 » PyMem_FREE(s);

3547 PyErr_SetString(PyExc_ValueError,"insecure string pickle");

3329 PyErr_SetString(PyExc_ValueError,"insecure string pickle");

3548 return -1;

3330 return -1;

3549 }

3331 }

3550

3332

3551

3333

3552 static int

3334 static int

3553 load_binstring(Unpicklerobject *self)

3335 load_binstring(Unpicklerobject *self)

3554 {

3336 {

3555 PyObject *py_string = 0;

3337 PyObject *py_string = 0;

3556 long l;

3338 long l;

(...skipping 948 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

4505 {

4287 {

4506 int s;

4288 int s;

4507

4289

4508 /* Note that we split the (pickle.py) stack into two stacks, an

4290 /* Note that we split the (pickle.py) stack into two stacks, an

4509 object stack and a mark stack. Here we push a mark onto the

4291 object stack and a mark stack. Here we push a mark onto the

4510 mark stack.

4292 mark stack.

4511 */

4293 */

4512

4294

4513 if ((self->num_marks + 1) >= self->marks_size) {

4295 if ((self->num_marks + 1) >= self->marks_size) {

4514 int *marks;

4296 int *marks;

4515 » » s=self->marks_size+20;

4297 » » s = self->marks_size+20;

4516 » » if (s <= self->num_marks) s=self->num_marks + 1;

4298 » » if (s <= self->num_marks)

4299 » » » s = self->num_marks + 1;

4517 if (self->marks == NULL)

4300 if (self->marks == NULL)

4518 » » » marks=(int *)malloc(s * sizeof(int));

4301 » » » marks = PyMem_NEW(int, s);

4519 » » else

4302 » » else {

4520 » » » marks=(int *)realloc(self->marks,

4303 » » » marks = self->marks;

4521 » » » » » » s * sizeof(int));

4304 » » » PyMem_RESIZE(marks, int, s);

4522 » » if (!marks) {

4305 » » }

4306 » » if (marks == NULL) {

4523 PyErr_NoMemory();

4307 PyErr_NoMemory();

4524 return -1;

4308 return -1;

4525 }

4309 }

4526 self->marks = marks;

4310 self->marks = marks;

4527 self->marks_size = s;

4311 self->marks_size = s;

4528 }

4312 }

4529

4313

4530 self->marks[self->num_marks++] = self->stack->length;

4314 self->marks[self->num_marks++] = self->stack->length;

4531

4315

4532 return 0;

4316 return 0;

(...skipping 989 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

5522 {

5306 {

5523 static char *kwlist[] = {"obj", "file", "protocol", NULL};

5307 static char *kwlist[] = {"obj", "file", "protocol", NULL};

5524 PyObject *ob, *file, *res = NULL;

5308 PyObject *ob, *file, *res = NULL;

5525 Picklerobject *pickler = 0;

5309 Picklerobject *pickler = 0;

5526 int proto = 0;

5310 int proto = 0;

5527

5311

5528 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,

5312 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,

5529 &ob, &file, &proto)))

5313 &ob, &file, &proto)))

5530 goto finally;

5314 goto finally;

5531

5315

5532 » if (!( pickler = newPicklerobject(file, proto)))

5316 » pickler = newPicklerobject(file, proto);

5317 » if (pickler == NULL)

5533 goto finally;

5318 goto finally;

5534

5319

5535 if (dump(pickler, ob) < 0)

5320 if (dump(pickler, ob) < 0)

5536 goto finally;

5321 goto finally;

5537

5322

5323 if (_Pickler_FlushToFile(pickler) < 0)

5324 goto finally;

5325

5538 Py_INCREF(Py_None);

5326 Py_INCREF(Py_None);

5539 res = Py_None;

5327 res = Py_None;

5540

5328

5541 finally:

5329 finally:

5542 Py_XDECREF(pickler);

5330 Py_XDECREF(pickler);

5543

5331

5544 return res;

5332 return res;

5545 }

5333 }

5546

5334

5547

5335

5548 /* dumps(obj, protocol=0). */

5336 /* dumps(obj, protocol=0). */

5549 static PyObject *

5337 static PyObject *

5550 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)

5338 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)

5551 {

5339 {

5552 static char *kwlist[] = {"obj", "protocol", NULL};

5340 static char *kwlist[] = {"obj", "protocol", NULL};

5553 » PyObject *ob, *file = 0, *res = NULL;

5341 » PyObject *ob, *res = NULL;

5554 » Picklerobject *pickler = 0;

5342 » Picklerobject *pickler = NULL;

5555 int proto = 0;

5343 int proto = 0;

5556

5344

5557 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,

5345 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,

5558 &ob, &proto)))

5346 &ob, &proto)))

5559 goto finally;

5347 goto finally;

5560

5348

5561 » if (!( file = PycStringIO->NewOutput(128)))

5349 » if ((pickler = newPicklerobject(NULL, proto)) == NULL)

5562 » » goto finally;

5563

5564 » if (!( pickler = newPicklerobject(file, proto)))

5565 goto finally;

5350 goto finally;

5566

5351

5567 if (dump(pickler, ob) < 0)

5352 if (dump(pickler, ob) < 0)

5568 goto finally;

5353 goto finally;

5569

5354

5570 » res = PycStringIO->cgetvalue(file);

5355 » res = _Pickler_GetString(pickler);

5571

5356

5572 finally:

5357 finally:

5573 Py_XDECREF(pickler);

5358 Py_XDECREF(pickler);

5574 Py_XDECREF(file);

5575

5576 return res;

5359 return res;

5577 }

5360 }

5578

5361

5579

5362

5580 /* load(fileobj). */

5363 /* load(fileobj). */

5581 static PyObject *

5364 static PyObject *

5582 cpm_load(PyObject *self, PyObject *ob)

5365 cpm_load(PyObject *self, PyObject *ob)

5583 {

5366 {

5584 Unpicklerobject *unpickler = 0;

5367 Unpicklerobject *unpickler = 0;

5585 PyObject *res = NULL;

5368 PyObject *res = NULL;

(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

5901 "1.0", /* Original protocol 0 */

5684 "1.0", /* Original protocol 0 */

5902 "1.1", /* Protocol 0 + INST */

5685 "1.1", /* Protocol 0 + INST */

5903 "1.2", /* Original protocol 1 */

5686 "1.2", /* Original protocol 1 */

5904 "1.3", /* Protocol 1 + BINFLOAT */

5687 "1.3", /* Protocol 1 + BINFLOAT */

5905 "2.0"); /* Original protocol 2 */

5688 "2.0"); /* Original protocol 2 */

5906 PyDict_SetItemString(d, "format_version", format_version);

5689 PyDict_SetItemString(d, "format_version", format_version);

5907 PyDict_SetItemString(d, "compatible_formats", compatible_formats);

5690 PyDict_SetItemString(d, "compatible_formats", compatible_formats);

5908 Py_XDECREF(format_version);

5691 Py_XDECREF(format_version);

5909 Py_XDECREF(compatible_formats);

5692 Py_XDECREF(compatible_formats);

5910 }

5693 }

OLD

NEW