> On Wed, 28 Aug 2013 12:45:12 +0900 (JST) > [email protected] (YAMAMOTO Takashi) wrote: > >> i don't think the patch makes sense. >> >> the real problem is that your command ends up with importing >> two copies of switches module. you can avoid the problem by: >> ryu-manager --verbose --observe-links ryu.topology.switches > > I guess that we had better to support passing a file name as a command > line option. Here is a workaround.
if we want to support it, it's better to try to avoid double-import in the first place. how about this? YAMAMOTO Takashi >From 0f7ed720eeda1e1f695121a4eeda7e64e81c1ae4 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi <[email protected]> Date: Wed, 28 Aug 2013 15:28:10 +0900 Subject: [PATCH] avoid importing a module twice due to the way utils.import_module is implemented, "ryu-manager ryu/app/switches.py" ends up with loading switches.py module twice. this commit fixes that by checking if module pathnames specified on the command line is aliases of already loaded modules. while the check is incomplete, it should cover the most of useful cases. Signed-off-by: YAMAMOTO Takashi <[email protected]> --- ryu/utils.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/ryu/utils.py b/ryu/utils.py index dd22084..e8fc7a2 100644 --- a/ryu/utils.py +++ b/ryu/utils.py @@ -39,15 +39,46 @@ import re LOG = logging.getLogger('ryu.utils') +def chop_py_suffix(p): + for suf in ['.py', '.pyc', '.pyo']: + if p.endswith(suf): + return p[:-len(suf)] + return p + + +def _likely_same(a, b): + if os.path.samefile(a, b): + return True + if chop_py_suffix(a) == chop_py_suffix(b): + return True + return False + + +def _find_loaded_module(modpath): + # copy() to avoid RuntimeError: dictionary changed size during iteration + for k, m in sys.modules.copy().iteritems(): + if not hasattr(m, '__file__'): + continue + if _likely_same(m.__file__, modpath): + return m + return None + + def import_module(modname): try: __import__(modname) except: - sys.path.append(os.path.dirname(os.path.abspath(modname))) + abspath = os.path.abspath(modname) + mod = _find_loaded_module(abspath) + if mod: + return mod + opath = sys.path + sys.path.append(os.path.dirname(abspath)) name = os.path.basename(modname) if name.endswith('.py'): name = name[:-3] __import__(name) + sys.path = opath return sys.modules[name] return sys.modules[modname] -- 1.8.3.1 > > diff --git a/ryu/utils.py b/ryu/utils.py > index d5b4479..13415ea 100644 > --- a/ryu/utils.py > +++ b/ryu/utils.py > @@ -20,6 +20,7 @@ import logging > import os > import sys > import re > +from oslo.config import cfg > > LOG = logging.getLogger('ryu.utils') > > @@ -32,7 +33,17 @@ def import_module(modname): > name = os.path.basename(modname) > if name.endswith('.py'): > name = name[:-3] > - __import__(name) > + try: > + __import__(name) > + except cfg.ArgsAlreadyParsedError: > + # ryu-manager needs to import modules calling > + # register_cli_opts. Thus, passing a imported file as a > + # command line option for ryu-manager results in double import > + # (e.g. ryu.topology.switches). The following code is not > + # perfect but better than nothing. > + for key in sys.modules.keys(): > + if key.endswith(name): > + return sys.modules[key] > return sys.modules[name] > return sys.modules[modname] > > > ------------------------------------------------------------------------------ > Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! > Discover the easy way to master current and previous Microsoft technologies > and advance your career. Get an incredible 1,500+ hours of step-by-step > tutorial videos with LearnDevNow. Subscribe today and save! > http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk > _______________________________________________ > Ryu-devel mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/ryu-devel ------------------------------------------------------------------------------ Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! Discover the easy way to master current and previous Microsoft technologies and advance your career. Get an incredible 1,500+ hours of step-by-step tutorial videos with LearnDevNow. Subscribe today and save! http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
