Skip to content

Commit

Permalink
Merge pygobject-6170: Passing tests with newer versions of pygobject.
Browse files Browse the repository at this point in the history
Author: itamar
Review: exarkun
Fixes: twisted#6170

On newer versions of pygobject (e.g. v3.4, included in Ubuntu 12.10) Gtk is unimportable if X11 isn't available. This fixes the tests to deal with this brokenness.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@36515 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
itamarst committed Dec 4, 2012
1 parent bbc9494 commit 4142aee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
13 changes: 13 additions & 0 deletions twisted/internet/gtk3reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@
Then use twisted.internet APIs as usual.
"""

import os

from twisted.internet import gireactor
from twisted.python import runtime

# Newer versions of gtk3/pygoject raise a RuntimeError, or just break in a
# confusing manner, if the program is not running under X11. We therefore try
# to fail in a more reasonable manner, and check for $DISPLAY as a reasonable
# approximation of availability of X11. This is somewhat over-aggressive,
# since some older versions of gtk3/pygobject do work with missing $DISPLAY,
# but it's too hard to figure out which, so we always require it.
if (runtime.platform.getType() == 'posix' and
not runtime.platform.isMacOSX() and not os.environ.get("DISPLAY")):
raise ImportError(
"Gtk3 requires X11, and no DISPLAY environment variable is set")


class Gtk3Reactor(gireactor.GIReactor):
"""
Expand Down
59 changes: 48 additions & 11 deletions twisted/internet/test/test_gireactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@

import sys, os
try:
from twisted.internet import gireactor, gtk3reactor
from gi.repository import Gtk, Gio
from twisted.internet import gireactor
from gi.repository import Gio
except ImportError:
gireactor = None
gtk3reactor = None
else:
# gtk3reactor may be unavailable even if gireactor is available; in
# particular in pygobject 3.4/gtk 3.6, when no X11 DISPLAY is found.
try:
from twisted.internet import gtk3reactor
except ImportError:
gtk3reactor = None
else:
from gi.repository import Gtk

from twisted.python.util import sibpath
from twisted.python.runtime import platform
from twisted.internet.defer import Deferred
from twisted.internet.error import ReactorAlreadyRunning
from twisted.internet import reactor
from twisted.internet.protocol import ProcessProtocol
from twisted.trial.unittest import TestCase, SkipTest
from twisted.internet.test.reactormixins import ReactorBuilder
from twisted.test.test_twisted import SetAsideModule

# Skip all tests if gi is unavailable:
if gireactor is None:
skip = "gtk3/gi not importable"


class GApplicationRegistration(ReactorBuilder, TestCase):
Expand All @@ -31,10 +45,6 @@ class GApplicationRegistration(ReactorBuilder, TestCase):
reactor-running infrastructure, but don't need its test-creation
functionality.
"""
if gireactor is None:
skip = "gtk3/gi not importable"


def runReactor(self, app, reactor):
"""
Register the app, run the reactor, make sure app was activated, and
Expand Down Expand Up @@ -88,6 +98,10 @@ def test_gtkApplicationActivate(self):

self.runReactor(app, reactor)

if gtk3reactor is None:
test_gtkApplicationActivate.skip = (
"Gtk unavailable (may require running with X11 DISPLAY env set)")


def test_portable(self):
"""
Expand Down Expand Up @@ -164,10 +178,6 @@ class PygtkCompatibilityTests(TestCase):
possible.
"""

if gireactor is None:
skip = "gtk3/gi not importable"


def test_noCompatibilityLayer(self):
"""
If no compatiblity layer is present, imports of gobject and friends
Expand All @@ -190,6 +200,7 @@ def processExited(self, reason):
result.callback(self.data)

path = sibpath(__file__, "process_gireactornocompat.py")
from twisted.internet import reactor
reactor.spawnProcess(Stdout(), sys.executable, [sys.executable, path],
env=os.environ)
result.addCallback(self.assertEqual, "success")
Expand All @@ -205,3 +216,29 @@ def test_compatibilityLayer(self):
raise SkipTest("This version of gi doesn't include pygtkcompat.")
import gobject
self.assertTrue(gobject.__name__.startswith("gi."))



class Gtk3ReactorTests(TestCase):
"""
Tests for L{gtk3reactor}.
"""

def test_requiresDISPLAY(self):
"""
On X11, L{gtk3reactor} is unimportable if the C{DISPLAY} environment
variable is not set.
"""
display = os.environ.get("DISPLAY", None)
if display is not None:
self.addCleanup(os.environ.__setitem__, "DISPLAY", display)
del os.environ["DISPLAY"]
with SetAsideModule("twisted.internet.gtk3reactor"):
exc = self.assertRaises(ImportError,
__import__, "twisted.internet.gtk3reactor")
self.assertEqual(
exc.args[0],
"Gtk3 requires X11, and no DISPLAY environment variable is set")

if platform.getType() != "posix" or platform.isMacOSX():
test_requiresDISPLAY.skip = "This test is only relevant when using X11"
1 change: 1 addition & 0 deletions twisted/topfiles/6170.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.internet.test.test_gireactor no longer fails when using pygobject 3.4 and gtk 3.6 when X11 is unavailable.

0 comments on commit 4142aee

Please sign in to comment.