From 1cd54e6d4565308d6a8eb3dd2bb1f214f8f38aca Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Sat, 11 Apr 2009 16:20:11 +0200 Subject: [PATCH] Fix uninstalled usage with python 2.6. Fixes #576546 Also imports submodules as mentionned by Philippe Normand. --- gst/__init__.py | 23 +++++---- gstlibtoolimporter.py | 112 ++++++++++++++++++++++++++++++++++++++++++ gstltihooks.py | 71 -------------------------- testsuite/common.py | 7 +-- 4 files changed, 127 insertions(+), 86 deletions(-) create mode 100644 gstlibtoolimporter.py diff --git a/gst/__init__.py b/gst/__init__.py index 03ffc252e3..c62ef69d99 100644 --- a/gst/__init__.py +++ b/gst/__init__.py @@ -21,12 +21,11 @@ # # Author: David I. Lehn -__gstltihooks_used__ = False try: - import gstltihooks - __gstltihooks_used__ = True -except: - pass + import gstlibtoolimporter + gstlibtoolimporter.install() +except ImportError: + gstlibtoolimporter = None import sys @@ -155,7 +154,6 @@ class Fraction(Value): def __float__(self): return float(self.num) / float(self.denom) -import sys try: dlsave = sys.getdlopenflags() from DLFCN import RTLD_GLOBAL, RTLD_LAZY @@ -220,10 +218,11 @@ if locals().has_key("message_new_buffering"): # with other people's module importers # it also clears out the module completely as if it were never loaded, # so that if anyone else imports gstltihooks the hooks get installed -if __gstltihooks_used__: - gstltihooks.uninstall() - __gstltihooks_used__ = False - del gstltihooks +if gstlibtoolimporter is not None: + import audio + import pbutils + import tag + import video + gstlibtoolimporter.uninstall() import sys - del sys.modules['gstltihooks'] - + del sys.modules["gstlibtoolimporter"] diff --git a/gstlibtoolimporter.py b/gstlibtoolimporter.py new file mode 100644 index 0000000000..4b08de4804 --- /dev/null +++ b/gstlibtoolimporter.py @@ -0,0 +1,112 @@ +# -*- Mode: Python -*- +# vi:si:et:sw=4:sts=4:ts=4 +# +# gst-python +# Copyright (C) 2009 Alessandro Decina +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +# +# Author: Alessandro Decina + +# importer for uninstalled setup, see PEP +# http://www.python.org/dev/peps/pep-0302/ + +import os +import sys +import imp + +class Loader(object): + def __init__(self, fileobj, filename, description): + self.fileobj = fileobj + self.filename = filename + self.description = description + + def find_real_filename(self): + dlname = None + installed = False + for line in self.fileobj: + if len(line) > 7 and line[:7] == 'dlname=': + dlname = line[8:-2] + elif len(line) > 10 and line[:10] == 'installed=': + installed = line[10:-1] == 'yes' + + if not dlname: + return None + + if installed or os.path.dirname(self.filename).endswith('.libs'): + filename = os.path.join(os.path.dirname(self.filename), dlname) + else: + filename = os.path.join(os.path.dirname(self.filename), '.libs', dlname) + + return filename + + def load_module(self, name): + try: + module = sys.modules[name] + self.fileobj.close() + + return module + except KeyError: + pass + + filename = self.find_real_filename() + self.fileobj.close() + if filename is None: + raise ImportError("No module named %s" % name) + + fileobj = file(filename, 'rb') + + module = imp.new_module(name) + sys.modules[name] = module + + imp.load_module(name, fileobj, filename, self.description) + fileobj.close() + + return module + +class Importer(object): + def find_module(self, name, path=None): + if path is None: + path = sys.path + + for directory in path: + fileobj, filename, description = self.find_libtool_module(name, directory) + if fileobj is not None: + return Loader(fileobj, filename, description) + + return None + + def find_libtool_module(self, name, directory): + name = name.split(".")[-1] + absname = os.path.join(directory, name) + for suffix in ('.la', '.module.la'): + filename = absname + suffix + try: + fileobj = file(filename, 'rb') + except IOError: + continue + + return fileobj, filename, (suffix, 'rb', imp.C_EXTENSION) + + return None, None, None + +importer = Importer() + +def install(): + sys.meta_path.append(importer) + +def uninstall(): + sys.meta_path.remove(importer) diff --git a/gstltihooks.py b/gstltihooks.py index 2b4d0b0a1e..e69de29bb2 100644 --- a/gstltihooks.py +++ b/gstltihooks.py @@ -1,71 +0,0 @@ -# -*- Mode: Python -*- -# vi:si:et:sw=4:sts=4:ts=4 - -# ltihooks.py: python import hooks that understand libtool libraries. -# Copyright (C) 2000 James Henstridge. -# renamed to gstltihooks.py so it does not accidentally get imported by -# an installed copy of gtk -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -import os, ihooks - -class LibtoolHooks(ihooks.Hooks): - def get_suffixes(self): - """Like normal get_suffixes, but adds .la suffixes to list""" - ret = ihooks.Hooks.get_suffixes(self) - ret.insert(0, ('module.la', 'rb', 3)) - ret.insert(0, ('.la', 'rb', 3)) - return ret - - def load_dynamic(self, name, filename, file=None): - """Like normal load_dynamic, but treat .la files specially""" - if len(filename) > 3 and filename[-3:] == '.la': - fp = open(filename, 'r') - dlname = '' - installed = 1 - line = fp.readline() - while line: - # dlname: the name that we can dlopen - if len(line) > 7 and line[:7] == 'dlname=': - dlname = line[8:-2] - # installed: whether it's already installed - elif len(line) > 10 and line[:10] == 'installed=': - installed = line[10:-1] == 'yes' - line = fp.readline() - fp.close() - if dlname: - if installed: - filename = os.path.join(os.path.dirname(filename), - dlname) - else: - # if .libs already there, don't need to add it again - if os.path.dirname(filename).endswith('.libs'): - filename = os.path.join(os.path.dirname(filename), - dlname) - else: - filename = os.path.join(os.path.dirname(filename), - '.libs', dlname) - return ihooks.Hooks.load_dynamic(self, name, filename, file) - -importer = ihooks.ModuleImporter() -importer.set_hooks(LibtoolHooks()) - -def install(): - importer.install() -def uninstall(): - importer.uninstall() - -install() diff --git a/testsuite/common.py b/testsuite/common.py index f63b5e65a3..5ec46f4ca6 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -89,10 +89,11 @@ except ImportError: file = gst.pbutils.__file__ assert file.startswith(path), 'bad gst.pbutils path: %s' % file -# testhelper needs gstltihooks -import gstltihooks +# testhelper needs gstlibtoolimporter +import gstlibtoolimporter +gstlibtoolimporter.install() import testhelper -gstltihooks.uninstall() +gstlibtoolimporter.uninstall() _stderr = None