Utility functions and classes used by nose internally.
Return absolute, normalized path to directory, if it exists; None otherwise.
Return absolute, normalized path to file (optionally in directory where), or None if the file can’t be found either in where or the current working directory.
A name is file-like if it is a path that exists, or it has a directory part, or it ends in .py, or it isn’t a legal python identifier.
Get the line number of a function. First looks for compat_co_firstlineno, then func_code.co_first_lineno.
Find the python source file for a package, relative to a particular directory (defaults to current working directory if not given).
Find the full dotted package name for a given python source file name. Returns None if the file is not a python source file.
>>> getpackage('foo.py')
'foo'
>>> getpackage('biff/baf.py')
'baf'
>>> getpackage('nose/util.py')
'nose.util'
Works for directories too.
>>> getpackage('nose')
'nose'
>>> getpackage('nose/plugins')
'nose.plugins'
And __init__ files stuck onto directories
>>> getpackage('nose/plugins/__init__.py')
'nose.plugins'
Absolute paths also work.
>>> path = os.path.abspath(os.path.join('nose', 'plugins'))
>>> getpackage(path)
'nose.plugins'
Is obj a class? Inspect’s isclass is too liberal and returns True for objects that can’t be subclasses of anything.
Is this path a package directory?
>>> ispackage('nose')
True
>>> ispackage('unit_tests')
False
>>> ispackage('nose/plugins')
True
>>> ispackage('nose/loader.py')
False
Is this a property?
>>> class Foo:
... def got(self):
... return 2
... def get(self):
... return 1
... get = property(get)
>>> isproperty(Foo.got)
False
>>> isproperty(Foo.get)
True
Draw a 70-char-wide divider, with label in the middle.
>>> ln('hello there')
'---------------------------- hello there -----------------------------'
Simple ordered dict implementation, based on:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
Sort key function factory that puts items that match a regular expression last.
>>> from nose.config import Config
>>> from nose.pyversion import sort_list
>>> c = Config()
>>> regex = c.testMatch
>>> entries = ['.', '..', 'a_test', 'src', 'lib', 'test', 'foo.py']
>>> sort_list(entries, regex_last_key(regex))
>>> entries
['.', '..', 'foo.py', 'lib', 'src', 'a_test', 'test']
Resolve a dotted name to a module and its parts. This is stolen wholesale from unittest.TestLoader.loadTestByName.
>>> resolve_name('nose.util')
<module 'nose.util' from...>
>>> resolve_name('nose.util.resolve_name')
<function resolve_name at...>
Split a test name into a 3-tuple containing file, module, and callable names, any of which (but not all) may be blank.
Test names are in the form:
file_or_module:callable
Either side of the : may be dotted. To change the splitting behavior, you can alter nose.util.split_test_re.
Find the python source file for a .pyc, .pyo or $py.class file on jython. Returns the filename provided if it is not a python source file.
Find the test address for a test, which may be a module, filename, class, method or function.
Convert a value that may be a list or a (possibly comma-separated) string into a list. The exception: None is returned as None, not [None].
>>> tolist(["one", "two"])
['one', 'two']
>>> tolist("hello")
['hello']
>>> tolist("separate,values, with, commas, spaces , are ,ok")
['separate', 'values', 'with', 'commas', 'spaces', 'are', 'ok']
Make a class appear to reside in module, rather than the module in which it is actually defined.
>>> from nose.failure import Failure
>>> Failure.__module__
'nose.failure'
>>> Nf = transplant_class(Failure, __name__)
>>> Nf.__module__
'nose.util'
>>> Nf.__name__
'Failure'
Make a function imported from module A appear as if it is located in module B.
>>> from pprint import pprint
>>> pprint.__module__
'pprint'
>>> pp = transplant_func(pprint, __name__)
>>> pp.__module__
'nose.util'
The original function is not modified.
>>> pprint.__module__
'pprint'
Calling the transplanted function calls the original.
>>> pp([1, 2])
[1, 2]
>>> pprint([1,2])
[1, 2]
Given a list of possible method names, try to run them with the provided object. Keep going until something works. Used to run setup/teardown methods for module, package, and function tests.