Example #1
0
static void
obj_dealloc(PyObj self)
{
	Datum d = PyPgObject_GetDatum(self);

	PyPgObject_SetDatum(self, 0);
	if (PyPgType_ShouldFree(Py_TYPE(self)))
	{
		if (PointerIsValid(DatumGetPointer(d)))
		{
			MemoryContext former = CurrentMemoryContext;
			PG_TRY();
			{
				pfree(DatumGetPointer(d));

				/*
				 * When PLPY_STRANGE_THINGS is defined.
				 */
				RaiseAStrangeError
			}
			PG_CATCH();
			{
				PyErr_EmitPgErrorAsWarning("failed to deallocate Datum");
			}
			PG_END_TRY();
			MemoryContextSwitchTo(former);
		}
	}

	Py_TYPE(self)->tp_free(self);
}
Example #2
0
static void
tupd_dealloc(PyObj self)
{
	MemoryContext former = CurrentMemoryContext;

	Py_XDECREF(PyPgTupleDesc_GetNames(self));
	PyPgTupleDesc_SetNames(self, NULL);

	Py_XDECREF(PyPgTupleDesc_GetNameMap(self));
	PyPgTupleDesc_SetNameMap(self, NULL);

	Py_XDECREF(PyPgTupleDesc_GetTypesTuple(self));
	PyPgTupleDesc_SetTypesTuple(self, NULL);

	PG_TRY();
	{
		TupleDesc td;
		int *map;

		td = PyPgTupleDesc_GetTupleDesc(self);
		PyPgTupleDesc_SetTupleDesc(self, NULL);
		if (td != NULL)
			FreeTupleDesc(td);

		map = PyPgTupleDesc_GetIndexMap(self);
		PyPgTupleDesc_SetIndexMap(self, NULL);
		if (map != NULL)
			pfree(map);

		map = PyPgTupleDesc_GetFreeMap(self);
		PyPgTupleDesc_SetFreeMap(self, NULL);
		if (map != NULL)
			pfree(map);

		/*
		 * When PLPY_STRANGE_THINGS is defined.
		 */
		RaiseAStrangeError
	}
	PG_CATCH();
	{
		PyErr_EmitPgErrorAsWarning("could not deallocate Postgres.TupleDesc fields");
	}
	PG_END_TRY();
	/*
	 * Normally, this doesn't matter, but it is possible for the context
	 * to be switched by the above code..
	 */
	MemoryContextSwitchTo(former);

	Py_TYPE(self)->tp_free(self);
}
Example #3
0
static void
statement_dealloc(PyObj self)
{
	PyPgStatement ps;
	SPIPlanPtr plan, splan;
	MemoryContext memory;
	PyObj ob;

	ob = PyPgStatement_GetString(self);
	PyPgStatement_SetString(self, NULL);
	Py_XDECREF(ob);

	ob = PyPgStatement_GetInput(self);
	PyPgStatement_SetInput(self, NULL);
	Py_XDECREF(ob);

	ob = PyPgStatement_GetOutput(self);
	PyPgStatement_SetOutput(self, NULL);
	Py_XDECREF(ob);

	ob = PyPgStatement_GetParameters(self);
	PyPgStatement_SetParameters(self, NULL);
	Py_XDECREF(ob);

	ps = (PyPgStatement) self;
	plan = ps->ps_plan;
	splan = ps->ps_scroll_plan;
	ps->ps_plan = NULL;
	ps->ps_scroll_plan = NULL;
	memory = PyPgStatement_GetMemory(self);

	/*
	 * It's allocated in the statment's memory context, so just NULL it.
	 */
	PyPgStatement_SetParameterTypes(self, NULL);
	PyPgStatement_SetPath(self, NULL);

	if (plan != NULL || splan != NULL || memory != NULL)
	{
		MemoryContext former = CurrentMemoryContext;

		PG_TRY();
		{
			if (memory)
				MemoryContextDelete(memory);
			if (plan)
				SPI_freeplan(plan);
			if (splan)
				SPI_freeplan(splan);

			/*
			 * When PLPY_STRANGE_THINGS is defined.
			 */
			RaiseAStrangeError
		}
		PG_CATCH();
		{
			PyErr_EmitPgErrorAsWarning("could not deallocate statement plans");
		}
		PG_END_TRY();

		MemoryContextSwitchTo(former);
	}