File: pyfile.py

package info (click to toggle)
ipython 0.13.1-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,752 kB
  • sloc: python: 69,537; makefile: 355; lisp: 272; sh: 80; objc: 37
file content (23 lines) | stat: -rw-r--r-- 840 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Utilities for working with Python source files.

Exposes various functions from recent Python standard libraries, along with
equivalents for older Python versions.
"""
import os.path

try:    # Python 3.2
    from imp import source_from_cache, cache_from_source
except ImportError:
    # Python <= 3.1: .pyc files go next to .py
    def source_from_cache(path):
        basename, ext = os.path.splitext(path)
        if ext not in ('.pyc', '.pyo'):
            raise ValueError('Not a cached Python file extension', ext)
        # Should we look for .pyw files?
        return basename + '.py'
    
    def cache_from_source(path, debug_override=None):
        if debug_override is None:
            debug_override = __debug__
        basename, ext = os.path.splitext(path)
        return basename + '.pyc' if debug_override else '.pyo'