Warning

This project is being split into 3 separate projects:

  • friendly_traceback,

  • friendly, and

  • friendly_idle.

The documentation does not reflect this change.

If you are a end-user, just install friendly until further notice.

Editor plugins

As mentioned in Using with an editor or IDE, one can launch a program by writing an auxiliary file that uses friendly.run(). Perhaps your favourite editor support plugins that can replace such auxiliary scripts by a menu item or an additional button. If that is the case, we encourage you to do so, and let us know so that we can make other users aware.

friendly.run() makes use of another function called exec_code() found in editors_helpers.py. This file contains additional functions that might be useful to turn into plugins, for example enabling to check the syntax without actually running a program.

Again, if you do so, let us know, and we will add any function you use to the public API. In the meantime, here’s some information about these functions.

editors_helpers.py

The functions in this module have been created so that user editors/IDEs could use Friendly without having to change the content of their own programs.

None of these are part of the public API.

If you make use of any other function here, please file an issue so it can be determined if it should be added to the public API.

check_syntax(*, source=None, filename='Fake_filename', path=None, include=None, lang=None)[source]

This uses Python’s compile() builtin which does some analysis of its code argument and will raise an exception if it identifies some syntax errors, but also some less common “overflow” and “value” errors.

Note that there are a few syntax errors that are not caught by this, as they are identified by Python very late in its execution process. See for example this blog post

This function can either be used on a file, using the path argument, or on some code passed as a string, using the source argument. For the latter case, one can also specify a corresponding filename: this could be useful if this function is invoked from a GUI-based editor.

Note that the path argument, if provided, takes precedence over the source argument.

Two additional named arguments, include and lang, can be provided to temporarily set the values to be used during this function call. The original values are restored at the end.

If friendly exception hook has not been set up prior to calling check_syntax, it will only be used for the duration of this function call.

Returns a tuple containing a code object and a filename if no exception has been raised, False otherwise.

exec_code(*, source=None, path=None, include=None, lang=None)[source]

This uses check_syntax to see if the code is valid and, if so, executes it into a globals dict containing only {"__name__": "__main__"}. If no SyntaxError exception is raised, this dict is returned; otherwise, an empty dict is returned.

It can either be used on a file, using the path argument, or on some code passed as a string, using the source argument.

Note that the path argument, if provided, takes precedence over the source argument.

Two additional named arguments, include and lang, can be provided to temporarily set the values to be used during this function call. The original values are restored at the end.

If friendly exception hook has not been set up prior to calling check_syntax, it will only be used for the duration of this function call.