diff options
author | Philippe Pepiot <philippe.pepiot@logilab.fr> | 2017-01-20 16:07:39 +0100 |
---|---|---|
committer | Philippe Pepiot <philippe.pepiot@logilab.fr> | 2017-01-20 16:07:39 +0100 |
commit | 1f42d92dbca788c4ab7750ae8359b974134cf595 (patch) | |
tree | 3dd9f331ed43500907045ab01677438e71b6bb3a | |
parent | 6c9ed73a6a26affe441840b2e7e37c397a6d2221 (diff) | |
download | logilab-common-1f42d92dbca788c4ab7750ae8359b974134cf595.tar.gz |
[modutils] deprecate modpath_from_file
The method will likely fail if there somes symlinks in your python environment
and will certainly fail on a file that isn't in sys.path (like custom importers
using sys.meta_path).
-rw-r--r-- | logilab/common/modutils.py | 6 | ||||
-rw-r--r-- | test/unittest_modutils.py | 8 |
2 files changed, 11 insertions, 3 deletions
diff --git a/logilab/common/modutils.py b/logilab/common/modutils.py index 31b5b27..030cfa3 100644 --- a/logilab/common/modutils.py +++ b/logilab/common/modutils.py @@ -49,6 +49,7 @@ except ImportError: ZIPFILE = object() from logilab.common import STD_BLACKLIST, _handle_blacklist +from logilab.common.deprecation import deprecated # Notes about STD_LIB_DIR # Consider arch-specific installation for STD_LIB_DIR definition @@ -234,8 +235,11 @@ def _path_from_filename(filename): return filename +@deprecated('you should avoid using modpath_from_file()') def modpath_from_file(filename, extrapath=None): - """given a file path return the corresponding splitted module's name + """DEPRECATED: doens't play well with symlinks and sys.meta_path + + Given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.') :type filename: str diff --git a/test/unittest_modutils.py b/test/unittest_modutils.py index d1fcaf9..ec2a5c8 100644 --- a/test/unittest_modutils.py +++ b/test/unittest_modutils.py @@ -21,6 +21,7 @@ unit tests for module modutils (module manipulation utilities) import doctest import sys +import warnings try: __file__ except NameError: @@ -117,8 +118,11 @@ class modpath_from_file_tc(ModutilsTestCase): """ given an absolute file path return the python module's path as a list """ def test_knownValues_modpath_from_file_1(self): - self.assertEqual(modutils.modpath_from_file(modutils.__file__), - ['logilab', 'common', 'modutils']) + with warnings.catch_warnings(record=True) as warns: + self.assertEqual(modutils.modpath_from_file(modutils.__file__), + ['logilab', 'common', 'modutils']) + self.assertIn('you should avoid using modpath_from_file()', + [str(w.message) for w in warns]) def test_knownValues_modpath_from_file_2(self): self.assertEqual(modutils.modpath_from_file('unittest_modutils.py', |