Fix crash in depgraph._remove_pkg

Make sure we don't try to remove a packages twice. This happened in the
past when a package was its own digraph-child.

One way to trigger this is a DEPEND on itself as in the test case.

This bug was reported by slyfox on IRC. In his case the cause for the
self-edge is unknown. Unfortunately the system state is lost.
diff --git a/pym/_emerge/depgraph.py b/pym/_emerge/depgraph.py
index 2ed7aeb..9698607 100644
--- a/pym/_emerge/depgraph.py
+++ b/pym/_emerge/depgraph.py
@@ -2388,8 +2388,15 @@
 		Remove a package and all its then parentless digraph
 		children from all depgraph datastructures.
 		"""
+		debug = "--debug" in self._frozen_config.myopts
+		if debug:
+			writemsg_level(
+				"Removing package: %s\n" % pkg,
+				level=logging.DEBUG, noiselevel=-1)
+
 		try:
-			children = self._dynamic_config.digraph.child_nodes(pkg)
+			children = [child for child in self._dynamic_config.digraph.child_nodes(pkg) \
+				if child is not pkg]
 			self._dynamic_config.digraph.remove(pkg)
 		except KeyError:
 			children = []
diff --git a/pym/portage/tests/resolver/test_slot_collisions.py b/pym/portage/tests/resolver/test_slot_collisions.py
index fdd6dd6..85afc09 100644
--- a/pym/portage/tests/resolver/test_slot_collisions.py
+++ b/pym/portage/tests/resolver/test_slot_collisions.py
@@ -226,3 +226,34 @@
 				self.assertEqual(test_case.test_success, True, test_case.fail_msg)
 		finally:
 			playground.cleanup()
+
+
+	def testSelfDEPENDRemovalCrash(self):
+		"""
+		Make sure we don't try to remove a packages twice. This happened
+		in the past when a package had a DEPEND on itself.
+		"""
+		ebuilds = {
+			"dev-libs/A-1": { "RDEPEND": "=dev-libs/X-1" },
+			"dev-libs/B-1": { "RDEPEND": "dev-libs/X" },
+
+			"dev-libs/X-1": { },
+			"dev-libs/X-2": { "DEPEND": ">=dev-libs/X-2" },
+			}
+
+		test_cases = (
+			ResolverPlaygroundTestCase(
+				["dev-libs/A", "dev-libs/B"],
+				all_permutations = True,
+				success = True,
+				ignore_mergelist_order = True,
+				mergelist = ["dev-libs/X-1", "dev-libs/A-1", "dev-libs/B-1"]),
+			)
+
+		playground = ResolverPlayground(ebuilds=ebuilds, debug=False)
+		try:
+			for test_case in test_cases:
+				playground.run_TestCase(test_case)
+				self.assertEqual(test_case.test_success, True, test_case.fail_msg)
+		finally:
+			playground.cleanup()