From 8430dbf041ce70dcba81101678beee0492252fd2 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 14 Jul 2009 20:43:56 +0200 Subject: python/configuration.cc: Unify all Configuration,ConfigurationPtr,ConfigurationSub into one type. This makes the whole API easier. The disadvantage is that we require 8 bytes (??) more per object for the owner object pointer, which is NULL for most cases (the only exception being objects created by Configuration.sub_tree). --- python/apt_pkgmodule.cc | 9 ++-- python/apt_pkgmodule.h | 6 +-- python/configuration.cc | 116 ++++-------------------------------------------- 3 files changed, 13 insertions(+), 118 deletions(-) (limited to 'python') diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc index 3f18f785..e4fd0dfc 100644 --- a/python/apt_pkgmodule.cc +++ b/python/apt_pkgmodule.cc @@ -527,8 +527,7 @@ extern "C" void initapt_pkg() #endif { // Finalize our types to add slots, etc. - if (PyType_Ready(&PyConfigurationPtr_Type) == -1) INIT_ERROR; - if (PyType_Ready(&PyConfigurationSub_Type) == -1) INIT_ERROR; + if (PyType_Ready(&PyConfiguration_Type) == -1) INIT_ERROR; if (PyType_Ready(&PyCacheFile_Type) == -1) INIT_ERROR; // Initialize the module @@ -539,8 +538,10 @@ extern "C" void initapt_pkg() #endif // Global variable linked to the global configuration class - CppPyObject *Config = CppPyObject_NEW(&PyConfigurationPtr_Type); + CppOwnedPyObject *Config = CppOwnedPyObject_NEW(NULL, &PyConfiguration_Type); Config->Object = _config; + // Global configuration, should never be deleted. + Config->NoDelete = true; PyModule_AddObject(Module,"config",Config); #ifdef COMPAT_0_7 Py_INCREF(Config); @@ -568,8 +569,6 @@ extern "C" void initapt_pkg() ADDTYPE(Module,"Cdrom",&PyCdrom_Type); /* ========================= configuration.cc ========================= */ ADDTYPE(Module,"Configuration",&PyConfiguration_Type); - //ADDTYPE(Module,"ConfigurationSub",&PyConfigurationSub_Type); // NO __new__() - //ADDTYPE(Module,"ConfigurationPtr",&PyConfigurationPtr_Type); // NO __new__() /* ========================= depcache.cc ========================= */ ADDTYPE(Module,"ActionGroup",&PyActionGroup_Type); ADDTYPE(Module,"DepCache",&PyDepCache_Type); diff --git a/python/apt_pkgmodule.h b/python/apt_pkgmodule.h index 7b835ca8..f77c5e73 100644 --- a/python/apt_pkgmodule.h +++ b/python/apt_pkgmodule.h @@ -14,12 +14,8 @@ #include // Configuration Stuff -#define Configuration_Check(op) ((op)->ob_type == &PyConfiguration_Type || \ - (op)->ob_type == &PyConfigurationPtr_Type || \ - (op)->ob_type == &PyConfigurationSub_Type) +#define Configuration_Check(op) ((op)->ob_type == &PyConfiguration_Type) extern PyTypeObject PyConfiguration_Type; -extern PyTypeObject PyConfigurationPtr_Type; -extern PyTypeObject PyConfigurationSub_Type; extern PyTypeObject PyVersion_Type; extern char *doc_LoadConfig; diff --git a/python/configuration.cc b/python/configuration.cc index ba075133..e7e31cc8 100644 --- a/python/configuration.cc +++ b/python/configuration.cc @@ -5,11 +5,8 @@ Configuration - Binding for the configuration object. - There are three seperate classes.. - Configuration - A stand alone configuration instance - ConfigurationPtr - A pointer to a configuration instance, used only - for the global instance (_config) - ConfigurationSub - A subtree - has a reference to its owner. + The Configuration object can have an owner (a parent Configuration object), + and it always uses a pointer. The wrapping is mostly 1:1 with the C++ code, but there are additions to wrap the linked tree walking into nice flat sequence walking. @@ -25,33 +22,13 @@ #include /*}}}*/ -/* If we create a sub tree then it is of this type, the Owner is used - to manage reference counting. */ -struct SubConfiguration : public CppPyObject -{ - PyObject *Owner; -}; - - /*}}}*/ -// CnfSubFree - Free a sub configuration /*{{{*/ -// --------------------------------------------------------------------- -/* */ -void CnfSubFree(PyObject *Obj) -{ - SubConfiguration *Self = (SubConfiguration *)Obj; - Py_DECREF(Self->Owner); - CppDealloc(Obj); -} - /*}}}*/ // GetSelf - Convert PyObject to Configuration /*{{{*/ // --------------------------------------------------------------------- /* */ static inline Configuration &GetSelf(PyObject *Obj) { - if (Obj->ob_type == &PyConfigurationPtr_Type) - return *GetCpp(Obj); - return GetCpp(Obj); + return *GetCpp(Obj); } /*}}}*/ @@ -160,13 +137,8 @@ static PyObject *CnfSubTree(PyObject *Self,PyObject *Args) return 0; } - // Create a new sub configuration. - SubConfiguration *New = (SubConfiguration*)(&PyConfigurationSub_Type) - ->tp_alloc(&PyConfigurationSub_Type,0); - new (&New->Object) Configuration(Itm); - New->Owner = Self; - Py_INCREF(Self); - return New; + return CppOwnedPyObject_NEW(Self,&PyConfiguration_Type, + new Configuration(Itm)); } // Return a list of items at a specific level @@ -495,7 +467,7 @@ static PyObject *CnfNew(PyTypeObject *type, PyObject *args, PyObject *kwds) { char *kwlist[] = {NULL}; if (PyArg_ParseTupleAndKeywords(args,kwds,"",kwlist) == 0) return 0; - return CppPyObject_NEW(type); + return CppOwnedPyObject_NEW(NULL, type, new Configuration()); } // Type for a Normal Configuration object @@ -505,10 +477,10 @@ PyTypeObject PyConfiguration_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Configuration", // tp_name - sizeof(CppPyObject), // tp_basicsize + sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr @@ -545,75 +517,3 @@ PyTypeObject PyConfiguration_Type = CnfNew, // tp_new }; -PyTypeObject PyConfigurationPtr_Type = -{ - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "apt_pkg.ConfigurationPtr", // tp_name - sizeof(CppPyObject), // tp_basicsize - 0, // tp_itemsize - // Methods - (destructor)PyObject_Free, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - &ConfigurationSeq, // tp_as_sequence - &ConfigurationMap, // 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 - "ConfigurationPtr Object", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - CnfMethods, // tp_methods - 0, // tp_members - 0, // tp_getset - &PyConfiguration_Type, // tp_base -}; - -PyTypeObject PyConfigurationSub_Type = -{ - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "apt_pkg.ConfigurationSub", // tp_name - sizeof(SubConfiguration), // tp_basicsize - 0, // tp_itemsize - // Methods - CnfSubFree, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - &ConfigurationSeq, // tp_as_sequence - &ConfigurationMap, // 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 - "ConfigurationSub Object", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - CnfMethods, // tp_methods - 0, // tp_members - 0, // tp_getset - &PyConfiguration_Type, // tp_base -}; - -- cgit v1.2.3