[bitbake-devel,1/2] utils: Avoid warnings about deprecated imp module

Submitted by Richard Purdie on Nov. 13, 2018, 10:59 p.m. | Patch ID: 156263

Details

Message ID 20181113225942.17780-1-richard.purdie@linuxfoundation.org
State New
Headers show

Commit Message

Richard Purdie Nov. 13, 2018, 10:59 p.m.
The imp module is deprecated, port the code over to use importlib.

bitbake/lib/bb/utils.py:30: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/utils.py | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

Patch hide | download patch | download mbox

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 73b6cb423b..1cadbc5bcd 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -27,7 +27,7 @@  import bb
 import bb.msg
 import multiprocessing
 import fcntl
-import imp
+import importlib
 import itertools
 import subprocess
 import glob
@@ -43,7 +43,7 @@  from contextlib import contextmanager
 from ctypes import cdll
 
 logger = logging.getLogger("BitBake.Util")
-python_extensions = [e for e, _, _ in imp.get_suffixes()]
+python_extensions = importlib.machinery.all_suffixes()
 
 
 def clean_context():
@@ -1544,12 +1544,9 @@  def export_proxies(d):
 def load_plugins(logger, plugins, pluginpath):
     def load_plugin(name):
         logger.debug(1, 'Loading plugin %s' % name)
-        fp, pathname, description = imp.find_module(name, [pluginpath])
-        try:
-            return imp.load_module(name, fp, pathname, description)
-        finally:
-            if fp:
-                fp.close()
+        spec = importlib.machinery.PathFinder.find_spec(name, path=[pluginpath] )
+        if spec:
+            return spec.loader.load_module()
 
     logger.debug(1, 'Loading plugins from %s...' % pluginpath)
 

Comments

Armin Kuster Nov. 13, 2018, 11:11 p.m.
On 11/13/18 2:59 PM, Richard Purdie wrote:
> The imp module is deprecated, port the code over to use importlib.
>
> bitbake/lib/bb/utils.py:30: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
>   import imp

so host side? Thud and Sumo backport?

- armin

>
> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
> ---
>  lib/bb/utils.py | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/lib/bb/utils.py b/lib/bb/utils.py
> index 73b6cb423b..1cadbc5bcd 100644
> --- a/lib/bb/utils.py
> +++ b/lib/bb/utils.py
> @@ -27,7 +27,7 @@ import bb
>  import bb.msg
>  import multiprocessing
>  import fcntl
> -import imp
> +import importlib
>  import itertools
>  import subprocess
>  import glob
> @@ -43,7 +43,7 @@ from contextlib import contextmanager
>  from ctypes import cdll
>  
>  logger = logging.getLogger("BitBake.Util")
> -python_extensions = [e for e, _, _ in imp.get_suffixes()]
> +python_extensions = importlib.machinery.all_suffixes()
>  
>  
>  def clean_context():
> @@ -1544,12 +1544,9 @@ def export_proxies(d):
>  def load_plugins(logger, plugins, pluginpath):
>      def load_plugin(name):
>          logger.debug(1, 'Loading plugin %s' % name)
> -        fp, pathname, description = imp.find_module(name, [pluginpath])
> -        try:
> -            return imp.load_module(name, fp, pathname, description)
> -        finally:
> -            if fp:
> -                fp.close()
> +        spec = importlib.machinery.PathFinder.find_spec(name, path=[pluginpath] )
> +        if spec:
> +            return spec.loader.load_module()
>  
>      logger.debug(1, 'Loading plugins from %s...' % pluginpath)
>
Richard Purdie Nov. 13, 2018, 11:13 p.m.
On Tue, 2018-11-13 at 15:11 -0800, akuster808 wrote:
> On 11/13/18 2:59 PM, Richard Purdie wrote:
> > The imp module is deprecated, port the code over to use importlib.
> > 
> > bitbake/lib/bb/utils.py:30: DeprecationWarning: the imp module is
> > deprecated in favour of importlib; see the module's documentation
> > for alternative uses
> >   import imp
> 
> so host side? Thud and Sumo backport?

Yes, but I think the patch crashes and burns on python34 as I clearly
didn't understand the docs correctly as I thought this was 3.4 safe.

So more investigation/work needed...

We hide most of these warnings anyway, I just decided we should really
fix some of them before 3.7+ bites us.

Cheers,

Richard