##// END OF EJS Templates
support for unicode identifiers...
support for unicode identifiers This rewrites some of the regular expressions that are used to match Python identifiers, so that they are unicode compatible. In Python 3, identifiers can contain unicode characters as long as the first character is not numeric. Examples for the changes: • inputtransformer: ``` In [1]: π = 3.14 In [2]: π.is_integer? Object `is_integer` not found. ``` ---------- • namespace: ``` π.is_integ*? ``` or ``` In [1]: %psearch π.is_integ Python identifiers can only contain ascii characters. ``` ---------- • prefilter: ``` %autocall 1 φ = float get_ipython().prefilter("φ 3") # should be 'φ(3)', but returns 'φ 3' ``` ---------- • completerlib: If there is a file e.g. named `π.py` in the current directory, then ``` import IPython IPython.core.completerlib.module_list('.') # should contain module 'π' ```

File last commit:

r25582:6e079503
r25595:d9c0e690
Show More
module_paths.py
73 lines | 2.4 KiB | text/x-python | PythonLexer
"""Utility functions for finding modules
Utility functions for finding modules on sys.path.
`find_module` returns a path to module or None, given certain conditions.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2011, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Stdlib imports
import importlib
import os
import sys
# Third-party imports
# Our own imports
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
def find_mod(module_name):
"""
Find module `module_name` on sys.path, and return the path to module `module_name`.
- If `module_name` refers to a module directory, then return path to __init__ file.
- If `module_name` is a directory without an __init__file, return None.
- If module is missing or does not have a `.py` or `.pyw` extension, return None.
- Note that we are not interested in running bytecode.
- Otherwise, return the fill path of the module.
Parameters
----------
module_name : str
Returns
-------
module_path : str
Path to module `module_name`, its __init__.py, or None,
depending on above conditions.
"""
loader = importlib.util.find_spec(module_name)
module_path = loader.origin
if module_path is None:
if loader.loader in sys.meta_path:
return loader.loader
return None
else:
split_path = module_path.split(".")
if split_path[-1] in ["py", "pyw"]:
return module_path
else:
return None