Note
This documentation is for a development version of IPython. There may be significant differences from the latest stable release (1.2.1).
Tools to open .py files as Unicode, using the encoding specified within the file, as per PEP 263.
Much of the code is taken from the tokenize module in Python 3.2.
The detect_encoding() function is used to detect the encoding that should be used to decode a Python source file. It requires one argment, readline, in the same way as the tokenize() generator.
It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (left as bytes) it has read in.
It detects the encoding from the presence of a utf-8 bom or an encoding cookie as specified in pep-0263. If both a bom and a cookie are present, but disagree, a SyntaxError will be raised. If the encoding cookie is an invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found, ‘utf-8-sig’ is returned.
If no encoding is specified, then the default of ‘utf-8’ will be returned.
Open a file in read only mode using the encoding detected by detect_encoding().
Converts a bytes string with python source code to unicode.
Unicode strings are passed through unchanged. Byte strings are checked for the python source file encoding cookie to determine encoding. txt can be either a bytes buffer or a string containing the source code.
Generator to pull lines from a text-mode file, skipping the encoding cookie if it is found in the first two lines.
Read a Python file, using the encoding declared inside the file.
Parameters: | filename : str
skip_encoding_cookie : bool
|
---|---|
Returns: | A unicode string containing the contents of the file. |
Read a Python file from a URL, using the encoding declared inside the file.
Parameters: | url : str
errors : str
skip_encoding_cookie : bool
|
---|---|
Returns: | A unicode string containing the contents of the file. |