summaryrefslogtreecommitdiffstats
path: root/python/cache.cc
blob: 81bbb9640bd8bd714fc42c9c0f78c1e8c675fd38 (plain)
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: cache.cc,v 1.5 2003/06/03 03:03:23 mdz Exp $
/* ######################################################################

   Cache - Wrapper for the cache related functions

   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include "generic.h"
#include "apt_pkgmodule.h"

#include <apt-pkg/pkgcache.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/error.h>
#include <apt-pkg/packagemanager.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/algorithms.h>
#include <apt-pkg/update.h>

#include <Python.h>
#include "progress.h"

class pkgSourceList;

// must be in sync with pkgCache::DepType in libapt
// it sucks to have it here duplicated, but we get it
// translated from libapt and that is certainly not what
// we want in a programing interface
const char *UntranslatedDepTypes[] =
{
   "", "Depends","PreDepends","Suggests",
   "Recommends","Conflicts","Replaces",
   "Obsoletes", "Breaks", "Enhances"
};

									/*}}}*/

template<typename T> struct IterListStruct
{
   T Iter;
   unsigned long LastIndex;

   IterListStruct(T const &I) : Iter(I), LastIndex(0) {}
   IterListStruct() : LastIndex(0) {};

   bool move(unsigned long Index) {
       if ((unsigned)Index >= Count())
       {
          PyErr_SetNone(PyExc_IndexError);
          return false;
       }

       if ((unsigned)Index < LastIndex)
       {
          LastIndex = 0;
          Iter = Begin();
       }

       while ((unsigned)Index > LastIndex)
       {
          LastIndex++;
          Iter++;
          if (Iter.end() == true)
          {
         PyErr_SetNone(PyExc_IndexError);
         return false;
          }
       }
       return true;
    }

    virtual unsigned Count() = 0;
    virtual T Begin() = 0;

};

struct PkgListStruct : public IterListStruct<pkgCache::PkgIterator> {
    unsigned Count() { return Iter.Cache()->HeaderP->PackageCount; }
    pkgCache::PkgIterator Begin() { return Iter.Cache()->PkgBegin(); }

    PkgListStruct(pkgCache::PkgIterator const &I) { Iter = I; }
};

struct GrpListStruct : public IterListStruct<pkgCache::GrpIterator> {
    unsigned Count() { return Iter.Cache()->HeaderP->GroupCount; }
    pkgCache::GrpIterator Begin() { return Iter.Cache()->GrpBegin(); }
    GrpListStruct(pkgCache::GrpIterator const &I) { Iter = I; }
};

struct RDepListStruct
{
   pkgCache::DepIterator Iter;
   pkgCache::DepIterator Start;
   unsigned long LastIndex;
   unsigned long Len;

   RDepListStruct(pkgCache::DepIterator const &I) : Iter(I), Start(I),
                         LastIndex(0)
   {
      Len = 0;
      pkgCache::DepIterator D = I;
      for (; D.end() == false; D++)
	 Len++;
   }
   RDepListStruct() {abort();};  // G++ Bug..
};

static PyObject *CreateProvides(PyObject *Owner,pkgCache::PrvIterator I)
{
   PyObject *List = PyList_New(0);
   for (; I.end() == false; I++)
   {
      PyObject *Obj;
      PyObject *Ver;
      Ver = CppPyObject_NEW<pkgCache::VerIterator>(Owner,&PyVersion_Type,
							I.OwnerVer());
      Obj = Py_BuildValue("ssN",I.ParentPkg().Name(),I.ProvideVersion(),
			  Ver);
      PyList_Append(List,Obj);
      Py_DECREF(Obj);
   }
   return List;
}

// Cache Class								/*{{{*/
// ---------------------------------------------------------------------

static const char *cache_update_doc =
    "update(progress, sources: SourceList, pulse_interval: int) -> bool\n\n"
    "Update the index files used by the cache. A call to this method\n"
    "does not affect the current Cache object; instead, a new one\n"
    "should be created in order to use the changed index files.\n\n"
    "The parameter 'progress' can be used to specify an\n"
    "apt.progress.base.AcquireProgress() object , which will report\n"
    "progress information while the index files are being fetched.\n"
    "The parameter 'sources', if provided, is an apt_pkg.SourcesList\n"
    "object listing the remote repositories to be used.\n"
    "The 'pulse_interval' parameter indicates how long (in microseconds)\n"
    "to wait between calls to the pulse() method of the 'progress' object.\n"
    "The default is 500000 microseconds.";
static PyObject *PkgCacheUpdate(PyObject *Self,PyObject *Args)
{
   PyObject *pyFetchProgressInst = 0;
   PyObject *pySourcesList = 0;
   int pulseInterval = 0;
   if (PyArg_ParseTuple(Args, "OO!|i", &pyFetchProgressInst,
            &PySourceList_Type, &pySourcesList, &pulseInterval) == 0)
      return 0;

   PyFetchProgress progress;
   progress.setCallbackInst(pyFetchProgressInst);
   pkgSourceList *source = GetCpp<pkgSourceList*>(pySourcesList);
   bool res = ListUpdate(progress, *source, pulseInterval);

   PyObject *PyRes = PyBool_FromLong(res);
   return HandleErrors(PyRes);
}


static PyMethodDef PkgCacheMethods[] =
{
   {"update",PkgCacheUpdate,METH_VARARGS,cache_update_doc},
   {}
};

static PyObject *PkgCacheGetGroupCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber(Cache->HeaderP->GroupCount);
}

static PyObject *PkgCacheGetGroups(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return CppPyObject_NEW<GrpListStruct>(Self,&PyGroupList_Type,Cache->GrpBegin());
}

static PyObject *PkgCacheGetPolicy(PyObject *Self, void*) {
   PyObject *CacheFilePy = GetOwner<pkgCache*>(Self);
   pkgCacheFile *CacheF = GetCpp<pkgCacheFile*>(CacheFilePy);
   pkgDepCache *DepCache = (pkgDepCache *)(*CacheF);

   pkgPolicy *Policy = (pkgPolicy *)&DepCache->GetPolicy();
   CppPyObject<pkgPolicy*> *PyPolicy =
        CppPyObject_NEW<pkgPolicy*>(Self,&PyPolicy_Type,Policy);
   // Policy should not be deleted, it is managed by CacheFile.
   PyPolicy->NoDelete = true;
   return PyPolicy;
}

static PyObject *PkgCacheGetPackages(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return CppPyObject_NEW<PkgListStruct>(Self,&PyPackageList_Type,Cache->PkgBegin());
}

static PyObject *PkgCacheGetPackageCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber((int)Cache->HeaderP->PackageCount);
}

static PyObject *PkgCacheGetVersionCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber(Cache->HeaderP->VersionCount);
}
static PyObject *PkgCacheGetDependsCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber(Cache->HeaderP->DependsCount);
}

static PyObject *PkgCacheGetPackageFileCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber(Cache->HeaderP->PackageFileCount);
}

static PyObject *PkgCacheGetVerFileCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber(Cache->HeaderP->VerFileCount);
}

static PyObject *PkgCacheGetProvidesCount(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   return MkPyNumber(Cache->HeaderP->ProvidesCount);
}

static PyObject *PkgCacheGetFileList(PyObject *Self, void*) {
   pkgCache *Cache = GetCpp<pkgCache *>(Self);
   PyObject *List = PyList_New(0);
   for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I.end() == false; I++)
   {
      PyObject *Obj;
      Obj = CppPyObject_NEW<pkgCache::PkgFileIterator>(Self,&PyPackageFile_Type,I);
      PyList_Append(List,Obj);
      Py_DECREF(Obj);
   }
   return List;
}

static PyObject *PkgCacheGetIsMultiArch(PyObject *Self, void*) {
    pkgCache *Cache = GetCpp<pkgCache *>(Self);
    return PyBool_FromLong(Cache->MultiArchCache());
}

static PyGetSetDef PkgCacheGetSet[] = {
   {"depends_count",PkgCacheGetDependsCount,0,
    "The number of apt_pkg.Dependency objects stored in the cache."},
   {"file_list",PkgCacheGetFileList,0,
    "A list of apt_pkg.PackageFile objects stored in the cache."},
   {"group_count",PkgCacheGetGroupCount,0,
    "The number of apt_pkg.Group objects stored in the cache."},
   {"groups",  PkgCacheGetGroups, 0, "A list of Group objects in the cache"},
   {"policy",  PkgCacheGetPolicy, 0, "The PkgPolicy for the cache"},
   {"is_multi_arch", PkgCacheGetIsMultiArch, 0,
    "Whether the cache supports multi-arch."},
   {"package_count",PkgCacheGetPackageCount,0,
    "The number of apt_pkg.Package objects stored in the cache."},
   {"package_file_count",PkgCacheGetPackageFileCount,0,
    "The number of apt_pkg.PackageFile objects stored in the cache."},
   {"packages",PkgCacheGetPackages,0,
    "A list of apt_pkg.Package objects stored in the cache."},
   {"provides_count",PkgCacheGetProvidesCount,0,
    "Number of Provides relations described in the cache."},
   {"ver_file_count",PkgCacheGetVerFileCount,0,
    "The number of (Version, PackageFile) relations."},
   {"version_count",PkgCacheGetVersionCount,0,
    "The number of apt_pkg.Version objects stored in the cache."},
   {}
};

// Helper to call FindPkg(name) or FindPkg(name, architecture)
static pkgCache::PkgIterator CacheFindPkg(PyObject *self, PyObject *arg)
{
    const char *name;
    const char *architecture;
    pkgCache *cache = GetCpp<pkgCache *>(self);

    name = PyObject_AsString(arg);

    if (name != NULL)
        return cache->FindPkg(name);

    PyErr_Clear();

    if (PyArg_ParseTuple(arg, "ss", &name, &architecture) == 0) {
        PyErr_Clear();
        PyErr_Format(PyExc_TypeError, "Expected a string or a pair of strings");
        return pkgCache::PkgIterator();
    }

    return cache->FindPkg(name, architecture);
}

// Map access, operator []
static PyObject *CacheMapOp(PyObject *Self,PyObject *Arg)
{
   pkgCache::PkgIterator Pkg = CacheFindPkg(Self, Arg);
   if (Pkg.end() == true)
   {
      if (!PyErr_Occurred())
        PyErr_SetObject(PyExc_KeyError,Arg);
      return 0;
   }

   return CppPyObject_NEW<pkgCache::PkgIterator>(Self,&PyPackage_Type,Pkg);
}

// Check whether the cache contains a package with a given name.
static int CacheContains(PyObject *Self,PyObject *Arg)
{
   bool res = (CacheFindPkg(Self, Arg).end() == false);
   PyErr_Clear();
   return res;
}

static PyObject *PkgCacheNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
{
   PyObject *pyCallbackInst = 0;
   char *kwlist[] = {"progress", 0};

   if (PyArg_ParseTupleAndKeywords(Args, kwds, "|O", kwlist,
                                   &pyCallbackInst) == 0)
      return 0;

    if (_system == 0) {
        PyErr_SetString(PyExc_ValueError,"_system not initialized");
        return 0;
    }

   pkgCacheFile *Cache = new pkgCacheFile();

   if (pyCallbackInst == Py_None) {
      OpProgress Prog;
      if (Cache->Open(&Prog,false) == false)
	     return HandleErrors();
   } else if(pyCallbackInst != 0) {
      // sanity check for the progress object, see #497049
      if (PyObject_HasAttrString(pyCallbackInst, "done") != true) {
        PyErr_SetString(PyExc_ValueError,
                        "OpProgress object must implement done()");
        return 0;
      }
      if (PyObject_HasAttrString(pyCallbackInst, "update") != true) {
        PyErr_SetString(PyExc_ValueError,
                        "OpProgress object must implement update()");
        return 0;
      }
      PyOpProgress progress;
      progress.setCallbackInst(pyCallbackInst);
      if (Cache->Open(&progress,false) == false)
         return HandleErrors();
   }
   else {
      OpTextProgress Prog;
      if (Cache->Open(&Prog,false) == false)
	     return HandleErrors();
   }

   // ensure that the states are correct (LP: #659438)
   pkgApplyStatus(*Cache);

   CppPyObject<pkgCacheFile*> *CacheFileObj =
	   CppPyObject_NEW<pkgCacheFile*>(0,&PyCacheFile_Type, Cache);

   CppPyObject<pkgCache *> *CacheObj =
	   CppPyObject_NEW<pkgCache *>(CacheFileObj,type,
					    (pkgCache *)(*Cache));

   // Do not delete the pointer to the pkgCache, it is managed by pkgCacheFile.
   CacheObj->NoDelete = true;
   Py_DECREF(CacheFileObj);
   return CacheObj;
}

static Py_ssize_t CacheMapLen(PyObject *Self)
{
    return GetCpp<pkgCache*>(Self)->HeaderP->PackageCount;
}

static char *doc_PkgCache = "Cache([progress]) -> Cache() object.\n\n"
    "The APT cache file contains a hash table mapping names of binary\n"
    "packages to their metadata. A Cache object is the in-core\n"
    "representation of the same. It provides access to APT’s idea of the\n"
    "list of available packages.\n"
    "The optional parameter *progress* can be used to specify an \n"
    "apt.progress.base.OpProgress() object (or similar) which reports\n"
    "progress information while the cache is being opened.  If this\n"
    "parameter is not supplied, the progress will be reported in simple,\n"
    "human-readable text to standard output. If it is None, no output\n"
    "will be made.\n\n"
    "The cache can be used like a mapping from package names to Package\n"
    "objects (although only getting items is supported). Instead of a name,\n"
    "a tuple of a name and an architecture may be used.";
static PySequenceMethods CacheSeq = {0,0,0,0,0,0,0,CacheContains,0,0};
static PyMappingMethods CacheMap = {CacheMapLen,CacheMapOp,0};
PyTypeObject PyCache_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.Cache",                     // tp_name
   sizeof(CppPyObject<pkgCache *>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDeallocPtr<pkgCache *>,      // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   &CacheSeq,                           // tp_as_sequence
   &CacheMap,		                // tp_as_mapping
   0,                                   // tp_hash
   0,                                   // tp_call
   0,                                   // tp_str
   _PyAptObject_getattro,               // tp_getattro
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
   (Py_TPFLAGS_DEFAULT |                // tp_flags
    Py_TPFLAGS_BASETYPE |
    Py_TPFLAGS_HAVE_GC),
   doc_PkgCache,                        // tp_doc
   CppTraverse<pkgCache *>,        // tp_traverse
   CppClear<pkgCache *>,           // tp_clear
   0,                                   // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   PkgCacheMethods,                     // tp_methods
   0,                                   // tp_members
   PkgCacheGetSet,                      // tp_getset
   0,                                   // tp_base
   0,                                   // tp_dict
   0,                                   // tp_descr_get
   0,                                   // tp_descr_set
   0,                                   // tp_dictoffset
   0,                                   // tp_init
   0,                                   // tp_alloc
   PkgCacheNew,                         // tp_new
};
									/*}}}*/
// PkgCacheFile Class							/*{{{*/
// ---------------------------------------------------------------------
PyTypeObject PyCacheFile_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "pkgCacheFile",                      // tp_name
   sizeof(CppPyObject<pkgCacheFile*>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDeallocPtr<pkgCacheFile*>,       // 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
};

// Package List Class							/*{{{*/
// ---------------------------------------------------------------------
static Py_ssize_t PkgListLen(PyObject *Self)
{
   return GetCpp<PkgListStruct>(Self).Iter.Cache()->HeaderP->PackageCount;
}

static PyObject *PkgListItem(PyObject *iSelf,Py_ssize_t Index)
{
   PkgListStruct &Self = GetCpp<PkgListStruct>(iSelf);

   if (!Self.move(Index))
      return 0;
   return CppPyObject_NEW<pkgCache::PkgIterator>(GetOwner<PkgListStruct>(iSelf),&PyPackage_Type,
						      Self.Iter);
}

static PySequenceMethods PkgListSeq =
{
   PkgListLen,
   0,                // concat
   0,                // repeat
   PkgListItem,
   0,                // slice
   0,                // assign item
   0                 // assign slice
};

static const char *packagelist_doc =
    "A PackageList is an internally used structure to represent\n"
    "the 'packages' attribute of apt_pkg.Cache objects in a more\n"
    "efficient manner by creating Package objects only when they\n"
    "are accessed.";

PyTypeObject PyPackageList_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.PackageList",               // tp_name
   sizeof(CppPyObject<PkgListStruct>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<PkgListStruct>,      // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   &PkgListSeq,                         // 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 | Py_TPFLAGS_HAVE_GC, // tp_flags
   packagelist_doc,                     // tp_doc
   CppTraverse<PkgListStruct>,     // tp_traverse
   CppClear<PkgListStruct>,        // tp_clear
};

/* The same for groups */
static Py_ssize_t GrpListLen(PyObject *Self)
{
   return GetCpp<GrpListStruct>(Self).Iter.Cache()->HeaderP->GroupCount;
}

static PyObject *GrpListItem(PyObject *iSelf,Py_ssize_t Index)
{
   GrpListStruct &Self = GetCpp<GrpListStruct>(iSelf);

   if (!Self.move(Index))
      return 0;
   return CppPyObject_NEW<pkgCache::GrpIterator>(GetOwner<GrpListStruct>(iSelf),&PyGroup_Type,
						      Self.Iter);
}

static PySequenceMethods GrpListSeq =
{
   GrpListLen,
   0,                // concat
   0,                // repeat
   GrpListItem,
   0,                // slice
   0,                // assign item
   0                 // assign slice
};

static const char *grouplist_doc =
    "A GroupList is an internally used structure to represent\n"
    "the 'groups' attribute of apt_pkg.Cache objects in a more\n"
    "efficient manner by creating Group objects only when they\n"
    "are accessed.";

PyTypeObject PyGroupList_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.GroupList",               // tp_name
   sizeof(CppPyObject<GrpListStruct>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<GrpListStruct>,      // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   &GrpListSeq,                         // 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 | Py_TPFLAGS_HAVE_GC, // tp_flags
   grouplist_doc,                     // tp_doc
   CppTraverse<GrpListStruct>,     // tp_traverse
   CppClear<GrpListStruct>,        // tp_clear
};


#define Owner (GetOwner<pkgCache::PkgIterator>(Self))
#define MkGet(PyFunc,Ret) static PyObject *PyFunc(PyObject *Self,void*) \
{ \
    pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self); \
    return Ret; \
}

MkGet(PackageGetName,CppPyString(Pkg.Name()))
MkGet(PackageGetArch,CppPyString(Pkg.Arch()))
MkGet(PackageGetRevDependsList,CppPyObject_NEW<RDepListStruct>(Owner,
                               &PyDependencyList_Type, Pkg.RevDependsList()))
MkGet(PackageGetProvidesList,CreateProvides(Owner,Pkg.ProvidesList()))
MkGet(PackageGetSelectedState,MkPyNumber(Pkg->SelectedState))
MkGet(PackageGetInstState,MkPyNumber(Pkg->InstState))
MkGet(PackageGetCurrentState,MkPyNumber(Pkg->CurrentState))
MkGet(PackageGetID,MkPyNumber(Pkg->ID))
#
MkGet(PackageGetEssential,PyBool_FromLong((Pkg->Flags & pkgCache::Flag::Essential) != 0))
MkGet(PackageGetImportant,PyBool_FromLong((Pkg->Flags & pkgCache::Flag::Important) != 0))
#undef MkGet
#undef Owner

static const char PackageGetFullName_doc[] =
    "get_fullname([pretty: bool = False]) -> str\n\n"
    "Get the full name of the package, including the architecture. If\n"
    "'pretty' is True, the architecture is omitted for native packages,\n"
    "that is, and amd64 apt package on an amd64 system would give 'apt'.";
static PyObject *PackageGetFullName(PyObject *Self,PyObject *Args,PyObject *kwds)
{
   pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self);
   char pretty = 0;
   char *kwlist[] = {"pretty", 0};

   if (PyArg_ParseTupleAndKeywords(Args, kwds, "|b", kwlist,
                                   &pretty) == 0)
      return 0;


   return CppPyString(Pkg.FullName(pretty));
}

static PyObject *PackageGetVersionList(PyObject *Self,void*)
{
   pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::PkgIterator>(Self);

   PyObject *List = PyList_New(0);
   for (pkgCache::VerIterator I = Pkg.VersionList(); I.end() == false; I++)
   {
      PyObject *Obj;
      Obj = CppPyObject_NEW<pkgCache::VerIterator>(Owner,&PyVersion_Type,I);
      PyList_Append(List,Obj);
      Py_DECREF(Obj);
   }
   return List;
}

static PyObject *PackageGetHasVersions(PyObject *Self,void*)
{
   pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self);
   return PyBool_FromLong(Pkg.VersionList().end() == false);
}

static PyObject *PackageGetHasProvides(PyObject *Self,void*)
{
   pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self);
   return PyBool_FromLong(Pkg.ProvidesList().end() == false);
}

static PyObject *PackageGetCurrentVer(PyObject *Self,void*)
{
   pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::PkgIterator>(Self);
   if (Pkg->CurrentVer == 0)
   {
      Py_INCREF(Py_None);
      return Py_None;
   }
   return CppPyObject_NEW<pkgCache::VerIterator>(Owner,&PyVersion_Type,
							 Pkg.CurrentVer());
}


static PyMethodDef PackageMethods[] =
{
   {"get_fullname",(PyCFunction)PackageGetFullName,METH_VARARGS|METH_KEYWORDS,
    PackageGetFullName_doc},
   {}
};

static PyGetSetDef PackageGetSet[] = {
    {"name",PackageGetName,0,
     "The name of the package."},
    {"architecture",PackageGetArch,0, "The architecture of the package."},
    {"rev_depends_list",PackageGetRevDependsList,0,
     "An apt_pkg.DependencyList object of all reverse dependencies."},
    {"provides_list",PackageGetProvidesList,0,
     "A list of all packages providing this package. The list contains\n"
     "tuples in the format (providesname, providesver, version)\n"
     "where 'version' is an apt_pkg.Version object."},
    {"selected_state",PackageGetSelectedState,0,
     "The state of the selection, which can be compared against the constants\n"
     "SELSTATE_DEINSTALL, SELSTATE_HOLD, SELSTATE_INSTALL, SELSTATE_PURGE,\n"
     "SELSTATE_UNKNOWN of the apt_pkg module."},
    {"inst_state",PackageGetInstState,0,
     "The state of the install, which be compared against the constants\n"
     "INSTSTATE_HOLD, INSTSTATE_HOLD_REINSTREQ, INSTSTATE_OK,\n"
     "INSTSTATE_REINSTREQ of the apt_pkg module."},
    {"current_state",PackageGetCurrentState,0,
     "The current state, which can be compared against the constants\n"
     "CURSTATE_CONFIG_FILES, CURSTATE_HALF_CONFIGURED,\n"
     "CURSTATE_HALF_INSTALLED, CURSTATE_INSTALLED, CURSTATE_NOT_INSTALLED,\n"
     "CURSTATE_UNPACKED of the apt_pkg module."},
    {"id",PackageGetID,0,
     "The numeric ID of the package"},
    {"essential",PackageGetEssential,0,
     "Boolean value determining whether the package is essential."},
    {"important",PackageGetImportant,0,
     "Boolean value determining whether the package has the 'important'\n"
     "flag set ('Important: yes' in the Packages file). No longer used."},
    {"version_list",PackageGetVersionList,0,
     "A list of all apt_pkg.Version objects for this package."},
    {"current_ver",PackageGetCurrentVer,0,
     "The version of the package currently installed or None."},
    {"has_versions",PackageGetHasVersions,0,
     "Whether the package has at least one version in the cache."},
    {"has_provides",PackageGetHasProvides,0,
     "Whether the package is provided by at least one other package."},
    {}
};

static PyObject *PackageRepr(PyObject *Self)
{
   pkgCache::PkgIterator &Pkg = GetCpp<pkgCache::PkgIterator>(Self);
   return PyString_FromFormat("<%s object: name:'%s' id:%u>", Self->ob_type->tp_name,
                              Pkg.Name(), Pkg->ID);
}

static const char *package_doc =
    "Represent a package. A package is uniquely identified by its name\n"
    "and each package can have zero or more versions which can be\n"
    "accessed via the version_list property. Packages can be installed\n"
    "and removed by apt_pkg.DepCache.";

PyTypeObject PyPackage_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.Package",                 // tp_name
   sizeof(CppPyObject<pkgCache::PkgIterator>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<pkgCache::PkgIterator>,  // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   PackageRepr,                         // tp_repr
   0,                                   // tp_as_number
   0,                                   // tp_as_sequence
   0,			                // tp_as_mapping
   0,                                   // tp_hash
   0,                                   // tp_call
   0,                                   // tp_str
   _PyAptObject_getattro,               // tp_getattro
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
   package_doc,                         // tp_doc
   CppTraverse<pkgCache::PkgIterator>, // tp_traverse
   CppClear<pkgCache::PkgIterator>,// tp_clear
   0,                                   // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   PackageMethods,                      // tp_methods
   0,                                   // tp_members
   PackageGetSet,                       // tp_getset
};

#define Description_MkGet(PyFunc,Ret) static PyObject \
   *PyFunc(PyObject *Self,void*) { \
       pkgCache::DescIterator &Desc = GetCpp<pkgCache::DescIterator>(Self); \
       return Ret; }

Description_MkGet(DescriptionGetLanguageCode,
                  CppPyString(Desc.LanguageCode()))
Description_MkGet(DescriptionGetMd5,CppPyString(Desc.md5()))
#undef Description_MkGet

static PyObject *DescriptionGetFileList(PyObject *Self,void*)
{
   pkgCache::DescIterator &Desc = GetCpp<pkgCache::DescIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::DescIterator>(Self);

   /* The second value in the tuple is the index of the VF item. If the
      user wants to request a lookup then that number will be used.
      Maybe later it can become an object. */
   PyObject *List = PyList_New(0);
   for (pkgCache::DescFileIterator I = Desc.FileList(); I.end() == false; I++)
   {
      PyObject *DescFile;
      PyObject *Obj;
      DescFile = CppPyObject_NEW<pkgCache::PkgFileIterator>(Owner,&PyPackageFile_Type,I.File());
      Obj = Py_BuildValue("NN",DescFile,MkPyNumber(I.Index()));
      PyList_Append(List,Obj);
      Py_DECREF(Obj);
   }
   return List;
}

static PyGetSetDef DescriptionGetSet[] = {
    {"language_code",DescriptionGetLanguageCode,0,
     "The language code of the description. Empty string for untranslated\n"
     "descriptions."},
    {"md5",DescriptionGetMd5,0,
     "The MD5 hash of the description."},
    {"file_list",DescriptionGetFileList,0,
     "A list of all apt_pkg.PackageFile objects related to this description."},
    {}
};

static PyObject *DescriptionRepr(PyObject *Self)
{
   pkgCache::DescIterator &Desc = GetCpp<pkgCache::DescIterator>(Self);
   return PyString_FromFormat("<%s object: language_code:'%s' md5:'%s' ",
                              Self->ob_type->tp_name, Desc.LanguageCode(),
                              Desc.md5());
}

static const char *description_doc =
    "Represent a package description and some attributes. Needed for\n"
    "things like translated descriptions.";

PyTypeObject PyDescription_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.Description",               // tp_name
   sizeof(CppPyObject<pkgCache::DescIterator>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<pkgCache::DescIterator>,          // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   DescriptionRepr,                         // tp_repr
   0,                                   // tp_as_number
   0,		                        // tp_as_sequence
   0,			                // tp_as_mapping
   0,                                   // tp_hash
   0,                                   // tp_call
   0,                                   // tp_str
   _PyAptObject_getattro,               // tp_getattro
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
   description_doc,                     // tp_doc
   CppTraverse<pkgCache::DescIterator>, // tp_traverse
   CppClear<pkgCache::DescIterator>,// tp_clear
   0,                                   // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   0,                                   // tp_methods
   0,                                   // tp_members
   DescriptionGetSet,                   // tp_getset
};
									/*}}}*/
// Version Class							/*{{{*/
// ---------------------------------------------------------------------

/* This is the simple depends result, the elements are split like
   ParseDepends does */
static PyObject *MakeDepends(PyObject *Owner,pkgCache::VerIterator &Ver,
			     bool AsObj)
{
   PyObject *Dict = PyDict_New();
   PyObject *LastDep = 0;
   unsigned LastDepType = 0;
   for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
   {
      pkgCache::DepIterator Start;
      pkgCache::DepIterator End;
      D.GlobOr(Start,End);

      // Switch/create a new dict entry
      if (LastDepType != Start->Type || LastDep != 0)
      {
	 PyObject *Dep = CppPyString(UntranslatedDepTypes[Start->Type]);
	 LastDepType = Start->Type;
	 LastDep = PyDict_GetItem(Dict,Dep);
	 if (LastDep == 0)
	 {
	    LastDep = PyList_New(0);
	    PyDict_SetItem(Dict,Dep,LastDep);
	    Py_DECREF(LastDep);
	 }
	 Py_DECREF(Dep);
      }

      PyObject *OrGroup = PyList_New(0);
      while (1)
      {
	 PyObject *Obj;
	 if (AsObj == true)
	    Obj = CppPyObject_NEW<pkgCache::DepIterator>(Owner,&PyDependency_Type,
							 Start);
	 else
	 {
	    if (Start->Version == 0)
	       Obj = Py_BuildValue("sss",
				   Start.TargetPkg().Name(),
				   "",
				   Start.CompType());
	    else
	       Obj = Py_BuildValue("sss",
				   Start.TargetPkg().Name(),
				   Start.TargetVer(),
				   Start.CompType());
	 }
	 PyList_Append(OrGroup,Obj);
	 Py_DECREF(Obj);

	 if (Start == End)
	    break;
	 Start++;
      }

      PyList_Append(LastDep,OrGroup);
      Py_DECREF(OrGroup);
   }

   return Dict;
}

static inline pkgCache::VerIterator Version_GetVer(PyObject *Self) {
   return GetCpp<pkgCache::VerIterator>(Self);
}

// Version attributes.
static PyObject *VersionGetVerStr(PyObject *Self, void*) {
   return CppPyString(Version_GetVer(Self).VerStr());
}
static PyObject *VersionGetSection(PyObject *Self, void*) {
   return CppPyString(Version_GetVer(Self).Section());
}
static PyObject *VersionGetArch(PyObject *Self, void*) {
   return CppPyString(Version_GetVer(Self).Arch());
}
static PyObject *VersionGetFileList(PyObject *Self, void*) {
   pkgCache::VerIterator &Ver = GetCpp<pkgCache::VerIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::VerIterator>(Self);
   PyObject *List = PyList_New(0);
   for (pkgCache::VerFileIterator I = Ver.FileList(); I.end() == false; I++)
   {
      PyObject *PkgFile;
      PyObject *Obj;
      PkgFile = CppPyObject_NEW<pkgCache::PkgFileIterator>(Owner,&PyPackageFile_Type,I.File());
      Obj = Py_BuildValue("NN",PkgFile,MkPyNumber(I.Index()));
      PyList_Append(List,Obj);
      Py_DECREF(Obj);
   }
   return List;
}

static PyObject *VersionGetDependsListStr(PyObject *Self, void*) {
   pkgCache::VerIterator &Ver = GetCpp<pkgCache::VerIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::VerIterator>(Self);
   return MakeDepends(Owner,Ver,false);
}
static PyObject *VersionGetDependsList(PyObject *Self, void*) {
   pkgCache::VerIterator &Ver = GetCpp<pkgCache::VerIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::VerIterator>(Self);
   return MakeDepends(Owner,Ver,true);
}
static PyObject *VersionGetParentPkg(PyObject *Self, void*) {
   PyObject *Owner = GetOwner<pkgCache::VerIterator>(Self);
   return CppPyObject_NEW<pkgCache::PkgIterator>(Owner,&PyPackage_Type,
                                                      Version_GetVer(Self).ParentPkg());
}
static PyObject *VersionGetProvidesList(PyObject *Self, void*) {
   PyObject *Owner = GetOwner<pkgCache::VerIterator>(Self);
   return CreateProvides(Owner,Version_GetVer(Self).ProvidesList());
}
static PyObject *VersionGetSize(PyObject *Self, void*) {
   return MkPyNumber(Version_GetVer(Self)->Size);
}
static PyObject *VersionGetInstalledSize(PyObject *Self, void*) {
   return MkPyNumber(Version_GetVer(Self)->InstalledSize);
}
static PyObject *VersionGetHash(PyObject *Self, void*) {
   return MkPyNumber(Version_GetVer(Self)->Hash);
}
static PyObject *VersionGetID(PyObject *Self, void*) {
   return MkPyNumber(Version_GetVer(Self)->ID);
}
static PyObject *VersionGetPriority(PyObject *Self, void*) {
   return MkPyNumber(Version_GetVer(Self)->Priority);
}
static PyObject *VersionGetPriorityStr(PyObject *Self, void*) {
   return CppPyString(Version_GetVer(Self).PriorityType());
}
static PyObject *VersionGetDownloadable(PyObject *Self, void*) {
   return PyBool_FromLong(Version_GetVer(Self).Downloadable());
}
static PyObject *VersionGetTranslatedDescription(PyObject *Self, void*) {
   pkgCache::VerIterator &Ver = GetCpp<pkgCache::VerIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::VerIterator>(Self);
   return CppPyObject_NEW<pkgCache::DescIterator>(Owner,
                    &PyDescription_Type,
                    Ver.TranslatedDescription());
}

static PyObject *VersionGetIsSecurityUpdate(PyObject *Self, void*) {
   return PyBool_FromLong(Version_GetVer(Self).IsSecurityUpdate());
}

static PyObject *VersionGetMultiArch(PyObject *Self, void*)
{
	return MkPyNumber(Version_GetVer(Self)->MultiArch);
}

#if 0 // FIXME: enable once pkgSourceList is stored somewhere
static PyObject *VersionGetIsTrusted(PyObject *Self, void*) {
   else if (strcmp("IsTrusted", Name) == 0)
   {
      pkgSourceList Sources;
      Sources.ReadMainList();
      for(pkgCache::VerFileIterator i = Ver.FileList(); !i.end(); i++)
      {
	 pkgIndexFile *index;
	 if(Sources.FindIndex(i.File(), index) && !index->IsTrusted())
	    Py_RETURN_FALSE;
      }
      Py_RETURN_TRUE;
   }
}
#endif

#define NOTNULL(x) (x ? x : "")

static PyObject *VersionRepr(PyObject *Self)
{
   pkgCache::VerIterator &Ver = GetCpp<pkgCache::VerIterator>(Self);
   return PyString_FromFormat("<%s object: Pkg:'%s' Ver:'%s' Section:'%s' "
                              " Arch:'%s' Size:%lu ISize:%lu Hash:%u ID:%u "
                              "Priority:%u>", Self->ob_type->tp_name,
                              Ver.ParentPkg().Name(), Ver.VerStr(),
                              NOTNULL(Ver.Section()), NOTNULL(Ver.Arch()),
                              (unsigned long)Ver->Size,
                              (unsigned long)Ver->InstalledSize,
	                          Ver->Hash, Ver->ID, Ver->Priority);
}
#undef NOTNULL

static PyObject *version_richcompare(PyObject *obj1, PyObject *obj2, int op)
{
    if (!PyVersion_Check(obj2))
        return Py_INCREF(Py_NotImplemented), Py_NotImplemented;

    const pkgCache::VerIterator &a = GetCpp<pkgCache::VerIterator>(obj1);
    const pkgCache::VerIterator &b = GetCpp<pkgCache::VerIterator>(obj2);
    const int comparison = _system->VS->CmpVersion(a.VerStr(), b.VerStr());
    switch (op) {
        case Py_LT: return PyBool_FromLong(comparison < 0);
        case Py_LE: return PyBool_FromLong(comparison <= 0);
        case Py_EQ: return PyBool_FromLong(comparison == 0);
        case Py_NE: return PyBool_FromLong(comparison != 0);
        case Py_GE: return PyBool_FromLong(comparison >= 0);
        case Py_GT: return PyBool_FromLong(comparison > 0);
        default: return NULL; // should not happen.
    }
}

static PyGetSetDef VersionGetSet[] = {
   {"arch",VersionGetArch,0,
    "The architecture of this specific version of the package."},
   {"depends_list",VersionGetDependsList,0,
    "A dictionary mapping dependency types to lists (A) of lists (B) of\n"
    "apt_pkg.Dependency objects. The lists (B) represent or dependencies\n"
    "like 'a || b'."},
   {"depends_list_str",VersionGetDependsListStr,0,
    "Same as depends_list, except that the apt_pkg.Dependency objects\n"
    "are 3-tuples of the form (name, version, operator); where operator\n"
    "is one of '<', '<=', '=', '>=', '>'."},
   {"downloadable",VersionGetDownloadable,0,
    "Whether the version can be downloaded."},
   {"file_list",VersionGetFileList,0,
    "A list of tuples (packagefile: apt_pkg.PackageFile, index: int) for the\n"
    "PackageFile objects related to this package. The index can be used\n"
    "to retrieve the record of this package version."},
   {"hash",VersionGetHash,0,
    "The numeric hash of the version used in the internal storage."},
   {"id",VersionGetID,0,
    "The numeric ID of the package."},
   {"installed_size",VersionGetInstalledSize,0,
    "The installed size of this package version."},
   {"multi_arch",VersionGetMultiArch,0,
    "Multi-arch state of this package, as an integer. See\n"
    "the various MULTI_ARCH_* members."},
   {"parent_pkg",VersionGetParentPkg,0,
    "The parent package of this version."},
   {"priority",VersionGetPriority,0,
    "The priority of the package as an integer, which can be compared to\n"
    "the constants PRI_EXTRA, PRI_IMPORTANT, PRI_OPTIONAL, PRI_REQUIRED,\n"
    "PRI_STANDARD of the apt_pkg module."},
   {"priority_str",VersionGetPriorityStr,0,
    "The priority of the package, as a string."},
   {"provides_list",VersionGetProvidesList,0,
    "A list of all packages provided by this version. The list contains\n"
    "tuples in the format (providesname, providesver, version)\n"
    "where 'version' is an apt_pkg.Version object."},
   {"section",VersionGetSection,0,
    "The section of this package version."},
   {"size",VersionGetSize,0,
    "The size of the package file."},
   {"translated_description",VersionGetTranslatedDescription,0,
    "An apt_pkg.Description object for the translated description if\n"
    "available or the untranslated fallback."},
   {"is_security_update",VersionGetIsSecurityUpdate,0,
    "Whether this version is a security update."},
   {"ver_str",VersionGetVerStr,0,
    "The version string."},
   {}
};

PyTypeObject PyVersion_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.Version",                   // tp_name
   sizeof(CppPyObject<pkgCache::VerIterator>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<pkgCache::VerIterator>,          // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   VersionRepr,                         // tp_repr
   0,                                   // tp_as_number
   0,		                        // tp_as_sequence
   0,			                // tp_as_mapping
   0,                                   // tp_hash
   0,                                   // tp_call
   0,                                   // tp_str
   _PyAptObject_getattro,               // tp_getattro
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,                  // tp_flags
   "Version Object",                    // tp_doc
   CppTraverse<pkgCache::VerIterator>, // tp_traverse
   CppClear<pkgCache::VerIterator>,// tp_clear
   version_richcompare,                 // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   0,                                   // tp_methods
   0,                                   // tp_members
   VersionGetSet,                       // tp_getset
};

									/*}}}*/

// PackageFile Class							/*{{{*/
// ---------------------------------------------------------------------
static PyObject *PackageFile_GetFileName(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyPath(File.FileName());
}

static PyObject *PackageFile_GetArchive(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Archive());
}

static PyObject *PackageFile_GetComponent(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Component());
}

static PyObject *PackageFile_GetVersion(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Version());
}

static PyObject *PackageFile_GetOrigin(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Origin());
}

static PyObject *PackageFile_GetLabel(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Label());
}

static PyObject *PackageFile_GetArchitecture(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Architecture());
}

static PyObject *PackageFile_GetCodename(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Codename());
}

static PyObject *PackageFile_GetSite(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.Site());
}

static PyObject *PackageFile_GetIndexType(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return CppPyString(File.IndexType());
}
static PyObject *PackageFile_GetSize(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return MkPyNumber(File->Size);
}

static PyObject *PackageFile_GetNotSource(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return PyBool_FromLong((File->Flags & pkgCache::Flag::NotSource) != 0);
}
static PyObject *PackageFile_GetNotAutomatic(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return PyBool_FromLong((File->Flags & pkgCache::Flag::NotAutomatic) != 0);
}

static PyObject *PackageFile_GetID(PyObject *Self,void*)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);
    return MkPyNumber(File->ID);
}

#define S(s) (s == NULL ? "" : s)
static PyObject *PackageFileRepr(PyObject *Self)
{
    pkgCache::PkgFileIterator &File = GetCpp<pkgCache::PkgFileIterator>(Self);

    return PyString_FromFormat("<%s object: filename:'%s'"
                               "  a=%s,c=%s,v=%s,o=%s,l=%s arch='%s' site='%s'"
                               " IndexType='%s' Size=%lu ID:%u>",
                               Self->ob_type->tp_name, File.FileName(),
                               S(File.Archive()),
                               S(File.Component()),S(File.Version()),
                               S(File.Origin()),S(File.Label()),
                               S(File.Architecture()),S(File.Site()),
                               S(File.IndexType()),File->Size,File->ID);
}
#undef S

static PyGetSetDef PackageFileGetSet[] = {
  {"architecture",PackageFile_GetArchitecture,0,
   "The architecture of the package file. Unused, empty string nowadays."},
  {"archive",PackageFile_GetArchive,0,
   "The archive of the package file (i.e. 'Suite' in the Release file)."},
  {"component",PackageFile_GetComponent,0,
   "The component of this package file (e.g. 'main')."},
  {"codename",PackageFile_GetCodename,0,
   "The codename of this package file (e.g. squeeze-updates)."},
  {"filename",PackageFile_GetFileName,0,
   "The path to the file."},
  {"id",PackageFile_GetID,0,
   "The numeric ID of this PackageFile object."},
  {"index_type",PackageFile_GetIndexType,0,
   "A string describing the type of index. Known values are\n"
   "'Debian Package Index', 'Debian Translation Index', and\n"
   "'Debian dpkg status file'."},
  {"label",PackageFile_GetLabel,0,
   "The label set in the release file (e.g. 'Debian')."},
  {"not_automatic",PackageFile_GetNotAutomatic,0,
   "Whether the NotAutomatic flag is set in the Release file."},
  {"not_source",PackageFile_GetNotSource,0,
   "Whether this package file lacks an active (sources.list) source;"
   "packages listed in such a file cannot be downloaded."},
  {"origin",PackageFile_GetOrigin,0,
   "The origin set in the release file."},
  {"site",PackageFile_GetSite,0,
   "The hostname of the location this file comes from."},
  {"size",PackageFile_GetSize,0,
   "The size of the file."},
  {"version",PackageFile_GetVersion,0,
   "The version set in the release file (e.g. '5.0.X' for lenny, where X\n"
   "is a point release)."},
  {}
};

static const char *packagefile_doc =
    "A package file is an index file stored in the cache with some\n"
    "additional pieces of information.";

PyTypeObject PyPackageFile_Type = {
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.PackageFile",                                // tp_name
   sizeof(CppPyObject<pkgCache::PkgFileIterator>),  // tp_basicsize
   0,                                                    // tp_itemsize
   CppDealloc<pkgCache::PkgFileIterator>,           // tp_dealloc
   0,                                                    // tp_print
   0,                                                    // tp_getattr
   0,                                                    // tp_setattr
   0,                                                    // tp_compare
   PackageFileRepr,                                      // tp_repr
   0,                                                    // tp_as_number
   0,                                                    // tp_as_sequence
   0,                                                    // tp_as_mapping
   0,                                                    // tp_hash
   0,                                                    // tp_call
   0,                                                    // tp_str
   _PyAptObject_getattro,                                // tp_getattro
   0,                                                    // tp_setattro
   0,                                                    // tp_as_buffer
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,              // tp_flags
   packagefile_doc,                                      // tp_doc
   CppTraverse<pkgCache::PkgFileIterator>,          // tp_traverse
   CppClear<pkgCache::PkgFileIterator>,             // tp_clear
   0,                                                    // tp_richcompare
   0,                                                    // tp_weaklistoffset
   0,                                                    // tp_iter
   0,                                                    // tp_iternext
   0,                                                    // tp_methods
   0,                                                    // tp_members
   PackageFileGetSet,                                    // tp_getset
};

// depends class
static PyObject *DependencyRepr(PyObject *Self)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);

   return PyString_FromFormat("<%s object: pkg:'%s' ver:'%s' comp:'%s'>",
	                          Self->ob_type->tp_name, Dep.TargetPkg().Name(),
	                          (Dep.TargetVer() == 0 ? "" : Dep.TargetVer()),
	                          Dep.CompType());
}

static PyObject *DepAllTargets(PyObject *Self,PyObject *Args)
{
   if (PyArg_ParseTuple(Args,"") == 0)
      return 0;

   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::DepIterator>(Self);

   std::unique_ptr<pkgCache::Version *[]> Vers(Dep.AllTargets());
   PyObject *List = PyList_New(0);
   for (pkgCache::Version **I = Vers.get(); *I != 0; I++)
   {
      PyObject *Obj;
      Obj = CppPyObject_NEW<pkgCache::VerIterator>(Owner,&PyVersion_Type,
							pkgCache::VerIterator(*Dep.Cache(),*I));
      PyList_Append(List,Obj);
      Py_DECREF(Obj);
   }
   return List;
}

static PyMethodDef DependencyMethods[] =
{
   {"all_targets",DepAllTargets,METH_VARARGS,
    "all_targets() -> list\n\n"
    "A list of all possible apt_pkg.Version objects which satisfy this\n"
    "dependency."},
   {}
};

// Dependency Class							/*{{{*/
// ---------------------------------------------------------------------

static PyObject *DependencyGetTargetVer(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   if (Dep->Version == 0)
      return CppPyString("");
   return CppPyString(Dep.TargetVer());
}

static PyObject *DependencyGetTargetPkg(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::DepIterator>(Self);
   return CppPyObject_NEW<pkgCache::PkgIterator>(Owner,&PyPackage_Type,
                                                      Dep.TargetPkg());
}

static PyObject *DependencyGetParentVer(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::DepIterator>(Self);
   return CppPyObject_NEW<pkgCache::VerIterator>(Owner,&PyVersion_Type,
                                                      Dep.ParentVer());
}

static PyObject *DependencyGetParentPkg(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   PyObject *Owner = GetOwner<pkgCache::DepIterator>(Self);
   return CppPyObject_NEW<pkgCache::PkgIterator>(Owner,&PyPackage_Type,
                                                      Dep.ParentPkg());
}

static PyObject *DependencyGetCompType(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   return CppPyString(Dep.CompType());
}

static PyObject *DependencyGetCompTypeDeb(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   return CppPyString(pkgCache::CompTypeDeb(Dep->CompareOp));
}

static PyObject *DependencyGetDepType(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   return CppPyString(Dep.DepType());
}

static PyObject *DependencyGetDepTypeUntranslated(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   return CppPyString(UntranslatedDepTypes[Dep->Type]);
}

static PyObject *DependencyGetDepTypeEnum(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   return MkPyNumber(Dep->Type);
}

static PyObject *DependencyGetID(PyObject *Self,void*)
{
   pkgCache::DepIterator &Dep = GetCpp<pkgCache::DepIterator>(Self);
   return MkPyNumber(Dep->ID);
}

static PyGetSetDef DependencyGetSet[] = {
   {"comp_type",DependencyGetCompType,0,
    "The type of comparison in mathematical notation, as a string, namely one "
    "of:\n"
    "'<', '<=', '=', '!=', '>=', '>', ''.\n"
    "The empty string will be returned in case of an unversioned dependency."},
   {"comp_type_deb",DependencyGetCompTypeDeb,0,
    "The type of comparison in Debian notation, as a string, namely one of:\n"
    "'<<', '<=', '=', '!=', '>=', '>>', ''.\n"
    "The empty string will be returned in case of an unversioned dependency.\n"
    "For details see the Debian Policy Manual on the syntax of relationship "
    "fields:\n"
    "https://www.debian.org/doc/debian-policy/ch-relationships.html#s-depsyntax"},
   {"dep_type",DependencyGetDepType,0,
    "The type of the dependency; may be translated"},
   {"dep_type_untranslated",DependencyGetDepTypeUntranslated,0,
    "Same as dep_type, but guaranteed to be untranslated."},
   {"dep_type_enum",DependencyGetDepTypeEnum,0,
    "Same as dep_type, but with a numeric value instead of a string. Can\n"
    "be compared against the TYPE_ constants defined in this class."},
   {"id",DependencyGetID,0,
    "The numeric ID of this dependency object."},
   {"parent_pkg",DependencyGetParentPkg,0,
    "The apt_pkg.Package object of the package which depends."},
   {"parent_ver",DependencyGetParentVer,0,
    "The apt_pkg.Version object of the package which depends."},
   {"target_pkg",DependencyGetTargetPkg,0,
    "The apt_pkg.Package object of the package depended upon"},
   {"target_ver",DependencyGetTargetVer,0,
    "The version of the package depended upon as a string"},
   {}
};

static const char *dependency_doc =
    "Represent a dependency from one package version to a package,\n"
    "and (optionally) a version relation (e.g. >= 1). Dependency\n"
    "objects also provide useful functions like all_targets()\n"
    "for selecting packages to satisfy the dependency.";

PyTypeObject PyDependency_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.Dependency",                // tp_name
   sizeof(CppPyObject<pkgCache::DepIterator>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<pkgCache::DepIterator>,          // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   DependencyRepr,                      // tp_repr
   0,                                   // tp_as_number
   0,		                        // tp_as_sequence
   0,			                // tp_as_mapping
   0,                                   // tp_hash
   0,                                   // tp_call
   0,                                   // tp_str
   _PyAptObject_getattro,               // tp_getattro
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
   dependency_doc,                      // tp_doc
   CppTraverse<pkgCache::DepIterator>, // tp_traverse
   CppClear<pkgCache::DepIterator>, // tp_clear
   0,                                   // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   DependencyMethods,                   // tp_methods
   0,                                   // tp_members
   DependencyGetSet,                    // tp_getset
};

									/*}}}*/
									/*}}}*/
// Reverse Dependency List Class					/*{{{*/
// ---------------------------------------------------------------------
static Py_ssize_t RDepListLen(PyObject *Self)
{
   return GetCpp<RDepListStruct>(Self).Len;
}

static PyObject *RDepListItem(PyObject *iSelf,Py_ssize_t Index)
{
   RDepListStruct &Self = GetCpp<RDepListStruct>(iSelf);
   if (Index < 0 || (unsigned)Index >= Self.Len)
   {
      PyErr_SetNone(PyExc_IndexError);
      return 0;
   }

   if ((unsigned)Index < Self.LastIndex)
   {
      Self.LastIndex = 0;
      Self.Iter = Self.Start;
   }

   while ((unsigned)Index > Self.LastIndex)
   {
      Self.LastIndex++;
      Self.Iter++;
      if (Self.Iter.end() == true)
      {
	 PyErr_SetNone(PyExc_IndexError);
	 return 0;
      }
   }

   return CppPyObject_NEW<pkgCache::DepIterator>(GetOwner<RDepListStruct>(iSelf),
						      &PyDependency_Type,Self.Iter);
}

static PySequenceMethods RDepListSeq =
{
   RDepListLen,
   0,                // concat
   0,                // repeat
   RDepListItem,
   0,                // slice
   0,                // assign item
   0                 // assign slice
};

static const char *dependencylist_doc =
    "A simple list-like type for representing multiple dependency\n"
    "objects in an efficient manner; without having to generate\n"
    "all Dependency objects in advance.";
PyTypeObject PyDependencyList_Type =
{
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
   "apt_pkg.DependencyList",             // tp_name
   sizeof(CppPyObject<RDepListStruct>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<RDepListStruct>,      // tp_dealloc
   0,                                   // tp_print
   0,                                   // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   &RDepListSeq,                         // 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 | Py_TPFLAGS_HAVE_GC, // tp_flags
   dependencylist_doc,             // tp_doc
   CppTraverse<RDepListStruct>,    // tp_traverse
   CppClear<RDepListStruct>,       // tp_clear
};

									/*}}}*/