Changeset 14 for trunk/ext/_syckmodule.c
- Timestamp:
- 07/28/05 04:45:26 (8 years ago)
- File:
-
- 1 edited
-
trunk/ext/_syckmodule.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ext/_syckmodule.c
r12 r14 3 3 #include <syck.h> 4 4 5 /* Python 2.2 compatibility. */ 5 /**************************************************************************** 6 * Python 2.2 compatibility. 7 ****************************************************************************/ 6 8 7 9 #ifndef PyDoc_STR … … 15 17 #endif 16 18 17 /* Global objects: _syck.error, 'scalar', 'seq', 'map', 18 '1quote', '2quote', 'fold', 'literal', 'plain', '+', '-'. */ 19 /**************************************************************************** 20 * Global objects: _syck.error, 'scalar', 'seq', 'map', 21 * '1quote', '2quote', 'fold', 'literal', 'plain', '+', '-'. 22 ****************************************************************************/ 19 23 20 24 static PyObject *PySyck_Error; … … 33 37 static PyObject *PySyck_KeepChomp; 34 38 35 /* The type _syck.Node. */ 39 /**************************************************************************** 40 * The type _syck.Node. 41 ****************************************************************************/ 36 42 37 43 PyDoc_STRVAR(PySyckNode_doc, … … 41 47 "directly. You may use _syck.Node for type checking.\n"); 42 48 49 typedef struct { 50 PyObject_HEAD 51 /* Common fields for all Node types: */ 52 PyObject *value; /* always an object */ 53 PyObject *tag; /* a string object or NULL */ 54 PyObject *anchor; /* a string object or NULL */ 55 } PySyckNodeObject; 56 57 58 static int 59 PySyckNode_clear(PySyckNodeObject *self) 60 { 61 PyObject *tmp; 62 63 tmp = self->value; 64 self->value = NULL; 65 Py_XDECREF(tmp); 66 67 tmp = self->tag; 68 self->tag = NULL; 69 Py_XDECREF(tmp); 70 71 tmp = self->anchor; 72 self->value = NULL; 73 Py_XDECREF(tmp); 74 75 return 0; 76 } 77 78 static int 79 PySyckNode_traverse(PySyckNodeObject *self, visitproc visit, void *arg) 80 { 81 int ret; 82 83 if (self->value) 84 if ((ret = visit(self->value, arg)) != 0) 85 return ret; 86 87 if (self->tag) 88 if ((ret = visit(self->tag, arg)) != 0) 89 return ret; 90 91 if (self->anchor) 92 if ((ret = visit(self->anchor, arg)) != 0) 93 return ret; 94 95 return 0; 96 } 97 98 static void 99 PySyckNode_dealloc(PySyckNodeObject *self) 100 { 101 PySyckNode_clear(self); 102 self->ob_type->tp_free((PyObject *)self); 103 } 104 105 static PyObject * 106 PySyckNode_getkind(PySyckNodeObject *self, PyObject **closure) 107 { 108 Py_INCREF(*closure); 109 return *closure; 110 } 111 112 static PyObject * 113 PySyckNode_getvalue(PySyckNodeObject *self, void *closure) 114 { 115 Py_INCREF(self->value); 116 return self->value; 117 } 118 119 static PyObject * 120 PySyckNode_gettag(PySyckNodeObject *self, void *closure) 121 { 122 PyObject *value = self->tag ? self->tag : Py_None; 123 Py_INCREF(value); 124 return value; 125 } 126 127 static int 128 PySyckNode_settag(PySyckNodeObject *self, PyObject *value, void *closure) 129 { 130 if (!value) { 131 PyErr_SetString(PyExc_TypeError, "cannot delete 'tag'"); 132 return -1; 133 } 134 135 if (value == Py_None) { 136 Py_XDECREF(self->tag); 137 self->tag = NULL; 138 return 0; 139 } 140 141 if (!PyString_Check(value)) { 142 PyErr_SetString(PyExc_TypeError, "'tag' must be a string"); 143 return -1; 144 } 145 146 Py_XDECREF(self->tag); 147 Py_INCREF(value); 148 self->tag = value; 149 150 return 0; 151 } 152 153 static PyObject * 154 PySyckNode_getanchor(PySyckNodeObject *self, void *closure) 155 { 156 PyObject *value = self->anchor ? self->anchor : Py_None; 157 Py_INCREF(value); 158 return value; 159 } 160 161 static int 162 PySyckNode_setanchor(PySyckNodeObject *self, PyObject *value, void *closure) 163 { 164 if (!value) { 165 PyErr_SetString(PyExc_TypeError, "cannot delete 'anchor'"); 166 return -1; 167 } 168 169 if (value == Py_None) { 170 Py_XDECREF(self->anchor); 171 self->anchor = NULL; 172 return 0; 173 } 174 175 if (!PyString_Check(value)) { 176 PyErr_SetString(PyExc_TypeError, "'anchor' must be a string"); 177 return -1; 178 } 179 180 Py_XDECREF(self->anchor); 181 Py_INCREF(value); 182 self->anchor = value; 183 184 return 0; 185 } 186 43 187 static PyTypeObject PySyckNode_Type = { 44 188 PyObject_HEAD_INIT(NULL) 45 189 0, /* ob_size */ 46 190 "_syck.Node", /* tp_name */ 47 sizeof(PyObject), /* tp_basicsize */ 191 sizeof(PySyckNodeObject), /* tp_basicsize */ 192 0, /* tp_itemsize */ 193 (destructor)PySyckNode_dealloc, /* tp_dealloc */ 194 0, /* tp_print */ 195 0, /* tp_getattr */ 196 0, /* tp_setattr */ 197 0, /* tp_compare */ 198 0, /* tp_repr */ 199 0, /* tp_as_number */ 200 0, /* tp_as_sequence */ 201 0, /* tp_as_mapping */ 202 0, /* tp_hash */ 203 0, /* tp_call */ 204 0, /* tp_str */ 205 0, /* tp_getattro */ 206 0, /* tp_setattro */ 207 0, /* tp_as_buffer */ 208 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /* tp_flags */ 209 PySyckNode_doc, /* tp_doc */ 210 (traverseproc)PySyckNode_traverse, /* tp_traverse */ 211 (inquiry)PySyckNode_clear, /* tp_clear */ 212 }; 213 214 /**************************************************************************** 215 * The type _syck.Scalar. 216 ****************************************************************************/ 217 218 PyDoc_STRVAR(PySyckScalar_doc, 219 "Scalar(value='', tag=None, style=None, indent=0, width=0, chomp=None)\n" 220 " -> a Scalar node\n\n" 221 "_syck.Scalar represents a scalar node in Syck parser and emitter\n" 222 "graphs. A scalar node points to a single string value.\n\n" 223 "Attributes:\n\n" 224 "kind -- always 'scalar'; read-only\n" 225 "value -- the node value, a string\n" 226 "tag -- the node tag; a string or None\n" 227 "anchor -- the name of the node anchor or None; read-only\n" 228 "style -- the node style; None (means literal or plain),\n" 229 " '1quote', '2quote', 'fold', 'literal', 'plain'\n" 230 "indent -- indentation, an integer; 0 means default\n" 231 "width -- the preferred width; 0 means default\n" 232 "chomp -- None (clip), '-' (strip), or '+' (keep)\n"); 233 234 typedef struct { 235 PyObject_HEAD 236 /* Common fields for all Node types: */ 237 PyObject *value; /* always a string object */ 238 PyObject *tag; /* a string object or NULL */ 239 PyObject *anchor; /* a string object or NULL */ 240 /* Scalar-specific fields: */ 241 enum scalar_style style; 242 int indent; 243 int width; 244 char chomp; 245 } PySyckScalarObject; 246 247 static PyObject * 248 PySyckScalar_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 249 { 250 PySyckScalarObject *self; 251 252 self = (PySyckScalarObject *)type->tp_alloc(type, 0); 253 if (!self) return NULL; 254 255 self->value = PyString_FromString(""); 256 if (!self->value) { 257 Py_DECREF(self); 258 return NULL; 259 } 260 261 self->tag = NULL; 262 self->anchor = NULL; 263 self->style = scalar_none; 264 self->indent = 0; 265 self->width = 0; 266 self->chomp = 0; 267 268 return (PyObject *)self; 269 } 270 271 static int 272 PySyckScalar_setvalue(PySyckScalarObject *self, PyObject *value, void *closure) 273 { 274 if (!value) { 275 PyErr_SetString(PyExc_TypeError, "cannot delete 'value'"); 276 return -1; 277 } 278 if (!PyString_Check(value)) { 279 PyErr_SetString(PyExc_TypeError, "'value' must be a string"); 280 return -1; 281 } 282 283 Py_DECREF(self->value); 284 Py_INCREF(value); 285 self->value = value; 286 287 return 0; 288 } 289 290 static PyObject * 291 PySyckScalar_getstyle(PySyckScalarObject *self, void *closure) 292 { 293 PyObject *value; 294 295 switch (self->style) { 296 case scalar_1quote: value = PySyck_1QuoteStyle; break; 297 case scalar_2quote: value = PySyck_2QuoteStyle; break; 298 case scalar_fold: value = PySyck_FoldStyle; break; 299 case scalar_literal: value = PySyck_LiteralStyle; break; 300 case scalar_plain: value = PySyck_PlainStyle; break; 301 default: value = Py_None; 302 } 303 304 Py_INCREF(value); 305 return value; 306 } 307 308 static int 309 PySyckScalar_setstyle(PySyckScalarObject *self, PyObject *value, void *closure) 310 { 311 char *str; 312 313 if (!value) { 314 PyErr_SetString(PyExc_TypeError, "cannot delete 'style'"); 315 return -1; 316 } 317 318 if (value == Py_None) { 319 self->style = scalar_none; 320 return 0; 321 } 322 323 if (!PyString_Check(value)) { 324 PyErr_SetString(PyExc_TypeError, "'style' must be a string or None"); 325 return -1; 326 } 327 328 str = PyString_AsString(value); 329 if (!str) return -1; 330 331 if (strcmp(str, "1quote") == 0) 332 self->style = scalar_1quote; 333 else if (strcmp(str, "2quote") == 0) 334 self->style = scalar_2quote; 335 else if (strcmp(str, "fold") == 0) 336 self->style = scalar_fold; 337 else if (strcmp(str, "literal") == 0) 338 self->style = scalar_literal; 339 else if (strcmp(str, "plain") == 0) 340 self->style = scalar_plain; 341 else { 342 PyErr_SetString(PyExc_TypeError, "unknown 'style'"); 343 return -1; 344 } 345 346 return 0; 347 } 348 349 static PyObject * 350 PySyckScalar_getindent(PySyckScalarObject *self, void *closure) 351 { 352 return PyInt_FromLong(self->indent); 353 } 354 355 static int 356 PySyckScalar_setindent(PySyckScalarObject *self, PyObject *value, void *closure) 357 { 358 if (!value) { 359 PyErr_SetString(PyExc_TypeError, "cannot delete 'indent'"); 360 return -1; 361 } 362 363 if (!PyInt_Check(value)) { 364 PyErr_SetString(PyExc_TypeError, "'indent' must be an integer"); 365 return -1; 366 } 367 368 self->indent = PyInt_AS_LONG(value); 369 370 return 0; 371 } 372 373 static PyObject * 374 PySyckScalar_getwidth(PySyckScalarObject *self, void *closure) 375 { 376 return PyInt_FromLong(self->width); 377 } 378 379 static int 380 PySyckScalar_setwidth(PySyckScalarObject *self, PyObject *value, void *closure) 381 { 382 if (!value) { 383 PyErr_SetString(PyExc_TypeError, "cannot delete 'width'"); 384 return -1; 385 } 386 387 if (!PyInt_Check(value)) { 388 PyErr_SetString(PyExc_TypeError, "'width' must be an integer"); 389 return -1; 390 } 391 392 self->width = PyInt_AS_LONG(value); 393 394 return 0; 395 } 396 397 static PyObject * 398 PySyckScalar_getchomp(PySyckScalarObject *self, void *closure) 399 { 400 PyObject *value; 401 402 switch (self->chomp) { 403 case NL_CHOMP: value = PySyck_StripChomp; break; 404 case NL_KEEP: value = PySyck_KeepChomp; break; 405 default: value = Py_None; 406 } 407 408 Py_INCREF(value); 409 return value; 410 } 411 412 static int 413 PySyckScalar_setchomp(PySyckScalarObject *self, PyObject *value, void *closure) 414 { 415 char *str; 416 417 if (!value) { 418 PyErr_SetString(PyExc_TypeError, "cannot delete 'chomp'"); 419 return -1; 420 } 421 422 if (value == Py_None) { 423 self->chomp = 0; 424 return 0; 425 } 426 427 if (!PyString_Check(value)) { 428 PyErr_SetString(PyExc_TypeError, "'chomp' must be '+', '-', or None"); 429 return -1; 430 } 431 432 str = PyString_AsString(value); 433 if (!str) return -1; 434 435 if (strcmp(str, "-") == 0) 436 self->chomp = NL_CHOMP; 437 else if (strcmp(str, "+") == 0) 438 self->chomp = NL_KEEP; 439 else { 440 PyErr_SetString(PyExc_TypeError, "'chomp' must be '+', '-', or None"); 441 return -1; 442 } 443 444 return 0; 445 } 446 447 static int 448 PySyckScalar_init(PySyckScalarObject *self, PyObject *args, PyObject *kwds) 449 { 450 PyObject *value = NULL; 451 PyObject *tag = NULL; 452 PyObject *anchor = NULL; 453 PyObject *style = NULL; 454 PyObject *indent = NULL; 455 PyObject *width = NULL; 456 PyObject *chomp = NULL; 457 458 static char *kwdlist[] = {"value", "tag", "anchor", 459 "style", "indent", "width", "chomp", NULL}; 460 461 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOO", kwdlist, 462 &value, &tag, &anchor, &style, &indent, &width, &chomp)) 463 return -1; 464 465 if (value && PySyckScalar_setvalue(self, value, NULL) < 0) 466 return -1; 467 468 if (tag && PySyckNode_settag((PySyckNodeObject *)self, tag, NULL) < 0) 469 return -1; 470 471 if (anchor && PySyckNode_setanchor((PySyckNodeObject *)self, anchor, NULL) < 0) 472 return -1; 473 474 if (style && PySyckScalar_setstyle(self, style, NULL) < 0) 475 return -1; 476 477 if (indent && PySyckScalar_setindent(self, indent, NULL) < 0) 478 return -1; 479 480 if (width && PySyckScalar_setwidth(self, width, NULL) < 0) 481 return -1; 482 483 if (chomp && PySyckScalar_setchomp(self, chomp, NULL) < 0) 484 return -1; 485 486 return 0; 487 } 488 489 static PyGetSetDef PySyckScalar_getsetters[] = { 490 {"kind", (getter)PySyckNode_getkind, NULL, 491 "the node kind", &PySyck_ScalarKind}, 492 {"value", (getter)PySyckNode_getvalue, (setter)PySyckScalar_setvalue, 493 "the node value", NULL}, 494 {"tag", (getter)PySyckNode_gettag, (setter)PySyckNode_settag, 495 "the node tag", NULL}, 496 {"anchor", (getter)PySyckNode_getanchor, (setter)PySyckNode_setanchor, 497 "the node anchor", NULL}, 498 {"style", (getter)PySyckScalar_getstyle, (setter)PySyckScalar_setstyle, 499 "the node style", NULL}, 500 {"indent", (getter)PySyckScalar_getindent, (setter)PySyckScalar_setindent, 501 "the field indentation", NULL}, 502 {"width", (getter)PySyckScalar_getwidth, (setter)PySyckScalar_setwidth, 503 "the field width", NULL}, 504 {"chomp", (getter)PySyckScalar_getchomp, (setter)PySyckScalar_setchomp, 505 "the chomping method", NULL}, 506 {NULL} /* Sentinel */ 507 }; 508 509 static PyTypeObject PySyckScalar_Type = { 510 PyObject_HEAD_INIT(NULL) 511 0, /* ob_size */ 512 "_syck.Scalar", /* tp_name */ 513 sizeof(PySyckScalarObject), /* tp_basicsize */ 48 514 0, /* tp_itemsize */ 49 515 0, /* tp_dealloc */ … … 63 529 0, /* tp_as_buffer */ 64 530 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ 65 PySyckNode_doc, /* tp_doc */ 531 PySyckScalar_doc, /* tp_doc */ 532 0, /* tp_traverse */ 533 0, /* tp_clear */ 534 0, /* tp_richcompare */ 535 0, /* tp_weaklistoffset */ 536 0, /* tp_iter */ 537 0, /* tp_iternext */ 538 0, /* tp_methods */ 539 0, /* tp_members */ 540 PySyckScalar_getsetters, /* tp_getset */ 541 &PySyckNode_Type, /* tp_base */ 542 0, /* tp_dict */ 543 0, /* tp_descr_get */ 544 0, /* tp_descr_set */ 545 0, /* tp_dictoffset */ 546 (initproc)PySyckScalar_init, /* tp_init */ 547 0, /* tp_alloc */ 548 PySyckScalar_new, /* tp_new */ 66 549 }; 67 550 68 /* The type _syck.Scalar */ 69 70 PyDoc_STRVAR(PySyckScalar_doc, 71 "The Scalar node type\n\n" 72 "_syck.Scalar represents a scalar node in Syck parser and emitter\n" 73 "graph. A scalar node points to a single string value.\n\n" 551 /**************************************************************************** 552 * The type _syck.Seq. 553 ****************************************************************************/ 554 555 PyDoc_STRVAR(PySyckSeq_doc, 556 "Seq(value=[], tag=None, inline=False) -> a Seq node\n\n" 557 "_syck.Seq represents a sequence node in Syck parser and emitter\n" 558 "graphs. A sequence node points to an ordered set of subnodes.\n\n" 74 559 "Attributes:\n\n" 75 "kind -- always 's calar'; read-only\n"76 "value -- the node value, a string\n"560 "kind -- always 'seq'; read-only\n" 561 "value -- the node value, a list\n" 77 562 "tag -- the node tag; a string or None\n" 78 563 "anchor -- the name of the node anchor or None; read-only\n" 79 "style -- the node style; None (means literal or plain),\n" 80 " '1quote', '2quote', 'fold', 'literal', 'plain'\n" 81 "indent -- indentation, an integer; 0 means default\n" 82 "width -- the preferred width; 0 means default\n" 83 "chomp -- None (clip), '-' (strip), or '+' (keep)\n"); 564 "inline -- is the node inline? False or True\n"); 84 565 85 566 typedef struct { 86 567 PyObject_HEAD 87 PyObject *value; 88 PyObject *tag; 89 PyObject *anchor; 90 enum scalar_style style; 91 int indent; 92 int width; 93 char chomp; 94 } PySyckScalarObject; 95 96 static int 97 PySyckScalar_clear(PySyckScalarObject *self) 98 { 99 Py_XDECREF(self->value); 100 self->value = NULL; 101 Py_XDECREF(self->tag); 102 self->tag = NULL; 103 Py_XDECREF(self->anchor); 104 self->anchor = NULL; 105 106 return 0; 107 } 108 109 static int 110 PySyckScalar_traverse(PySyckScalarObject *self, visitproc visit, void *arg) 111 { 112 if (self->value && visit(self->value, arg) < 0) 113 return -1; 114 if (self->tag && visit(self->tag, arg) < 0) 115 return -1; 116 if (self->anchor && visit(self->anchor, arg) < 0) 117 return -1; 118 119 return 0; 120 } 121 122 static void 123 PySyckScalar_dealloc(PySyckScalarObject *self) 124 { 125 PySyckScalar_clear(self); 126 self->ob_type->tp_free((PyObject *)self); 127 } 128 129 static PyObject * 130 PySyckScalar_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 131 { 132 PySyckScalarObject *self; 133 134 self = (PySyckScalarObject *)type->tp_alloc(type, 0); 568 /* Common fields for all Node types: */ 569 PyObject *value; /* always an object */ 570 PyObject *tag; /* a string object or NULL */ 571 PyObject *anchor; /* a string object or NULL */ 572 /* Seq-specific fields: */ 573 enum seq_style style; 574 } PySyckSeqObject; 575 576 static PyObject * 577 PySyckSeq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 578 { 579 PySyckSeqObject *self; 580 581 self = (PySyckSeqObject *)type->tp_alloc(type, 0); 135 582 if (!self) return NULL; 136 583 137 self->value = Py String_FromString("");584 self->value = PyList_New(0); 138 585 if (!self->value) { 139 586 Py_DECREF(self); … … 143 590 self->tag = NULL; 144 591 self->anchor = NULL; 145 self->style = scalar_none; 146 self->indent = 0; 147 self->width = 0; 148 self->chomp = 0; 592 self->style = seq_none; 149 593 150 594 return (PyObject *)self; 151 595 } 152 596 153 static PyObject * 154 PySyckScalar_getkind(PySyckScalarObject *self, void *closure) 155 { 156 Py_INCREF(PySyck_ScalarKind); 157 return PySyck_ScalarKind; 158 } 159 160 static PyObject * 161 PySyckScalar_getvalue(PySyckScalarObject *self, void *closure) 162 { 163 Py_INCREF(self->value); 164 return self->value; 165 } 166 167 static int 168 PySyckScalar_setvalue(PySyckScalarObject *self, PyObject *value, void *closure) 597 static int 598 PySyckSeq_setvalue(PySyckSeqObject *self, PyObject *value, void *closure) 169 599 { 170 600 if (!value) { … … 172 602 return -1; 173 603 } 174 if (!PyS tring_Check(value)) {175 PyErr_SetString(PyExc_TypeError, "'value' must be a s tring");604 if (!PySequence_Check(value)) { /* PySequence_Check always succeeds */ 605 PyErr_SetString(PyExc_TypeError, "'value' must be a sequence"); 176 606 return -1; 177 607 } … … 185 615 186 616 static PyObject * 187 PySyckScalar_gettag(PySyckScalarObject *self, void *closure) 188 { 189 PyObject *value = self->tag ? self->tag : Py_None; 617 PySyckSeq_getinline(PySyckSeqObject *self, void *closure) 618 { 619 PyObject *value = (self->style == seq_inline) ? Py_True : Py_False; 620 190 621 Py_INCREF(value); 191 622 return value; … … 193 624 194 625 static int 195 PySyckS calar_settag(PySyckScalarObject *self, PyObject *value, void *closure)626 PySyckSeq_setinline(PySyckSeqObject *self, PyObject *value, void *closure) 196 627 { 197 628 if (!value) { 198 PyErr_SetString(PyExc_TypeError, "cannot delete 'tag'"); 199 return -1; 200 } 201 202 if (value == Py_None) { 203 Py_XDECREF(self->tag); 204 self->tag = NULL; 205 return 0; 206 } 207 208 if (!PyString_Check(value)) { 209 PyErr_SetString(PyExc_TypeError, "'tag' must be a string"); 210 return -1; 211 } 212 213 Py_XDECREF(self->tag); 214 Py_INCREF(value); 215 self->tag = value; 216 217 return 0; 218 } 219 220 static PyObject * 221 PySyckScalar_getanchor(PySyckScalarObject *self, void *closure) 222 { 223 PyObject *value = self->anchor ? self->anchor : Py_None; 224 Py_INCREF(value); 225 return value; 226 } 227 228 static int 229 PySyckScalar_setanchor(PySyckScalarObject *self, PyObject *value, void *closure) 230 { 231 if (!value) { 232 PyErr_SetString(PyExc_TypeError, "cannot delete 'anchor'"); 233 return -1; 234 } 235 236 if (value == Py_None) { 237 Py_XDECREF(self->anchor); 238 self->anchor = NULL; 239 return 0; 240 } 241 242 if (!PyString_Check(value)) { 243 PyErr_SetString(PyExc_TypeError, "'anchor' must be a string"); 244 return -1; 245 } 246 247 Py_XDECREF(self->anchor); 248 Py_INCREF(value); 249 self->anchor = value; 250 251 return 0; 252 } 253 254 static PyObject * 255 PySyckScalar_getstyle(PySyckScalarObject *self, void *closure) 256 { 257 PyObject *value; 258 259 switch (self->style) { 260 case scalar_1quote: value = PySyck_1QuoteStyle; break; 261 case scalar_2quote: value = PySyck_2QuoteStyle; break; 262 case scalar_fold: value = PySyck_FoldStyle; break; 263 case scalar_literal: value = PySyck_LiteralStyle; break; 264 case scalar_plain: value = PySyck_PlainStyle; break; 265 default: value = Py_None; 266 } 267 268 Py_INCREF(value); 269 return value; 270 } 271 272 static int 273 PySyckScalar_setstyle(PySyckScalarObject *self, PyObject *value, void *closure) 274 { 275 char *str; 276 277 if (!value) { 278 PyErr_SetString(PyExc_TypeError, "cannot delete 'style'"); 279 return -1; 280 } 281 282 if (value == Py_None) { 283 self->style = scalar_none; 284 return 0; 285 } 286 287 if (!PyString_Check(value)) { 288 PyErr_SetString(PyExc_TypeError, "'style' must be a string or None"); 289 return -1; 290 } 291 292 str = PyString_AsString(value); 293 if (!str) return -1; 294 295 if (strcmp(str, "1quote") == 0) 296 self->style = scalar_1quote; 297 else if (strcmp(str, "2quote") == 0) 298 self->style = scalar_2quote; 299 else if (strcmp(str, "fold") == 0) 300 self->style = scalar_fold; 301 else if (strcmp(str, "literal") == 0) 302 self->style = scalar_literal; 303 else if (strcmp(str, "plain") == 0) 304 self->style = scalar_plain; 305 else { 306 PyErr_SetString(PyExc_TypeError, "unknown 'style'"); 307 return -1; 308 } 309 310 return 0; 311 } 312 313 static PyObject * 314 PySyckScalar_getindent(PySyckScalarObject *self, void *closure) 315 { 316 return PyInt_FromLong(self->indent); 317 } 318 319 static int 320 PySyckScalar_setindent(PySyckScalarObject *self, PyObject *value, void *closure) 321 { 322 if (!value) { 323 PyErr_SetString(PyExc_TypeError, "cannot delete 'indent'"); 629 PyErr_SetString(PyExc_TypeError, "cannot delete 'inline'"); 324 630 return -1; 325 631 } 326 632 327 633 if (!PyInt_Check(value)) { 328 PyErr_SetString(PyExc_TypeError, "'indent' must be an integer"); 329 return -1; 330 } 331 332 self->indent = PyInt_AS_LONG(value); 333 334 return 0; 335 } 336 337 static PyObject * 338 PySyckScalar_getwidth(PySyckScalarObject *self, void *closure) 339 { 340 return PyInt_FromLong(self->width); 341 } 342 343 static int 344 PySyckScalar_setwidth(PySyckScalarObject *self, PyObject *value, void *closure) 345 { 346 if (!value) { 347 PyErr_SetString(PyExc_TypeError, "cannot delete 'width'"); 348 return -1; 349 } 350 351 if (!PyInt_Check(value)) { 352 PyErr_SetString(PyExc_TypeError, "'width' must be an integer"); 353 return -1; 354 } 355 356 self->width = PyInt_AS_LONG(value); 357 358 return 0; 359 } 360 361 static PyObject * 362 PySyckScalar_getchomp(PySyckScalarObject *self, void *closure) 363 { 364 PyObject *value; 365 366 switch (self->chomp) { 367 case NL_CHOMP: value = PySyck_StripChomp; break; 368 case NL_KEEP: value = PySyck_KeepChomp; break; 369 default: value = Py_None; 370 } 371 372 Py_INCREF(value); 373 return value; 374 } 375 376 static int 377 PySyckScalar_setchomp(PySyckScalarObject *self, PyObject *value, void *closure) 378 { 379 char *str; 380 381 if (!value) { 382 PyErr_SetString(PyExc_TypeError, "cannot delete 'chomp'"); 383 return -1; 384 } 385 386 if (value == Py_None) { 387 self->chomp = 0; 388 return 0; 389 } 390 391 if (!PyString_Check(value)) { 392 PyErr_SetString(PyExc_TypeError, "'chomp' must be '+', '-', or None"); 393 return -1; 394 } 395 396 str = PyString_AsString(value); 397 if (!str) return -1; 398 399 if (strcmp(str, "-") == 0) 400 self->chomp = NL_CHOMP; 401 else if (strcmp(str, "+") == 0) 402 self->chomp = NL_KEEP; 403 else { 404 PyErr_SetString(PyExc_TypeError, "'chomp' must be '+', '-', or None"); 405 return -1; 406 } 407 408 return 0; 409 } 410 411 static int 412 PySyckScalar_init(PySyckScalarObject *self, PyObject *args, PyObject *kwds) 634 PyErr_SetString(PyExc_TypeError, "'inline' must be a Boolean object"); 635 return -1; 636 } 637 638 self->style = PyInt_AS_LONG(value) ? seq_inline : seq_none; 639 640 return 0; 641 } 642 643 static int 644 PySyckSeq_init(PySyckSeqObject *self, PyObject *args, PyObject *kwds) 413 645 { 414 646 PyObject *value = NULL; 415 647 PyObject *tag = NULL; 416 648 PyObject *anchor = NULL; 417 PyObject *style = NULL; 418 PyObject *indent = NULL; 419 PyObject *width = NULL; 420 PyObject *chomp = NULL; 421 422 static char *kwdlist[] = {"value", "tag", "anchor", 423 "style", "indent", "width", "chomp", NULL}; 424 425 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOO", kwdlist, 426 &value, &tag, &anchor, &style, &indent, &width, &chomp)) 427 return -1; 428 429 if (value && PySyckScalar_setvalue(self, value, NULL) < 0) 430 return -1; 431 432 if (tag && PySyckScalar_settag(self, tag, NULL) < 0) 433 return -1; 434 435 if (anchor && PySyckScalar_setanchor(self, anchor, NULL) < 0) 436 return -1; 437 438 if (style && PySyckScalar_setstyle(self, style, NULL) < 0) 439 return -1; 440 441 if (indent && PySyckScalar_setindent(self, indent, NULL) < 0) 442 return -1; 443 444 if (width && PySyckScalar_setwidth(self, width, NULL) < 0) 445 return -1; 446 447 if (chomp && PySyckScalar_setchomp(self, chomp, NULL) < 0) 448 return -1; 449 450 return 0; 451 } 452 453 static PyGetSetDef PySyckScalar_getsetters[] = { 454 {"kind", (getter)PySyckScalar_getkind, NULL, 455 "the node kind", NULL}, 456 {"value", (getter)PySyckScalar_getvalue, (setter)PySyckScalar_setvalue, 649 PyObject *inline_ = NULL; 650 651 static char *kwdlist[] = {"value", "tag", "anchor", "inline", NULL}; 652 653 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwdlist, 654 &value, &tag, &anchor, &inline_)) 655 return -1; 656 657 if (value && PySyckSeq_setvalue(self, value, NULL) < 0) 658 return -1; 659 660 if (tag && PySyckNode_settag((PySyckNodeObject *)self, tag, NULL) < 0) 661 return -1; 662 663 if (anchor && PySyckNode_setanchor((PySyckNodeObject *)self, anchor, NULL) < 0) 664 return -1; 665 666 if (inline_ && PySyckSeq_setinline(self, inline_, NULL) < 0) 667 return -1; 668 669 return 0; 670 } 671 672 static PyGetSetDef PySyckSeq_getsetters[] = { 673 {"kind", (getter)PySyckNode_getkind, NULL, 674 "the node kind", &PySyck_SeqKind}, 675 {"value", (getter)PySyckNode_getvalue, (setter)PySyckSeq_setvalue, 457 676 "the node value", NULL}, 458 {"tag", (getter)PySyck Scalar_gettag, (setter)PySyckScalar_settag,677 {"tag", (getter)PySyckNode_gettag, (setter)PySyckNode_settag, 459 678 "the node tag", NULL}, 460 {"anchor", (getter)PySyck Scalar_getanchor, (setter)PySyckScalar_setanchor,679 {"anchor", (getter)PySyckNode_getanchor, (setter)PySyckNode_setanchor, 461 680 "the node anchor", NULL}, 462 {" style", (getter)PySyckScalar_getstyle, (setter)PySyckScalar_setstyle,681 {"inline", (getter)PySyckSeq_getinline, (setter)PySyckSeq_setinline, 463 682 "the node style", NULL}, 464 {"indent", (getter)PySyckScalar_getindent, (setter)PySyckScalar_setindent,465 "the field indentation", NULL},466 {"width", (getter)PySyckScalar_getwidth, (setter)PySyckScalar_setwidth,467 "the field width", NULL},468 {"chomp", (getter)PySyckScalar_getchomp, (setter)PySyckScalar_setchomp,469 "the chomping method", NULL},470 683 {NULL} /* Sentinel */ 471 684 }; 472 685 473 static PyTypeObject PySyckS calar_Type = {686 static PyTypeObject PySyckSeq_Type = { 474 687 PyObject_HEAD_INIT(NULL) 475 688 0, /* ob_size */ 476 "_syck.S calar",/* tp_name */477 sizeof(PySyckS calarObject),/* tp_basicsize */689 "_syck.Seq", /* tp_name */ 690 sizeof(PySyckSeqObject), /* tp_basicsize */ 478 691 0, /* tp_itemsize */ 479 (destructor)PySyckScalar_dealloc,/* tp_dealloc */692 0, /* tp_dealloc */ 480 693 0, /* tp_print */ 481 694 0, /* tp_getattr */ … … 493 706 0, /* tp_as_buffer */ 494 707 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /* tp_flags */ 495 PySyckS calar_doc,/* tp_doc */496 (traverseproc)PySyck Scalar_traverse,/* tp_traverse */497 (inquiry)PySyck Scalar_clear,/* tp_clear */708 PySyckSeq_doc, /* tp_doc */ 709 (traverseproc)PySyckNode_traverse, /* tp_traverse */ 710 (inquiry)PySyckNode_clear, /* tp_clear */ 498 711 0, /* tp_richcompare */ 499 712 0, /* tp_weaklistoffset */ … … 502 715 0, /* tp_methods */ 503 716 0, /* tp_members */ 504 PySyckS calar_getsetters,/* tp_getset */717 PySyckSeq_getsetters, /* tp_getset */ 505 718 &PySyckNode_Type, /* tp_base */ 506 719 0, /* tp_dict */ … … 508 721 0, /* tp_descr_set */ 509 722 0, /* tp_dictoffset */ 510 (initproc)PySyckS calar_init,/* tp_init */723 (initproc)PySyckSeq_init, /* tp_init */ 511 724 0, /* tp_alloc */ 512 PySyckScalar_new, /* tp_new */ 725 PySyckSeq_new, /* tp_new */ 726 }; 727 728 /**************************************************************************** 729 * The type _syck.Map. 730 ****************************************************************************/ 731 732 PyDoc_STRVAR(PySyckMap_doc, 733 "Map(value='', tag=None, inline=False) -> a Map node\n\n" 734 "_syck.Map represents a mapping node in Syck parser and emitter\n" 735 "graphs. A mapping node points to an unordered collections of pairs.\n\n" 736 "Attributes:\n\n" 737 "kind -- always 'map'; read-only\n" 738 "value -- the node value, a dictionary\n" 739 "tag -- the node tag; a string or None\n" 740 "anchor -- the name of the node anchor or None; read-only\n" 741 "inline -- is the node inline? False or True\n"); 742 743 typedef struct { 744 PyObject_HEAD 745 /* Common fields for all Node types: */ 746 PyObject *value; /* always an object */ 747 PyObject *tag; /* a string object or NULL */ 748 PyObject *anchor; /* a string object or NULL */ 749 /* Map-specific fields: */ 750 enum map_style style; 751 } PySyckMapObject; 752 753 static PyObject * 754 PySyckMap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 755 { 756 PySyckMapObject *self; 757 758 self = (PySyckMapObject *)type->tp_alloc(type, 0); 759 if (!self) return NULL; 760 761 self->value = PyDict_New(); 762 if (!self->value) { 763 Py_DECREF(self); 764 return NULL; 765 } 766 767 self->tag = NULL; 768 self->anchor = NULL; 769 self->style = seq_none; 770 771 return (PyObject *)self; 772 } 773 774 static int 775 PySyckMap_setvalue(PySyckMapObject *self, PyObject *value, void *closure) 776 { 777 if (!value) { 778 PyErr_SetString(PyExc_TypeError, "cannot delete 'value'"); 779 return -1; 780 } 781 if (!PyMapping_Check(value)) { /* PyMapping_Check always succeeds */ 782 PyErr_SetString(PyExc_TypeError, "'value' must be a mapping"); 783 return -1; 784 } 785 786 Py_DECREF(self->value); 787 Py_INCREF(value); 788 self->value = value; 789 790 return 0; 791 } 792 793 static PyObject * 794 PySyckMap_getinline(PySyckMapObject *self, void *closure) 795 { 796 PyObject *value = (self->style == map_inline) ? Py_True : Py_False; 797 798 Py_INCREF(value); 799 return value; 800 } 801 802 static int 803 PySyckMap_setinline(PySyckMapObject *self, PyObject *value, void *closure) 804 { 805 if (!value) { 806 PyErr_SetString(PyExc_TypeError, "cannot delete 'inline'"); 807 return -1; 808 } 809 810 if (!PyInt_Check(value)) { 811 PyErr_SetString(PyExc_TypeError, "'inline' must be a Boolean object"); 812 return -1; 813 } 814 815 self->style = PyInt_AS_LONG(value) ? map_inline : map_none; 816 817 return 0; 818 } 819 820 static int 821 PySyckMap_init(PySyckMapObject *self, PyObject *args, PyObject *kwds) 822 { 823 PyObject *value = NULL; 824 PyObject *tag = NULL; 825 PyObject *anchor = NULL; 826 PyObject *inline_ = NULL; 827 828 static char *kwdlist[] = {"value", "tag", "anchor", "inline", NULL}; 829 830 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwdlist, 831 &value, &tag, &anchor, &inline_)) 832 return -1; 833 834 if (value && PySyckMap_setvalue(self, value, NULL) < 0) 835 return -1; 836 837 if (tag && PySyckNode_settag((PySyckNodeObject *)self, tag, NULL) < 0) 838 return -1; 839 840 if (anchor && PySyckNode_setanchor((PySyckNodeObject *)self, anchor, NULL) < 0) 841 return -1; 842 843 if (inline_ && PySyckMap_setinline(self, inline_, NULL) < 0) 844 return -1; 845 846 return 0; 847 } 848 849 static PyGetSetDef PySyckMap_getsetters[] = { 850 {"kind", (getter)PySyckNode_getkind, NULL, 851 "the node kind", &PySyck_MapKind}, 852 {"value", (getter)PySyckNode_getvalue, (setter)PySyckMap_setvalue, 853 "the node value", NULL}, 854 {"tag", (getter)PySyckNode_gettag, (setter)PySyckNode_settag, 855 "the node tag", NULL}, 856 {"anchor", (getter)PySyckNode_getanchor, (setter)PySyckNode_setanchor, 857 "the node anchor", NULL}, 858 {"inline", (getter)PySyckMap_getinline, (setter)PySyckMap_setinline, 859 "the node style", NULL}, 860 {NULL} /* Sentinel */ 861 }; 862 863 static PyTypeObject PySyckMap_Type = { 864 PyObject_HEAD_INIT(NULL) 865 0, /* ob_size */ 866 "_syck.Map", /* tp_name */ 867 sizeof(PySyckMapObject), /* tp_basicsize */ 868 0, /* tp_itemsize */ 869 0, /* tp_dealloc */ 870 0, /* tp_print */ 871 0, /* tp_getattr */ 872 0, /* tp_setattr */ 873 0, /* tp_compare */ 874 0, /* tp_repr */ 875 0, /* tp_as_number */ 876 0, /* tp_as_sequence */ 877 0, /* tp_as_mapping */ 878 0, /* tp_hash */ 879 0, /* tp_call */ 880 0, /* tp_str */ 881 0, /* tp_getattro */ 882 0, /* tp_setattro */ 883 0, /* tp_as_buffer */ 884 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /* tp_flags */ 885 PySyckMap_doc, /* tp_doc */ 886 (traverseproc)PySyckNode_traverse, /* tp_traverse */ 887 (inquiry)PySyckNode_clear, /* tp_clear */ 888 0, /* tp_richcompare */ 889 0, /* tp_weaklistoffset */ 890 0, /* tp_iter */ 891 0, /* tp_iternext */ 892 0, /* tp_methods */ 893 0, /* tp_members */ 894 PySyckMap_getsetters, /* tp_getset */ 895 &PySyckNode_Type, /* tp_base */ 896 0, /* tp_dict */ 897 0, /* tp_descr_get */ 898 0, /* tp_descr_set */ 899 0, /* tp_dictoffset */ 900 (initproc)PySyckMap_init, /* tp_init */ 901 0, /* tp_alloc */ 902 PySyckMap_new, /* tp_new */ 513 903 }; 514 904 … … 1046 1436 if (PyType_Ready(&PySyckScalar_Type) < 0) 1047 1437 return; 1438 if (PyType_Ready(&PySyckSeq_Type) < 0) 1439 return; 1440 if (PyType_Ready(&PySyckMap_Type) < 0) 1441 return; 1048 1442 1049 1443 PySyck_Error = PyErr_NewException("_syck.error", NULL, NULL); … … 1082 1476 if (PyModule_AddObject(m, "Scalar", (PyObject *)&PySyckScalar_Type) < 0) 1083 1477 return; 1084 } 1085 1086 1478 1479 Py_INCREF(&PySyckSeq_Type); 1480 if (PyModule_AddObject(m, "Seq", (PyObject *)&PySyckSeq_Type) < 0) 1481 return; 1482 1483 Py_INCREF(&PySyckMap_Type); 1484 if (PyModule_AddObject(m, "Map", (PyObject *)&PySyckMap_Type) < 0) 1485 return; 1486 } 1487 1488
Note: See TracChangeset
for help on using the changeset viewer.
