File: cupsipp.c

package info (click to toggle)
python-cups 1.9.73-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 460 kB
  • sloc: ansic: 8,863; python: 325; makefile: 58
file content (947 lines) | stat: -rw-r--r-- 23,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
 * cups - Python bindings for CUPS
 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014  Red Hat, Inc.
 * Author: Tim Waugh <twaugh@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "cupsipp.h"
#include "cupsmodule.h"

/* Compatibility code for older (pre-2.5) versions of Python */
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif

//////////////////
// IPPAttribute //
//////////////////

static PyObject *
IPPAttribute_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
  IPPAttribute *self;
  self = (IPPAttribute *) type->tp_alloc (type, 0);
  if (self != NULL) {
    self->group_tag = IPP_TAG_ZERO;
    self->value_tag = IPP_TAG_ZERO;
    self->name = NULL;
    self->values = NULL;
  }

  return (PyObject *) self;
}

static int
IPPAttribute_init (IPPAttribute *self, PyObject *args, PyObject *kwds)
{
  ipp_tag_t group_tag, value_tag;
  PyObject *nameobj;
  char *name;
  PyObject *value = NULL;
  PyObject *list = NULL;

  if (!PyArg_ParseTuple (args, "iiO|O", &group_tag, &value_tag, &nameobj,
			 &value))
    return -1;

  if (UTF8_from_PyObj (&name, nameobj) == NULL)
    return -1;

  if (value == NULL) {
    switch (value_tag) {
    case IPP_TAG_ZERO:
    case IPP_TAG_NOVALUE:
    case IPP_TAG_NOTSETTABLE:
    case IPP_TAG_ADMINDEFINE:
      break;

    default:
      PyErr_SetString (PyExc_RuntimeError, "missing value list");
      return -1;
    }
  } else {
    size_t i, n;
    int valid = 1;

    if (PyList_Check (value)) {
      list = value;
      Py_INCREF (value);
      n = PyList_Size (value);
    } else {
      list = PyList_New (0);
      PyList_Append (list, value);
      n = 1;
    }

    for (i = 0; valid && i < n; i++) {
      PyObject *v = PyList_GetItem (list, i); // borrowed
      switch (value_tag) {
      case IPP_TAG_INTEGER:
      case IPP_TAG_ENUM:
      case IPP_TAG_RANGE:
#if PY_MAJOR_VERSION >= 3
	valid = PyLong_Check (v);
#else
	valid = PyInt_Check (v);
#endif
	break;

      case IPP_TAG_BOOLEAN:
	valid = PyBool_Check (v);
	break;

      case IPP_TAG_TEXT:
	valid = PyUnicode_Check (v);
	break;

      case IPP_TAG_NAME:
      case IPP_TAG_KEYWORD:
      case IPP_TAG_URI:
      case IPP_TAG_MIMETYPE:
      case IPP_TAG_CHARSET:
      case IPP_TAG_LANGUAGE:
	valid = (PyUnicode_Check (v) || PyBytes_Check (v));
	break;

      default:
	valid = 0;
      }
    }

    if (!valid) {
      PyErr_SetString (PyExc_RuntimeError, "invalid value");
      Py_DECREF (list);
      return -1;
    }
  }

  self->group_tag = group_tag;
  self->value_tag = value_tag;
  self->values = list;
  self->name = name;
  return 0;
}

static void
IPPAttribute_dealloc (IPPAttribute *self)
{
  Py_XDECREF (self->values);
  if (self->name)
    free (self->name);

  ((PyObject *)self)->ob_type->tp_free ((PyObject *) self);
}

static PyObject *
IPPAttribute_repr (IPPAttribute *self)
{
  PyObject *ret;
  PyObject *values_repr = NULL;
  char *values = NULL;
  char buffer[1024];
  if (self->values) {
    values_repr = PyList_Type.tp_repr (self->values);
    UTF8_from_PyObj (&values, values_repr);
  }

  snprintf (buffer, sizeof (buffer), "<cups.IPPAttribute %s (%d:%d)%s%s>",
	    self->name, self->group_tag, self->value_tag,
	    values ? ": " : "",
	    values ? values : "");
#if PY_MAJOR_VERSION >= 3
  ret = PyUnicode_FromString (buffer);
#else
  ret = PyBytes_FromString (buffer);
#endif

  free (values);
  Py_XDECREF (values_repr);
  return ret;
}

//////////////////
// IPPAttribute // ATTRIBUTES
//////////////////

static PyObject *
IPPAttribute_getGroupTag (IPPAttribute *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (self->group_tag);
#else
  return PyInt_FromLong (self->group_tag);
#endif
}

static PyObject *
IPPAttribute_getValueTag (IPPAttribute *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (self->value_tag);
#else
  return PyInt_FromLong (self->value_tag);
#endif
}

static PyObject *
IPPAttribute_getName (IPPAttribute *self, void *closure)
{
  return PyUnicode_FromString (self->name);
}

static PyObject *
IPPAttribute_getValues (IPPAttribute *self, void *closure)
{
  Py_INCREF (self->values);
  return self->values;
}

PyGetSetDef IPPAttribute_getseters[] =
  {
    { "group_tag",
      (getter) IPPAttribute_getGroupTag, (setter) NULL,
      "IPP group tag", NULL },

    { "value_tag",
      (getter) IPPAttribute_getValueTag, (setter) NULL,
      "IPP value tag", NULL },

    { "name",
      (getter) IPPAttribute_getName, (setter) NULL,
      "IPP attribute name", NULL },

    { "values",
      (getter) IPPAttribute_getValues, (setter) NULL,
      "List of values" },

    { NULL }, /* Sentinel */
  };

PyMethodDef IPPAttribute_methods[] =
  {
    { NULL } /* Sentinel */
  };

PyTypeObject cups_IPPAttributeType =
  {
    PyVarObject_HEAD_INIT(NULL, 0)
    "cups.IPPAttribute",       /*tp_name*/
    sizeof(IPPAttribute),      /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)IPPAttribute_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    (reprfunc)IPPAttribute_repr, /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
    "IPP Attribute\n"
    "=============\n"
    "  An IPP attribute.\n\n"
    "@type group_tag: int\n"
    "@ivar group_tag: IPP group tag\n"
    "@type value_tag: int\n"
    "@ivar value_tag: IPP value tag\n"
    "@type values: list\n"
    "@ivar value: list of values\n"
    "",                        /* tp_doc */
    0,                         /* tp_traverse */
    0,                         /* tp_clear */
    0,                         /* tp_richcompare */
    0,                         /* tp_weaklistoffset */
    0,                         /* tp_iter */
    0,                         /* tp_iternext */
    IPPAttribute_methods,      /* tp_methods */
    0,                         /* tp_members */
    IPPAttribute_getseters,    /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)IPPAttribute_init, /* tp_init */
    0,                         /* tp_alloc */
    IPPAttribute_new,          /* tp_new */
  };

////////////////
// IPPRequest //
////////////////

static PyObject *
IPPRequest_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
  IPPRequest *self;
  self = (IPPRequest *) type->tp_alloc (type, 0);
  if (self != NULL)
    self->ipp = NULL;

  return (PyObject *) self;
}

static int
IPPRequest_init (IPPRequest *self, PyObject *args, PyObject *kwds)
{
  unsigned long op = -1;
  if (!PyArg_ParseTuple (args, "|i", &op))
    return -1;

  if (op == -1)
    self->ipp = ippNew ();
  else
    self->ipp = ippNewRequest (op);

  return 0;
}

static void
IPPRequest_dealloc (IPPRequest *self)
{
  ippDelete (self->ipp);
  ((PyObject *)self)->ob_type->tp_free ((PyObject *) self);
}

static IPPAttribute *
build_IPPAttribute (ipp_attribute_t *attr)
{
  IPPAttribute *attribute = NULL;
  PyObject *largs = NULL;
  PyObject *lkwlist = NULL;
  PyObject *values = NULL;

  debugprintf ("%s: ", ippGetName (attr));
  if (ippGetValueTag (attr) == IPP_TAG_ZERO ||
      ippGetValueTag (attr) == IPP_TAG_NOVALUE ||
      ippGetValueTag (attr) == IPP_TAG_NOTSETTABLE ||
      ippGetValueTag (attr) == IPP_TAG_ADMINDEFINE) {
    debugprintf ("no value\n");
  } else {
    PyObject *value = NULL;
    int i;
    int unknown_value_tag = 0;

    values = PyList_New (0);
    if (!values)
      goto out;

    for (i = 0; i < ippGetCount (attr); i++) {
      switch (ippGetValueTag (attr)) {
      case IPP_TAG_INTEGER:
      case IPP_TAG_ENUM:
      case IPP_TAG_RANGE:
#if PY_MAJOR_VERSION >= 3
	value = PyLong_FromLong (ippGetInteger (attr, i));
#else
	value = PyInt_FromLong (ippGetInteger (attr, i));
#endif
	debugprintf ("i%d", ippGetInteger (attr, i));
	break;

      case IPP_TAG_BOOLEAN:
	value = PyBool_FromLong (ippGetBoolean (attr, i));
	debugprintf ("b%d", ippGetInteger (attr, i));
	break;

      case IPP_TAG_TEXT:
	value = PyUnicode_Decode (ippGetString (attr, i, NULL),
				  strlen (ippGetString (attr, i, NULL)),
				  "utf-8", NULL);
	debugprintf ("u%s", ippGetString (attr, i, NULL));
	break;

      case IPP_TAG_NAME:
      case IPP_TAG_KEYWORD:
      case IPP_TAG_URI:
      case IPP_TAG_MIMETYPE:
      case IPP_TAG_CHARSET:
      case IPP_TAG_LANGUAGE:
	value = PyUnicode_FromString (ippGetString (attr, i, NULL));
	debugprintf ("s%s", ippGetString (attr, i, NULL));
	break;

      default:
	value = NULL;
	unknown_value_tag = 1;
	debugprintf ("Unable to encode value tag %d\n", ippGetValueTag (attr));
      }

      if (!value)
	break; /* out of values loop */

      debugprintf ("(%p), ", value);
      if (PyList_Append (values, value) != 0) {
	Py_DECREF (values);
	Py_DECREF (value);
	goto out;
      }

      Py_DECREF (value);
    }

    if (unknown_value_tag) {
      Py_DECREF (values);
      goto out;
    }

    debugprintf ("\n");
  }

  if (values) {
    largs = Py_BuildValue ("(iisO)",
			   ippGetGroupTag (attr),
			   ippGetValueTag (attr),
			   ippGetName (attr),
			   values);
    Py_DECREF (values);
    values = NULL;
  } else
    largs = Py_BuildValue ("(iis)", ippGetGroupTag (attr), ippGetValueTag (attr),
			   ippGetName (attr) ? ippGetName (attr) : "");

  if (!largs)
    goto out;

  lkwlist = Py_BuildValue ("{}");
  if (!lkwlist)
    goto out;

  attribute = (IPPAttribute *) PyType_GenericNew (&cups_IPPAttributeType,
						  largs, lkwlist);
  if (!attribute)
    goto out;

  if (IPPAttribute_init (attribute, largs, lkwlist) != 0) {
    Py_DECREF (attribute);
    attribute = NULL;
  }

out:
  if (values)
    Py_DECREF (values);

  if (largs)
    Py_DECREF (largs);

  if (lkwlist)
    Py_DECREF (lkwlist);

  return attribute;
}

static PyObject *
IPPRequest_addSeparator (IPPRequest *self)
{
  ipp_attribute_t *attr = ippAddSeparator (self->ipp);
  return (PyObject *) build_IPPAttribute (attr);
}

static PyObject *
IPPRequest_add (IPPRequest *self, PyObject *args)
{
  PyObject *obj;
  IPPAttribute *attribute;
  Py_ssize_t num_values;
  void *values = NULL;
  size_t value_size = 0;
  int i;

  if (!PyArg_ParseTuple (args, "O", &obj))
    return NULL;

  if (Py_TYPE(obj) != &cups_IPPAttributeType) {
    PyErr_SetString (PyExc_TypeError, "Parameter must be IPPAttribute");
    return NULL;
  }

  attribute = (IPPAttribute *) obj;
  num_values = PyList_Size (attribute->values);
  switch (attribute->value_tag) {
  case IPP_TAG_INTEGER:
  case IPP_TAG_ENUM:
  case IPP_TAG_RANGE:
    value_size = sizeof (int);
    break;

  case IPP_TAG_BOOLEAN:
    value_size = sizeof (char);
    break;

  case IPP_TAG_NAME:
  case IPP_TAG_KEYWORD:
  case IPP_TAG_URI:
  case IPP_TAG_MIMETYPE:
  case IPP_TAG_CHARSET:
  case IPP_TAG_LANGUAGE:
    value_size = sizeof (char *);
    break;

  default:
    break;
  }

  values = calloc (num_values, value_size);
  if (!values) {
    PyErr_SetString (PyExc_MemoryError, "Unable to allocate memory");
    return NULL;
  }

  switch (attribute->value_tag) {
  case IPP_TAG_INTEGER:
  case IPP_TAG_ENUM:
  case IPP_TAG_RANGE:
    for (i = 0; i < num_values; i++) {
      PyObject *item = PyList_GetItem (attribute->values, i);
      if (PyLong_Check (item))
	((int *)values)[i] = PyLong_AsLong (item);
#if PY_MAJOR_VERSION < 3
      else if (PyInt_Check (item))
	((int *)values)[i] = PyInt_AsLong (item);
#endif
    }
    ippAddIntegers (self->ipp,
		    attribute->group_tag,
		    attribute->value_tag,
		    attribute->name,
		    num_values,
		    values);
    break;

  case IPP_TAG_BOOLEAN:
    for (i = 0; i < num_values; i++) {
      PyObject *item = PyList_GetItem (attribute->values, i);
      ((char *)values)[i] = (item == Py_True ? 1 : 0);
    }
    ippAddBooleans (self->ipp,
		    attribute->group_tag,
		    attribute->name,
		    num_values,
		    values);
    break;

  case IPP_TAG_NAME:
  case IPP_TAG_KEYWORD:
  case IPP_TAG_URI:
  case IPP_TAG_MIMETYPE:
  case IPP_TAG_CHARSET:
  case IPP_TAG_LANGUAGE:
    for (i = 0; i < num_values; i++) {
      PyObject *item = PyList_GetItem (attribute->values, i);
      char *str;
#if PY_MAJOR_VERSION >= 3
      str = strdup (PyUnicode_AsUTF8 (item));
#else
      str = strdup (PyBytes_AsString (item));
#endif
      ((char **)values)[i] = str;
      if (!str)
	break;
    }
    if (i < num_values) {
      int j;
      for (j = 0; j < i; j++)
	free (((char **)values)[j]);
      PyErr_SetString (PyExc_MemoryError, "Unable to allocate memory");
      free (values);
      return NULL;
    }
    ippAddStrings (self->ipp,
		   attribute->group_tag,
		   attribute->value_tag,
		   attribute->name,
		   num_values,
		   NULL,
		   values);
    for (i = 0; i < num_values; i++)
      free (((char **)values)[i]);

    break;

  default:
    break;
  }

  free (values);
  Py_INCREF (obj);
  return obj;
}

static ssize_t
cupsipp_iocb_read (PyObject *callable, ipp_uchar_t *buffer, size_t len)
{
  PyObject *args = Py_BuildValue ("(i)", len);
  PyObject *result;
  Py_ssize_t got = -1;
  char *gotbuffer;

  debugprintf ("-> cupsipp_iocb_read\n");

  if (!args) {
    debugprintf ("Py_BuildValue failed\n");
    goto out;
  }

  result = PyEval_CallObject (callable, args);
  Py_DECREF (args);

  if (result == NULL) {
    debugprintf ("Exception in read callback\n");
    goto out;
  }

  if (PyUnicode_Check (result) || PyBytes_Check (result)) {
    if (PyUnicode_Check (result)) {
      PyObject *stringobj = PyUnicode_AsUTF8String (result);
      PyBytes_AsStringAndSize (stringobj, &gotbuffer, &got);
    } else {
      PyBytes_AsStringAndSize (result, &gotbuffer, &got);
    }

    if (got > len) {
      debugprintf ("More data returned than requested!  Truncated...\n");
      got = len;
    }
    memcpy (buffer, gotbuffer, got);
  } else {
    debugprintf ("Unknown result object type!\n");
  }

  Py_DECREF (result);

 out:
  debugprintf ("<- cupsipp_iocb_read() == %zd\n", got);
  return got;
}

static ssize_t
cupsipp_iocb_write (PyObject *callable, ipp_uchar_t *buffer, size_t len)
{
  PyObject *args = Py_BuildValue ("(y#)", buffer, len);
  PyObject *result = NULL;
  Py_ssize_t wrote = -1;

  debugprintf ("-> cupsipp_iocb_write\n");

  if (!args) {
    debugprintf ("Py_BuildValue failed\n");
    goto out;
  }

  result = PyEval_CallObject (callable, args);
  Py_DECREF (args);

  if (result == NULL) {
    debugprintf ("Exception in write callback\n");
    goto out;
  }

  if (PyLong_Check (result))
    wrote = PyLong_AsLong (result);
#if PY_MAJOR_VERSION < 3
  else if (PyInt_Check (result))
    wrote = PyInt_AsLong (result);
#endif
  else {
    debugprintf ("Bad return value\n");
    goto out;
  }

out:
  if (result)
    Py_DECREF (result);

  debugprintf ("<- cupsipp_iocb_write()\n");
  return wrote;
}

static PyObject *
IPPRequest_readIO (IPPRequest *self, PyObject *args, PyObject *kwds)
{
  PyObject *cb;
  char blocking = 1;
  ipp_state_t state;
  static char *kwlist[] = { "read_fn", "blocking", NULL };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "O|b", kwlist,
				    &cb, &blocking))
    return NULL;

  if (!PyCallable_Check (cb)) {
    PyErr_SetString (PyExc_TypeError, "Parameter must be callable");
    return NULL;
  }

  state = ippReadIO (cb, (ipp_iocb_t) cupsipp_iocb_read,
		     blocking, NULL, self->ipp);
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (state);
#else
  return PyInt_FromLong (state);
#endif
}

static PyObject *
IPPRequest_writeIO (IPPRequest *self, PyObject *args, PyObject *kwds)
{
  PyObject *cb;
  char blocking = 1;
  ipp_state_t state;
  static char *kwlist[] = { "write_fn", "blocking", NULL };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "O|b", kwlist,
				    &cb, &blocking))
    return NULL;

  if (!PyCallable_Check (cb)) {
    PyErr_SetString (PyExc_TypeError, "Parameter must be callable");
    return NULL;
  }

  state = ippWriteIO (cb, (ipp_iocb_t) cupsipp_iocb_write,
		     blocking, NULL, self->ipp);
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (state);
#else
  return PyInt_FromLong (state);
#endif
}

/* Request properties */

static PyObject *
IPPRequest_getAttributes (IPPRequest *self, void *closure)
{
  PyObject *attrs = PyList_New (0);
  ipp_attribute_t *attr;
  for (attr = ippFirstAttribute (self->ipp); attr; attr = ippNextAttribute(self->ipp))
    {
      IPPAttribute *attribute = build_IPPAttribute (attr);
      if (!attribute)
	goto fail;

      if (PyList_Append (attrs, (PyObject *) attribute) != 0)
	goto fail;
    }

  return attrs;

 fail:
  Py_DECREF (attrs);
  return NULL;
}

static PyObject *
IPPRequest_getOperation (IPPRequest *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (ippGetOperation (self->ipp));
#else
  return PyInt_FromLong (ippGetOperation (self->ipp));
#endif
}

static PyObject *
IPPRequest_getStatuscode (IPPRequest *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (ippGetStatusCode (self->ipp));
#else
  return PyInt_FromLong (ippGetStatusCode (self->ipp));
#endif
}

static int
IPPRequest_setState (IPPRequest *self, PyObject *value, void *closure)
{
  int state;

  if (value == NULL)
  {
    PyErr_SetString(PyExc_TypeError, "Cannot delete state");
    return -1;
  }

  if (PyLong_Check(value))
    state = PyLong_AsLong (value);
#if PY_MAJOR_VERSION < 3
  else if (PyInt_Check(value))
    state = PyInt_AsLong (value);
#endif
  else
  {
    PyErr_SetString(PyExc_TypeError, "state must be an integer");
    return -1;
  }

  ippSetState (self->ipp, state);
  return 0;
}

static PyObject *
IPPRequest_getState (IPPRequest *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
  return PyLong_FromLong (ippGetState (self->ipp));
#else
  return PyInt_FromLong (ippGetState (self->ipp));
#endif
}

static int
IPPRequest_setStatuscode (IPPRequest *self, PyObject *value, void *closure)
{
  int statuscode;

  if (value == NULL)
  {
    PyErr_SetString(PyExc_TypeError, "Cannot delete statuscode");
    return -1;
  }

  if (PyLong_Check(value))
    statuscode = PyLong_AsLong (value);
#if PY_MAJOR_VERSION < 3
  else if (PyInt_Check(value))
    statuscode = PyInt_AsLong (value);
#endif
  else
  {
    PyErr_SetString(PyExc_TypeError, "statuscode must be an integer");
    return -1;
  }

  ippSetStatusCode (self->ipp, statuscode);
  return 0;
}

PyGetSetDef IPPRequest_getseters[] =
  {
    { "attributes",
      (getter) IPPRequest_getAttributes, (setter) NULL,
      "IPP request attributes", NULL },

    { "operation",
      (getter) IPPRequest_getOperation, (setter) NULL,
      "IPP request operation", NULL },

    { "state",
      (getter) IPPRequest_getState,
      (setter) IPPRequest_setState,
      "IPP request transfer state", NULL },

    { "statuscode",
      (getter) IPPRequest_getStatuscode,
      (setter) IPPRequest_setStatuscode,
      "IPP response status code", NULL },

    { NULL } /* Sentinel */
  };

PyMethodDef IPPRequest_methods[] =
  {
    { "addSeparator",
      (PyCFunction) IPPRequest_addSeparator, METH_NOARGS,
      "addSeparator() -> IPPAttribute\n\n"
      "@return: IPP request attribute" },

    { "add",
      (PyCFunction) IPPRequest_add, METH_VARARGS,
      "add(attr) -> IPPAttribute\n\n"
      "@type attr: IPPAttribute\n"
      "@param attr: Attribute to add to the request\n"
      "@return: IPP request attribute" },

    { "readIO",
      (PyCFunction) IPPRequest_readIO, METH_VARARGS | METH_KEYWORDS,
      "readIO(read_fn, blocking=True) -> IPP state\n\n"
      "@type read_fn: Callable function\n"
      "@param read_fn: A callback, taking a single integer argument for\n"
      "'size', for reading IPP data\n"
      "@type blocking: Boolean\n"
      "@param blocking: whether to continue reading until a complete\n"
      "request is read\n"
      "@return: IPP state value" },

    { "writeIO",
      (PyCFunction) IPPRequest_writeIO, METH_VARARGS | METH_KEYWORDS,
      "writeIO(write_fn, blocking=True) -> IPP state\n\n"
      "@type write_fn: Callable function\n"
      "@param write_fn: A callback, taking a bytes object, for writing\n"
      "IPP data\n"
      "@type blocking: Boolean\n"
      "@param blocking: whether to continue reading until a complete\n"
      "request is written\n"
      "@return: IPP state value" },

    { NULL } /* Sentinel */
  };

PyTypeObject cups_IPPRequestType =
  {
    PyVarObject_HEAD_INIT(NULL, 0)
    "cups.IPPRequest",         /*tp_name*/
    sizeof(IPPRequest),        /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)IPPRequest_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
    "IPP Request\n"
    "===========\n"
    "  An IPP request.  The constructor takes an optional argument of the\n"
    "operation code.\n\n"
    "",                        /* tp_doc */
    0,                         /* tp_traverse */
    0,                         /* tp_clear */
    0,                         /* tp_richcompare */
    0,                         /* tp_weaklistoffset */
    0,                         /* tp_iter */
    0,                         /* tp_iternext */
    IPPRequest_methods,        /* tp_methods */
    0,                         /* tp_members */
    IPPRequest_getseters,      /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)IPPRequest_init, /* tp_init */
    0,                         /* tp_alloc */
    IPPRequest_new,          /* tp_new */
  };