Skip to main content

Python 2.5 compatibility wrapper for Python 2.7 code.

Project description

seven is a Python 2.6+ compatibility layer for Python 2.5. It allows you to import Python 2.6 or 2.7 code in a 2.5 interpreter. It is mainly intended to be used on Google App Engine.

Basic usage

Before importing non-compatible code, you need to start the seven import hook by calling seven.start():

>>> import seven
>>> seven.start()

>>> import incompatible.modules

>>> seven.stop()  # optional

The above will install an import hook and preprocess all modules before importing them using lib2to3 from the Python 2.7 stdlib. The installed hook will process all imported python modules until you call seven.stop(). If you don’t stop it, all imports will be processed.

Import whitelists

Preprocessing all imported modules might not always be a good idea, mainly for two reasons. Firs, it comes with a slight performance penalty. Second, more important reason is that exceptions raised in modules that have been processed by the import hook can be really hard to debug. Line numbers are usually wrong, and the filename is not displayed correctly. These things might be improved in the future, but for now it is advised that only the necessary modules be processed.

The following example will only preprocess the modules foo, spam.eggs and their submodules, foo.* and spam.eggs.*:

>>> seven.start(['foo', 'spam.eggs'])

What gets fixed?

Of course, not all fixers have been implemented yet. The following features are available (without using from __future__ imports):

  • with statement (from __future__ import with_statement)

  • absolute imports (from __future__ import absolute_imports)

  • integer division (from __future__ import division)

  • class decorators

Class decorators are converted like this:

@decorated
class C:
    pass

becomes:

class C:
    pass
C = decorated(C)

What needs to be implemented?

The following features do not have fixers yet:

  • class decorators with indentation

  • except Exception as e:

  • advanced string formatting

  • print as a function

  • set literals

  • dict and set comprehensions

  • multiple context managers in one with statement

  • etc.

How to write fixers?

By subclassing seven.lib2to3.fixer_base.BaseFix. You can also fork this project on GitHub, have a look at the existing fixers in src/seven/fixes and add your own, thend send me a pull request.

Changelog

0.5 (2010-12-04)

  • Initial public release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seven-0.5.tar.gz (53.9 kB view details)

Uploaded Source

File details

Details for the file seven-0.5.tar.gz.

File metadata

  • Download URL: seven-0.5.tar.gz
  • Upload date:
  • Size: 53.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for seven-0.5.tar.gz
Algorithm Hash digest
SHA256 c4ab967b5227f2f7ec3b17e1d91836488cf9a37759abf9e2fb6b74377dd97e56
MD5 e2b25bcb061f436441b92ba94f54769d
BLAKE2b-256 4bd41b05bf67f97ce9a67a184e078616a099dd31a559eb1d008ac56da6224327

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page