@@ -367,7 +367,12 @@ def has_sequence(types, doing_specialization):
367367
368368
369369class StaticVisitor (PickleVisitor ):
370- '''Very simple, always emit this static code'''
370+ CODE = '''Very simple, always emit this static code. Overide CODE'''
371+
372+ def visit (self , object ):
373+ self .emit (self .CODE , 0 , reflow = False )
374+
375+ class FreeUtilVisitor (StaticVisitor ):
371376
372377 CODE = '''static void
373378free_seq_exprs(asdl_seq *seq)
@@ -390,10 +395,6 @@ class StaticVisitor(PickleVisitor):
390395}
391396'''
392397
393- def visit (self , object ):
394- self .emit (self .CODE , 0 , reflow = False )
395-
396-
397398class FreeVisitor (PickleVisitor ):
398399
399400 def func_begin (self , name , has_seq ):
@@ -483,6 +484,77 @@ def free(self, field, value, depth):
483484 self .emit ("free_%s((%s)%s);" % (field .type , ctype , value ), depth )
484485
485486
487+ class MarshalUtilVisitor (StaticVisitor ):
488+
489+ CODE = '''
490+ #define CHECKSIZE(BUF, OFF, MIN) { \\
491+ int need = *(OFF) + MIN; \\
492+ if (need >= PyString_GET_SIZE(*(BUF))) { \\
493+ int newsize = PyString_GET_SIZE(*(BUF)) * 2; \\
494+ if (newsize < need) \\
495+ newsize = need; \\
496+ if (_PyString_Resize((BUF), newsize) < 0) \\
497+ return 0; \\
498+ } \\
499+ }
500+
501+ static int
502+ marshal_write_int(PyObject **buf, int *offset, int x)
503+ {
504+ char *s;
505+
506+ CHECKSIZE(buf, offset, 4)
507+ s = PyString_AS_STRING(*buf) + (*offset);
508+ s[0] = (x & 0xff);
509+ s[1] = (x >> 8) & 0xff;
510+ s[2] = (x >> 16) & 0xff;
511+ s[3] = (x >> 24) & 0xff;
512+ *offset += 4;
513+ return 1;
514+ }
515+
516+ static int
517+ marshal_write_bool(PyObject **buf, int *offset, bool b)
518+ {
519+ if (b)
520+ marshal_write_int(buf, offset, 1);
521+ else
522+ marshal_write_int(buf, offset, 0);
523+ return 1;
524+ }
525+
526+ static int
527+ marshal_write_identifier(PyObject **buf, int *offset, identifier id)
528+ {
529+ int l = PyString_GET_SIZE(id);
530+ marshal_write_int(buf, offset, l);
531+ CHECKSIZE(buf, offset, l);
532+ memcpy(PyString_AS_STRING(*buf) + *offset,
533+ PyString_AS_STRING(id), l);
534+ *offset += l;
535+ return 1;
536+ }
537+
538+ static int
539+ marshal_write_string(PyObject **buf, int *offset, string s)
540+ {
541+ int len = PyString_GET_SIZE(s);
542+ marshal_write_int(buf, offset, len);
543+ CHECKSIZE(buf, offset, len);
544+ memcpy(PyString_AS_STRING(*buf) + *offset,
545+ PyString_AS_STRING(s), len);
546+ *offset += len;
547+ return 1;
548+ }
549+
550+ static int
551+ marshal_write_object(PyObject **buf, int *offset, object s)
552+ {
553+ /* XXX */
554+ return 0;
555+ }
556+ '''
557+
486558class MarshalFunctionVisitor (PickleVisitor ):
487559
488560 def func_begin (self , name , has_seq ):
@@ -563,6 +635,7 @@ def __init__(self, *visitors):
563635 def visit (self , object ):
564636 for v in self .visitors :
565637 v .visit (object )
638+ v .emit ("" , 0 )
566639
567640def main (srcfile ):
568641 auto_gen_msg = '/* File automatically generated by %s */\n ' % sys .argv [0 ]
@@ -595,8 +668,9 @@ def main(srcfile):
595668 print >> f
596669 v = ChainOfVisitors (MarshalPrototypeVisitor (f ),
597670 FunctionVisitor (f ),
598- StaticVisitor (f ),
671+ FreeUtilVisitor (f ),
599672 FreeVisitor (f ),
673+ MarshalUtilVisitor (f ),
600674 MarshalFunctionVisitor (f ),
601675 )
602676 v .visit (mod )
0 commit comments